Skip to content

Commit 2582d97

Browse files
authored
make mocking conns slightly easier for handleNewTCPConn (#200)
1 parent 5852927 commit 2582d97

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

cmd/application/conns.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,30 @@ func (cm *connManager) handleNewConn(regManager *cj.RegistrationManager, clientC
113113
cm.handleNewTCPConn(regManager, clientConn, originalDstIP)
114114
}
115115

116+
func getRemoteAsIP(conn net.Conn) (remoteIP net.IP) {
117+
remoteAddr := conn.RemoteAddr()
118+
switch addr := remoteAddr.(type) {
119+
case *net.TCPAddr:
120+
remoteIP = addr.IP
121+
case *net.UDPAddr:
122+
remoteIP = addr.IP
123+
default:
124+
a := remoteAddr.String()
125+
// try parsing the returned address string as host:port
126+
host, _, err := net.SplitHostPort(a)
127+
if err != nil {
128+
// try parsing the returned address string as just an IP address
129+
remoteIP = net.ParseIP(a)
130+
break
131+
} else {
132+
// try parsing the returned host portion of the address as an IP address as opposed to a
133+
// domain name or other string.
134+
remoteIP = net.ParseIP(host)
135+
}
136+
}
137+
return
138+
}
139+
116140
func (cm *connManager) handleNewTCPConn(regManager *cj.RegistrationManager, clientConn net.Conn, originalDstIP net.IP) {
117141
isIPv4 := originalDstIP.To4() != nil
118142
var originalDst, originalSrc string
@@ -125,15 +149,13 @@ func (cm *connManager) handleNewTCPConn(regManager *cj.RegistrationManager, clie
125149
flowDescription := fmt.Sprintf("%s -> %s ", originalSrc, originalDst)
126150
logger := log.New(os.Stdout, "[CONN] "+flowDescription, golog.Ldate|golog.Lmicroseconds)
127151

128-
remoteAddr := clientConn.RemoteAddr()
129-
var remoteIP net.IP
130-
if addr, ok := remoteAddr.(*net.TCPAddr); ok {
131-
remoteIP = addr.IP
132-
} else if addr, ok := remoteAddr.(*net.UDPAddr); ok {
133-
remoteIP = addr.IP
134-
} else {
152+
remoteIP := getRemoteAsIP(clientConn)
153+
if remoteIP == nil {
154+
// Socket returned non-IP Remote Address - we can't really use that. If testing w/ pipe try
155+
// wrapping with struct to provide mock RemoteAddr which return a real IP address.
135156
return
136157
}
158+
137159
var asn uint = 0
138160
var cc string
139161
var err error

0 commit comments

Comments
 (0)