@@ -16,7 +16,6 @@ package outline
1616
1717import (
1818 "errors"
19- "fmt"
2019 "io"
2120 "time"
2221
@@ -41,10 +40,7 @@ type Tunnel interface {
4140type 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
7769func (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 {
9381func (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}
0 commit comments