Skip to content

Commit a92c4e1

Browse files
author
pzrr@qq.com
committed
fixed for devfeel#20 cann't get RemoteIP and RemoteAddr in win10
1 parent ad5c77b commit a92c4e1

2 files changed

Lines changed: 7 additions & 6 deletions

File tree

‎example/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func main() {
6464

6565
func Index(ctx dotweb.Context) error {
6666
ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
67-
_, err := ctx.WriteStringC(201, "index => ", ctx.RouterParams)
67+
_, err := ctx.WriteStringC(201, "index => ", ctx.RemoteIP())
6868
return err
6969
}
7070

‎request.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,13 @@ func (req *Request) PostBody() []byte {
110110
//RemoteAddr to an "IP" address
111111
func (req *Request) RemoteIP() string {
112112
fullIp := req.Request.RemoteAddr
113-
s := strings.Split(fullIp, ":")
114-
if len(s) > 1 {
115-
return s[0]
116-
} else {
117-
return fullIp
113+
//special: if run in win10, localIp will be like "[::]:port"
114+
//fixed for #20 cann't get RemoteIP and RemoteAddr in win10
115+
lastFlagIndex := strings.LastIndex(fullIp, ":")
116+
if lastFlagIndex >= 0 {
117+
return fullIp[:lastFlagIndex]
118118
}
119+
return fullIp
119120
}
120121

121122
//RemoteAddr to an "IP:port" address

0 commit comments

Comments
 (0)