Compare commits

...

4 Commits

Author SHA1 Message Date
Evan Chen 5e8c3bd70a fix: algo on geting ip 2021-10-19 00:47:09 +08:00
Evan Chen aa3d4a1615 refactor: move main to cmd/ 2021-10-18 09:14:30 +08:00
Evan Chen 18b81a4a32 refactor: change structure 2021-10-17 05:23:55 +08:00
Evan Chen 1dd6c3c7b8 docs: typo 2021-10-17 02:30:03 +08:00
9 changed files with 47 additions and 26 deletions

View File

@ -1,4 +1,4 @@
# 0.1.5
# 0.1.6
## Feature

View File

@ -9,7 +9,7 @@ COPY . .
RUN VERSION=$(git describe --tags) BUILD=$(git rev-parse --short HEAD) && \
GOOS=linux GOARCH=amd64 \
go build -ldflags "-X main.Version=${VERSION} -X main.Build=${BUILD} -w" \
-o /go/bin/myip main.go
-o /go/bin/myip cmd/myip/main.go
FROM alpine:3.14

View File

@ -10,6 +10,9 @@ LDFLAGS=-ldflags "-X main.Version=${VERSION} -X main.Build=${BUILD} -w"
PLATFORMS=darwin linux
ARCHITECTURES=amd64
APPS=myip myip-basic
default: build
install:
@ -21,25 +24,31 @@ clean:
.PHONY: basic
basic:
go build ${LDFLAGS} -o dist/myip-basic basic/main.go
go build ${LDFLAGS} -o dist/${BASIC} ${BASIC_PATH}
build:
go build ${LDFLAGS} -o dist/myip
go build ${LDFLAGS} -o dist/${FULL} ${FULL_PATH}
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)-basic_$(VERSION)_$(GOOS)_$(GOARCH)/$(PROJ)-basic basic/main.go)))
$(foreach GOOS, $(PLATFORMS), $(foreach GOARCH, $(ARCHITECTURES), $(foreach APP, $(APPS),\
$(shell export GOOS=$(GOOS); export GOARCH=$(GOARCH); go build ${LDFLAGS} -o dist/$(APP) cmd/$(APP)/main.go) \
$(shell cd dist; tar -czf ${APP}_$(VERSION)_$(GOOS)_$(GOARCH).tar.gz ${APP}) \
$(shell rm dist/${APP}) \
)))
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)-basic_$(VERSION)_windows_amd64/$(PROJ)-basic.exe basic/main.go)
$(foreach APP, $(APPS), \
$(shell export GOOS=windows; export GOARCH=amd64; go build ${LDFLAGS} -o dist/${APP}.exe cmd/$(APP)/main.go) \
$(shell cd dist; tar -czf ${APP}_$(VERSION)_windows_amd64.tar.gz ${APP}.exe) \
$(shell rm dist/${APP}.exe) \
)
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)-basic_$(VERSION)_darwin_arm64/$(PROJ)-basic basic/main.go)
$(foreach APP, $(APPS),\
$(shell export GOOS=darwin; export GOARCH=arm64; go build ${LDFLAGS} -o dist/$(APP) cmd/$(APP)/main.go) \
$(shell cd dist; tar -czf ${APP}_$(VERSION)_darwin_arm64.tar.gz ${APP}) \
$(shell rm dist/${APP}) \
)
build-zip:
cd dist; for f in * ;do if ! [[ $$f =~ "gz" ]] ; then tar -czf $${f}.tar.gz $${f}; fi done
@ -61,6 +70,7 @@ docker-save:
docker save $(HUB)/$(HUB_PROJECT)/$(PROJ):$(VERSION) | gzip > dist/$(PROJ)-image-$(VERSION)-${BUILD}.tar.gz
release: clean binary build-zip docker docker-save docker-push
release: clean binary docker docker-save
.PHONY: build docker release clean all binary install

View File

@ -36,6 +36,13 @@ Use "myip [command] --help" for more information about a command.
```
## Install
```
sudo rm -f /usr/local/bin/myip
sudo sh -c "curl -fsSL https://release.kumoly.io/tools/myip/0.1.6/linux/amd64 | tar -C /usr/local/bin/ -xz"
```
## Docker
### Server
@ -71,7 +78,7 @@ docker run --name myip -d --restart=always hub.kumoly.io/tools/myip myip --cron
## Cron
```
ield name | Mandatory? | Allowed values | Allowed special characters
field name | Mandatory? | Allowed values | Allowed special characters
---------- | ---------- | -------------- | --------------------------
Seconds | Yes | 0-59 | * / , -
Minutes | Yes | 0-59 | * / , -

View File

@ -1,4 +1,4 @@
package cmd
package myip
import (
"errors"
@ -75,6 +75,9 @@ func init() {
ClientCmd.Flags().BoolVar(&cronMode, "cron", false, "run as cron service")
ClientCmd.Flags().StringVarP(&cronSpec, "spec", "s", "0 */5 * * * *", "hostname to connect")
ClientCmd.AddCommand(ServerCmd)
ClientCmd.AddCommand(LocalIPCmd)
}
func Scan() error {

View File

@ -29,7 +29,7 @@ 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.StringVar(&port, "p", "5080", "port to connect")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: myip-basic [options]\n")

View File

@ -6,7 +6,7 @@ import (
"os"
"github.com/spf13/cobra"
"kumoly.io/tools/myip/cmd"
"kumoly.io/tools/myip"
)
var Version = "v0.1.0"
@ -24,14 +24,12 @@ var versionCmd = &cobra.Command{
func init() {
log.SetFlags(0)
log.SetOutput(os.Stdout)
cmd.ClientCmd.Version = Version + "-" + Build
cmd.ClientCmd.AddCommand(versionCmd)
cmd.ClientCmd.AddCommand(cmd.ServerCmd)
cmd.ClientCmd.AddCommand(cmd.LocalIPCmd)
myip.ClientCmd.Version = Version + "-" + Build
myip.ClientCmd.AddCommand(versionCmd)
}
func main() {
if err := cmd.ClientCmd.Execute(); err != nil {
if err := myip.ClientCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

View File

@ -1,4 +1,4 @@
package cmd
package myip
import (
"github.com/spf13/cobra"

View File

@ -1,4 +1,4 @@
package cmd
package myip
import (
"fmt"
@ -7,6 +7,7 @@ import (
"net"
"net/http"
"os"
"strings"
"time"
"github.com/spf13/cobra"
@ -70,7 +71,9 @@ func StartServer() error {
func GetIP(r *http.Request) string {
ip := r.Header.Get("X-Real-Ip")
if ip == "" {
ip = r.Header.Get("X-Forwarded-For")
ips := r.Header.Get("X-Forwarded-For")
ipArr := strings.Split(ips, ",")
ip = strings.Trim(ipArr[len(ipArr)-1], " ")
}
if ip == "" {
var err error