Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8278a63

Browse files
committed
完善 IP 获取
1 parent 14ce9c3 commit 8278a63

File tree

2 files changed

+5
-21
lines changed

2 files changed

+5
-21
lines changed

websites/code/studygolang/src/controller/websocket.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
package controller
88

99
import (
10-
"go.net/websocket"
11-
"logger"
12-
"service"
1310
"strconv"
14-
"strings"
1511
"sync"
1612
"time"
13+
14+
"go.net/websocket"
15+
"logger"
16+
"service"
1717
"util"
1818
)
1919

@@ -30,12 +30,7 @@ func WsHandler(wsConn *websocket.Conn) {
3030
req := wsConn.Request()
3131
user, err := strconv.Atoi(req.FormValue("uid"))
3232
if err != nil || user == 0 {
33-
remoteAddr := req.Header.Get("Remote_addr")
34-
if remoteAddr == "" {
35-
remoteAddr = req.RemoteAddr
36-
}
37-
pos := strings.LastIndex(remoteAddr, ":")
38-
ip := remoteAddr[:pos]
33+
ip := util.Ip(req)
3934
logger.Debugln("user ip:", ip)
4035
user = int(util.Ip2long(ip))
4136
}

websites/code/studygolang/src/util/tool.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
package util
88

99
import (
10-
"encoding/binary"
11-
"net"
1210
"strconv"
1311
"strings"
1412
)
@@ -30,12 +28,3 @@ func Join(ins []int, sep string) string {
3028
}
3129
return strings.Join(strSlice, sep)
3230
}
33-
34-
func Ip2long(ipstr string) uint32 {
35-
ip := net.ParseIP(ipstr)
36-
if ip == nil {
37-
return 0
38-
}
39-
ip = ip.To4()
40-
return binary.BigEndian.Uint32(ip)
41-
}

0 commit comments

Comments
 (0)