pull/6/head
Evan Chen 2021-10-15 18:57:15 +08:00
parent 87ad47e371
commit 4af693d17c
3 changed files with 118 additions and 31 deletions

View File

@ -1,2 +1,4 @@
build: build:
go build -o bin/myip go build -o bin/myip
# release:

0
go.sum Normal file
View File

99
main.go
View File

@ -4,10 +4,12 @@ import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net" "net"
"net/http" "net/http"
"os" "os"
"strings" "strings"
"text/tabwriter"
) )
var ( var (
@ -17,16 +19,30 @@ var (
envP string envP string
host string host string
envHost string envHost string
showVer bool
showHeader bool
showAll bool
showTab bool
// iface string
// envIface string
) )
var Version = "v0.1.0"
func init() { func init() {
envP = os.Getenv("MYIP_PORT") envP = os.Getenv("MYIP_PORT")
envHost = os.Getenv("MYIP_HOST") envHost = os.Getenv("MYIP_HOST")
flag.BoolVar(&showVer, "v", false, "show version")
flag.BoolVar(&serverMode, "s", false, "run in server mode") flag.BoolVar(&serverMode, "s", false, "run in server mode")
flag.BoolVar(&showAll, "a", false, "show all interface")
flag.BoolVar(&showHeader, "t", false, "show header")
flag.BoolVar(&showTab, "T", false, "show tab")
flag.StringVar(&host, "u", "kumoly.io", "hostname to ask for") 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 listen|connect")
flag.StringVar(&addr, "b", "0.0.0.0", "the address the server binds on") flag.StringVar(&addr, "b", "0.0.0.0", "the address the server binds on")
// flag.StringVar(&port, "p", "5080", "port to listen|connect")
} }
func usage() { func usage() {
@ -36,6 +52,10 @@ func usage() {
func main() { func main() {
flag.Parse() flag.Parse()
if showVer {
fmt.Println(Version)
return
}
if envP != "" { if envP != "" {
port = envP port = envP
} }
@ -44,6 +64,27 @@ func main() {
} }
if serverMode { if serverMode {
StartServer()
} else {
var wFlag uint = 0
if showTab {
wFlag = tabwriter.Debug
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', wFlag)
if showHeader {
fmt.Fprintln(w, "\tInterface\tIP\t")
}
fmt.Fprintln(w, fmt.Sprintf("\t%v\t%v\t", "PublicIP", GetPublicIP()))
if showAll {
for k, v := range GetLocalIP() {
fmt.Fprintln(w, fmt.Sprintf("\t%v\t%v\t", strings.Trim(k, " "), v))
}
}
w.Flush()
}
}
func StartServer() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ip := r.Header.Get("X-Real-Ip") ip := r.Header.Get("X-Real-Ip")
if ip == "" { if ip == "" {
@ -59,24 +100,68 @@ func main() {
fmt.Fprint(w, ip+"\n") fmt.Fprint(w, ip+"\n")
}) })
http.HandleFunc("/version", func(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, Version)
})
err := http.ListenAndServe(addr+":"+port, nil) err := http.ListenAndServe(addr+":"+port, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
}
} else { func GetLocalIP() map[string]string {
ret := map[string]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
}
case *net.IPAddr:
continue
// ip = v.IP
// if ip.IsLoopback() {
// continue
// }
}
ret[i.Name] = ip.String()
break
}
}
return ret
}
func GetPublicIP() string {
res, err := http.Get("http://" + host + ":" + port) res, err := http.Get("http://" + host + ":" + port)
if err != nil { if err != nil {
fmt.Println(err) log.Println(err)
os.Exit(2) return ""
} }
body, err := ioutil.ReadAll(res.Body) body, err := ioutil.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
if err != nil { if err != nil {
fmt.Println(err) log.Println(err)
os.Exit(3) return ""
}
fmt.Println(strings.TrimRight(string(body), "\r\n"))
} }
return strings.TrimRight(string(body), "\r\n")
} }