derp/derphttp: don't use x/net/proxy for SOCKS on iOS

We don't want those extra dependencies on iOS, at least yet.

Especially since there's no way to set the relevant environment
variables so it's just bloat with no benefits. Perhaps we'll need to
do SOCKS on iOS later, but probably differently if/when so.

Updates #227

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2020-05-13 22:35:17 -07:00
parent 42a0e0c601
commit 040a0d5121
2 changed files with 32 additions and 8 deletions

View File

@ -24,7 +24,6 @@ import (
"sync" "sync"
"time" "time"
"golang.org/x/net/proxy"
"tailscale.com/derp" "tailscale.com/derp"
"tailscale.com/net/dnscache" "tailscale.com/net/dnscache"
"tailscale.com/net/tlsdial" "tailscale.com/net/tlsdial"
@ -149,11 +148,10 @@ func (c *Client) connect(ctx context.Context, caller string) (client *derp.Clien
host := c.url.Hostname() host := c.url.Hostname()
hostOrIP := host hostOrIP := host
var d dialer = new(net.Dialer) var stdDialer dialer = new(net.Dialer)
var usingProxy bool var dialer = stdDialer
if cd, ok := proxy.FromEnvironmentUsing(d).(dialer); ok { if wrapDialer != nil {
usingProxy = d != cd dialer = wrapDialer(dialer)
d = cd
} }
if c.DNSCache != nil { if c.DNSCache != nil {
@ -161,12 +159,14 @@ func (c *Client) connect(ctx context.Context, caller string) (client *derp.Clien
if err == nil { if err == nil {
hostOrIP = ip.String() hostOrIP = ip.String()
} }
if err != nil && !usingProxy { if err != nil && dialer == stdDialer {
// Return an error if we're not using a dial
// proxy that can do DNS lookups for us.
return nil, err return nil, err
} }
} }
tcpConn, err = d.DialContext(ctx, "tcp", net.JoinHostPort(hostOrIP, urlPort(c.url))) tcpConn, err = dialer.DialContext(ctx, "tcp", net.JoinHostPort(hostOrIP, urlPort(c.url)))
if err != nil { if err != nil {
return nil, fmt.Errorf("dial of %q: %v", host, err) return nil, fmt.Errorf("dial of %q: %v", host, err)
} }
@ -326,3 +326,7 @@ func (c *Client) closeForReconnect(brokenClient *derp.Client) {
} }
var ErrClientClosed = errors.New("derphttp.Client closed") var ErrClientClosed = errors.New("derphttp.Client closed")
// wrapDialer, if non-nil, specifies a function to wrap a dialer in a
// SOCKS-using dialer. It's set conditionally by socks.go.
var wrapDialer func(dialer) dialer

20
derp/derphttp/socks.go Normal file
View File

@ -0,0 +1,20 @@
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !ios
package derphttp
import "golang.org/x/net/proxy"
func init() {
wrapDialer = wrapSocks
}
func wrapSocks(d dialer) dialer {
if cd, ok := proxy.FromEnvironmentUsing(d).(dialer); ok {
return cd
}
return d
}