Skip to content

Commit 9870e97

Browse files
committed
Merge pull request getlantern#18 from getlantern/2135
getlantern#2135 Switched to using net.SplitHostAndPort
2 parents 7740cf1 + cbd64fe commit 9870e97

10 files changed

Lines changed: 58 additions & 35 deletions

File tree

src/github.com/getlantern/enproxy/conn_http.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net"
77
"net/http"
88
"strconv"
9-
"strings"
109
)
1110

1211
// Intercept intercepts a CONNECT request, hijacks the underlying client
@@ -69,8 +68,8 @@ func respondOK(writer io.Writer, req *http.Request) error {
6968
// hostIncludingPort extracts the host:port from a request. It fills in a
7069
// a default port if none was found in the request.
7170
func hostIncludingPort(req *http.Request, defaultPort int) string {
72-
parts := strings.Split(req.Host, ":")
73-
if len(parts) == 1 {
71+
_, port, err := net.SplitHostPort(req.Host)
72+
if port == "" || err != nil {
7473
return req.Host + ":" + strconv.Itoa(defaultPort)
7574
} else {
7675
return req.Host

src/github.com/getlantern/enproxy/proxy.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ func (p *Proxy) handleWrite(resp http.ResponseWriter, req *http.Request, lc *laz
160160
// Pipe request
161161
n, err := io.Copy(connOut, req.Body)
162162
if p.OnBytesReceived != nil && n > 0 {
163-
p.OnBytesReceived(clientIpFor(req), lc.addr, req, n)
163+
clientIp := clientIpFor(req)
164+
if clientIp != "" {
165+
p.OnBytesReceived(clientIp, lc.addr, req, n)
166+
}
164167
}
165168
if err != nil && err != io.EOF {
166169
badGateway(resp, fmt.Sprintf("Unable to write to connOut: %s", err))
@@ -222,7 +225,7 @@ func (p *Proxy) handleRead(resp http.ResponseWriter, req *http.Request, lc *lazy
222225

223226
// Write if necessary
224227
if n > 0 {
225-
if p.OnBytesSent != nil && n > 0 {
228+
if clientIp != "" && p.OnBytesSent != nil && n > 0 {
226229
p.OnBytesSent(clientIp, lc.addr, req, int64(n))
227230
}
228231

@@ -303,7 +306,12 @@ func (p *Proxy) getLazyConn(id string, addr string) (l *lazyConn, isNew bool) {
303306
func clientIpFor(req *http.Request) string {
304307
clientIp := req.Header.Get("X-Forwarded-For")
305308
if clientIp == "" {
306-
clientIp = strings.Split(req.RemoteAddr, ":")[0]
309+
clientIp, _, err := net.SplitHostPort(req.RemoteAddr)
310+
if err != nil {
311+
log.Debugf("Unable to split RemoteAddr %v: %v", err)
312+
return ""
313+
}
314+
return clientIp
307315
}
308316
// clientIp may contain multiple ips, use the first
309317
ips := strings.Split(clientIp, ",")

src/github.com/getlantern/flashlight/client/handler.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net"
77
"net/http"
88
"strconv"
9-
"strings"
109
)
1110

1211
const (
@@ -113,8 +112,8 @@ func respondBadGateway(w io.Writer, msg string) error {
113112
// hostIncludingPort extracts the host:port from a request. It fills in a
114113
// a default port if none was found in the request.
115114
func hostIncludingPort(req *http.Request, defaultPort int) string {
116-
parts := strings.Split(req.Host, ":")
117-
if len(parts) == 1 {
115+
_, port, err := net.SplitHostPort(req.Host)
116+
if port == "" || err != nil {
118117
return req.Host + ":" + strconv.Itoa(defaultPort)
119118
} else {
120119
return req.Host

src/github.com/getlantern/flashlight/client/stats.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package client
22

33
import (
44
"net"
5-
"strings"
65

76
"github.com/getlantern/bytecounting"
87

@@ -16,7 +15,12 @@ func withStats(conn net.Conn, err error) (net.Conn, error) {
1615
if err != nil {
1716
return conn, err
1817
}
19-
ip := strings.Split(conn.RemoteAddr().String(), ":")[0]
18+
remoteAddr := conn.RemoteAddr().String()
19+
ip, _, err := net.SplitHostPort(remoteAddr)
20+
if err != nil {
21+
log.Debugf("Unable to split host and port for %v, skipping byte counting: %v", remoteAddr, err)
22+
return conn, nil
23+
}
2024
return &bytecounting.Conn{
2125
Orig: conn,
2226
OnRead: func(bytes int64) {

src/github.com/getlantern/flashlight/server/server.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88
"reflect"
99
"strconv"
10-
"strings"
1110
"sync"
1211
"time"
1312

@@ -181,14 +180,16 @@ func (server *Server) stopNattywad() {
181180
}
182181

183182
func mapPort(addr string, port int) error {
184-
parts := strings.Split(addr, ":")
183+
internalIP, internalPortString, err := net.SplitHostPort(addr)
184+
if err != nil {
185+
return fmt.Errorf("Unable to split host and port for %v: %v", addr, err)
186+
}
185187

186-
internalPort, err := strconv.Atoi(parts[1])
188+
internalPort, err := strconv.Atoi(internalPortString)
187189
if err != nil {
188190
return fmt.Errorf("Unable to parse local port: ")
189191
}
190192

191-
internalIP := parts[0]
192193
if internalIP == "" {
193194
internalIP, err = determineInternalIP()
194195
if err != nil {
@@ -234,14 +235,14 @@ func determineInternalIP() (string, error) {
234235
return "", fmt.Errorf("Unable to determine local IP: %s", err)
235236
}
236237
defer conn.Close()
237-
return strings.Split(conn.LocalAddr().String(), ":")[0], nil
238+
host, _, err := net.SplitHostPort(conn.LocalAddr().String())
239+
return host, err
238240
}
239241

240242
func onBytesGiven(destAddr string, req *http.Request, bytes int64) {
241-
port := "0"
242-
parts := strings.Split(destAddr, ":")
243-
if len(parts) > 1 {
244-
port = parts[1]
243+
_, port, _ := net.SplitHostPort(destAddr)
244+
if port == "" {
245+
port = "0"
245246
}
246247

247248
given := statreporter.CountryDim().

src/github.com/getlantern/flashlight/statserver/statserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (s *server) listenAndServe() {
9090
var err error
9191
s.l, err = net.Listen("tcp", s.addr)
9292
if err != nil {
93-
log.Errorf("Unable to listen at %v: %v", err)
93+
log.Errorf("Unable to listen at %v: %v", s.addr, err)
9494
return
9595
}
9696
s.clients = make(map[int]*client)

src/github.com/getlantern/flashlight/util/http.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package util
33
import (
44
"crypto/tls"
55
"fmt"
6+
"net"
67
"net/http"
78
"net/url"
8-
"strings"
99

1010
"github.com/getlantern/keyman"
1111
)
@@ -26,7 +26,11 @@ func HTTPClient(rootCA string, proxyAddr string) (*http.Client, error) {
2626
}
2727
if proxyAddr != "" {
2828
tr.Proxy = func(req *http.Request) (*url.URL, error) {
29-
noHostSpecified := len(strings.Split(proxyAddr, ":")[0]) == 0
29+
host, _, err := net.SplitHostPort(proxyAddr)
30+
if err != nil {
31+
return nil, fmt.Errorf("Unable to split host and port for %v: %v", proxyAddr, err)
32+
}
33+
noHostSpecified := host == ""
3034
if noHostSpecified {
3135
// For addresses of the form ":8080", prepend the loopback IP
3236
proxyAddr = "127.0.0.1" + proxyAddr

src/github.com/getlantern/fronted/fronted_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net"
77
"net/http"
88
"strconv"
9-
"strings"
109
"sync"
1110
"testing"
1211
"time"
@@ -236,9 +235,11 @@ func startServer(t *testing.T, allowNonGlobal bool) net.Listener {
236235
}
237236

238237
func dialerFor(t *testing.T, l net.Listener) *Dialer {
239-
addrParts := strings.Split(l.Addr().String(), ":")
240-
host := addrParts[0]
241-
port, err := strconv.Atoi(addrParts[1])
238+
host, portString, err := net.SplitHostPort(l.Addr().String())
239+
if err != nil {
240+
t.Fatalf("Unable to split host and port: %v", err)
241+
}
242+
port, err := strconv.Atoi(portString)
242243
if err != nil {
243244
t.Fatalf("Unable to parse port: %s", err)
244245
}

src/github.com/getlantern/fronted/server.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net"
77
"net/http"
88
"os"
9-
"strings"
109
"time"
1110

1211
"github.com/getlantern/enproxy"
@@ -103,7 +102,11 @@ func (server *Server) listen() (net.Listener, error) {
103102
}
104103

105104
func (server *Server) listenTLS() (net.Listener, error) {
106-
err := server.CertContext.InitServerCert(strings.Split(server.Addr, ":")[0])
105+
host, _, err := net.SplitHostPort(server.Addr)
106+
if err != nil {
107+
return nil, fmt.Errorf("Unable to split host and port for %v: %v", server.Addr, err)
108+
}
109+
err = server.CertContext.InitServerCert(host)
107110
if err != nil {
108111
return nil, fmt.Errorf("Unable to init server cert: %s", err)
109112
}
@@ -161,7 +164,12 @@ func (server *Server) Serve(l net.Listener) error {
161164
// in a countingConn if an InstanceId was configured.
162165
func (server *Server) dialDestination(addr string) (net.Conn, error) {
163166
if !server.AllowNonGlobalDestinations {
164-
host := strings.Split(addr, ":")[0]
167+
host, _, err := net.SplitHostPort(addr)
168+
if err != nil {
169+
err = fmt.Errorf("Unable to split host and port for %v: %v", addr, err)
170+
log.Error(err.Error())
171+
return nil, err
172+
}
165173
ipAddr, err := net.ResolveIPAddr("ip", host)
166174
if err != nil {
167175
err = fmt.Errorf("Unable to resolve destination IP addr: %s", err)

src/github.com/getlantern/tlsdialer/tlsdialer.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ package tlsdialer
66
import (
77
"crypto/tls"
88
"crypto/x509"
9+
"fmt"
910
"net"
10-
"strings"
1111
"time"
1212

1313
"github.com/getlantern/golog"
@@ -126,11 +126,10 @@ func DialForTimings(dialer *net.Dialer, network, addr string, sendServerName boo
126126
result.ConnectTime = time.Now().Sub(start)
127127
log.Tracef("Dialed in %s", result.ConnectTime)
128128

129-
colonPos := strings.LastIndex(addr, ":")
130-
if colonPos == -1 {
131-
colonPos = len(addr)
129+
hostname, _, err := net.SplitHostPort(addr)
130+
if err != nil {
131+
return result, fmt.Errorf("Unable to split host and port for %v: %v", addr, err)
132132
}
133-
hostname := addr[:colonPos]
134133

135134
if config == nil {
136135
config = &tls.Config{}

0 commit comments

Comments
 (0)