From 0401a91ef49befdd7d9dafbced973b211c97b616 Mon Sep 17 00:00:00 2001 From: Kirill Motkov Date: Fri, 14 Jun 2019 16:47:28 +0300 Subject: [PATCH] Some code improvements * Rewrite empty string checks more idiomatically. * Change strings.ToLower comparisons to strings.EqualFold. * Rewrite switch statement with only one case as if. --- app/dns/server.go | 4 ++-- app/proxyman/inbound/inbound.go | 2 +- app/proxyman/outbound/outbound.go | 2 +- app/reverse/bridge.go | 4 ++-- app/reverse/portal.go | 4 ++-- app/router/balancing.go | 2 +- common/protocol/http/headers.go | 4 ++-- common/protocol/http/sniff.go | 2 +- common/protocol/tls/sniff.go | 3 +-- common/strmatcher/domain_matcher.go | 2 +- infra/conf/api.go | 2 +- infra/conf/router.go | 2 +- infra/conf/shadowsocks.go | 4 ++-- infra/conf/transport_internet.go | 6 +++--- infra/control/cert.go | 2 +- infra/control/command.go | 2 +- infra/control/verify.go | 4 ++-- proxy/http/server.go | 10 +++++----- proxy/vmess/inbound/inbound.go | 2 +- proxy/vmess/validator.go | 2 +- transport/internet/domainsocket/config.go | 2 +- transport/internet/headers/http/config.go | 2 +- transport/internet/http/config.go | 2 +- transport/internet/tls/config.go | 2 +- transport/internet/tls/tls.go | 2 +- transport/internet/websocket/config.go | 2 +- 26 files changed, 38 insertions(+), 39 deletions(-) diff --git a/app/dns/server.go b/app/dns/server.go index e4fc34eb3..48165c0b3 100644 --- a/app/dns/server.go +++ b/app/dns/server.go @@ -42,7 +42,7 @@ func New(ctx context.Context, config *Config) (*Server, error) { clients: make([]Client, 0, len(config.NameServers)+len(config.NameServer)), tag: config.Tag, } - if len(server.tag) == 0 { + if server.tag == "" { server.tag = generateRandomTag() } if len(config.ClientIp) > 0 { @@ -196,7 +196,7 @@ func toNetIP(ips []net.Address) []net.IP { } func (s *Server) lookupIPInternal(domain string, option IPOption) ([]net.IP, error) { - if len(domain) == 0 { + if domain == "" { return nil, newError("empty domain name") } diff --git a/app/proxyman/inbound/inbound.go b/app/proxyman/inbound/inbound.go index e80015e1a..528356942 100644 --- a/app/proxyman/inbound/inbound.go +++ b/app/proxyman/inbound/inbound.go @@ -68,7 +68,7 @@ func (m *Manager) GetHandler(ctx context.Context, tag string) (inbound.Handler, // RemoveHandler implements inbound.Manager. func (m *Manager) RemoveHandler(ctx context.Context, tag string) error { - if len(tag) == 0 { + if tag == "" { return common.ErrNoClue } diff --git a/app/proxyman/outbound/outbound.go b/app/proxyman/outbound/outbound.go index ce5cc5c7e..0b4899454 100644 --- a/app/proxyman/outbound/outbound.go +++ b/app/proxyman/outbound/outbound.go @@ -123,7 +123,7 @@ func (m *Manager) AddHandler(ctx context.Context, handler outbound.Handler) erro // RemoveHandler implements outbound.Manager. func (m *Manager) RemoveHandler(ctx context.Context, tag string) error { - if len(tag) == 0 { + if tag == "" { return common.ErrNoClue } m.access.Lock() diff --git a/app/reverse/bridge.go b/app/reverse/bridge.go index 3be8f7c45..502745485 100644 --- a/app/reverse/bridge.go +++ b/app/reverse/bridge.go @@ -27,10 +27,10 @@ type Bridge struct { // NewBridge creates a new Bridge instance. func NewBridge(config *BridgeConfig, dispatcher routing.Dispatcher) (*Bridge, error) { - if len(config.Tag) == 0 { + if config.Tag == "" { return nil, newError("bridge tag is empty") } - if len(config.Domain) == 0 { + if config.Domain == "" { return nil, newError("bridge domain is empty") } diff --git a/app/reverse/portal.go b/app/reverse/portal.go index 8d624cd97..ed846fe0c 100644 --- a/app/reverse/portal.go +++ b/app/reverse/portal.go @@ -28,11 +28,11 @@ type Portal struct { } func NewPortal(config *PortalConfig, ohm outbound.Manager) (*Portal, error) { - if len(config.Tag) == 0 { + if config.Tag == "" { return nil, newError("portal tag is empty") } - if len(config.Domain) == 0 { + if config.Domain == "" { return nil, newError("portal domain is empty") } diff --git a/app/router/balancing.go b/app/router/balancing.go index c6debe6ac..0cede3922 100644 --- a/app/router/balancing.go +++ b/app/router/balancing.go @@ -39,7 +39,7 @@ func (b *Balancer) PickOutbound() (string, error) { return "", newError("no available outbounds selected") } tag := b.strategy.PickOutbound(tags) - if len(tag) == 0 { + if tag == "" { return "", newError("balancing strategy returns empty tag") } return tag, nil diff --git a/common/protocol/http/headers.go b/common/protocol/http/headers.go index db497ac44..95eb53066 100644 --- a/common/protocol/http/headers.go +++ b/common/protocol/http/headers.go @@ -11,7 +11,7 @@ import ( // ParseXForwardedFor parses X-Forwarded-For header in http headers, and return the IP list in it. func ParseXForwardedFor(header http.Header) []net.Address { xff := header.Get("X-Forwarded-For") - if len(xff) == 0 { + if xff == "" { return nil } list := strings.Split(xff, ",") @@ -38,7 +38,7 @@ func RemoveHopByHopHeaders(header http.Header) { connections := header.Get("Connection") header.Del("Connection") - if len(connections) == 0 { + if connections == "" { return } for _, h := range strings.Split(connections, ",") { diff --git a/common/protocol/http/sniff.go b/common/protocol/http/sniff.go index 79ac4ad1c..9974dd277 100644 --- a/common/protocol/http/sniff.go +++ b/common/protocol/http/sniff.go @@ -44,7 +44,7 @@ var ( func beginWithHTTPMethod(b []byte) error { for _, m := range &methods { - if len(b) >= len(m) && strings.ToLower(string(b[:len(m)])) == m { + if len(b) >= len(m) && strings.EqualFold(string(b[:len(m)]), m) { return nil } diff --git a/common/protocol/tls/sniff.go b/common/protocol/tls/sniff.go index 85ca4820f..a4df5c796 100644 --- a/common/protocol/tls/sniff.go +++ b/common/protocol/tls/sniff.go @@ -81,8 +81,7 @@ func ReadClientHello(data []byte, h *SniffHeader) error { return errNotClientHello } - switch extension { - case 0x00: /* extensionServerName */ + if extension == 0x00 { /* extensionServerName */ d := data[:length] if len(d) < 2 { return errNotClientHello diff --git a/common/strmatcher/domain_matcher.go b/common/strmatcher/domain_matcher.go index 750c7af90..aabbf43eb 100644 --- a/common/strmatcher/domain_matcher.go +++ b/common/strmatcher/domain_matcher.go @@ -51,7 +51,7 @@ func (g *DomainMatcherGroup) addMatcher(m domainMatcher, value uint32) { } func (g *DomainMatcherGroup) Match(domain string) uint32 { - if len(domain) == 0 { + if domain == "" { return 0 } diff --git a/infra/conf/api.go b/infra/conf/api.go index 7856138d1..4ddfe37b6 100644 --- a/infra/conf/api.go +++ b/infra/conf/api.go @@ -16,7 +16,7 @@ type ApiConfig struct { } func (c *ApiConfig) Build() (*commander.Config, error) { - if len(c.Tag) == 0 { + if c.Tag == "" { return nil, newError("Api tag can't be empty.") } diff --git a/infra/conf/router.go b/infra/conf/router.go index 9c697349c..b3870e45d 100644 --- a/infra/conf/router.go +++ b/infra/conf/router.go @@ -23,7 +23,7 @@ type BalancingRule struct { } func (r *BalancingRule) Build() (*router.BalancingRule, error) { - if len(r.Tag) == 0 { + if r.Tag == "" { return nil, newError("empty balancer tag") } if len(r.Selectors) == 0 { diff --git a/infra/conf/shadowsocks.go b/infra/conf/shadowsocks.go index 7e4e6194b..8f61ccf84 100644 --- a/infra/conf/shadowsocks.go +++ b/infra/conf/shadowsocks.go @@ -46,7 +46,7 @@ func (v *ShadowsocksServerConfig) Build() (proto.Message, error) { config.UdpEnabled = v.UDP config.Network = v.NetworkList.Build() - if len(v.Password) == 0 { + if v.Password == "" { return nil, newError("Shadowsocks password is not specified.") } account := &shadowsocks.Account{ @@ -103,7 +103,7 @@ func (v *ShadowsocksClientConfig) Build() (proto.Message, error) { if server.Port == 0 { return nil, newError("Invalid Shadowsocks port.") } - if len(server.Password) == 0 { + if server.Password == "" { return nil, newError("Shadowsocks password is not specified.") } account := &shadowsocks.Account{ diff --git a/infra/conf/transport_internet.go b/infra/conf/transport_internet.go index f5417f034..9000e24cb 100644 --- a/infra/conf/transport_internet.go +++ b/infra/conf/transport_internet.go @@ -134,7 +134,7 @@ type WebSocketConfig struct { // Build implements Buildable. func (c *WebSocketConfig) Build() (proto.Message, error) { path := c.Path - if len(path) == 0 && len(c.Path2) > 0 { + if path == "" && c.Path2 != "" { path = c.Path2 } header := make([]*websocket.Header, 0, 32) @@ -380,7 +380,7 @@ func (c *StreamConfig) Build() (*internet.StreamConfig, error) { } config.ProtocolName = protocol } - if strings.ToLower(c.Security) == "tls" { + if strings.EqualFold(c.Security, "tls") { tlsSettings := c.TLSSettings if tlsSettings == nil { tlsSettings = &TLSConfig{} @@ -469,7 +469,7 @@ type ProxyConfig struct { // Build implements Buildable. func (v *ProxyConfig) Build() (*internet.ProxyConfig, error) { - if len(v.Tag) == 0 { + if v.Tag == "" { return nil, newError("Proxy tag is not set.") } return &internet.ProxyConfig{ diff --git a/infra/control/cert.go b/infra/control/cert.go index bcda6974b..1112c73ea 100644 --- a/infra/control/cert.go +++ b/infra/control/cert.go @@ -21,7 +21,7 @@ func (l *stringList) String() string { } func (l *stringList) Set(v string) error { - if len(v) == 0 { + if v == "" { return newError("empty value") } *l = append(*l, v) diff --git a/infra/control/command.go b/infra/control/command.go index 3bd3c8040..a261646ac 100644 --- a/infra/control/command.go +++ b/infra/control/command.go @@ -22,7 +22,7 @@ var ( func RegisterCommand(cmd Command) error { entry := strings.ToLower(cmd.Name()) - if len(entry) == 0 { + if entry == "" { return newError("empty command name") } commandRegistry[entry] = cmd diff --git a/infra/control/verify.go b/infra/control/verify.go index 5cb09e4b3..3aeab578f 100644 --- a/infra/control/verify.go +++ b/infra/control/verify.go @@ -128,11 +128,11 @@ func (c *VerifyCommand) Execute(args []string) error { } target := fs.Arg(0) - if len(target) == 0 { + if target == "" { return newError("empty file path.") } - if len(*sigFile) == 0 { + if *sigFile == "" { *sigFile = target + ".sig" } diff --git a/proxy/http/server.go b/proxy/http/server.go index 3ab9d3114..7d7bcb54c 100755 --- a/proxy/http/server.go +++ b/proxy/http/server.go @@ -120,11 +120,11 @@ Start: } defaultPort := net.Port(80) - if strings.ToLower(request.URL.Scheme) == "https" { + if strings.EqualFold(request.URL.Scheme, "https") { defaultPort = net.Port(443) } host := request.Host - if len(host) == 0 { + if host == "" { host = request.URL.Host } dest, err := http_proto.ParseHost(host, defaultPort) @@ -137,7 +137,7 @@ Start: Status: log.AccessAccepted, }) - if strings.ToUpper(request.Method) == "CONNECT" { + if strings.EqualFold(request.Method, "CONNECT") { return s.handleConnect(ctx, request, reader, conn, dest, dispatcher) } @@ -211,7 +211,7 @@ func (s *Server) handleConnect(ctx context.Context, request *http.Request, reade var errWaitAnother = newError("keep alive") func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, writer io.Writer, dest net.Destination, dispatcher routing.Dispatcher) error { - if !s.config.AllowTransparent && len(request.URL.Host) <= 0 { + if !s.config.AllowTransparent && request.URL.Host == "" { // RFC 2068 (HTTP/1.1) requires URL to be absolute URL in HTTP proxy. response := &http.Response{ Status: "Bad Request", @@ -235,7 +235,7 @@ func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, wri http_proto.RemoveHopByHopHeaders(request.Header) // Prevent UA from being set to golang's default ones - if len(request.Header.Get("User-Agent")) == 0 { + if request.Header.Get("User-Agent") == "" { request.Header.Set("User-Agent", "") } diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 924157186..540d1f663 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -167,7 +167,7 @@ func (h *Handler) AddUser(ctx context.Context, user *protocol.MemoryUser) error } func (h *Handler) RemoveUser(ctx context.Context, email string) error { - if len(email) == 0 { + if email == "" { return newError("Email must not be empty.") } if !h.usersByEmail.Remove(email) { diff --git a/proxy/vmess/validator.go b/proxy/vmess/validator.go index 8a2cffe26..1690fac72 100644 --- a/proxy/vmess/validator.go +++ b/proxy/vmess/validator.go @@ -149,7 +149,7 @@ func (v *TimedUserValidator) Remove(email string) bool { email = strings.ToLower(email) idx := -1 for i, u := range v.users { - if strings.ToLower(u.user.Email) == email { + if strings.EqualFold(u.user.Email, email) { idx = i break } diff --git a/transport/internet/domainsocket/config.go b/transport/internet/domainsocket/config.go index ca80c351e..09cfabb26 100644 --- a/transport/internet/domainsocket/config.go +++ b/transport/internet/domainsocket/config.go @@ -12,7 +12,7 @@ const protocolName = "domainsocket" func (c *Config) GetUnixAddr() (*net.UnixAddr, error) { path := c.Path - if len(path) == 0 { + if path == "" { return nil, newError("empty domain socket path") } if c.Abstract && path[0] != '\x00' { diff --git a/transport/internet/headers/http/config.go b/transport/internet/headers/http/config.go index 99f015bf1..32a068eb8 100644 --- a/transport/internet/headers/http/config.go +++ b/transport/internet/headers/http/config.go @@ -57,7 +57,7 @@ func (v *RequestConfig) GetFullVersion() string { func (v *ResponseConfig) HasHeader(header string) bool { cHeader := strings.ToLower(header) for _, tHeader := range v.Header { - if strings.ToLower(tHeader.Name) == cHeader { + if strings.EqualFold(tHeader.Name, cHeader) { return true } } diff --git a/transport/internet/http/config.go b/transport/internet/http/config.go index ae50de6a8..b433e331c 100644 --- a/transport/internet/http/config.go +++ b/transport/internet/http/config.go @@ -33,7 +33,7 @@ func (c *Config) getRandomHost() string { } func (c *Config) getNormalizedPath() string { - if len(c.Path) == 0 { + if c.Path == "" { return "/" } if c.Path[0] != '/' { diff --git a/transport/internet/tls/config.go b/transport/internet/tls/config.go index 4f8bed7e3..1d41ed4aa 100644 --- a/transport/internet/tls/config.go +++ b/transport/internet/tls/config.go @@ -236,7 +236,7 @@ type Option func(*tls.Config) // WithDestination sets the server name in TLS config. func WithDestination(dest net.Destination) Option { return func(config *tls.Config) { - if dest.Address.Family().IsDomain() && len(config.ServerName) == 0 { + if dest.Address.Family().IsDomain() && config.ServerName == "" { config.ServerName = dest.Address.Domain() } } diff --git a/transport/internet/tls/tls.go b/transport/internet/tls/tls.go index dd9a0bb80..b1523b7ca 100644 --- a/transport/internet/tls/tls.go +++ b/transport/internet/tls/tls.go @@ -33,7 +33,7 @@ func (c *conn) HandshakeAddress() net.Address { return nil } state := c.Conn.ConnectionState() - if len(state.ServerName) == 0 { + if state.ServerName == "" { return nil } return net.ParseAddress(state.ServerName) diff --git a/transport/internet/websocket/config.go b/transport/internet/websocket/config.go index 34640e7e0..982cf7309 100644 --- a/transport/internet/websocket/config.go +++ b/transport/internet/websocket/config.go @@ -13,7 +13,7 @@ const protocolName = "websocket" func (c *Config) GetNormalizedPath() string { path := c.Path - if len(path) == 0 { + if path == "" { return "/" } if path[0] != '/' {