0.1.5-rc1 (#6)

Co-authored-by: Evan Chen <evanchen@kumoly.io>
Reviewed-on: https://kumoly.io/git/tools/myip/pulls/6
Co-authored-by: evanchen <evanchen@kumoly.io>
Co-committed-by: evanchen <evanchen@kumoly.io>
dev^2 0.1.5
evanchen 2021-10-16 16:52:31 +00:00
parent edcc074672
commit 48c5706703
5 changed files with 155 additions and 3 deletions

View File

@ -1,3 +1,20 @@
# 0.1.5
## Feature
* add minimal basic client
# 0.1.4
## Feature
* Servers should log time
## Fix
* Connection to host hangs indefinitely
* iface should be sorted
# 0.1.3 # 0.1.3
## Feature ## Feature

View File

@ -19,6 +19,10 @@ install:
clean: clean:
rm -rf dist rm -rf dist
.PHONY: basic
basic:
go build ${LDFLAGS} -o dist/myip-basic basic/main.go
build: build:
go build ${LDFLAGS} -o dist/myip go build ${LDFLAGS} -o dist/myip
@ -26,11 +30,16 @@ build-unix:
$(foreach GOOS, $(PLATFORMS), $(foreach GOARCH, $(ARCHITECTURES), \ $(foreach GOOS, $(PLATFORMS), $(foreach GOARCH, $(ARCHITECTURES), \
$(shell export GOOS=$(GOOS); export GOARCH=$(GOARCH); go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_$(GOOS)_$(GOARCH)/$(PROJ)))) $(shell export GOOS=$(GOOS); export GOARCH=$(GOARCH); go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_$(GOOS)_$(GOARCH)/$(PROJ))))
$(foreach GOOS, $(PLATFORMS), $(foreach GOARCH, $(ARCHITECTURES), \
$(shell export GOOS=$(GOOS); export GOARCH=$(GOARCH); go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_$(GOOS)_$(GOARCH)/$(PROJ)-basic basic/main.go)))
build-win: build-win:
$(shell export GOOS=windows; export GOARCH=amd64; go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_windows_amd64/$(PROJ).exe) $(shell export GOOS=windows; export GOARCH=amd64; go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_windows_amd64/$(PROJ).exe)
$(shell export GOOS=windows; export GOARCH=amd64; go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_windows_amd64/$(PROJ)-basic.exe basic/main.go)
build-mac-m1: build-mac-m1:
$(shell export GOOS=darwin; export GOARCH=arm64; go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_darwin_arm64/$(PROJ)) $(shell export GOOS=darwin; export GOARCH=arm64; go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_darwin_arm64/$(PROJ))
$(shell export GOOS=darwin; export GOARCH=arm64; go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_darwin_arm64/$(PROJ)-basic basic/main.go)
build-zip: build-zip:
cd dist; for f in * ;do if ! [[ $$f =~ "gz" ]] ; then tar -czf $${f}.tar.gz $${f}; fi done cd dist; for f in * ;do if ! [[ $$f =~ "gz" ]] ; then tar -czf $${f}.tar.gz $${f}; fi done

View File

@ -37,14 +37,29 @@ Use "myip [command] --help" for more information about a command.
## Docker ## Docker
Server: ### Server
```shell ```shell
docker pull hub.kumoly.io/tools/myip:latest docker pull hub.kumoly.io/tools/myip:latest
docker run --name myip-server -d -p 5080:5080 --restart=always hub.kumoly.io/tools/myip docker run --name myip-server -d -p 5080:5080 --restart=always hub.kumoly.io/tools/myip
``` ```
Client: Or you can use docker-compose.yaml
```yaml
services:
myip:
image: hub.kumoly.io/tools/myip
container_name: myip-server
restart: always
ports:
- 5080:5080
volumes:
- /etc/localtime:/etc/localtime:ro
```
### Client
```shell ```shell
docker pull hub.kumoly.io/tools/myip:latest docker pull hub.kumoly.io/tools/myip:latest

111
basic/main.go Normal file
View File

@ -0,0 +1,111 @@
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strings"
"time"
)
var (
port string
host string
showVer bool
)
var name string
var Version = "v0.1.0"
var Build = "v0.1.0"
func init() {
name, _ = os.Hostname()
flag.BoolVar(&showVer, "v", false, "show version")
flag.StringVar(&host, "u", "kumoly.io", "hostname to ask for")
flag.StringVar(&port, "p", "5080", "port to listen|connect")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: myip-basic [options]\n")
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
if showVer {
fmt.Println(Version + "-" + Build)
return
}
fmt.Printf("%s ", GetPublicIP())
for _, v := range GetLocalIP() {
fmt.Printf("%s ", v)
}
fmt.Printf("\n")
}
func GetLocalIP() []string {
ret := []string{}
ifaces, err := net.Interfaces()
if err != nil {
log.Println(err)
return ret
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
log.Println(err)
return ret
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
if ip.IsLoopback() {
continue
}
if ip.To4() == nil {
continue
}
default:
continue
}
ret = append(ret, ip.String())
break
}
}
return ret
}
func GetPublicIP() string {
protocol := "http://"
req, _ := http.NewRequest("GET", protocol+host+":"+port+"?name="+name, nil)
client := &http.Client{
Timeout: time.Second * 10,
}
res, err := client.Do(req)
if err != nil {
log.Println(err)
return ""
}
if res.StatusCode != 200 {
err = errors.New("http status error: " + res.Status)
log.Println(err)
return ""
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println(err)
return ""
}
return strings.TrimRight(string(body), "\r\n")
}

View File

@ -19,9 +19,9 @@ var ServerCmd = &cobra.Command{
} }
var ( var (
// declared in client // declared in client
// port string // port string
addr string addr string
) )