pull/6/head
Evan Chen 2021-10-16 03:15:49 +08:00
parent 1241b21691
commit 574f628c4b
7 changed files with 77 additions and 9 deletions

View File

@ -1,3 +1,10 @@
# 0.1.3
## Feature
* Add cron job to client
* Client can report to server there name
# 0.1.2
## Fix

View File

@ -5,7 +5,7 @@ HUB=hub.kumoly.io
HUB_PROJECT=tools
LDFLAGS=-ldflags "-X main.Version=${VERSION} -X main.Build=${BUILD} -w"
PLATFORMS=darwin linux windows
PLATFORMS=darwin linux
ARCHITECTURES=amd64
default: build
@ -20,16 +20,21 @@ clean:
build:
go build ${LDFLAGS} -o dist/myip
build-bins:
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))))
build-m1:
build-win:
$(shell export GOOS=windows; export GOARCH=arm64; go build ${LDFLAGS} -o dist/$(PROJ)_$(VERSION)_windows_arm64/$(PROJ).exe)
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
binary: build-unix build-win build-mac-m1
docker:
docker build --target builder -t $(HUB)/$(HUB_PROJECT)/$(PROJ):builder .
docker build \
@ -45,6 +50,6 @@ docker-save:
docker save $(HUB)/$(HUB_PROJECT)/$(PROJ):$(VERSION) | gzip > dist/$(PROJ)-image-$(VERSION)-${BUILD}.tar.gz
release: clean build-bins build-m1 build-zip docker docker-save docker-push
release: clean binary build-zip docker docker-save docker-push
.PHONY: build docker release clean all
.PHONY: build docker release clean all binary install

View File

@ -39,3 +39,19 @@ Server:
docker pull hub.kumoly.io/tools/myip:latest
docker run --name myip-server -d -p 5080:5080 --restart=always hub.kumoly.io/tools/myip
```
## Cron
```
ield name | Mandatory? | Allowed values | Allowed special characters
---------- | ---------- | -------------- | --------------------------
Seconds | Yes | 0-59 | * / , -
Minutes | Yes | 0-59 | * / , -
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 | * / , - ?
```
**Duration**: `myip -altc --cron --spec "@every 5m"`

View File

@ -11,12 +11,14 @@ import (
"strings"
"text/tabwriter"
"github.com/robfig/cron/v3"
"github.com/spf13/cobra"
)
var (
port string
host string
name string
showTitle bool
showList bool
@ -25,19 +27,37 @@ var (
noPub bool
secure bool
cronMode bool
cronSpec string
)
var ClientCmd = &cobra.Command{
Use: "myip",
Short: "myip is a easy way to get public ip of current system.",
Run: func(cmd *cobra.Command, args []string) {
if name == "" {
name, _ = os.Hostname()
}
if cronMode {
block := make(chan struct{}, 1)
c := cron.New()
c.AddFunc(cronSpec, func() {
Scan()
fmt.Println()
})
c.Start()
<-block
} else {
Scan()
}
},
}
func init() {
ClientCmd.Flags().StringVarP(&port, "port", "p", "5080", "server port to connect")
ClientCmd.Flags().StringVarP(&host, "host", "u", "kumoly.io", "hostname to connect")
ClientCmd.Flags().StringVarP(&name, "name", "n", "", "tell the server who you are")
ClientCmd.Flags().BoolVarP(&showList, "list", "l", false, "show list")
ClientCmd.Flags().BoolVarP(&showTitle, "title", "t", false, "show title, used with --list")
@ -46,6 +66,9 @@ func init() {
ClientCmd.Flags().BoolVar(&noPub, "no-pub", false, "disable PublicIP")
ClientCmd.Flags().BoolVar(&secure, "secure", false, "use https")
ClientCmd.Flags().BoolVar(&cronMode, "cron", false, "run as cron service")
ClientCmd.Flags().StringVarP(&cronSpec, "spec", "s", "0 */5 * * * *", "hostname to connect")
}
func Scan() error {
@ -150,7 +173,7 @@ func GetPublicIP() (string, error) {
if secure {
protocol = "https://"
}
res, err := http.Get(protocol + host + ":" + port)
res, err := http.Get(protocol + host + ":" + port + "?name=" + name)
if err != nil {
log.Println(err)
return "", err

View File

@ -42,6 +42,7 @@ func StartServer() error {
ip = r.RemoteAddr
}
}
r.Header.Add("X-Myip", ip)
fmt.Fprint(w, ip+"\n")
})
@ -55,7 +56,18 @@ func StartServer() error {
func simpleLogger(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
// for name, values := range r.Header {
// // Loop over all values for the name.
// for _, value := range values {
// 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"))
handler.ServeHTTP(w, r)
})
}

5
go.mod
View File

@ -2,7 +2,10 @@ module kumoly.io/tools/myip
go 1.17
require github.com/spf13/cobra v1.2.1
require (
github.com/robfig/cron/v3 v3.0.1
github.com/spf13/cobra v1.2.1
)
require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect

2
go.sum
View File

@ -192,6 +192,8 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=