Browse Source

Chore: add Type() method to proxy.Proxy

pull/76/head
xjasonlyu 4 years ago
parent
commit
a84eebe284
  1. 6
      proxy/base.go
  2. 6
      proxy/direct.go
  3. 6
      proxy/proxy.go
  4. 6
      proxy/shadowsocks.go
  5. 6
      proxy/socks5.go

6
proxy/base.go

@ -8,6 +8,8 @@ import (
"github.com/xjasonlyu/tun2socks/common/adapter"
)
var _ Proxy = (*Base)(nil)
type Base struct {
addr string
}
@ -20,6 +22,10 @@ func (b *Base) Addr() string {
return b.addr
}
func (b *Base) Type() string {
return "base"
}
func (b *Base) DialContext(context.Context, *adapter.Metadata) (net.Conn, error) {
return nil, errors.New("not supported")
}

6
proxy/direct.go

@ -8,6 +8,8 @@ import (
"github.com/xjasonlyu/tun2socks/component/dialer"
)
var _ Proxy = (*Direct)(nil)
type Direct struct {
*Base
}
@ -16,6 +18,10 @@ func NewDirect() *Direct {
return &Direct{}
}
func (d *Direct) Type() string {
return "direct"
}
func (d *Direct) DialContext(ctx context.Context, metadata *adapter.Metadata) (net.Conn, error) {
c, err := dialer.DialContext(ctx, "tcp", metadata.DestinationAddress())
if err != nil {

6
proxy/proxy.go

@ -22,6 +22,12 @@ type Dialer interface {
DialUDP(*adapter.Metadata) (net.PacketConn, error)
}
type Proxy interface {
Dialer
Addr() string
Type() string
}
// SetDialer sets default Dialer.
func SetDialer(d Dialer) {
_defaultDialer = d

6
proxy/shadowsocks.go

@ -14,6 +14,8 @@ import (
"github.com/Dreamacro/go-shadowsocks2/core"
)
var _ Proxy = (*ShadowSocks)(nil)
type ShadowSocks struct {
*Base
@ -37,6 +39,10 @@ func NewShadowSocks(addr, method, password, obfsMode, obfsHost string) (*ShadowS
}, nil
}
func (ss *ShadowSocks) Type() string {
return "ss"
}
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 {

6
proxy/socks5.go

@ -12,6 +12,8 @@ import (
"github.com/xjasonlyu/tun2socks/component/socks5"
)
var _ Proxy = (*Socks5)(nil)
type Socks5 struct {
*Base
@ -27,6 +29,10 @@ func NewSocks5(addr, user, pass string) (*Socks5, error) {
}, nil
}
func (ss *Socks5) Type() string {
return "socks5"
}
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 {

Loading…
Cancel
Save