From d759d16944c1dfb2e72b9dc76265c849d901a82c Mon Sep 17 00:00:00 2001 From: Dreamacro <8615343+Dreamacro@users.noreply.github.com> Date: Wed, 24 Mar 2021 01:00:21 +0800 Subject: [PATCH] Style: cleanup code --- adapters/outboundgroup/util.go | 36 ++++++++++------------ adapters/provider/parser.go | 2 +- component/process/process_freebsd_amd64.go | 2 +- component/process/process_windows.go | 12 ++++---- component/trie/domain.go | 6 +--- component/vmess/h2.go | 5 +-- proxy/http/server.go | 10 +++--- proxy/listener.go | 7 ++--- rules/process.go | 10 +++--- 9 files changed, 40 insertions(+), 50 deletions(-) diff --git a/adapters/outboundgroup/util.go b/adapters/outboundgroup/util.go index c8a6a858..70d71078 100644 --- a/adapters/outboundgroup/util.go +++ b/adapters/outboundgroup/util.go @@ -16,25 +16,7 @@ func addrToMetadata(rawAddress string) (addr *C.Metadata, err error) { } ip := net.ParseIP(host) - if ip != nil { - if ip.To4() != nil { - addr = &C.Metadata{ - AddrType: C.AtypIPv4, - Host: "", - DstIP: ip, - DstPort: port, - } - return - } else { - addr = &C.Metadata{ - AddrType: C.AtypIPv6, - Host: "", - DstIP: ip, - DstPort: port, - } - return - } - } else { + if ip == nil { addr = &C.Metadata{ AddrType: C.AtypDomainName, Host: host, @@ -42,7 +24,23 @@ func addrToMetadata(rawAddress string) (addr *C.Metadata, err error) { DstPort: port, } return + } else if ip4 := ip.To4(); ip4 != nil { + addr = &C.Metadata{ + AddrType: C.AtypIPv4, + Host: "", + DstIP: ip4, + DstPort: port, + } + return } + + addr = &C.Metadata{ + AddrType: C.AtypIPv6, + Host: "", + DstIP: ip, + DstPort: port, + } + return } func tcpKeepAlive(c net.Conn) { diff --git a/adapters/provider/parser.go b/adapters/provider/parser.go index 1c9b2e68..aa5d7ae2 100644 --- a/adapters/provider/parser.go +++ b/adapters/provider/parser.go @@ -40,7 +40,7 @@ func ParseProxyProvider(name string, mapping map[string]interface{}) (ProxyProvi return nil, err } - var hcInterval uint = 0 + var hcInterval uint if schema.HealthCheck.Enable { hcInterval = uint(schema.HealthCheck.Interval) } diff --git a/component/process/process_freebsd_amd64.go b/component/process/process_freebsd_amd64.go index 5a80670e..94f744da 100644 --- a/component/process/process_freebsd_amd64.go +++ b/component/process/process_freebsd_amd64.go @@ -173,7 +173,7 @@ func (s *searcher) searchSocketPid(socket uint64) (uint32, error) { } func newSearcher(major int) *searcher { - var s *searcher = nil + var s *searcher switch major { case 11: s = &searcher{ diff --git a/component/process/process_windows.go b/component/process/process_windows.go index ba96c7e4..834bc824 100644 --- a/component/process/process_windows.go +++ b/component/process/process_windows.go @@ -22,8 +22,8 @@ const ( ) var ( - getExTcpTable uintptr - getExUdpTable uintptr + getExTCPTable uintptr + getExUDPTable uintptr queryProcName uintptr once sync.Once @@ -35,12 +35,12 @@ func initWin32API() error { return fmt.Errorf("LoadLibrary iphlpapi.dll failed: %s", err.Error()) } - getExTcpTable, err = windows.GetProcAddress(h, tcpTableFunc) + getExTCPTable, err = windows.GetProcAddress(h, tcpTableFunc) if err != nil { return fmt.Errorf("GetProcAddress of %s failed: %s", tcpTableFunc, err.Error()) } - getExUdpTable, err = windows.GetProcAddress(h, udpTableFunc) + getExUDPTable, err = windows.GetProcAddress(h, udpTableFunc) if err != nil { return fmt.Errorf("GetProcAddress of %s failed: %s", udpTableFunc, err.Error()) } @@ -76,10 +76,10 @@ func findProcessName(network string, ip net.IP, srcPort int) (string, error) { var fn uintptr switch network { case TCP: - fn = getExTcpTable + fn = getExTCPTable class = tcpTablePidConn case UDP: - fn = getExUdpTable + fn = getExUDPTable class = udpTablePid default: return "", ErrInvalidNetwork diff --git a/component/trie/domain.go b/component/trie/domain.go index 909cccf0..c063686e 100644 --- a/component/trie/domain.go +++ b/component/trie/domain.go @@ -122,11 +122,7 @@ func (t *DomainTrie) search(node *Node, parts []string) *Node { } } - if c := node.getChild(dotWildcard); c != nil { - return c - } - - return nil + return node.getChild(dotWildcard) } // New returns a new, empty Trie. diff --git a/component/vmess/h2.go b/component/vmess/h2.go index 2c4c47fa..d4e81607 100644 --- a/component/vmess/h2.go +++ b/component/vmess/h2.go @@ -90,10 +90,7 @@ func (hc *h2Conn) Close() error { if err := hc.ClientConn.Shutdown(hc.res.Request.Context()); err != nil { return err } - if err := hc.Conn.Close(); err != nil { - return err - } - return nil + return hc.Conn.Close() } func StreamH2Conn(conn net.Conn, cfg *H2Config) (net.Conn, error) { diff --git a/proxy/http/server.go b/proxy/http/server.go index 7cf2b1c1..75f4fc36 100644 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -16,19 +16,19 @@ import ( "github.com/Dreamacro/clash/tunnel" ) -type HttpListener struct { +type HTTPListener struct { net.Listener address string closed bool cache *cache.Cache } -func NewHttpProxy(addr string) (*HttpListener, error) { +func NewHTTPProxy(addr string) (*HTTPListener, error) { l, err := net.Listen("tcp", addr) if err != nil { return nil, err } - hl := &HttpListener{l, addr, false, cache.New(30 * time.Second)} + hl := &HTTPListener{l, addr, false, cache.New(30 * time.Second)} go func() { log.Infoln("HTTP proxy listening at: %s", addr) @@ -48,12 +48,12 @@ func NewHttpProxy(addr string) (*HttpListener, error) { return hl, nil } -func (l *HttpListener) Close() { +func (l *HTTPListener) Close() { l.closed = true l.Listener.Close() } -func (l *HttpListener) Address() string { +func (l *HTTPListener) Address() string { return l.address } diff --git a/proxy/listener.go b/proxy/listener.go index 3ccfbe96..ec9b69f9 100644 --- a/proxy/listener.go +++ b/proxy/listener.go @@ -19,7 +19,7 @@ var ( socksListener *socks.SockListener socksUDPListener *socks.SockUDPListener - httpListener *http.HttpListener + httpListener *http.HTTPListener redirListener *redir.RedirListener redirUDPListener *redir.RedirUDPListener tproxyListener *redir.TProxyListener @@ -78,7 +78,7 @@ func ReCreateHTTP(port int) error { } var err error - httpListener, err = http.NewHttpProxy(addr) + httpListener, err = http.NewHTTPProxy(addr) if err != nil { return err } @@ -316,9 +316,8 @@ func genAddr(host string, port int, allowLan bool) string { if allowLan { if host == "*" { return fmt.Sprintf(":%d", port) - } else { - return fmt.Sprintf("%s:%d", host, port) } + return fmt.Sprintf("%s:%d", host, port) } return fmt.Sprintf("127.0.0.1:%d", port) diff --git a/rules/process.go b/rules/process.go index 3f502e7f..638ec723 100644 --- a/rules/process.go +++ b/rules/process.go @@ -45,15 +45,15 @@ func (ps *Process) Match(metadata *C.Metadata) bool { return strings.EqualFold(cached.(string), ps.process) } -func (p *Process) Adapter() string { - return p.adapter +func (ps *Process) Adapter() string { + return ps.adapter } -func (p *Process) Payload() string { - return p.process +func (ps *Process) Payload() string { + return ps.process } -func (p *Process) ShouldResolveIP() bool { +func (ps *Process) ShouldResolveIP() bool { return false }