From cc6208b483394153bd449a476a4dd3360a802657 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Sat, 16 Oct 2021 23:50:05 +0800 Subject: [PATCH] feat: add minimal basic client --- Makefile | 9 ++++ basic/main.go | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 basic/main.go 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/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") +}