Compare commits

...

6 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
Evan Chen e63496a6b6 feat: #8, #9; fix: #10 2021-10-17 02:22:55 +08:00
Evan Chen f805554534 build: update makefile 2021-10-17 01:00:27 +08:00
11 changed files with 148 additions and 40 deletions

View File

@ -1,3 +1,14 @@
# 0.1.6
## Feature
* add subcommand local for shorthand
* allow server to log to file
## Fix
* Chinese breaking the format
# 0.1.5
## 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
@ -18,7 +18,7 @@ ENV PATH="/go/bin:${PATH}"
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=builder /go/bin/myip /go/bin/myip
CMD ["/go/bin/myip","serve"]
CMD ["/go/bin/myip","serve","-f","/var/log/myip.log"]
# use cmd instead for flexibility
# ENTRYPOINT ["/go/bin/myip","serve"]

View File

@ -1,6 +1,6 @@
SHELL := /bin/bash
VERSION=$(shell git describe --tags)
VERSION=$(shell git describe --tags --abbrev=0)
BUILD=$(shell git rev-parse --short HEAD)
PROJ := $(shell basename "$(PWD)")
HUB=hub.kumoly.io
@ -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)_$(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)_$(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)_$(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

@ -3,7 +3,7 @@
a tool that can get your public ip
```shell
```
myip is a easy way to get public ip of current system.
Usage:
@ -13,12 +13,12 @@ Usage:
Available Commands:
completion generate the autocompletion script for the specified shell
help Help about any command
local short hand for `myip -altc --no-pub`
serve Simple server that returns the resolved IP.
version version
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")
@ -26,6 +26,7 @@ Flags:
-n, --name string tell the server who you are
--no-pub disable PublicIP
-p, --port string server port to connect (default "5080")
-c, --pretty show table cell, must used with --list
--secure use https
-s, --spec string hostname to connect (default "0 */5 * * * *")
-t, --title show title, used with --list
@ -35,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
@ -56,6 +64,7 @@ services:
- 5080:5080
volumes:
- /etc/localtime:/etc/localtime:ro
- ./log:/var/log
```
@ -69,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"
@ -10,9 +10,10 @@ import (
"os"
"sort"
"strings"
"text/tabwriter"
"time"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/robfig/cron/v3"
"github.com/spf13/cobra"
)
@ -44,9 +45,6 @@ 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()
@ -69,7 +67,7 @@ func init() {
ClientCmd.Flags().BoolVarP(&showList, "list", "l", false, "show list")
ClientCmd.Flags().BoolVarP(&showTitle, "title", "t", false, "show title, used with --list")
ClientCmd.Flags().BoolVarP(&showCell, "cell", "c", false, "show cell, must used with --list")
ClientCmd.Flags().BoolVarP(&showCell, "pretty", "c", false, "show table cell, must used with --list")
ClientCmd.Flags().BoolVarP(&showAll, "all", "a", false, "show local ip")
ClientCmd.Flags().BoolVar(&noPub, "no-pub", false, "disable PublicIP")
@ -77,10 +75,16 @@ 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 {
// fmt.Printf("title: %v\nlist: %v\nall: %v\ntab: %v\n", showTitle, showList, showAll, showCell)
if name == "" {
name, _ = os.Hostname()
}
if !showAll {
if noPub {
return errors.New("`--no-pub` can only be used with `--all`")
@ -104,24 +108,33 @@ func Scan() error {
return err
}
if showList {
var wFlag uint = 0
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
if showCell {
wFlag = tabwriter.Debug
t.SetStyle(table.StyleLight)
} else {
t.Style().Options.DrawBorder = false
t.Style().Options.SeparateColumns = false
t.Style().Options.SeparateFooter = false
t.Style().Options.SeparateHeader = false
t.Style().Options.SeparateRows = false
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', wFlag)
if showTitle {
fmt.Fprintln(w, "\tInterface\tIP\t")
t.AppendHeader(table.Row{"Interface", "IP"})
if showCell {
fmt.Fprintln(w, "\t\t\t")
t.AppendSeparator()
}
}
if !noPub {
fmt.Fprintf(w, "\t%v\t%v\t\n", "PublicIP", ip)
t.AppendRow([]interface{}{"PublicIP", ip})
}
for _, iface := range local {
fmt.Fprintf(w, "\t%v\t%v\t\n", iface.Name, iface.IP)
t.AppendRow([]interface{}{iface.Name, iface.IP})
}
return w.Flush()
t.Render()
// fmt.Println(t.Render())
return nil
} else {
if !noPub {

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,13 +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)
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)
}

3
go.mod
View File

@ -3,11 +3,14 @@ module kumoly.io/tools/myip
go 1.17
require (
github.com/jedib0t/go-pretty/v6 v6.2.4
github.com/robfig/cron/v3 v3.0.1
github.com/spf13/cobra v1.2.1
)
require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
)

12
go.sum
View File

@ -57,6 +57,7 @@ github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@ -67,6 +68,7 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fzipp/gocyclo v0.3.1/go.mod h1:DJHO6AUmbdqj2ET4Z9iArSuwWgYDRryYt2wASxc7x3E=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
@ -160,6 +162,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jedib0t/go-pretty/v6 v6.2.4 h1:wdaj2KHD2W+mz8JgJ/Q6L/T5dB7kyqEFI16eLq7GEmk=
github.com/jedib0t/go-pretty/v6 v6.2.4/go.mod h1:+nE9fyyHGil+PuISTCrp7avEdo6bqoMwqZnuiK2r2a0=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
@ -173,6 +177,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
@ -188,7 +194,9 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
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=
@ -216,6 +224,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@ -337,6 +346,7 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180816055513-1c9583448a9c/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@ -377,6 +387,7 @@ golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@ -555,6 +566,7 @@ gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

33
localip.go Normal file
View File

@ -0,0 +1,33 @@
package myip
import (
"github.com/spf13/cobra"
)
var LocalIPCmd = &cobra.Command{
Use: "local",
Short: "short hand for `myip -altc --no-pub`",
Run: func(cmd *cobra.Command, args []string) {
showList = true
showTitle = true
showCell = true
showAll = true
noPub = true
revNoPub, err := cmd.Flags().GetBool("show-public")
if err == nil && revNoPub {
noPub = false
}
Scan()
},
}
func init() {
LocalIPCmd.Flags().StringVarP(&port, "port", "p", "5080", "server port to connect")
LocalIPCmd.Flags().StringVarP(&host, "host", "u", "kumoly.io", "hostname to connect")
LocalIPCmd.Flags().StringVarP(&name, "name", "n", "", "tell the server who you are")
LocalIPCmd.Flags().BoolVar(&secure, "secure", false, "use https")
LocalIPCmd.Flags().BoolP("show-public", "P", false, "show public ip")
}

View File

@ -1,10 +1,13 @@
package cmd
package myip
import (
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/spf13/cobra"
@ -14,6 +17,15 @@ var ServerCmd = &cobra.Command{
Use: "serve",
Short: "Simple server that returns the resolved IP.",
Run: func(cmd *cobra.Command, args []string) {
if logToFile != "" {
f, err := os.OpenFile(logToFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("Error opening file: %v", err)
}
defer f.Close()
mwriter := io.MultiWriter(f, os.Stderr)
log.SetOutput(mwriter)
}
StartServer()
},
}
@ -23,11 +35,15 @@ var (
// port string
addr string
logToFile string
)
func init() {
ServerCmd.Flags().StringVarP(&port, "port", "p", "5080", "server port to listen")
ServerCmd.Flags().StringVarP(&addr, "address", "b", "0.0.0.0", "address the server binds on")
ServerCmd.Flags().StringVarP(&logToFile, "log", "f", "", "log to file if filepath provided")
}
func StartServer() error {
@ -55,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