Skip to content

Commit

Permalink
Move geoip check before net.ListenPacket
Browse files Browse the repository at this point in the history
This ensures that `clientLocation` is populated before the error
return, which triggers a metrics report that includes location
via an earlier `defer`.
  • Loading branch information
Ben Schwartz committed May 21, 2020
1 parent 8c69318 commit b6e7da8
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions shadowsocks/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,16 @@ func (s *udpService) Start() {

targetConn, clientLocation := nm.Get(clientAddr.String())
if targetConn == nil {
targetConn, err = net.ListenPacket("udp", "")
if err != nil {
return onet.NewConnectionError("ERR_CREATE_SOCKET", "Failed to create UDP socket", err)
}
clientLocation, locErr := s.m.GetLocation(clientAddr)
if locErr != nil {
logger.Warningf("Failed location lookup: %v", locErr)
}
logger.Debugf("Got location \"%v\" for IP %v", clientLocation, clientAddr.String())

targetConn, err = net.ListenPacket("udp", "")
if err != nil {
return onet.NewConnectionError("ERR_CREATE_SOCKET", "Failed to create UDP socket", err)
}
nm.Add(clientAddr, s.clientConn, cipher, targetConn, clientLocation, keyID)
}
logger.Debugf("UDP NAT: client %v <-> proxy exit %v", clientAddr, targetConn.LocalAddr())
Expand Down

0 comments on commit b6e7da8

Please sign in to comment.