Skip to content

Commit b93ca3c

Browse files
committed
view ip
1 parent e308b3a commit b93ca3c

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

proxy.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ func (p *proxy) admin(w http.ResponseWriter, r *http.Request) {
4646
}
4747
newData = newData + service + "=>" + newUrl + "\n"
4848
}
49+
ip := ClientIP(r)
50+
if ip != "" {
51+
ip = "本机IP[" + ip + "]<br>"
52+
}
4953
w.Header().Set("Content-Type", "text/html;charset=utf-8")
50-
w.Write([]byte("<html><head><title>开发联调神器</title></head><form method=\"POST\"><center>开发联调神器(保存前先刷新)<br><textarea placeholder=\"test=>192.168.8.8:8080\r\n效果:\r\n" + GetURL(r) + "/test/product/list => http://192.168.8.8:8080/product/list\" autofocus name=\"data\" rows=\"30\" cols=\"100\">" + newData + "</textarea><br><input type=\"submit\" value=\"提交\"></center></form><html>"))
54+
w.Write([]byte("<html><head><title>开发联调神器</title></head><form method=\"POST\"><center>开发联调神器(先刷新再保存)<br>" + ip + "<textarea placeholder=\"test=>192.168.8.8:8080\r\n效果:\r\n" + GetURL(r) + "/test/product/list => http://192.168.8.8:8080/product/list\" autofocus name=\"data\" rows=\"30\" cols=\"100\">" + newData + "</textarea><br><input type=\"submit\" value=\"提交\"></center></form><html>"))
5155
}
5256

5357
func (p *proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -91,23 +95,4 @@ func (p *proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9195
proxy.ServeHTTP(w, r)
9296
}
9397

94-
//获取url中的第一个参数
95-
func singleJoiningSlash(a, b string) string {
96-
aslash := strings.HasSuffix(a, "/")
97-
bslash := strings.HasPrefix(b, "/")
98-
switch {
99-
case aslash && bslash:
100-
return a + b[1:]
101-
case !aslash && !bslash:
102-
return a + "/" + b
103-
}
104-
return a + b
105-
}
10698

107-
func GetURL(r *http.Request) (Url string) {
108-
scheme := "http://"
109-
if r.TLS != nil {
110-
scheme = "https://"
111-
}
112-
return strings.Join([]string{scheme, r.Host}, "")
113-
}

util.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package dev
2+
3+
import (
4+
"net"
5+
"net/http"
6+
"strings"
7+
)
8+
9+
//获取url中的第一个参数
10+
func singleJoiningSlash(a, b string) string {
11+
aslash := strings.HasSuffix(a, "/")
12+
bslash := strings.HasPrefix(b, "/")
13+
switch {
14+
case aslash && bslash:
15+
return a + b[1:]
16+
case !aslash && !bslash:
17+
return a + "/" + b
18+
}
19+
return a + b
20+
}
21+
22+
//获取协议
23+
func GetURL(r *http.Request) (Url string) {
24+
scheme := "http://"
25+
if r.TLS != nil {
26+
scheme = "https://"
27+
}
28+
return strings.Join([]string{scheme, r.Host}, "")
29+
}
30+
31+
//获取ClientIP
32+
func ClientIP(r *http.Request) string {
33+
xForwardedFor := r.Header.Get("X-Forwarded-For")
34+
ip := strings.TrimSpace(strings.Split(xForwardedFor, ",")[0])
35+
if ip != "" {
36+
return ip
37+
}
38+
ip = strings.TrimSpace(r.Header.Get("X-Real-Ip"))
39+
if ip != "" {
40+
return ip
41+
}
42+
if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil {
43+
return ip
44+
}
45+
return ""
46+
}

0 commit comments

Comments
 (0)