From f73013006a4bad42325223901da3819130b20cba Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Sun, 30 Sep 2018 12:25:52 +0800 Subject: [PATCH] Chore: make the code more semantic --- adapters/{local => inbound}/http.go | 18 +++++++-------- adapters/{local => inbound}/https.go | 4 ++-- adapters/{local => inbound}/socket.go | 14 ++++++------ adapters/{local => inbound}/util.go | 8 +++---- adapters/{remote => outbound}/direct.go | 4 ++-- adapters/{remote => outbound}/fallback.go | 6 ++--- adapters/{remote => outbound}/reject.go | 2 +- adapters/{remote => outbound}/selector.go | 4 ++-- adapters/{remote => outbound}/shadowsocks.go | 22 +++++++++---------- adapters/{remote => outbound}/socks5.go | 8 +++---- adapters/{remote => outbound}/urltest.go | 6 ++--- adapters/{remote => outbound}/util.go | 6 ++--- adapters/{remote => outbound}/vmess.go | 22 +++++++++---------- {observable => common/observable}/iterable.go | 0 .../observable}/observable.go | 0 .../observable}/observable_test.go | 0 .../observable}/subscriber.go | 0 {common => component}/simple-obfs/http.go | 0 {common => component}/simple-obfs/tls.go | 0 {common => component}/vmess/aead.go | 0 {common => component}/vmess/chunk.go | 0 {common => component}/vmess/conn.go | 0 {common => component}/vmess/user.go | 0 {common => component}/vmess/vmess.go | 0 config/config.go | 4 ++-- constant/adapters.go | 4 ++-- constant/{addr.go => metadata.go} | 6 ++--- constant/rule.go | 2 +- hub/proxies.go | 2 +- proxy/http/server.go | 2 +- proxy/redir/tcp.go | 2 +- proxy/socks/tcp.go | 2 +- rules/domain_keyword.go | 6 ++--- rules/domain_suffix.go | 6 ++--- rules/domian.go | 6 ++--- rules/final.go | 2 +- rules/geoip.go | 6 ++--- rules/ipcidr.go | 6 ++--- tunnel/connection.go | 2 +- tunnel/tunnel.go | 22 +++++++++---------- 40 files changed, 102 insertions(+), 102 deletions(-) rename adapters/{local => inbound}/http.go (82%) rename adapters/{local => inbound}/https.go (77%) rename adapters/{local => inbound}/socket.go (72%) rename adapters/{local => inbound}/util.go (90%) rename adapters/{remote => outbound}/direct.go (78%) rename adapters/{remote => outbound}/fallback.go (92%) rename adapters/{remote => outbound}/reject.go (94%) rename adapters/{remote => outbound}/selector.go (89%) rename adapters/{remote => outbound}/shadowsocks.go (82%) rename adapters/{remote => outbound}/socks5.go (83%) rename adapters/{remote => outbound}/urltest.go (90%) rename adapters/{remote => outbound}/util.go (93%) rename adapters/{remote => outbound}/vmess.go (74%) rename {observable => common/observable}/iterable.go (100%) rename {observable => common/observable}/observable.go (100%) rename {observable => common/observable}/observable_test.go (100%) rename {observable => common/observable}/subscriber.go (100%) rename {common => component}/simple-obfs/http.go (100%) rename {common => component}/simple-obfs/tls.go (100%) rename {common => component}/vmess/aead.go (100%) rename {common => component}/vmess/chunk.go (100%) rename {common => component}/vmess/conn.go (100%) rename {common => component}/vmess/user.go (100%) rename {common => component}/vmess/vmess.go (100%) rename constant/{addr.go => metadata.go} (81%) diff --git a/adapters/local/http.go b/adapters/inbound/http.go similarity index 82% rename from adapters/local/http.go rename to adapters/inbound/http.go index ddd84611..01aa14b8 100644 --- a/adapters/local/http.go +++ b/adapters/inbound/http.go @@ -10,9 +10,9 @@ import ( // HTTPAdapter is a adapter for HTTP connection type HTTPAdapter struct { - addr *C.Addr - conn net.Conn - R *http.Request + metadata *C.Metadata + conn net.Conn + R *http.Request } // Close HTTP connection @@ -20,9 +20,9 @@ func (h *HTTPAdapter) Close() { h.conn.Close() } -// Addr return destination address -func (h *HTTPAdapter) Addr() *C.Addr { - return h.addr +// Metadata return destination metadata +func (h *HTTPAdapter) Metadata() *C.Metadata { + return h.metadata } // Conn return raw net.Conn of HTTP @@ -33,9 +33,9 @@ func (h *HTTPAdapter) Conn() net.Conn { // NewHTTP is HTTPAdapter generator func NewHTTP(request *http.Request, conn net.Conn) *HTTPAdapter { return &HTTPAdapter{ - addr: parseHTTPAddr(request), - R: request, - conn: conn, + metadata: parseHTTPAddr(request), + R: request, + conn: conn, } } diff --git a/adapters/local/https.go b/adapters/inbound/https.go similarity index 77% rename from adapters/local/https.go rename to adapters/inbound/https.go index 61d31255..6e36180b 100644 --- a/adapters/local/https.go +++ b/adapters/inbound/https.go @@ -8,7 +8,7 @@ import ( // NewHTTPS is HTTPAdapter generator func NewHTTPS(request *http.Request, conn net.Conn) *SocketAdapter { return &SocketAdapter{ - addr: parseHTTPAddr(request), - conn: conn, + metadata: parseHTTPAddr(request), + conn: conn, } } diff --git a/adapters/local/socket.go b/adapters/inbound/socket.go similarity index 72% rename from adapters/local/socket.go rename to adapters/inbound/socket.go index ff9b09c3..8d701ad6 100644 --- a/adapters/local/socket.go +++ b/adapters/inbound/socket.go @@ -9,8 +9,8 @@ import ( // SocketAdapter is a adapter for socks and redir connection type SocketAdapter struct { - conn net.Conn - addr *C.Addr + conn net.Conn + metadata *C.Metadata } // Close socks and redir connection @@ -18,9 +18,9 @@ func (s *SocketAdapter) Close() { s.conn.Close() } -// Addr return destination address -func (s *SocketAdapter) Addr() *C.Addr { - return s.addr +// Metadata return destination metadata +func (s *SocketAdapter) Metadata() *C.Metadata { + return s.metadata } // Conn return raw net.Conn @@ -31,7 +31,7 @@ func (s *SocketAdapter) Conn() net.Conn { // NewSocket is SocketAdapter generator func NewSocket(target socks.Addr, conn net.Conn) *SocketAdapter { return &SocketAdapter{ - conn: conn, - addr: parseSocksAddr(target), + conn: conn, + metadata: parseSocksAddr(target), } } diff --git a/adapters/local/util.go b/adapters/inbound/util.go similarity index 90% rename from adapters/local/util.go rename to adapters/inbound/util.go index 0a713421..06499f04 100644 --- a/adapters/local/util.go +++ b/adapters/inbound/util.go @@ -9,7 +9,7 @@ import ( "github.com/Dreamacro/go-shadowsocks2/socks" ) -func parseSocksAddr(target socks.Addr) *C.Addr { +func parseSocksAddr(target socks.Addr) *C.Metadata { var host, port string var ip net.IP @@ -29,7 +29,7 @@ func parseSocksAddr(target socks.Addr) *C.Addr { port = strconv.Itoa((int(target[1+net.IPv6len]) << 8) | int(target[1+net.IPv6len+1])) } - return &C.Addr{ + return &C.Metadata{ NetWork: C.TCP, AddrType: int(target[0]), Host: host, @@ -38,7 +38,7 @@ func parseSocksAddr(target socks.Addr) *C.Addr { } } -func parseHTTPAddr(request *http.Request) *C.Addr { +func parseHTTPAddr(request *http.Request) *C.Metadata { host := request.URL.Hostname() port := request.URL.Port() if port == "" { @@ -61,7 +61,7 @@ func parseHTTPAddr(request *http.Request) *C.Addr { addType = socks.AtypIPv4 } - return &C.Addr{ + return &C.Metadata{ NetWork: C.TCP, AddrType: addType, Host: host, diff --git a/adapters/remote/direct.go b/adapters/outbound/direct.go similarity index 78% rename from adapters/remote/direct.go rename to adapters/outbound/direct.go index 9d22f948..70a1d648 100644 --- a/adapters/remote/direct.go +++ b/adapters/outbound/direct.go @@ -31,8 +31,8 @@ func (d *Direct) Type() C.AdapterType { return C.Direct } -func (d *Direct) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) { - c, err := net.Dial("tcp", net.JoinHostPort(addr.String(), addr.Port)) +func (d *Direct) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) { + c, err := net.Dial("tcp", net.JoinHostPort(metadata.String(), metadata.Port)) if err != nil { return } diff --git a/adapters/remote/fallback.go b/adapters/outbound/fallback.go similarity index 92% rename from adapters/remote/fallback.go rename to adapters/outbound/fallback.go index 5fe7a773..435a2e83 100644 --- a/adapters/remote/fallback.go +++ b/adapters/outbound/fallback.go @@ -37,7 +37,7 @@ func (f *Fallback) Now() string { return f.proxies[0].RawProxy.Name() } -func (f *Fallback) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) { +func (f *Fallback) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) { idx := 0 var proxy *proxy for { @@ -45,7 +45,7 @@ func (f *Fallback) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) { if proxy == nil { break } - adapter, err = proxy.RawProxy.Generator(addr) + adapter, err = proxy.RawProxy.Generator(metadata) if err != nil { proxy.Valid = false idx++ @@ -99,7 +99,7 @@ func (f *Fallback) validTest() { } func NewFallback(name string, proxies []C.Proxy, rawURL string, delay time.Duration) (*Fallback, error) { - _, err := urlToAddr(rawURL) + _, err := urlToMetadata(rawURL) if err != nil { return nil, err } diff --git a/adapters/remote/reject.go b/adapters/outbound/reject.go similarity index 94% rename from adapters/remote/reject.go rename to adapters/outbound/reject.go index b4ce4d86..3bc8bdf3 100644 --- a/adapters/remote/reject.go +++ b/adapters/outbound/reject.go @@ -32,7 +32,7 @@ func (r *Reject) Type() C.AdapterType { return C.Reject } -func (r *Reject) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) { +func (r *Reject) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) { return &RejectAdapter{conn: &NopConn{}}, nil } diff --git a/adapters/remote/selector.go b/adapters/outbound/selector.go similarity index 89% rename from adapters/remote/selector.go rename to adapters/outbound/selector.go index 92326c1c..719335f8 100644 --- a/adapters/remote/selector.go +++ b/adapters/outbound/selector.go @@ -21,8 +21,8 @@ func (s *Selector) Type() C.AdapterType { return C.Selector } -func (s *Selector) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) { - return s.selected.Generator(addr) +func (s *Selector) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) { + return s.selected.Generator(metadata) } func (s *Selector) Now() string { diff --git a/adapters/remote/shadowsocks.go b/adapters/outbound/shadowsocks.go similarity index 82% rename from adapters/remote/shadowsocks.go rename to adapters/outbound/shadowsocks.go index 8c0a0987..3a46c034 100644 --- a/adapters/remote/shadowsocks.go +++ b/adapters/outbound/shadowsocks.go @@ -7,7 +7,7 @@ import ( "net/url" "strconv" - "github.com/Dreamacro/clash/common/simple-obfs" + "github.com/Dreamacro/clash/component/simple-obfs" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/go-shadowsocks2/core" @@ -44,7 +44,7 @@ func (ss *ShadowSocks) Type() C.AdapterType { return C.Shadowsocks } -func (ss *ShadowSocks) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) { +func (ss *ShadowSocks) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) { c, err := net.Dial("tcp", ss.server) if err != nil { return nil, fmt.Errorf("%s connect error", ss.server) @@ -58,7 +58,7 @@ func (ss *ShadowSocks) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err erro c = obfs.NewHTTPObfs(c, ss.obfsHost, port) } c = ss.cipher.StreamConn(c) - _, err = c.Write(serializesSocksAddr(addr)) + _, err = c.Write(serializesSocksAddr(metadata)) return &ShadowsocksAdapter{conn: c}, err } @@ -102,21 +102,21 @@ func parseURL(s string) (addr, cipher, password string, err error) { return } -func serializesSocksAddr(addr *C.Addr) []byte { +func serializesSocksAddr(metadata *C.Metadata) []byte { var buf [][]byte - aType := uint8(addr.AddrType) - p, _ := strconv.Atoi(addr.Port) + aType := uint8(metadata.AddrType) + p, _ := strconv.Atoi(metadata.Port) port := []byte{uint8(p >> 8), uint8(p & 0xff)} - switch addr.AddrType { + switch metadata.AddrType { case socks.AtypDomainName: - len := uint8(len(addr.Host)) - host := []byte(addr.Host) + len := uint8(len(metadata.Host)) + host := []byte(metadata.Host) buf = [][]byte{{aType, len}, host, port} case socks.AtypIPv4: - host := addr.IP.To4() + host := metadata.IP.To4() buf = [][]byte{{aType}, host, port} case socks.AtypIPv6: - host := addr.IP.To16() + host := metadata.IP.To16() buf = [][]byte{{aType}, host, port} } return bytes.Join(buf, nil) diff --git a/adapters/remote/socks5.go b/adapters/outbound/socks5.go similarity index 83% rename from adapters/remote/socks5.go rename to adapters/outbound/socks5.go index 5464dd98..c9a61494 100644 --- a/adapters/remote/socks5.go +++ b/adapters/outbound/socks5.go @@ -39,19 +39,19 @@ func (ss *Socks5) Type() C.AdapterType { return C.Socks5 } -func (ss *Socks5) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) { +func (ss *Socks5) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) { c, err := net.Dial("tcp", ss.addr) if err != nil { return nil, fmt.Errorf("%s connect error", ss.addr) } tcpKeepAlive(c) - if err := ss.shakeHand(addr, c); err != nil { + if err := ss.shakeHand(metadata, c); err != nil { return nil, err } return &Socks5Adapter{conn: c}, nil } -func (ss *Socks5) shakeHand(addr *C.Addr, rw io.ReadWriter) error { +func (ss *Socks5) shakeHand(metadata *C.Metadata, rw io.ReadWriter) error { buf := make([]byte, socks.MaxAddrLen) // VER, CMD, RSV @@ -71,7 +71,7 @@ func (ss *Socks5) shakeHand(addr *C.Addr, rw io.ReadWriter) error { } // VER, CMD, RSV, ADDR - if _, err := rw.Write(bytes.Join([][]byte{{5, 1, 0}, serializesSocksAddr(addr)}, []byte(""))); err != nil { + if _, err := rw.Write(bytes.Join([][]byte{{5, 1, 0}, serializesSocksAddr(metadata)}, []byte(""))); err != nil { return err } diff --git a/adapters/remote/urltest.go b/adapters/outbound/urltest.go similarity index 90% rename from adapters/remote/urltest.go rename to adapters/outbound/urltest.go index 3a0fa664..751a7edf 100644 --- a/adapters/remote/urltest.go +++ b/adapters/outbound/urltest.go @@ -28,8 +28,8 @@ func (u *URLTest) Now() string { return u.fast.Name() } -func (u *URLTest) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) { - return u.fast.Generator(addr) +func (u *URLTest) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) { + return u.fast.Generator(metadata) } func (u *URLTest) Close() { @@ -84,7 +84,7 @@ func (u *URLTest) speedTest() { } func NewURLTest(name string, proxies []C.Proxy, rawURL string, delay time.Duration) (*URLTest, error) { - _, err := urlToAddr(rawURL) + _, err := urlToMetadata(rawURL) if err != nil { return nil, err } diff --git a/adapters/remote/util.go b/adapters/outbound/util.go similarity index 93% rename from adapters/remote/util.go rename to adapters/outbound/util.go index e8a35006..1073c94e 100644 --- a/adapters/remote/util.go +++ b/adapters/outbound/util.go @@ -12,7 +12,7 @@ import ( // DelayTest get the delay for the specified URL func DelayTest(proxy C.Proxy, url string) (t int16, err error) { - addr, err := urlToAddr(url) + addr, err := urlToMetadata(url) if err != nil { return } @@ -43,7 +43,7 @@ func DelayTest(proxy C.Proxy, url string) (t int16, err error) { return } -func urlToAddr(rawURL string) (addr C.Addr, err error) { +func urlToMetadata(rawURL string) (addr C.Metadata, err error) { u, err := url.Parse(rawURL) if err != nil { return @@ -61,7 +61,7 @@ func urlToAddr(rawURL string) (addr C.Addr, err error) { } } - addr = C.Addr{ + addr = C.Metadata{ AddrType: C.AtypDomainName, Host: u.Hostname(), IP: nil, diff --git a/adapters/remote/vmess.go b/adapters/outbound/vmess.go similarity index 74% rename from adapters/remote/vmess.go rename to adapters/outbound/vmess.go index e98828c3..4bb0817b 100644 --- a/adapters/remote/vmess.go +++ b/adapters/outbound/vmess.go @@ -6,7 +6,7 @@ import ( "strconv" "strings" - "github.com/Dreamacro/clash/common/vmess" + "github.com/Dreamacro/clash/component/vmess" C "github.com/Dreamacro/clash/constant" ) @@ -38,13 +38,13 @@ func (ss *Vmess) Type() C.AdapterType { return C.Vmess } -func (ss *Vmess) Generator(addr *C.Addr) (adapter C.ProxyAdapter, err error) { +func (ss *Vmess) Generator(metadata *C.Metadata) (adapter C.ProxyAdapter, err error) { c, err := net.Dial("tcp", ss.server) if err != nil { return nil, fmt.Errorf("%s connect error", ss.server) } tcpKeepAlive(c) - c = ss.client.New(c, parseVmessAddr(addr)) + c = ss.client.New(c, parseVmessAddr(metadata)) return &VmessAdapter{conn: c}, err } @@ -67,26 +67,26 @@ func NewVmess(name string, server string, uuid string, alterID uint16, security }, nil } -func parseVmessAddr(info *C.Addr) *vmess.DstAddr { +func parseVmessAddr(metadata *C.Metadata) *vmess.DstAddr { var addrType byte var addr []byte - switch info.AddrType { + switch metadata.AddrType { case C.AtypIPv4: addrType = byte(vmess.AtypIPv4) addr = make([]byte, net.IPv4len) - copy(addr[:], info.IP.To4()) + copy(addr[:], metadata.IP.To4()) case C.AtypIPv6: addrType = byte(vmess.AtypIPv6) addr = make([]byte, net.IPv6len) - copy(addr[:], info.IP.To16()) + copy(addr[:], metadata.IP.To16()) case C.AtypDomainName: addrType = byte(vmess.AtypDomainName) - addr = make([]byte, len(info.Host)+1) - addr[0] = byte(len(info.Host)) - copy(addr[1:], []byte(info.Host)) + addr = make([]byte, len(metadata.Host)+1) + addr[0] = byte(len(metadata.Host)) + copy(addr[1:], []byte(metadata.Host)) } - port, _ := strconv.Atoi(info.Port) + port, _ := strconv.Atoi(metadata.Port) return &vmess.DstAddr{ AddrType: addrType, Addr: addr, diff --git a/observable/iterable.go b/common/observable/iterable.go similarity index 100% rename from observable/iterable.go rename to common/observable/iterable.go diff --git a/observable/observable.go b/common/observable/observable.go similarity index 100% rename from observable/observable.go rename to common/observable/observable.go diff --git a/observable/observable_test.go b/common/observable/observable_test.go similarity index 100% rename from observable/observable_test.go rename to common/observable/observable_test.go diff --git a/observable/subscriber.go b/common/observable/subscriber.go similarity index 100% rename from observable/subscriber.go rename to common/observable/subscriber.go diff --git a/common/simple-obfs/http.go b/component/simple-obfs/http.go similarity index 100% rename from common/simple-obfs/http.go rename to component/simple-obfs/http.go diff --git a/common/simple-obfs/tls.go b/component/simple-obfs/tls.go similarity index 100% rename from common/simple-obfs/tls.go rename to component/simple-obfs/tls.go diff --git a/common/vmess/aead.go b/component/vmess/aead.go similarity index 100% rename from common/vmess/aead.go rename to component/vmess/aead.go diff --git a/common/vmess/chunk.go b/component/vmess/chunk.go similarity index 100% rename from common/vmess/chunk.go rename to component/vmess/chunk.go diff --git a/common/vmess/conn.go b/component/vmess/conn.go similarity index 100% rename from common/vmess/conn.go rename to component/vmess/conn.go diff --git a/common/vmess/user.go b/component/vmess/user.go similarity index 100% rename from common/vmess/user.go rename to component/vmess/user.go diff --git a/common/vmess/vmess.go b/component/vmess/vmess.go similarity index 100% rename from common/vmess/vmess.go rename to component/vmess/vmess.go diff --git a/config/config.go b/config/config.go index fc8e0a27..c72ffcb2 100644 --- a/config/config.go +++ b/config/config.go @@ -10,9 +10,9 @@ import ( "sync" "time" - "github.com/Dreamacro/clash/adapters/remote" + "github.com/Dreamacro/clash/adapters/outbound" + "github.com/Dreamacro/clash/common/observable" C "github.com/Dreamacro/clash/constant" - "github.com/Dreamacro/clash/observable" R "github.com/Dreamacro/clash/rules" log "github.com/sirupsen/logrus" diff --git a/constant/adapters.go b/constant/adapters.go index 9f2e0406..69a00ba1 100644 --- a/constant/adapters.go +++ b/constant/adapters.go @@ -22,14 +22,14 @@ type ProxyAdapter interface { } type ServerAdapter interface { - Addr() *Addr + Metadata() *Metadata Close() } type Proxy interface { Name() string Type() AdapterType - Generator(addr *Addr) (ProxyAdapter, error) + Generator(metadata *Metadata) (ProxyAdapter, error) } // AdapterType is enum of adapter type diff --git a/constant/addr.go b/constant/metadata.go similarity index 81% rename from constant/addr.go rename to constant/metadata.go index e404b489..dfc274e7 100644 --- a/constant/addr.go +++ b/constant/metadata.go @@ -28,8 +28,8 @@ func (n *NetWork) String() string { type SourceType int -// Addr is used to store connection address -type Addr struct { +// Metadata is used to store connection address +type Metadata struct { NetWork NetWork Source SourceType AddrType int @@ -38,7 +38,7 @@ type Addr struct { Port string } -func (addr *Addr) String() string { +func (addr *Metadata) String() string { if addr.Host == "" { return addr.IP.String() } diff --git a/constant/rule.go b/constant/rule.go index db449c7e..1a8f5f97 100644 --- a/constant/rule.go +++ b/constant/rule.go @@ -33,7 +33,7 @@ func (rt RuleType) String() string { type Rule interface { RuleType() RuleType - IsMatch(addr *Addr) bool + IsMatch(metadata *Metadata) bool Adapter() string Payload() string } diff --git a/hub/proxies.go b/hub/proxies.go index 19bccca4..c0fe062b 100644 --- a/hub/proxies.go +++ b/hub/proxies.go @@ -6,7 +6,7 @@ import ( "strconv" "time" - A "github.com/Dreamacro/clash/adapters/remote" + A "github.com/Dreamacro/clash/adapters/outbound" C "github.com/Dreamacro/clash/constant" "github.com/go-chi/chi" diff --git a/proxy/http/server.go b/proxy/http/server.go index eb11c648..59830058 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -5,7 +5,7 @@ import ( "net" "net/http" - "github.com/Dreamacro/clash/adapters/local" + "github.com/Dreamacro/clash/adapters/inbound" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/tunnel" diff --git a/proxy/redir/tcp.go b/proxy/redir/tcp.go index 3f653dc9..f4b47f4b 100644 --- a/proxy/redir/tcp.go +++ b/proxy/redir/tcp.go @@ -3,7 +3,7 @@ package redir import ( "net" - "github.com/Dreamacro/clash/adapters/local" + "github.com/Dreamacro/clash/adapters/inbound" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/tunnel" diff --git a/proxy/socks/tcp.go b/proxy/socks/tcp.go index da9a85bd..57645dc3 100644 --- a/proxy/socks/tcp.go +++ b/proxy/socks/tcp.go @@ -3,7 +3,7 @@ package socks import ( "net" - "github.com/Dreamacro/clash/adapters/local" + "github.com/Dreamacro/clash/adapters/inbound" C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/tunnel" diff --git a/rules/domain_keyword.go b/rules/domain_keyword.go index 19ca2e22..f2b1dd1c 100644 --- a/rules/domain_keyword.go +++ b/rules/domain_keyword.go @@ -15,11 +15,11 @@ func (dk *DomainKeyword) RuleType() C.RuleType { return C.DomainKeyword } -func (dk *DomainKeyword) IsMatch(addr *C.Addr) bool { - if addr.AddrType != C.AtypDomainName { +func (dk *DomainKeyword) IsMatch(metadata *C.Metadata) bool { + if metadata.AddrType != C.AtypDomainName { return false } - domain := addr.Host + domain := metadata.Host return strings.Contains(domain, dk.keyword) } diff --git a/rules/domain_suffix.go b/rules/domain_suffix.go index d6dd9f97..c29ab06e 100644 --- a/rules/domain_suffix.go +++ b/rules/domain_suffix.go @@ -15,11 +15,11 @@ func (ds *DomainSuffix) RuleType() C.RuleType { return C.DomainSuffix } -func (ds *DomainSuffix) IsMatch(addr *C.Addr) bool { - if addr.AddrType != C.AtypDomainName { +func (ds *DomainSuffix) IsMatch(metadata *C.Metadata) bool { + if metadata.AddrType != C.AtypDomainName { return false } - domain := addr.Host + domain := metadata.Host return strings.HasSuffix(domain, "."+ds.suffix) || domain == ds.suffix } diff --git a/rules/domian.go b/rules/domian.go index a836ca67..181fc1d2 100644 --- a/rules/domian.go +++ b/rules/domian.go @@ -13,11 +13,11 @@ func (d *Domain) RuleType() C.RuleType { return C.Domain } -func (d *Domain) IsMatch(addr *C.Addr) bool { - if addr.AddrType != C.AtypDomainName { +func (d *Domain) IsMatch(metadata *C.Metadata) bool { + if metadata.AddrType != C.AtypDomainName { return false } - return addr.Host == d.domain + return metadata.Host == d.domain } func (d *Domain) Adapter() string { diff --git a/rules/final.go b/rules/final.go index f07fbab1..1cc3b888 100644 --- a/rules/final.go +++ b/rules/final.go @@ -12,7 +12,7 @@ func (f *Final) RuleType() C.RuleType { return C.FINAL } -func (f *Final) IsMatch(addr *C.Addr) bool { +func (f *Final) IsMatch(metadata *C.Metadata) bool { return true } diff --git a/rules/geoip.go b/rules/geoip.go index f79f26f0..ff52ff36 100644 --- a/rules/geoip.go +++ b/rules/geoip.go @@ -23,11 +23,11 @@ func (g *GEOIP) RuleType() C.RuleType { return C.GEOIP } -func (g *GEOIP) IsMatch(addr *C.Addr) bool { - if addr.IP == nil { +func (g *GEOIP) IsMatch(metadata *C.Metadata) bool { + if metadata.IP == nil { return false } - record, _ := mmdb.Country(*addr.IP) + record, _ := mmdb.Country(*metadata.IP) return record.Country.IsoCode == g.country } diff --git a/rules/ipcidr.go b/rules/ipcidr.go index baf44757..fe1a9d9c 100644 --- a/rules/ipcidr.go +++ b/rules/ipcidr.go @@ -15,12 +15,12 @@ func (i *IPCIDR) RuleType() C.RuleType { return C.IPCIDR } -func (i *IPCIDR) IsMatch(addr *C.Addr) bool { - if addr.IP == nil { +func (i *IPCIDR) IsMatch(metadata *C.Metadata) bool { + if metadata.IP == nil { return false } - return i.ipnet.Contains(*addr.IP) + return i.ipnet.Contains(*metadata.IP) } func (i *IPCIDR) Adapter() string { diff --git a/tunnel/connection.go b/tunnel/connection.go index 842927a1..3b69e506 100644 --- a/tunnel/connection.go +++ b/tunnel/connection.go @@ -7,7 +7,7 @@ import ( "net/http" "time" - "github.com/Dreamacro/clash/adapters/local" + "github.com/Dreamacro/clash/adapters/inbound" C "github.com/Dreamacro/clash/constant" ) diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index a3d29f68..fb432554 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -4,10 +4,10 @@ import ( "sync" "time" - LocalAdapter "github.com/Dreamacro/clash/adapters/local" + InboundAdapter "github.com/Dreamacro/clash/adapters/inbound" + "github.com/Dreamacro/clash/common/observable" cfg "github.com/Dreamacro/clash/config" C "github.com/Dreamacro/clash/constant" - "github.com/Dreamacro/clash/observable" "gopkg.in/eapache/channels.v1" ) @@ -84,7 +84,7 @@ func (t *Tunnel) process() { func (t *Tunnel) handleConn(localConn C.ServerAdapter) { defer localConn.Close() - addr := localConn.Addr() + metadata := localConn.Metadata() var proxy C.Proxy switch t.mode { @@ -94,9 +94,9 @@ func (t *Tunnel) handleConn(localConn C.ServerAdapter) { proxy = t.proxies["GLOBAL"] // Rule default: - proxy = t.match(addr) + proxy = t.match(metadata) } - remoConn, err := proxy.Generator(addr) + remoConn, err := proxy.Generator(metadata) if err != nil { t.logCh <- newLog(C.WARNING, "Proxy connect error: %s", err.Error()) return @@ -104,28 +104,28 @@ func (t *Tunnel) handleConn(localConn C.ServerAdapter) { defer remoConn.Close() switch adapter := localConn.(type) { - case *LocalAdapter.HTTPAdapter: + case *InboundAdapter.HTTPAdapter: t.handleHTTP(adapter, remoConn) - case *LocalAdapter.SocketAdapter: + case *InboundAdapter.SocketAdapter: t.handleSOCKS(adapter, remoConn) } } -func (t *Tunnel) match(addr *C.Addr) C.Proxy { +func (t *Tunnel) match(metadata *C.Metadata) C.Proxy { t.configLock.RLock() defer t.configLock.RUnlock() for _, rule := range t.rules { - if rule.IsMatch(addr) { + if rule.IsMatch(metadata) { a, ok := t.proxies[rule.Adapter()] if !ok { continue } - t.logCh <- newLog(C.INFO, "%v match %s using %s", addr.String(), rule.RuleType().String(), rule.Adapter()) + t.logCh <- newLog(C.INFO, "%v match %s using %s", metadata.String(), rule.RuleType().String(), rule.Adapter()) return a } } - t.logCh <- newLog(C.INFO, "%v doesn't match any rule using DIRECT", addr.String()) + t.logCh <- newLog(C.INFO, "%v doesn't match any rule using DIRECT", metadata.String()) return t.proxies["DIRECT"] }