From 48c570670394ec43f149b4f9430e084c059c99e9 Mon Sep 17 00:00:00 2001 From: evanchen Date: Sat, 16 Oct 2021 16:52:31 +0000 Subject: [PATCH] 0.1.5-rc1 (#6) Co-authored-by: Evan Chen Reviewed-on: https://kumoly.io/git/tools/myip/pulls/6 Co-authored-by: evanchen Co-committed-by: evanchen --- CHANGELOG.md | 17 ++++++++ Makefile | 9 ++++ README.md | 19 ++++++++- basic/main.go | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/server.go | 2 +- 5 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 basic/main.go diff --git a/CHANGELOG.md b/CHANGELOG.md index c9c2123..1f656c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 ## Feature diff --git a/Makefile b/Makefile index a8b9883..9ab89e5 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,10 @@ install: clean: rm -rf dist +.PHONY: basic +basic: + go build ${LDFLAGS} -o dist/myip-basic basic/main.go + build: go build ${LDFLAGS} -o dist/myip @@ -26,11 +30,16 @@ build-unix: $(foreach GOOS, $(PLATFORMS), $(foreach GOARCH, $(ARCHITECTURES), \ $(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: $(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: $(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: cd dist; for f in * ;do if ! [[ $$f =~ "gz" ]] ; then tar -czf $${f}.tar.gz $${f}; fi done diff --git a/README.md b/README.md index 1714486..93d212b 100644 --- a/README.md +++ b/README.md @@ -37,14 +37,29 @@ Use "myip [command] --help" for more information about a command. ## Docker -Server: +### Server ```shell docker pull hub.kumoly.io/tools/myip:latest 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 docker pull hub.kumoly.io/tools/myip:latest diff --git a/basic/main.go b/basic/main.go new file mode 100644 index 0000000..d12d0bc --- /dev/null +++ b/basic/main.go @@ -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") +} diff --git a/cmd/server.go b/cmd/server.go index 62e81a5..6322d55 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -19,9 +19,9 @@ var ServerCmd = &cobra.Command{ } var ( - // declared in client // port string + addr string )