Chore: rename proxy Type() to Proto()

This commit is contained in:
xjasonlyu
2021-02-07 10:38:07 +08:00
parent a3ff3a5a15
commit ee00315894
8 changed files with 38 additions and 15 deletions

View File

@@ -121,7 +121,7 @@ func (e *Engine) setStack() error {
log.Infof(
"[STACK] %s://%s <-> %s://%s",
e.device.Type(), e.device.Name(),
e.proxy.Type(), e.proxy.Addr(),
e.proxy.Proto(), e.proxy.Addr(),
)
return nil
}

View File

@@ -33,9 +33,8 @@ func parseDevice(s string, mtu uint32) (device.Device, error) {
}
func parseProxy(s string) (proxy.Proxy, error) {
const defaultProto = "socks5"
if !strings.Contains(s, "://") {
s = defaultProto + "://" + s
s = proxy.Socks5Proto.String() + "://" + s /* default protocol */
}
u, err := url.Parse(s)
@@ -46,11 +45,11 @@ func parseProxy(s string) (proxy.Proxy, error) {
proto := strings.ToLower(u.Scheme)
switch proto {
case "direct":
case proxy.DirectProto.String():
return proxy.NewDirect(), nil
case "socks5":
case proxy.Socks5Proto.String():
return proxy.NewSocks5(parseSocks(u))
case "ss":
case proxy.ShadowsocksProto.String():
return proxy.NewShadowsocks(parseShadowsocks(u))
default:
return nil, fmt.Errorf("unsupported protocol: %s", proto)

View File

@@ -22,8 +22,8 @@ func (b *Base) Addr() string {
return b.addr
}
func (b *Base) Type() string {
return "base"
func (b *Base) Proto() string {
return ""
}
func (b *Base) DialContext(context.Context, *adapter.Metadata) (net.Conn, error) {

View File

@@ -20,8 +20,8 @@ func NewDirect() *Direct {
}
}
func (d *Direct) Type() string {
return "direct"
func (d *Direct) Proto() string {
return DirectProto.String()
}
func (d *Direct) DialContext(ctx context.Context, metadata *adapter.Metadata) (net.Conn, error) {

24
proxy/proto.go Normal file
View File

@@ -0,0 +1,24 @@
package proxy
import "fmt"
const (
DirectProto Proto = iota
ShadowsocksProto
Socks5Proto
)
type Proto uint8
func (proto Proto) String() string {
switch proto {
case DirectProto:
return "direct"
case ShadowsocksProto:
return "ss"
case Socks5Proto:
return "socks5"
default:
return fmt.Sprintf("proto(%d)", proto)
}
}

View File

@@ -25,7 +25,7 @@ type Dialer interface {
type Proxy interface {
Dialer
Addr() string
Type() string
Proto() string
}
// SetDialer sets default Dialer.

View File

@@ -39,8 +39,8 @@ func NewShadowsocks(addr, method, password, obfsMode, obfsHost string) (*Shadows
}, nil
}
func (ss *Shadowsocks) Type() string {
return "ss"
func (ss *Shadowsocks) Proto() string {
return ShadowsocksProto.String()
}
func (ss *Shadowsocks) DialContext(ctx context.Context, metadata *adapter.Metadata) (c net.Conn, err error) {

View File

@@ -29,8 +29,8 @@ func NewSocks5(addr, user, pass string) (*Socks5, error) {
}, nil
}
func (ss *Socks5) Type() string {
return "socks5"
func (ss *Socks5) Proto() string {
return Socks5Proto.String()
}
func (ss *Socks5) DialContext(ctx context.Context, metadata *adapter.Metadata) (c net.Conn, err error) {