diff --git a/engine/parse.go b/engine/parse.go index f83649a..20ee46f 100644 --- a/engine/parse.go +++ b/engine/parse.go @@ -9,6 +9,7 @@ import ( "github.com/xjasonlyu/tun2socks/device" "github.com/xjasonlyu/tun2socks/device/tun" "github.com/xjasonlyu/tun2socks/proxy" + "github.com/xjasonlyu/tun2socks/proxy/proto" ) func parseDevice(s string, mtu uint32) (device.Device, error) { @@ -34,7 +35,7 @@ func parseDevice(s string, mtu uint32) (device.Device, error) { func parseProxy(s string) (proxy.Proxy, error) { if !strings.Contains(s, "://") { - s = proxy.Socks5Proto.String() + "://" + s /* default protocol */ + s = proto.Socks5.String() + "://" + s /* default protocol */ } u, err := url.Parse(s) @@ -42,17 +43,17 @@ func parseProxy(s string) (proxy.Proxy, error) { return nil, err } - proto := strings.ToLower(u.Scheme) + protocol := strings.ToLower(u.Scheme) - switch proto { - case proxy.DirectProto.String(): + switch protocol { + case proto.Direct.String(): return proxy.NewDirect(), nil - case proxy.Socks5Proto.String(): + case proto.Socks5.String(): return proxy.NewSocks5(parseSocks(u)) - case proxy.ShadowsocksProto.String(): + case proto.Shadowsocks.String(): return proxy.NewShadowsocks(parseShadowsocks(u)) default: - return nil, fmt.Errorf("unsupported protocol: %s", proto) + return nil, fmt.Errorf("unsupported protocol: %s", protocol) } } diff --git a/proxy/base.go b/proxy/base.go index ca5cb90..2185404 100755 --- a/proxy/base.go +++ b/proxy/base.go @@ -6,24 +6,26 @@ import ( "net" "github.com/xjasonlyu/tun2socks/common/adapter" + "github.com/xjasonlyu/tun2socks/proxy/proto" ) var _ Proxy = (*Base)(nil) type Base struct { - addr string + addr string + proto proto.Proto } -func NewBase(addr string) *Base { - return &Base{addr: addr} +func NewBase(addr string, proto proto.Proto) *Base { + return &Base{addr: addr, proto: proto} } func (b *Base) Addr() string { return b.addr } -func (b *Base) Proto() string { - return "" +func (b *Base) Proto() proto.Proto { + return b.proto } func (b *Base) DialContext(context.Context, *adapter.Metadata) (net.Conn, error) { diff --git a/proxy/direct.go b/proxy/direct.go index aa577de..6880c1f 100755 --- a/proxy/direct.go +++ b/proxy/direct.go @@ -6,6 +6,7 @@ import ( "github.com/xjasonlyu/tun2socks/common/adapter" "github.com/xjasonlyu/tun2socks/component/dialer" + "github.com/xjasonlyu/tun2socks/proxy/proto" ) var _ Proxy = (*Direct)(nil) @@ -16,14 +17,12 @@ type Direct struct { func NewDirect() *Direct { return &Direct{ - Base: &Base{}, + Base: &Base{ + proto: proto.Direct, + }, } } -func (d *Direct) Proto() string { - return DirectProto.String() -} - func (d *Direct) DialContext(ctx context.Context, metadata *adapter.Metadata) (net.Conn, error) { c, err := dialer.DialContext(ctx, "tcp", metadata.DestinationAddress()) if err != nil { diff --git a/proxy/proto.go b/proxy/proto/proto.go similarity index 60% rename from proxy/proto.go rename to proxy/proto/proto.go index 64c859b..4f7286d 100644 --- a/proxy/proto.go +++ b/proxy/proto/proto.go @@ -1,22 +1,22 @@ -package proxy +package proto import "fmt" const ( - DirectProto Proto = iota - ShadowsocksProto - Socks5Proto + Direct Proto = iota + Shadowsocks + Socks5 ) type Proto uint8 func (proto Proto) String() string { switch proto { - case DirectProto: + case Direct: return "direct" - case ShadowsocksProto: + case Shadowsocks: return "ss" - case Socks5Proto: + case Socks5: return "socks5" default: return fmt.Sprintf("proto(%d)", proto) diff --git a/proxy/proxy.go b/proxy/proxy.go index d9dadf5..df36883 100755 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -7,6 +7,7 @@ import ( "time" "github.com/xjasonlyu/tun2socks/common/adapter" + "github.com/xjasonlyu/tun2socks/proxy/proto" ) const ( @@ -25,7 +26,7 @@ type Dialer interface { type Proxy interface { Dialer Addr() string - Proto() string + Proto() proto.Proto } // SetDialer sets default Dialer. diff --git a/proxy/shadowsocks.go b/proxy/shadowsocks.go index cef2bb2..d8de273 100755 --- a/proxy/shadowsocks.go +++ b/proxy/shadowsocks.go @@ -10,6 +10,7 @@ import ( "github.com/xjasonlyu/tun2socks/component/dialer" obfs "github.com/xjasonlyu/tun2socks/component/simple-obfs" "github.com/xjasonlyu/tun2socks/component/socks5" + "github.com/xjasonlyu/tun2socks/proxy/proto" "github.com/Dreamacro/go-shadowsocks2/core" ) @@ -32,17 +33,16 @@ func NewShadowsocks(addr, method, password, obfsMode, obfsHost string) (*Shadows } return &Shadowsocks{ - Base: NewBase(addr), + Base: &Base{ + addr: addr, + proto: proto.Shadowsocks, + }, cipher: cipher, obfsMode: obfsMode, obfsHost: obfsHost, }, nil } -func (ss *Shadowsocks) Proto() string { - return ShadowsocksProto.String() -} - func (ss *Shadowsocks) DialContext(ctx context.Context, metadata *adapter.Metadata) (c net.Conn, err error) { c, err = dialer.DialContext(ctx, "tcp", ss.Addr()) if err != nil { diff --git a/proxy/socks5.go b/proxy/socks5.go index 2792b41..7ccbbe6 100755 --- a/proxy/socks5.go +++ b/proxy/socks5.go @@ -10,6 +10,7 @@ import ( "github.com/xjasonlyu/tun2socks/common/adapter" "github.com/xjasonlyu/tun2socks/component/dialer" "github.com/xjasonlyu/tun2socks/component/socks5" + "github.com/xjasonlyu/tun2socks/proxy/proto" ) var _ Proxy = (*Socks5)(nil) @@ -23,16 +24,15 @@ type Socks5 struct { func NewSocks5(addr, user, pass string) (*Socks5, error) { return &Socks5{ - Base: NewBase(addr), + Base: &Base{ + addr: addr, + proto: proto.Socks5, + }, user: user, pass: pass, }, nil } -func (ss *Socks5) Proto() string { - return Socks5Proto.String() -} - func (ss *Socks5) DialContext(ctx context.Context, metadata *adapter.Metadata) (c net.Conn, err error) { c, err = dialer.DialContext(ctx, "tcp", ss.Addr()) if err != nil {