1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

Merge pull request #1750 from sosiska/patch-1

Some code improvements
This commit is contained in:
Kslr 2019-07-04 21:40:48 +08:00 committed by GitHub
commit 3deb5f26c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 38 additions and 39 deletions

View File

@ -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")
}

View File

@ -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
}

View File

@ -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()

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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

View File

@ -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, ",") {

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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.")
}

View File

@ -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 {

View File

@ -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{

View File

@ -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{

View File

@ -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)

View File

@ -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

View File

@ -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"
}

View File

@ -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", "")
}

View File

@ -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) {

View File

@ -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
}

View File

@ -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' {

View File

@ -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
}
}

View File

@ -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] != '/' {

View File

@ -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()
}
}

View File

@ -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)

View File

@ -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] != '/' {