mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-08 10:10:12 +08:00
Refactor(proxy): use proto sub package
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
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) {
|
||||
|
@@ -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 {
|
||||
|
@@ -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)
|
@@ -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.
|
||||
|
@@ -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 {
|
||||
|
@@ -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 {
|
||||
|
Reference in New Issue
Block a user