Style: cleanup code

This commit is contained in:
Dreamacro 2021-03-24 01:00:21 +08:00
parent 807d53c1e7
commit d759d16944
9 changed files with 40 additions and 50 deletions

View file

@ -16,25 +16,7 @@ func addrToMetadata(rawAddress string) (addr *C.Metadata, err error) {
} }
ip := net.ParseIP(host) ip := net.ParseIP(host)
if ip != nil { 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 {
addr = &C.Metadata{ addr = &C.Metadata{
AddrType: C.AtypDomainName, AddrType: C.AtypDomainName,
Host: host, Host: host,
@ -42,7 +24,23 @@ func addrToMetadata(rawAddress string) (addr *C.Metadata, err error) {
DstPort: port, DstPort: port,
} }
return 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) { func tcpKeepAlive(c net.Conn) {

View file

@ -40,7 +40,7 @@ func ParseProxyProvider(name string, mapping map[string]interface{}) (ProxyProvi
return nil, err return nil, err
} }
var hcInterval uint = 0 var hcInterval uint
if schema.HealthCheck.Enable { if schema.HealthCheck.Enable {
hcInterval = uint(schema.HealthCheck.Interval) hcInterval = uint(schema.HealthCheck.Interval)
} }

View file

@ -173,7 +173,7 @@ func (s *searcher) searchSocketPid(socket uint64) (uint32, error) {
} }
func newSearcher(major int) *searcher { func newSearcher(major int) *searcher {
var s *searcher = nil var s *searcher
switch major { switch major {
case 11: case 11:
s = &searcher{ s = &searcher{

View file

@ -22,8 +22,8 @@ const (
) )
var ( var (
getExTcpTable uintptr getExTCPTable uintptr
getExUdpTable uintptr getExUDPTable uintptr
queryProcName uintptr queryProcName uintptr
once sync.Once once sync.Once
@ -35,12 +35,12 @@ func initWin32API() error {
return fmt.Errorf("LoadLibrary iphlpapi.dll failed: %s", err.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 { if err != nil {
return fmt.Errorf("GetProcAddress of %s failed: %s", tcpTableFunc, err.Error()) 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 { if err != nil {
return fmt.Errorf("GetProcAddress of %s failed: %s", udpTableFunc, err.Error()) 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 var fn uintptr
switch network { switch network {
case TCP: case TCP:
fn = getExTcpTable fn = getExTCPTable
class = tcpTablePidConn class = tcpTablePidConn
case UDP: case UDP:
fn = getExUdpTable fn = getExUDPTable
class = udpTablePid class = udpTablePid
default: default:
return "", ErrInvalidNetwork return "", ErrInvalidNetwork

View file

@ -122,11 +122,7 @@ func (t *DomainTrie) search(node *Node, parts []string) *Node {
} }
} }
if c := node.getChild(dotWildcard); c != nil { return node.getChild(dotWildcard)
return c
}
return nil
} }
// New returns a new, empty Trie. // New returns a new, empty Trie.

View file

@ -90,10 +90,7 @@ func (hc *h2Conn) Close() error {
if err := hc.ClientConn.Shutdown(hc.res.Request.Context()); err != nil { if err := hc.ClientConn.Shutdown(hc.res.Request.Context()); err != nil {
return err return err
} }
if err := hc.Conn.Close(); err != nil { return hc.Conn.Close()
return err
}
return nil
} }
func StreamH2Conn(conn net.Conn, cfg *H2Config) (net.Conn, error) { func StreamH2Conn(conn net.Conn, cfg *H2Config) (net.Conn, error) {

View file

@ -16,19 +16,19 @@ import (
"github.com/Dreamacro/clash/tunnel" "github.com/Dreamacro/clash/tunnel"
) )
type HttpListener struct { type HTTPListener struct {
net.Listener net.Listener
address string address string
closed bool closed bool
cache *cache.Cache cache *cache.Cache
} }
func NewHttpProxy(addr string) (*HttpListener, error) { func NewHTTPProxy(addr string) (*HTTPListener, error) {
l, err := net.Listen("tcp", addr) l, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
return nil, err 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() { go func() {
log.Infoln("HTTP proxy listening at: %s", addr) log.Infoln("HTTP proxy listening at: %s", addr)
@ -48,12 +48,12 @@ func NewHttpProxy(addr string) (*HttpListener, error) {
return hl, nil return hl, nil
} }
func (l *HttpListener) Close() { func (l *HTTPListener) Close() {
l.closed = true l.closed = true
l.Listener.Close() l.Listener.Close()
} }
func (l *HttpListener) Address() string { func (l *HTTPListener) Address() string {
return l.address return l.address
} }

View file

@ -19,7 +19,7 @@ var (
socksListener *socks.SockListener socksListener *socks.SockListener
socksUDPListener *socks.SockUDPListener socksUDPListener *socks.SockUDPListener
httpListener *http.HttpListener httpListener *http.HTTPListener
redirListener *redir.RedirListener redirListener *redir.RedirListener
redirUDPListener *redir.RedirUDPListener redirUDPListener *redir.RedirUDPListener
tproxyListener *redir.TProxyListener tproxyListener *redir.TProxyListener
@ -78,7 +78,7 @@ func ReCreateHTTP(port int) error {
} }
var err error var err error
httpListener, err = http.NewHttpProxy(addr) httpListener, err = http.NewHTTPProxy(addr)
if err != nil { if err != nil {
return err return err
} }
@ -316,9 +316,8 @@ func genAddr(host string, port int, allowLan bool) string {
if allowLan { if allowLan {
if host == "*" { if host == "*" {
return fmt.Sprintf(":%d", port) 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) return fmt.Sprintf("127.0.0.1:%d", port)

View file

@ -45,15 +45,15 @@ func (ps *Process) Match(metadata *C.Metadata) bool {
return strings.EqualFold(cached.(string), ps.process) return strings.EqualFold(cached.(string), ps.process)
} }
func (p *Process) Adapter() string { func (ps *Process) Adapter() string {
return p.adapter return ps.adapter
} }
func (p *Process) Payload() string { func (ps *Process) Payload() string {
return p.process return ps.process
} }
func (p *Process) ShouldResolveIP() bool { func (ps *Process) ShouldResolveIP() bool {
return false return false
} }