Skip to content
This repository was archived by the owner on May 6, 2024. It is now read-only.

Commit 2f14928

Browse files
author
Benjamin M. Schwartz
authored
Merge pull request #96 from Jigsaw-Code/bemasc-refactor
cleanup: Refactor tunnel creation function arguments
2 parents 1589410 + 6e331a5 commit 2f14928

File tree

6 files changed

+32
-44
lines changed

6 files changed

+32
-44
lines changed

outline/android/tun2socks.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/Jigsaw-Code/outline-go-tun2socks/outline"
2323
"github.com/Jigsaw-Code/outline-go-tun2socks/tunnel"
24+
"github.com/Jigsaw-Code/outline-ss-server/client"
2425
"github.com/eycorsican/go-tun2socks/common/log"
2526
)
2627

@@ -58,7 +59,11 @@ func ConnectShadowsocksTunnel(fd int, host string, port int, password, cipher st
5859
if err != nil {
5960
return nil, err
6061
}
61-
t, err := outline.NewTunnel(host, port, password, cipher, isUDPEnabled, tun)
62+
ssclient, err := client.NewClient(host, port, password, cipher)
63+
if err != nil {
64+
return nil, fmt.Errorf("failed to construct Shadowsocks client: %v", err)
65+
}
66+
t, err := outline.NewTunnel(ssclient, isUDPEnabled, tun)
6267
if err != nil {
6368
return nil, err
6469
}

outline/apple/tun2socks.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"time"
2424

2525
"github.com/Jigsaw-Code/outline-go-tun2socks/outline"
26+
"github.com/Jigsaw-Code/outline-ss-server/client"
2627
)
2728

2829
// OutlineTunnel embeds the tun2socks.Tunnel interface so it gets exported by gobind.
@@ -66,5 +67,10 @@ func ConnectShadowsocksTunnel(tunWriter TunWriter, host string, port int, passwo
6667
} else if port <= 0 || port > math.MaxUint16 {
6768
return nil, fmt.Errorf("Invalid port number: %v", port)
6869
}
69-
return outline.NewTunnel(host, port, password, cipher, isUDPEnabled, tunWriter)
70+
ssclient, err := client.NewClient(host, port, password, cipher)
71+
if err != nil {
72+
return nil, fmt.Errorf("failed to construct Shadowsocks client: %v", err)
73+
}
74+
75+
return outline.NewTunnel(ssclient, isUDPEnabled, tunWriter)
7076
}

outline/electron/main.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
oss "github.com/Jigsaw-Code/outline-go-tun2socks/outline/shadowsocks"
2828
"github.com/Jigsaw-Code/outline-go-tun2socks/shadowsocks"
29+
"github.com/Jigsaw-Code/outline-ss-server/client"
2930
"github.com/eycorsican/go-tun2socks/common/log"
3031
_ "github.com/eycorsican/go-tun2socks/common/log/simple" // Register a simple logger.
3132
"github.com/eycorsican/go-tun2socks/core"
@@ -115,16 +116,20 @@ func main() {
115116
// Output packets to TUN device
116117
core.RegisterOutputFn(tunDevice.Write)
117118

119+
ssclient, err := client.NewClient(*args.proxyHost, *args.proxyPort, *args.proxyPassword, *args.proxyCipher)
120+
if err != nil {
121+
log.Errorf("Failed to construct Shadowsocks client: %v", err)
122+
os.Exit(oss.IllegalConfiguration)
123+
}
124+
118125
// Register TCP and UDP connection handlers
119-
core.RegisterTCPConnHandler(
120-
shadowsocks.NewTCPHandler(*args.proxyHost, *args.proxyPort, *args.proxyPassword, *args.proxyCipher))
126+
core.RegisterTCPConnHandler(shadowsocks.NewTCPHandler(ssclient))
121127
if *args.dnsFallback {
122128
// UDP connectivity not supported, fall back to DNS over TCP.
123129
log.Debugf("Registering DNS fallback UDP handler")
124130
core.RegisterUDPConnHandler(dnsfallback.NewUDPHandler())
125131
} else {
126-
core.RegisterUDPConnHandler(
127-
shadowsocks.NewUDPHandler(*args.proxyHost, *args.proxyPort, *args.proxyPassword, *args.proxyCipher, udpTimeout))
132+
core.RegisterUDPConnHandler(shadowsocks.NewUDPHandler(ssclient, udpTimeout))
128133
}
129134

130135
// Configure LWIP stack to receive input data from the TUN device

outline/tunnel.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package outline
1616

1717
import (
1818
"errors"
19-
"fmt"
2019
"io"
2120
"time"
2221

@@ -41,10 +40,7 @@ type Tunnel interface {
4140
type outlinetunnel struct {
4241
tunnel.Tunnel
4342
lwipStack core.LWIPStack
44-
host string
45-
port int
46-
password string
47-
cipher string
43+
client shadowsocks.Client
4844
isUDPEnabled bool // Whether the tunnel supports proxying UDP.
4945
}
5046

@@ -56,30 +52,22 @@ type outlinetunnel struct {
5652
// `cipher` is the encryption cipher used by the Shadowsocks proxy.
5753
// `isUDPEnabled` indicates if the Shadowsocks proxy and the network support proxying UDP traffic.
5854
// `tunWriter` is used to output packets back to the TUN device. OutlineTunnel.Disconnect() will close `tunWriter`.
59-
func NewTunnel(host string, port int, password, cipher string, isUDPEnabled bool, tunWriter io.WriteCloser) (Tunnel, error) {
55+
func NewTunnel(client shadowsocks.Client, isUDPEnabled bool, tunWriter io.WriteCloser) (Tunnel, error) {
6056
if tunWriter == nil {
6157
return nil, errors.New("Must provide a TUN writer")
6258
}
63-
_, err := shadowsocks.NewClient(host, port, password, cipher)
64-
if err != nil {
65-
return nil, fmt.Errorf("Invalid Shadowsocks proxy parameters: %v", err.Error())
66-
}
6759
core.RegisterOutputFn(func(data []byte) (int, error) {
6860
return tunWriter.Write(data)
6961
})
7062
lwipStack := core.NewLWIPStack()
7163
base := tunnel.NewTunnel(tunWriter, lwipStack)
72-
t := &outlinetunnel{base, lwipStack, host, port, password, cipher, isUDPEnabled}
64+
t := &outlinetunnel{base, lwipStack, client, isUDPEnabled}
7365
t.registerConnectionHandlers()
7466
return t, nil
7567
}
7668

7769
func (t *outlinetunnel) UpdateUDPSupport() bool {
78-
client, err := shadowsocks.NewClient(t.host, t.port, t.password, t.cipher)
79-
if err != nil {
80-
return false
81-
}
82-
isUDPEnabled := oss.CheckUDPConnectivityWithDNS(client, shadowsocks.NewAddr("1.1.1.1:53", "udp")) == nil
70+
isUDPEnabled := oss.CheckUDPConnectivityWithDNS(t.client, shadowsocks.NewAddr("1.1.1.1:53", "udp")) == nil
8371
if t.isUDPEnabled != isUDPEnabled {
8472
t.isUDPEnabled = isUDPEnabled
8573
t.lwipStack.Close() // Close existing connections to avoid using the previous handlers.
@@ -93,10 +81,10 @@ func (t *outlinetunnel) UpdateUDPSupport() bool {
9381
func (t *outlinetunnel) registerConnectionHandlers() {
9482
var udpHandler core.UDPConnHandler
9583
if t.isUDPEnabled {
96-
udpHandler = oss.NewUDPHandler(t.host, t.port, t.password, t.cipher, 30*time.Second)
84+
udpHandler = oss.NewUDPHandler(t.client, 30*time.Second)
9785
} else {
9886
udpHandler = dnsfallback.NewUDPHandler()
9987
}
100-
core.RegisterTCPConnHandler(oss.NewTCPHandler(t.host, t.port, t.password, t.cipher))
88+
core.RegisterTCPConnHandler(oss.NewTCPHandler(t.client))
10189
core.RegisterUDPConnHandler(udpHandler)
10290
}

shadowsocks/tcp.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package shadowsocks
33
import (
44
"net"
55

6-
onet "github.com/Jigsaw-Code/outline-ss-server/net"
76
shadowsocks "github.com/Jigsaw-Code/outline-ss-server/client"
7+
onet "github.com/Jigsaw-Code/outline-ss-server/net"
88
"github.com/eycorsican/go-tun2socks/core"
99
)
1010

@@ -13,16 +13,7 @@ type tcpHandler struct {
1313
}
1414

1515
// NewTCPHandler returns a Shadowsocks TCP connection handler.
16-
//
17-
// `host` is the hostname of the Shadowsocks proxy server.
18-
// `port` is the port of the Shadowsocks proxy server.
19-
// `password` is password used to authenticate to the server.
20-
// `cipher` is the encryption cipher of the Shadowsocks proxy.
21-
func NewTCPHandler(host string, port int, password, cipher string) core.TCPConnHandler {
22-
client, err := shadowsocks.NewClient(host, port, password, cipher)
23-
if err != nil {
24-
return nil
25-
}
16+
func NewTCPHandler(client shadowsocks.Client) core.TCPConnHandler {
2617
return &tcpHandler{client}
2718
}
2819

shadowsocks/udp.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,9 @@ type udpHandler struct {
2020

2121
// NewUDPHandler returns a Shadowsocks UDP connection handler.
2222
//
23-
// `host` is the hostname of the Shadowsocks proxy server.
24-
// `port` is the port of the Shadowsocks proxy.
25-
// `password` is password used to authenticate to the proxy.
26-
// `cipher` is the encryption cipher of the Shadowsocks proxy.
23+
// `client` provides the Shadowsocks functionality.
2724
// `timeout` is the UDP read and write timeout.
28-
func NewUDPHandler(host string, port int, password, cipher string, timeout time.Duration) core.UDPConnHandler {
29-
client, err := shadowsocks.NewClient(host, port, password, cipher)
30-
if err != nil {
31-
return nil
32-
}
25+
func NewUDPHandler(client shadowsocks.Client, timeout time.Duration) core.UDPConnHandler {
3326
return &udpHandler{
3427
client: client,
3528
timeout: timeout,

0 commit comments

Comments
 (0)