add domian and defaut

master
dustoair 3 years ago
parent 64c2e7b448
commit 95042f5eb3

@ -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
```

@ -45,6 +45,8 @@ type IpInfo struct {
Province string
City string
ISP string
IP string
Domain string
}
func (ip IpInfo) String() string {

@ -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))

@ -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")
}
Loading…
Cancel
Save