Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use implicit name resolution for TCP destinations #101

Merged
merged 4 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions integration_test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"github.com/Jigsaw-Code/outline-ss-server/service/metrics"
ss "github.com/Jigsaw-Code/outline-ss-server/shadowsocks"
logging "github.com/op/go-logging"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const maxUDPPacketSize = 64 * 1024
Expand Down Expand Up @@ -162,6 +164,65 @@ func TestTCPEcho(t *testing.T) {
echoRunning.Wait()
}

type statusMetrics struct {
metrics.NoOpMetrics
sync.Mutex
statuses []string
}

func (m *statusMetrics) AddClosedTCPConnection(clientLocation, accessKey, status string, data metrics.ProxyMetrics, timeToCipher, duration time.Duration) {
m.Lock()
m.statuses = append(m.statuses, status)
m.Unlock()
}

func TestRestrictedAddresses(t *testing.T) {
proxyListener, err := net.ListenTCP("tcp", &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 0})
require.NoError(t, err, "ListenTCP failed: %v", err)
secrets := ss.MakeTestSecrets(1)
cipherList, err := service.MakeTestCiphers(secrets)
require.NoError(t, err)
const testTimeout = 200 * time.Millisecond
testMetrics := &statusMetrics{}
proxy := service.NewTCPService(cipherList, nil, testMetrics, testTimeout)
go proxy.Serve(proxyListener)

proxyHost, proxyPort, err := net.SplitHostPort(proxyListener.Addr().String())
require.NoError(t, err)
portNum, err := strconv.Atoi(proxyPort)
require.NoError(t, err)
client, err := client.NewClient(proxyHost, portNum, secrets[0], ss.TestCipher)
require.NoError(t, err, "Failed to create ShadowsocksClient")

buf := make([]byte, 10)

addresses := []string{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put the errors next to the IPs for readability?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"localhost:9999",
"[::1]:80",
"10.0.0.1:1234",
"[fc00::1]:54321",
}

expectedStatus := []string{
"ERR_ADDRESS_INVALID",
"ERR_ADDRESS_INVALID",
"ERR_ADDRESS_PRIVATE",
"ERR_ADDRESS_PRIVATE",
}

for _, address := range addresses {
conn, err := client.DialTCP(nil, address)
require.NoError(t, err, "Failed to dial %v", address)
n, err := conn.Read(buf)
assert.Equal(t, 0, n, "Server should close without replying on rejected address")
assert.Equal(t, io.EOF, err)
conn.Close()
}

proxy.GracefulStop()
assert.ElementsMatch(t, testMetrics.statuses, expectedStatus)
}

// Metrics about one UDP packet.
type udpRecord struct {
location, accessKey, status string
Expand Down
25 changes: 15 additions & 10 deletions service/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io/ioutil"
"net"
"sync"
"syscall"
"time"

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

func dialTarget(tgtAddr socks.Addr, proxyMetrics *metrics.ProxyMetrics, targetIPValidator onet.TargetIPValidator) (onet.DuplexConn, *onet.ConnectionError) {
tgtTCPAddr, err := net.ResolveTCPAddr("tcp", tgtAddr.String())
if err != nil {
return nil, onet.NewConnectionError("ERR_RESOLVE_ADDRESS", fmt.Sprintf("Failed to resolve target address %v", tgtAddr.String()), err)
}
if err := targetIPValidator(tgtTCPAddr.IP); err != nil {
return nil, err
}

tgtTCPConn, err := net.DialTCP("tcp", nil, tgtTCPAddr)
if err != nil {
var ipError *onet.ConnectionError
dialer := net.Dialer{Control: func(network, address string, c syscall.RawConn) error {
ip, _, _ := net.SplitHostPort(address)
ipError = targetIPValidator(net.ParseIP(ip))
if ipError != nil {
return errors.New(ipError.Message)
}
return nil
}}
tgtConn, err := dialer.Dial("tcp", tgtAddr.String())
if ipError != nil {
return nil, ipError
} else if err != nil {
return nil, onet.NewConnectionError("ERR_CONNECT", "Failed to connect to target", err)
}
tgtTCPConn := tgtConn.(*net.TCPConn)
tgtTCPConn.SetKeepAlive(true)
return metrics.MeasureConn(tgtTCPConn, &proxyMetrics.ProxyTarget, &proxyMetrics.TargetProxy), nil
}
Expand Down