Skip to content

Commit 9b1c52e

Browse files
author
Benjamin M. Schwartz
authored
Merge pull request #101 from Jigsaw-Code/bemasc-happy-eyeballs
Use implicit name resolution for TCP destinations
2 parents 4f3ce4d + 7bb30b3 commit 9b1c52e

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

integration_test/integration_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"github.com/Jigsaw-Code/outline-ss-server/service/metrics"
3030
ss "github.com/Jigsaw-Code/outline-ss-server/shadowsocks"
3131
logging "github.com/op/go-logging"
32+
"github.com/stretchr/testify/assert"
33+
"github.com/stretchr/testify/require"
3234
)
3335

3436
const maxUDPPacketSize = 64 * 1024
@@ -162,6 +164,65 @@ func TestTCPEcho(t *testing.T) {
162164
echoRunning.Wait()
163165
}
164166

167+
type statusMetrics struct {
168+
metrics.NoOpMetrics
169+
sync.Mutex
170+
statuses []string
171+
}
172+
173+
func (m *statusMetrics) AddClosedTCPConnection(clientLocation, accessKey, status string, data metrics.ProxyMetrics, timeToCipher, duration time.Duration) {
174+
m.Lock()
175+
m.statuses = append(m.statuses, status)
176+
m.Unlock()
177+
}
178+
179+
func TestRestrictedAddresses(t *testing.T) {
180+
proxyListener, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 0})
181+
require.NoError(t, err, "ListenTCP failed: %v", err)
182+
secrets := ss.MakeTestSecrets(1)
183+
cipherList, err := service.MakeTestCiphers(secrets)
184+
require.NoError(t, err)
185+
const testTimeout = 200 * time.Millisecond
186+
testMetrics := &statusMetrics{}
187+
proxy := service.NewTCPService(cipherList, nil, testMetrics, testTimeout)
188+
go proxy.Serve(proxyListener)
189+
190+
proxyHost, proxyPort, err := net.SplitHostPort(proxyListener.Addr().String())
191+
require.NoError(t, err)
192+
portNum, err := strconv.Atoi(proxyPort)
193+
require.NoError(t, err)
194+
client, err := client.NewClient(proxyHost, portNum, secrets[0], ss.TestCipher)
195+
require.NoError(t, err, "Failed to create ShadowsocksClient")
196+
197+
buf := make([]byte, 10)
198+
199+
addresses := []string{
200+
"localhost:9999",
201+
"[::1]:80",
202+
"10.0.0.1:1234",
203+
"[fc00::1]:54321",
204+
}
205+
206+
expectedStatus := []string{
207+
"ERR_ADDRESS_INVALID",
208+
"ERR_ADDRESS_INVALID",
209+
"ERR_ADDRESS_PRIVATE",
210+
"ERR_ADDRESS_PRIVATE",
211+
}
212+
213+
for _, address := range addresses {
214+
conn, err := client.DialTCP(nil, address)
215+
require.NoError(t, err, "Failed to dial %v", address)
216+
n, err := conn.Read(buf)
217+
assert.Equal(t, 0, n, "Server should close without replying on rejected address")
218+
assert.Equal(t, io.EOF, err)
219+
conn.Close()
220+
}
221+
222+
proxy.GracefulStop()
223+
assert.ElementsMatch(t, testMetrics.statuses, expectedStatus)
224+
}
225+
165226
// Metrics about one UDP packet.
166227
type udpRecord struct {
167228
location, accessKey, status string

service/tcp.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io/ioutil"
2424
"net"
2525
"sync"
26+
"syscall"
2627
"time"
2728

2829
onet "github.com/Jigsaw-Code/outline-ss-server/net"
@@ -149,18 +150,22 @@ func (s *tcpService) SetTargetIPValidator(targetIPValidator onet.TargetIPValidat
149150
}
150151

151152
func dialTarget(tgtAddr socks.Addr, proxyMetrics *metrics.ProxyMetrics, targetIPValidator onet.TargetIPValidator) (onet.DuplexConn, *onet.ConnectionError) {
152-
tgtTCPAddr, err := net.ResolveTCPAddr("tcp", tgtAddr.String())
153-
if err != nil {
154-
return nil, onet.NewConnectionError("ERR_RESOLVE_ADDRESS", fmt.Sprintf("Failed to resolve target address %v", tgtAddr.String()), err)
155-
}
156-
if err := targetIPValidator(tgtTCPAddr.IP); err != nil {
157-
return nil, err
158-
}
159-
160-
tgtTCPConn, err := net.DialTCP("tcp", nil, tgtTCPAddr)
161-
if err != nil {
153+
var ipError *onet.ConnectionError
154+
dialer := net.Dialer{Control: func(network, address string, c syscall.RawConn) error {
155+
ip, _, _ := net.SplitHostPort(address)
156+
ipError = targetIPValidator(net.ParseIP(ip))
157+
if ipError != nil {
158+
return errors.New(ipError.Message)
159+
}
160+
return nil
161+
}}
162+
tgtConn, err := dialer.Dial("tcp", tgtAddr.String())
163+
if ipError != nil {
164+
return nil, ipError
165+
} else if err != nil {
162166
return nil, onet.NewConnectionError("ERR_CONNECT", "Failed to connect to target", err)
163167
}
168+
tgtTCPConn := tgtConn.(*net.TCPConn)
164169
tgtTCPConn.SetKeepAlive(true)
165170
return metrics.MeasureConn(tgtTCPConn, &proxyMetrics.ProxyTarget, &proxyMetrics.TargetProxy), nil
166171
}

0 commit comments

Comments
 (0)