diff --git a/adapters/outbound/base.go b/adapter/adapter.go similarity index 61% rename from adapters/outbound/base.go rename to adapter/adapter.go index 6e16bbae..f3503f04 100644 --- a/adapters/outbound/base.go +++ b/adapter/adapter.go @@ -1,11 +1,12 @@ -package outbound +package adapter import ( "context" "encoding/json" - "errors" + "fmt" "net" "net/http" + "net/url" "time" "github.com/Dreamacro/clash/common/queue" @@ -14,97 +15,6 @@ import ( "go.uber.org/atomic" ) -type Base struct { - name string - addr string - tp C.AdapterType - udp bool -} - -// Name implements C.ProxyAdapter -func (b *Base) Name() string { - return b.name -} - -// Type implements C.ProxyAdapter -func (b *Base) Type() C.AdapterType { - return b.tp -} - -// StreamConn implements C.ProxyAdapter -func (b *Base) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) { - return c, errors.New("no support") -} - -// DialUDP implements C.ProxyAdapter -func (b *Base) DialUDP(metadata *C.Metadata) (C.PacketConn, error) { - return nil, errors.New("no support") -} - -// SupportUDP implements C.ProxyAdapter -func (b *Base) SupportUDP() bool { - return b.udp -} - -// MarshalJSON implements C.ProxyAdapter -func (b *Base) MarshalJSON() ([]byte, error) { - return json.Marshal(map[string]string{ - "type": b.Type().String(), - }) -} - -// Addr implements C.ProxyAdapter -func (b *Base) Addr() string { - return b.addr -} - -// Unwrap implements C.ProxyAdapter -func (b *Base) Unwrap(metadata *C.Metadata) C.Proxy { - return nil -} - -func NewBase(name string, addr string, tp C.AdapterType, udp bool) *Base { - return &Base{name, addr, tp, udp} -} - -type conn struct { - net.Conn - chain C.Chain -} - -// Chains implements C.Connection -func (c *conn) Chains() C.Chain { - return c.chain -} - -// AppendToChains implements C.Connection -func (c *conn) AppendToChains(a C.ProxyAdapter) { - c.chain = append(c.chain, a.Name()) -} - -func NewConn(c net.Conn, a C.ProxyAdapter) C.Conn { - return &conn{c, []string{a.Name()}} -} - -type packetConn struct { - net.PacketConn - chain C.Chain -} - -// Chains implements C.Connection -func (c *packetConn) Chains() C.Chain { - return c.chain -} - -// AppendToChains implements C.Connection -func (c *packetConn) AppendToChains(a C.ProxyAdapter) { - c.chain = append(c.chain, a.Name()) -} - -func newPacketConn(pc net.PacketConn, a C.ProxyAdapter) C.PacketConn { - return &packetConn{pc, []string{a.Name()}} -} - type Proxy struct { C.ProxyAdapter history *queue.Queue @@ -118,7 +28,7 @@ func (p *Proxy) Alive() bool { // Dial implements C.Proxy func (p *Proxy) Dial(metadata *C.Metadata) (C.Conn, error) { - ctx, cancel := context.WithTimeout(context.Background(), tcpTimeout) + ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTCPTimeout) defer cancel() return p.DialContext(ctx, metadata) } @@ -237,3 +147,31 @@ func (p *Proxy) URLTest(ctx context.Context, url string) (t uint16, err error) { func NewProxy(adapter C.ProxyAdapter) *Proxy { return &Proxy{adapter, queue.New(10), atomic.NewBool(true)} } + +func urlToMetadata(rawURL string) (addr C.Metadata, err error) { + u, err := url.Parse(rawURL) + if err != nil { + return + } + + port := u.Port() + if port == "" { + switch u.Scheme { + case "https": + port = "443" + case "http": + port = "80" + default: + err = fmt.Errorf("%s scheme not Support", rawURL) + return + } + } + + addr = C.Metadata{ + AddrType: C.AtypDomainName, + Host: u.Hostname(), + DstIP: nil, + DstPort: port, + } + return +} diff --git a/adapters/inbound/http.go b/adapter/inbound/http.go similarity index 100% rename from adapters/inbound/http.go rename to adapter/inbound/http.go diff --git a/adapters/inbound/https.go b/adapter/inbound/https.go similarity index 100% rename from adapters/inbound/https.go rename to adapter/inbound/https.go diff --git a/adapters/inbound/packet.go b/adapter/inbound/packet.go similarity index 100% rename from adapters/inbound/packet.go rename to adapter/inbound/packet.go diff --git a/adapters/inbound/socket.go b/adapter/inbound/socket.go similarity index 100% rename from adapters/inbound/socket.go rename to adapter/inbound/socket.go diff --git a/adapters/inbound/util.go b/adapter/inbound/util.go similarity index 100% rename from adapters/inbound/util.go rename to adapter/inbound/util.go diff --git a/adapter/outbound/base.go b/adapter/outbound/base.go new file mode 100644 index 00000000..a001d799 --- /dev/null +++ b/adapter/outbound/base.go @@ -0,0 +1,100 @@ +package outbound + +import ( + "encoding/json" + "errors" + "net" + + C "github.com/Dreamacro/clash/constant" +) + +type Base struct { + name string + addr string + tp C.AdapterType + udp bool +} + +// Name implements C.ProxyAdapter +func (b *Base) Name() string { + return b.name +} + +// Type implements C.ProxyAdapter +func (b *Base) Type() C.AdapterType { + return b.tp +} + +// StreamConn implements C.ProxyAdapter +func (b *Base) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) { + return c, errors.New("no support") +} + +// DialUDP implements C.ProxyAdapter +func (b *Base) DialUDP(metadata *C.Metadata) (C.PacketConn, error) { + return nil, errors.New("no support") +} + +// SupportUDP implements C.ProxyAdapter +func (b *Base) SupportUDP() bool { + return b.udp +} + +// MarshalJSON implements C.ProxyAdapter +func (b *Base) MarshalJSON() ([]byte, error) { + return json.Marshal(map[string]string{ + "type": b.Type().String(), + }) +} + +// Addr implements C.ProxyAdapter +func (b *Base) Addr() string { + return b.addr +} + +// Unwrap implements C.ProxyAdapter +func (b *Base) Unwrap(metadata *C.Metadata) C.Proxy { + return nil +} + +func NewBase(name string, addr string, tp C.AdapterType, udp bool) *Base { + return &Base{name, addr, tp, udp} +} + +type conn struct { + net.Conn + chain C.Chain +} + +// Chains implements C.Connection +func (c *conn) Chains() C.Chain { + return c.chain +} + +// AppendToChains implements C.Connection +func (c *conn) AppendToChains(a C.ProxyAdapter) { + c.chain = append(c.chain, a.Name()) +} + +func NewConn(c net.Conn, a C.ProxyAdapter) C.Conn { + return &conn{c, []string{a.Name()}} +} + +type packetConn struct { + net.PacketConn + chain C.Chain +} + +// Chains implements C.Connection +func (c *packetConn) Chains() C.Chain { + return c.chain +} + +// AppendToChains implements C.Connection +func (c *packetConn) AppendToChains(a C.ProxyAdapter) { + c.chain = append(c.chain, a.Name()) +} + +func newPacketConn(pc net.PacketConn, a C.ProxyAdapter) C.PacketConn { + return &packetConn{pc, []string{a.Name()}} +} diff --git a/adapters/outbound/direct.go b/adapter/outbound/direct.go similarity index 100% rename from adapters/outbound/direct.go rename to adapter/outbound/direct.go diff --git a/adapters/outbound/http.go b/adapter/outbound/http.go similarity index 100% rename from adapters/outbound/http.go rename to adapter/outbound/http.go diff --git a/adapters/outbound/reject.go b/adapter/outbound/reject.go similarity index 100% rename from adapters/outbound/reject.go rename to adapter/outbound/reject.go diff --git a/adapters/outbound/shadowsocks.go b/adapter/outbound/shadowsocks.go similarity index 96% rename from adapters/outbound/shadowsocks.go rename to adapter/outbound/shadowsocks.go index 6dcfc2c0..0fb3ab9a 100644 --- a/adapters/outbound/shadowsocks.go +++ b/adapter/outbound/shadowsocks.go @@ -2,7 +2,6 @@ package outbound import ( "context" - "encoding/json" "errors" "fmt" "net" @@ -105,13 +104,6 @@ func (ss *ShadowSocks) DialUDP(metadata *C.Metadata) (C.PacketConn, error) { return newPacketConn(&ssPacketConn{PacketConn: pc, rAddr: addr}, ss), nil } -// MarshalJSON implements C.ProxyAdapter -func (ss *ShadowSocks) MarshalJSON() ([]byte, error) { - return json.Marshal(map[string]string{ - "type": ss.Type().String(), - }) -} - func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) { addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port)) cipher := option.Cipher diff --git a/adapters/outbound/shadowsocksr.go b/adapter/outbound/shadowsocksr.go similarity index 95% rename from adapters/outbound/shadowsocksr.go rename to adapter/outbound/shadowsocksr.go index 635135fc..b44f46e5 100644 --- a/adapters/outbound/shadowsocksr.go +++ b/adapter/outbound/shadowsocksr.go @@ -2,7 +2,6 @@ package outbound import ( "context" - "encoding/json" "fmt" "net" "strconv" @@ -91,13 +90,6 @@ func (ssr *ShadowSocksR) DialUDP(metadata *C.Metadata) (C.PacketConn, error) { return newPacketConn(&ssPacketConn{PacketConn: pc, rAddr: addr}, ssr), nil } -// MarshalJSON implements C.ProxyAdapter -func (ssr *ShadowSocksR) MarshalJSON() ([]byte, error) { - return json.Marshal(map[string]string{ - "type": ssr.Type().String(), - }) -} - func NewShadowSocksR(option ShadowSocksROption) (*ShadowSocksR, error) { addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port)) cipher := option.Cipher diff --git a/adapters/outbound/snell.go b/adapter/outbound/snell.go similarity index 100% rename from adapters/outbound/snell.go rename to adapter/outbound/snell.go diff --git a/adapters/outbound/socks5.go b/adapter/outbound/socks5.go similarity index 98% rename from adapters/outbound/socks5.go rename to adapter/outbound/socks5.go index 466797de..26c7c06a 100644 --- a/adapters/outbound/socks5.go +++ b/adapter/outbound/socks5.go @@ -79,7 +79,7 @@ func (ss *Socks5) DialContext(ctx context.Context, metadata *C.Metadata) (_ C.Co // DialUDP implements C.ProxyAdapter func (ss *Socks5) DialUDP(metadata *C.Metadata) (_ C.PacketConn, err error) { - ctx, cancel := context.WithTimeout(context.Background(), tcpTimeout) + ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTCPTimeout) defer cancel() c, err := dialer.DialContext(ctx, "tcp", ss.addr) if err != nil { diff --git a/adapters/outbound/trojan.go b/adapter/outbound/trojan.go similarity index 95% rename from adapters/outbound/trojan.go rename to adapter/outbound/trojan.go index 929f4414..5d852735 100644 --- a/adapters/outbound/trojan.go +++ b/adapter/outbound/trojan.go @@ -3,7 +3,6 @@ package outbound import ( "context" "crypto/tls" - "encoding/json" "fmt" "net" "strconv" @@ -101,7 +100,7 @@ func (t *Trojan) DialUDP(metadata *C.Metadata) (_ C.PacketConn, err error) { } defer safeConnClose(c, err) } else { - ctx, cancel := context.WithTimeout(context.Background(), tcpTimeout) + ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTCPTimeout) defer cancel() c, err = dialer.DialContext(ctx, "tcp", t.addr) if err != nil { @@ -124,12 +123,6 @@ func (t *Trojan) DialUDP(metadata *C.Metadata) (_ C.PacketConn, err error) { return newPacketConn(pc, t), err } -func (t *Trojan) MarshalJSON() ([]byte, error) { - return json.Marshal(map[string]string{ - "type": t.Type().String(), - }) -} - func NewTrojan(option TrojanOption) (*Trojan, error) { addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port)) diff --git a/adapters/outbound/util.go b/adapter/outbound/util.go similarity index 76% rename from adapters/outbound/util.go rename to adapter/outbound/util.go index cd60d373..4b81eb5d 100644 --- a/adapters/outbound/util.go +++ b/adapter/outbound/util.go @@ -3,9 +3,7 @@ package outbound import ( "bytes" "crypto/tls" - "fmt" "net" - "net/url" "strconv" "sync" "time" @@ -15,43 +13,11 @@ import ( "github.com/Dreamacro/clash/transport/socks5" ) -const ( - tcpTimeout = 5 * time.Second -) - var ( globalClientSessionCache tls.ClientSessionCache once sync.Once ) -func urlToMetadata(rawURL string) (addr C.Metadata, err error) { - u, err := url.Parse(rawURL) - if err != nil { - return - } - - port := u.Port() - if port == "" { - switch u.Scheme { - case "https": - port = "443" - case "http": - port = "80" - default: - err = fmt.Errorf("%s scheme not Support", rawURL) - return - } - } - - addr = C.Metadata{ - AddrType: C.AtypDomainName, - Host: u.Hostname(), - DstIP: nil, - DstPort: port, - } - return -} - func tcpKeepAlive(c net.Conn) { if tcp, ok := c.(*net.TCPConn); ok { tcp.SetKeepAlive(true) diff --git a/adapters/outbound/vmess.go b/adapter/outbound/vmess.go similarity index 99% rename from adapters/outbound/vmess.go rename to adapter/outbound/vmess.go index 246450ac..672f767b 100644 --- a/adapters/outbound/vmess.go +++ b/adapter/outbound/vmess.go @@ -222,7 +222,7 @@ func (v *Vmess) DialUDP(metadata *C.Metadata) (_ C.PacketConn, err error) { c, err = v.client.StreamConn(c, parseVmessAddr(metadata)) } else { - ctx, cancel := context.WithTimeout(context.Background(), tcpTimeout) + ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTCPTimeout) defer cancel() c, err = dialer.DialContext(ctx, "tcp", v.addr) if err != nil { diff --git a/adapters/outboundgroup/common.go b/adapter/outboundgroup/common.go similarity index 90% rename from adapters/outboundgroup/common.go rename to adapter/outboundgroup/common.go index c5c719f2..31b8cb6b 100644 --- a/adapters/outboundgroup/common.go +++ b/adapter/outboundgroup/common.go @@ -3,7 +3,7 @@ package outboundgroup import ( "time" - "github.com/Dreamacro/clash/adapters/provider" + "github.com/Dreamacro/clash/adapter/provider" C "github.com/Dreamacro/clash/constant" ) diff --git a/adapters/outboundgroup/fallback.go b/adapter/outboundgroup/fallback.go similarity index 95% rename from adapters/outboundgroup/fallback.go rename to adapter/outboundgroup/fallback.go index b3dbe0cb..8b38f09c 100644 --- a/adapters/outboundgroup/fallback.go +++ b/adapter/outboundgroup/fallback.go @@ -4,8 +4,8 @@ import ( "context" "encoding/json" - "github.com/Dreamacro/clash/adapters/outbound" - "github.com/Dreamacro/clash/adapters/provider" + "github.com/Dreamacro/clash/adapter/outbound" + "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/common/singledo" C "github.com/Dreamacro/clash/constant" ) diff --git a/adapters/outboundgroup/loadbalance.go b/adapter/outboundgroup/loadbalance.go similarity index 97% rename from adapters/outboundgroup/loadbalance.go rename to adapter/outboundgroup/loadbalance.go index b44fade1..7856a07d 100644 --- a/adapters/outboundgroup/loadbalance.go +++ b/adapter/outboundgroup/loadbalance.go @@ -7,8 +7,8 @@ import ( "fmt" "net" - "github.com/Dreamacro/clash/adapters/outbound" - "github.com/Dreamacro/clash/adapters/provider" + "github.com/Dreamacro/clash/adapter/outbound" + "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/common/murmur3" "github.com/Dreamacro/clash/common/singledo" C "github.com/Dreamacro/clash/constant" diff --git a/adapters/outboundgroup/parser.go b/adapter/outboundgroup/parser.go similarity index 98% rename from adapters/outboundgroup/parser.go rename to adapter/outboundgroup/parser.go index 82ceaa79..d11040a5 100644 --- a/adapters/outboundgroup/parser.go +++ b/adapter/outboundgroup/parser.go @@ -4,7 +4,7 @@ import ( "errors" "fmt" - "github.com/Dreamacro/clash/adapters/provider" + "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/common/structure" C "github.com/Dreamacro/clash/constant" ) diff --git a/adapters/outboundgroup/relay.go b/adapter/outboundgroup/relay.go similarity index 95% rename from adapters/outboundgroup/relay.go rename to adapter/outboundgroup/relay.go index 6583d390..e4a897b8 100644 --- a/adapters/outboundgroup/relay.go +++ b/adapter/outboundgroup/relay.go @@ -6,8 +6,8 @@ import ( "errors" "fmt" - "github.com/Dreamacro/clash/adapters/outbound" - "github.com/Dreamacro/clash/adapters/provider" + "github.com/Dreamacro/clash/adapter/outbound" + "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/common/singledo" "github.com/Dreamacro/clash/component/dialer" C "github.com/Dreamacro/clash/constant" diff --git a/adapters/outboundgroup/selector.go b/adapter/outboundgroup/selector.go similarity index 96% rename from adapters/outboundgroup/selector.go rename to adapter/outboundgroup/selector.go index 67afaaab..c28b6f8c 100644 --- a/adapters/outboundgroup/selector.go +++ b/adapter/outboundgroup/selector.go @@ -5,8 +5,8 @@ import ( "encoding/json" "errors" - "github.com/Dreamacro/clash/adapters/outbound" - "github.com/Dreamacro/clash/adapters/provider" + "github.com/Dreamacro/clash/adapter/outbound" + "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/common/singledo" C "github.com/Dreamacro/clash/constant" ) diff --git a/adapters/outboundgroup/urltest.go b/adapter/outboundgroup/urltest.go similarity index 97% rename from adapters/outboundgroup/urltest.go rename to adapter/outboundgroup/urltest.go index 52c5fef9..0f59d4be 100644 --- a/adapters/outboundgroup/urltest.go +++ b/adapter/outboundgroup/urltest.go @@ -5,8 +5,8 @@ import ( "encoding/json" "time" - "github.com/Dreamacro/clash/adapters/outbound" - "github.com/Dreamacro/clash/adapters/provider" + "github.com/Dreamacro/clash/adapter/outbound" + "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/common/singledo" C "github.com/Dreamacro/clash/constant" ) diff --git a/adapters/outboundgroup/util.go b/adapter/outboundgroup/util.go similarity index 100% rename from adapters/outboundgroup/util.go rename to adapter/outboundgroup/util.go diff --git a/adapters/outbound/parser.go b/adapter/parser.go similarity index 63% rename from adapters/outbound/parser.go rename to adapter/parser.go index f0f894da..62d15225 100644 --- a/adapters/outbound/parser.go +++ b/adapter/parser.go @@ -1,8 +1,9 @@ -package outbound +package adapter import ( "fmt" + "github.com/Dreamacro/clash/adapter/outbound" "github.com/Dreamacro/clash/common/structure" C "github.com/Dreamacro/clash/constant" ) @@ -20,36 +21,36 @@ func ParseProxy(mapping map[string]interface{}) (C.Proxy, error) { ) switch proxyType { case "ss": - ssOption := &ShadowSocksOption{} + ssOption := &outbound.ShadowSocksOption{} err = decoder.Decode(mapping, ssOption) if err != nil { break } - proxy, err = NewShadowSocks(*ssOption) + proxy, err = outbound.NewShadowSocks(*ssOption) case "ssr": - ssrOption := &ShadowSocksROption{} + ssrOption := &outbound.ShadowSocksROption{} err = decoder.Decode(mapping, ssrOption) if err != nil { break } - proxy, err = NewShadowSocksR(*ssrOption) + proxy, err = outbound.NewShadowSocksR(*ssrOption) case "socks5": - socksOption := &Socks5Option{} + socksOption := &outbound.Socks5Option{} err = decoder.Decode(mapping, socksOption) if err != nil { break } - proxy = NewSocks5(*socksOption) + proxy = outbound.NewSocks5(*socksOption) case "http": - httpOption := &HttpOption{} + httpOption := &outbound.HttpOption{} err = decoder.Decode(mapping, httpOption) if err != nil { break } - proxy = NewHttp(*httpOption) + proxy = outbound.NewHttp(*httpOption) case "vmess": - vmessOption := &VmessOption{ - HTTPOpts: HTTPOptions{ + vmessOption := &outbound.VmessOption{ + HTTPOpts: outbound.HTTPOptions{ Method: "GET", Path: []string{"/"}, }, @@ -58,21 +59,21 @@ func ParseProxy(mapping map[string]interface{}) (C.Proxy, error) { if err != nil { break } - proxy, err = NewVmess(*vmessOption) + proxy, err = outbound.NewVmess(*vmessOption) case "snell": - snellOption := &SnellOption{} + snellOption := &outbound.SnellOption{} err = decoder.Decode(mapping, snellOption) if err != nil { break } - proxy, err = NewSnell(*snellOption) + proxy, err = outbound.NewSnell(*snellOption) case "trojan": - trojanOption := &TrojanOption{} + trojanOption := &outbound.TrojanOption{} err = decoder.Decode(mapping, trojanOption) if err != nil { break } - proxy, err = NewTrojan(*trojanOption) + proxy, err = outbound.NewTrojan(*trojanOption) default: return nil, fmt.Errorf("unsupport proxy type: %s", proxyType) } diff --git a/adapters/provider/fetcher.go b/adapter/provider/fetcher.go similarity index 100% rename from adapters/provider/fetcher.go rename to adapter/provider/fetcher.go diff --git a/adapters/provider/healthcheck.go b/adapter/provider/healthcheck.go similarity index 100% rename from adapters/provider/healthcheck.go rename to adapter/provider/healthcheck.go diff --git a/adapters/provider/parser.go b/adapter/provider/parser.go similarity index 100% rename from adapters/provider/parser.go rename to adapter/provider/parser.go diff --git a/adapters/provider/provider.go b/adapter/provider/provider.go similarity index 98% rename from adapters/provider/provider.go rename to adapter/provider/provider.go index 756a89d1..f87fe2a1 100644 --- a/adapters/provider/provider.go +++ b/adapter/provider/provider.go @@ -7,7 +7,7 @@ import ( "runtime" "time" - "github.com/Dreamacro/clash/adapters/outbound" + "github.com/Dreamacro/clash/adapter" C "github.com/Dreamacro/clash/constant" "gopkg.in/yaml.v2" @@ -133,7 +133,7 @@ func proxiesParse(buf []byte) (interface{}, error) { proxies := []C.Proxy{} for idx, mapping := range schema.Proxies { - proxy, err := outbound.ParseProxy(mapping) + proxy, err := adapter.ParseProxy(mapping) if err != nil { return nil, fmt.Errorf("proxy %d error: %w", idx, err) } diff --git a/adapters/provider/vehicle.go b/adapter/provider/vehicle.go similarity index 100% rename from adapters/provider/vehicle.go rename to adapter/provider/vehicle.go diff --git a/config/config.go b/config/config.go index 90331649..a256d282 100644 --- a/config/config.go +++ b/config/config.go @@ -8,16 +8,17 @@ import ( "os" "strings" - "github.com/Dreamacro/clash/adapters/outbound" - "github.com/Dreamacro/clash/adapters/outboundgroup" - "github.com/Dreamacro/clash/adapters/provider" + "github.com/Dreamacro/clash/adapter" + "github.com/Dreamacro/clash/adapter/outbound" + "github.com/Dreamacro/clash/adapter/outboundgroup" + "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/component/auth" "github.com/Dreamacro/clash/component/fakeip" "github.com/Dreamacro/clash/component/trie" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/dns" "github.com/Dreamacro/clash/log" - R "github.com/Dreamacro/clash/rules" + R "github.com/Dreamacro/clash/rule" T "github.com/Dreamacro/clash/tunnel" yaml "gopkg.in/yaml.v2" @@ -274,13 +275,13 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[ groupsConfig := cfg.ProxyGroup providersConfig := cfg.ProxyProvider - proxies["DIRECT"] = outbound.NewProxy(outbound.NewDirect()) - proxies["REJECT"] = outbound.NewProxy(outbound.NewReject()) + proxies["DIRECT"] = adapter.NewProxy(outbound.NewDirect()) + proxies["REJECT"] = adapter.NewProxy(outbound.NewReject()) proxyList = append(proxyList, "DIRECT", "REJECT") // parse proxy for idx, mapping := range proxiesConfig { - proxy, err := outbound.ParseProxy(mapping) + proxy, err := adapter.ParseProxy(mapping) if err != nil { return nil, nil, fmt.Errorf("proxy %d: %w", idx, err) } @@ -339,7 +340,7 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[ return nil, nil, fmt.Errorf("proxy group %s: the duplicate name", groupName) } - proxies[groupName] = outbound.NewProxy(group) + proxies[groupName] = adapter.NewProxy(group) } // initial compatible provider @@ -368,7 +369,7 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[ }, []provider.ProxyProvider{pd}, ) - proxies["GLOBAL"] = outbound.NewProxy(global) + proxies["GLOBAL"] = adapter.NewProxy(global) return proxies, providersMap, nil } diff --git a/config/utils.go b/config/utils.go index b3d198f7..387591d0 100644 --- a/config/utils.go +++ b/config/utils.go @@ -4,7 +4,7 @@ import ( "fmt" "strings" - "github.com/Dreamacro/clash/adapters/outboundgroup" + "github.com/Dreamacro/clash/adapter/outboundgroup" "github.com/Dreamacro/clash/common/structure" ) diff --git a/constant/adapters.go b/constant/adapters.go index 333243d8..733d86b9 100644 --- a/constant/adapters.go +++ b/constant/adapters.go @@ -27,6 +27,10 @@ const ( LoadBalance ) +const ( + DefaultTCPTimeout = 5 * time.Second +) + type Connection interface { Chains() Chain AppendToChains(adapter ProxyAdapter) diff --git a/hub/executor/executor.go b/hub/executor/executor.go index caf75aff..5d803106 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -6,9 +6,9 @@ import ( "os" "sync" - "github.com/Dreamacro/clash/adapters/outbound" - "github.com/Dreamacro/clash/adapters/outboundgroup" - "github.com/Dreamacro/clash/adapters/provider" + "github.com/Dreamacro/clash/adapter" + "github.com/Dreamacro/clash/adapter/outboundgroup" + "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/component/auth" "github.com/Dreamacro/clash/component/dialer" "github.com/Dreamacro/clash/component/profile" @@ -232,7 +232,7 @@ func patchSelectGroup(proxies map[string]C.Proxy) { } for name, proxy := range proxies { - outbound, ok := proxy.(*outbound.Proxy) + outbound, ok := proxy.(*adapter.Proxy) if !ok { continue } diff --git a/hub/route/provider.go b/hub/route/provider.go index 902f13af..f373d511 100644 --- a/hub/route/provider.go +++ b/hub/route/provider.go @@ -4,7 +4,7 @@ import ( "context" "net/http" - "github.com/Dreamacro/clash/adapters/provider" + "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/tunnel" "github.com/go-chi/chi/v5" diff --git a/hub/route/proxies.go b/hub/route/proxies.go index e541223d..bba9e2a8 100644 --- a/hub/route/proxies.go +++ b/hub/route/proxies.go @@ -7,8 +7,8 @@ import ( "strconv" "time" - "github.com/Dreamacro/clash/adapters/outbound" - "github.com/Dreamacro/clash/adapters/outboundgroup" + "github.com/Dreamacro/clash/adapter" + "github.com/Dreamacro/clash/adapter/outboundgroup" "github.com/Dreamacro/clash/component/profile/cachefile" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/tunnel" @@ -78,7 +78,7 @@ func updateProxy(w http.ResponseWriter, r *http.Request) { return } - proxy := r.Context().Value(CtxKeyProxy).(*outbound.Proxy) + proxy := r.Context().Value(CtxKeyProxy).(*adapter.Proxy) selector, ok := proxy.ProxyAdapter.(*outboundgroup.Selector) if !ok { render.Status(r, http.StatusBadRequest) diff --git a/proxy/http/server.go b/proxy/http/server.go index 75f4fc36..69117dbb 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -8,7 +8,7 @@ import ( "strings" "time" - adapters "github.com/Dreamacro/clash/adapters/inbound" + "github.com/Dreamacro/clash/adapter/inbound" "github.com/Dreamacro/clash/common/cache" "github.com/Dreamacro/clash/component/auth" "github.com/Dreamacro/clash/log" @@ -106,9 +106,9 @@ keepAlive: conn.Close() return } - tunnel.Add(adapters.NewHTTPS(request, conn)) + tunnel.Add(inbound.NewHTTPS(request, conn)) return } - tunnel.Add(adapters.NewHTTP(request, conn)) + tunnel.Add(inbound.NewHTTP(request, conn)) } diff --git a/proxy/redir/tcp.go b/proxy/redir/tcp.go index e53d000d..ef6718ec 100644 --- a/proxy/redir/tcp.go +++ b/proxy/redir/tcp.go @@ -3,7 +3,7 @@ package redir import ( "net" - "github.com/Dreamacro/clash/adapters/inbound" + "github.com/Dreamacro/clash/adapter/inbound" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/tunnel" diff --git a/proxy/redir/tproxy.go b/proxy/redir/tproxy.go index 576c4d37..44682362 100644 --- a/proxy/redir/tproxy.go +++ b/proxy/redir/tproxy.go @@ -3,7 +3,7 @@ package redir import ( "net" - "github.com/Dreamacro/clash/adapters/inbound" + "github.com/Dreamacro/clash/adapter/inbound" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/transport/socks5" diff --git a/proxy/redir/udp.go b/proxy/redir/udp.go index 57bd3ff3..e43af878 100644 --- a/proxy/redir/udp.go +++ b/proxy/redir/udp.go @@ -3,7 +3,7 @@ package redir import ( "net" - adapters "github.com/Dreamacro/clash/adapters/inbound" + "github.com/Dreamacro/clash/adapter/inbound" "github.com/Dreamacro/clash/common/pool" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/transport/socks5" @@ -75,5 +75,5 @@ func handleRedirUDP(pc net.PacketConn, buf []byte, lAddr *net.UDPAddr, rAddr *ne lAddr: lAddr, buf: buf, } - tunnel.AddPacket(adapters.NewPacket(target, pkt, C.TPROXY)) + tunnel.AddPacket(inbound.NewPacket(target, pkt, C.TPROXY)) } diff --git a/proxy/socks/tcp.go b/proxy/socks/tcp.go index 116ea3f6..a58d63d1 100644 --- a/proxy/socks/tcp.go +++ b/proxy/socks/tcp.go @@ -5,7 +5,7 @@ import ( "io/ioutil" "net" - adapters "github.com/Dreamacro/clash/adapters/inbound" + "github.com/Dreamacro/clash/adapter/inbound" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/log" authStore "github.com/Dreamacro/clash/proxy/auth" @@ -66,5 +66,5 @@ func HandleSocks(conn net.Conn) { io.Copy(ioutil.Discard, conn) return } - tunnel.Add(adapters.NewSocket(target, conn, C.SOCKS)) + tunnel.Add(inbound.NewSocket(target, conn, C.SOCKS)) } diff --git a/proxy/socks/udp.go b/proxy/socks/udp.go index a365353b..b41e6f18 100644 --- a/proxy/socks/udp.go +++ b/proxy/socks/udp.go @@ -3,7 +3,7 @@ package socks import ( "net" - adapters "github.com/Dreamacro/clash/adapters/inbound" + "github.com/Dreamacro/clash/adapter/inbound" "github.com/Dreamacro/clash/common/pool" "github.com/Dreamacro/clash/common/sockopt" C "github.com/Dreamacro/clash/constant" @@ -70,5 +70,5 @@ func handleSocksUDP(pc net.PacketConn, buf []byte, addr net.Addr) { payload: payload, bufRef: buf, } - tunnel.AddPacket(adapters.NewPacket(target, packet, C.SOCKS)) + tunnel.AddPacket(inbound.NewPacket(target, packet, C.SOCKS)) } diff --git a/rules/base.go b/rule/base.go similarity index 100% rename from rules/base.go rename to rule/base.go diff --git a/rules/domain.go b/rule/domain.go similarity index 100% rename from rules/domain.go rename to rule/domain.go diff --git a/rules/domain_keyword.go b/rule/domain_keyword.go similarity index 100% rename from rules/domain_keyword.go rename to rule/domain_keyword.go diff --git a/rules/domain_suffix.go b/rule/domain_suffix.go similarity index 100% rename from rules/domain_suffix.go rename to rule/domain_suffix.go diff --git a/rules/final.go b/rule/final.go similarity index 100% rename from rules/final.go rename to rule/final.go diff --git a/rules/geoip.go b/rule/geoip.go similarity index 100% rename from rules/geoip.go rename to rule/geoip.go diff --git a/rules/ipcidr.go b/rule/ipcidr.go similarity index 100% rename from rules/ipcidr.go rename to rule/ipcidr.go diff --git a/rules/parser.go b/rule/parser.go similarity index 100% rename from rules/parser.go rename to rule/parser.go diff --git a/rules/port.go b/rule/port.go similarity index 100% rename from rules/port.go rename to rule/port.go diff --git a/rules/process.go b/rule/process.go similarity index 100% rename from rules/process.go rename to rule/process.go diff --git a/test/snell_test.go b/test/snell_test.go index 0d9e059a..bc8b47aa 100644 --- a/test/snell_test.go +++ b/test/snell_test.go @@ -5,7 +5,7 @@ import ( "testing" "time" - "github.com/Dreamacro/clash/adapters/outbound" + "github.com/Dreamacro/clash/adapter/outbound" C "github.com/Dreamacro/clash/constant" "github.com/docker/docker/api/types/container" diff --git a/test/ss_test.go b/test/ss_test.go index 6876d3fb..ea33445c 100644 --- a/test/ss_test.go +++ b/test/ss_test.go @@ -4,7 +4,7 @@ import ( "testing" "time" - "github.com/Dreamacro/clash/adapters/outbound" + "github.com/Dreamacro/clash/adapter/outbound" "github.com/docker/docker/api/types/container" "github.com/stretchr/testify/assert" diff --git a/test/trojan_test.go b/test/trojan_test.go index c20f1279..236b5af1 100644 --- a/test/trojan_test.go +++ b/test/trojan_test.go @@ -5,7 +5,7 @@ import ( "testing" "time" - "github.com/Dreamacro/clash/adapters/outbound" + "github.com/Dreamacro/clash/adapter/outbound" C "github.com/Dreamacro/clash/constant" "github.com/docker/docker/api/types/container" diff --git a/test/vmess_test.go b/test/vmess_test.go index cf539bf8..92cb300e 100644 --- a/test/vmess_test.go +++ b/test/vmess_test.go @@ -5,7 +5,7 @@ import ( "testing" "time" - "github.com/Dreamacro/clash/adapters/outbound" + "github.com/Dreamacro/clash/adapter/outbound" C "github.com/Dreamacro/clash/constant" "github.com/docker/docker/api/types/container" diff --git a/tunnel/connection.go b/tunnel/connection.go index 24314fb6..2cc5964c 100644 --- a/tunnel/connection.go +++ b/tunnel/connection.go @@ -9,7 +9,7 @@ import ( "strings" "time" - "github.com/Dreamacro/clash/adapters/inbound" + "github.com/Dreamacro/clash/adapter/inbound" N "github.com/Dreamacro/clash/common/net" "github.com/Dreamacro/clash/common/pool" "github.com/Dreamacro/clash/component/resolver" diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index 77c24d88..18551ae8 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -7,8 +7,8 @@ import ( "sync" "time" - "github.com/Dreamacro/clash/adapters/inbound" - "github.com/Dreamacro/clash/adapters/provider" + "github.com/Dreamacro/clash/adapter/inbound" + "github.com/Dreamacro/clash/adapter/provider" "github.com/Dreamacro/clash/component/nat" "github.com/Dreamacro/clash/component/resolver" C "github.com/Dreamacro/clash/constant"