From 95042f5eb36b0e55815582106776c9722bd156f9 Mon Sep 17 00:00:00 2001 From: dustoair <107600816+dustoair@users.noreply.github.com> Date: Fri, 22 Jul 2022 17:52:03 +0800 Subject: [PATCH] add domian and defaut --- README.md | 31 ++++++++++++++++++++++++++++++- ip2region/ip2Region.go | 2 ++ main.go | 9 +++++++-- util/getRemoteIP.go | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 util/getRemoteIP.go diff --git a/README.md b/README.md index 4eb66a7..5691c7d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # ip地址归属地查询 http://127.0.0.1:8080?address=git.sre.ink +http://127.0.0.1:8080 ## build arm64 on oracle k8s ```bash cd /root @@ -9,7 +10,7 @@ cd IPRegion CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on go mod tidy CGO_ENABLED=0 GOOS=linux GOARCH=arm64 GO111MODULE=on go build -v -a -o ipregion.bin main.go docker build -t sre/ipregion:arm64 . -kubectl rollout restart deployment -n sre ginbase +kubectl rollout restart deployment -n sre ipregion ``` @@ -29,4 +30,32 @@ BenchmarkBinarySearch-16 34378 34766 ns/op PASS ``` +## ingress +```yaml +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: ingress-ip2region + namespace: sre + annotations: + kubernetes.io/ingress.class: "nginx" # 自动签发开关 + cert-manager.io/cluster-issuer: "letsencrypt-prod-http01" # 自动签发开关 +spec: + tls: + - hosts: + - test.com + secretName: ingress-tls-test-com # 需要修改 + rules: + - host: test.com + http: + paths: + - path: / + backend: + service: + name: ipregion + port: + number: 8080 + pathType: ImplementationSpecific +``` + diff --git a/ip2region/ip2Region.go b/ip2region/ip2Region.go index f290921..b4e22e4 100644 --- a/ip2region/ip2Region.go +++ b/ip2region/ip2Region.go @@ -45,6 +45,8 @@ type IpInfo struct { Province string City string ISP string + IP string + Domain string } func (ip IpInfo) String() string { diff --git a/main.go b/main.go index 430391e..79596d1 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "IPRegion/global" "IPRegion/ip2region" + "IPRegion/util" "encoding/json" "fmt" "log" @@ -18,9 +19,13 @@ func main() { func IPHandle(w http.ResponseWriter, req *http.Request) { address := req.FormValue("address") var ipInfo ip2region.IpInfo - if address != "" { - ipInfo, _ = ip2region.GetIPInfo(address, global.SearchType) + if address == "" { + addressReq, _ := util.GetIP(req) + address = addressReq } + + ipInfo, _ = ip2region.GetIPInfo(address, global.SearchType) + ipInfo.Domain = address res, _ := json.Marshal(ipInfo) fmt.Fprintln(w, string(res)) diff --git a/util/getRemoteIP.go b/util/getRemoteIP.go new file mode 100644 index 0000000..b23bf5f --- /dev/null +++ b/util/getRemoteIP.go @@ -0,0 +1,34 @@ +package util + +import ( + "errors" + "net" + "net/http" + "strings" +) + +// GetIP returns request real ip. +func GetIP(r *http.Request) (string, error) { + ip := r.Header.Get("X-Real-IP") + if net.ParseIP(ip) != nil { + return ip, nil + } + + ip = r.Header.Get("X-Forward-For") + for _, i := range strings.Split(ip, ",") { + if net.ParseIP(i) != nil { + return i, nil + } + } + + ip, _, err := net.SplitHostPort(r.RemoteAddr) + if err != nil { + return "", err + } + + if net.ParseIP(ip) != nil { + return ip, nil + } + + return "", errors.New("no valid ip found") +}