From 9810fe2a8e096eeab8fa26b5549ecb059885d09f Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Sat, 16 Oct 2021 03:37:48 +0800 Subject: [PATCH 1/8] docs: update --- Makefile | 2 +- README.md | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index abdc58c..4fc470f 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ build-unix: $(shell export GOOS=$(GOOS); export GOARCH=$(GOARCH); go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_$(GOOS)_$(GOARCH)/$(PROJ)))) build-win: - $(shell export GOOS=windows; export GOARCH=arm64; go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_windows_arm64/$(PROJ).exe) + $(shell export GOOS=windows; export GOARCH=amd64; go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_windows_amd64/$(PROJ).exe) build-mac-m1: $(shell export GOOS=darwin; export GOARCH=arm64; go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_darwin_arm64/$(PROJ)) diff --git a/README.md b/README.md index 1a02c48..1714486 100644 --- a/README.md +++ b/README.md @@ -19,16 +19,20 @@ Available Commands: Flags: -a, --all show local ip -c, --cell show cell, must used with --list + --cron run as cron service -h, --help help for myip -u, --host string hostname to connect (default "kumoly.io") -l, --list show list + -n, --name string tell the server who you are --no-pub disable PublicIP -p, --port string server port to connect (default "5080") --secure use https + -s, --spec string hostname to connect (default "0 */5 * * * *") -t, --title show title, used with --list -v, --version version for myip Use "myip [command] --help" for more information about a command. + ``` ## Docker @@ -40,6 +44,12 @@ 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: + +```shell +docker pull hub.kumoly.io/tools/myip:latest +docker run --name myip -d --restart=always hub.kumoly.io/tools/myip myip --cron --spec "@hourly" +``` ## Cron @@ -52,6 +62,16 @@ Hours | Yes | 0-23 | * / , - Day of month | Yes | 1-31 | * / , - ? Month | Yes | 1-12 or JAN-DEC | * / , - Day of week | Yes | 0-6 or SUN-SAT | * / , - ? + +Entry | Description | Equivalent To +----- | ----------- | ------------- +@yearly (or @annually) | Run once a year, midnight, Jan. 1st | 0 0 0 1 1 * +@monthly | Run once a month, midnight, first of month | 0 0 0 1 * * +@weekly | Run once a week, midnight between Sat/Sun | 0 0 0 * * 0 +@daily (or @midnight) | Run once a day, midnight | 0 0 0 * * * +@hourly | Run once an hour, beginning of hour | 0 0 * * * * + +@every ``` **Duration**: `myip -altc --cron --spec "@every 5m"` -- 2.40.1 From c81cbffbe1aa45a1730168288480290600288021 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Sat, 16 Oct 2021 22:21:59 +0800 Subject: [PATCH 2/8] feat: #2 --- Makefile | 4 +++- cmd/client.go | 26 ++++++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 4fc470f..a8b9883 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +SHELL := /bin/bash + VERSION=$(shell git describe --tags) BUILD=$(shell git rev-parse --short HEAD) PROJ := $(shell basename "$(PWD)") @@ -31,7 +33,7 @@ build-mac-m1: $(shell export GOOS=darwin; export GOARCH=arm64; go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_darwin_arm64/$(PROJ)) build-zip: - cd dist; for f in * ;do tar -czf $${f}.tar.gz $${f}; done + cd dist; for f in * ;do if ! [[ $$f =~ "gz" ]] ; then tar -czf $${f}.tar.gz $${f}; fi done binary: build-unix build-win build-mac-m1 diff --git a/cmd/client.go b/cmd/client.go index b467518..b423cc6 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -8,6 +8,7 @@ import ( "net" "net/http" "os" + "sort" "strings" "text/tabwriter" @@ -32,6 +33,12 @@ var ( cronSpec string ) +type Iface struct { + IP string + HardwareAddr string + Name string +} + var ClientCmd = &cobra.Command{ Use: "myip", Short: "myip is a easy way to get public ip of current system.", @@ -110,8 +117,8 @@ func Scan() error { if !noPub { fmt.Fprintf(w, "\t%v\t%v\t\n", "PublicIP", ip) } - for k, v := range local { - fmt.Fprintf(w, "\t%v\t%v\t\n", k, v) + for _, iface := range local { + fmt.Fprintf(w, "\t%v\t%v\t\n", iface.Name, iface.IP) } return w.Flush() @@ -120,7 +127,7 @@ func Scan() error { fmt.Printf("%v ", ip) } for _, v := range local { - fmt.Printf("%v ", v) + fmt.Printf("%v ", v.IP) } fmt.Printf("\n") } @@ -128,8 +135,8 @@ func Scan() error { return nil } -func GetLocalIP() (map[string]string, error) { - ret := map[string]string{} +func GetLocalIP() ([]Iface, error) { + ret := []Iface{} ifaces, err := net.Interfaces() if err != nil { log.Println(err) @@ -161,10 +168,17 @@ func GetLocalIP() (map[string]string, error) { // continue // } } - ret[i.Name] = ip.String() + ret = append(ret, Iface{ + IP: ip.String(), + Name: i.Name, + HardwareAddr: i.HardwareAddr.String(), + }) break } } + sort.Slice(ret, func(i, j int) bool { + return ret[i].Name < ret[j].Name + }) return ret, nil } -- 2.40.1 From 8aa35a546fd2f09be1a1f697fb7426c53cf169bb Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Sat, 16 Oct 2021 22:40:23 +0800 Subject: [PATCH 3/8] feat: #3 --- cmd/server.go | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index 3d1bdc9..62e81a5 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -5,6 +5,7 @@ import ( "log" "net" "net/http" + "time" "github.com/spf13/cobra" ) @@ -31,19 +32,16 @@ func init() { func StartServer() error { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - ip := r.Header.Get("X-Real-Ip") - if ip == "" { - ip = r.Header.Get("X-Forwarded-For") - } - if ip == "" { - var err error - ip, _, err = net.SplitHostPort(r.RemoteAddr) - if err != nil { - ip = r.RemoteAddr - } - } - r.Header.Add("X-Myip", ip) + ip := GetIP(r) fmt.Fprint(w, ip+"\n") + + // logging + user := r.URL.Query().Get("name") + if user == "" { + user = r.URL.String() + } + host, _, _ := net.SplitHostPort(r.RemoteAddr) + log.Printf("%s %s %s %s %s\n", time.Now().Format("2006-01-02-15:04:05"), host, ip, user, r.Header.Get("User-Agent")) }) err := http.ListenAndServe(addr+":"+port, simpleLogger(http.DefaultServeMux)) @@ -54,6 +52,21 @@ func StartServer() error { return nil } +func GetIP(r *http.Request) string { + ip := r.Header.Get("X-Real-Ip") + if ip == "" { + ip = r.Header.Get("X-Forwarded-For") + } + if ip == "" { + var err error + ip, _, err = net.SplitHostPort(r.RemoteAddr) + if err != nil { + ip = r.RemoteAddr + } + } + return ip +} + func simpleLogger(handler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -63,11 +76,7 @@ func simpleLogger(handler http.Handler) http.Handler { // fmt.Println(name, value) // } // } - user := r.URL.Query().Get("name") - if user == "" { - user = r.URL.String() - } - log.Printf("%s %s %s %s\n", r.RemoteAddr, r.Method, user, r.Header.Get("User-Agent")) + // log.Printf("%s %s %s %s\n", r.RemoteAddr, r.Method, r.URL, r.Header.Get("User-Agent")) handler.ServeHTTP(w, r) }) } -- 2.40.1 From 631261d8af5fd3bc907c02cd84171fda2258f156 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Sat, 16 Oct 2021 22:47:30 +0800 Subject: [PATCH 4/8] feat: #1 --- cmd/client.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/client.go b/cmd/client.go index b423cc6..83fd83f 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -11,6 +11,7 @@ import ( "sort" "strings" "text/tabwriter" + "time" "github.com/robfig/cron/v3" "github.com/spf13/cobra" @@ -187,7 +188,11 @@ func GetPublicIP() (string, error) { if secure { protocol = "https://" } - res, err := http.Get(protocol + host + ":" + port + "?name=" + name) + 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 "", err -- 2.40.1 From 8d4469954327b8437cd1d25848e8ee527cf20efa Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Sat, 16 Oct 2021 22:57:25 +0800 Subject: [PATCH 5/8] docs: update --- CHANGELOG.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9c2123..ca1e851 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,14 @@ +# 0.1.4 + +## Feature + +* Servers should log time + +## Fix + +* Connection to host hangs indefinitely +* iface should be sorted + # 0.1.3 ## Feature -- 2.40.1 From 0bfabdcaf92a5ab6b00019828a590602d17dccf4 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Sat, 16 Oct 2021 23:50:05 +0800 Subject: [PATCH 6/8] 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") +} -- 2.40.1 From 3c5db47542ae52ffd723f023d4fd96df01e041d8 Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Sat, 16 Oct 2021 23:59:13 +0800 Subject: [PATCH 7/8] docs: update --- README.md | 19 +++++++++++++++++-- cmd/server.go | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) 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/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 ) -- 2.40.1 From 9af52cdb0d2b1102923a22560c8153035bc9e15c Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Sun, 17 Oct 2021 00:50:41 +0800 Subject: [PATCH 8/8] docs: update --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca1e851..1f656c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.1.5 + +## Feature + +* add minimal basic client + # 0.1.4 ## Feature -- 2.40.1