feat: add minimal basic client
							parent
							
								
									8d44699543
								
							
						
					
					
						commit
						0bfabdcaf9
					
				
							
								
								
									
										9
									
								
								Makefile
								
								
								
								
							
							
						
						
									
										9
									
								
								Makefile
								
								
								
								
							| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue