mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 09:36:34 -05:00
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.
This commit is contained in:
parent
4c93d36d49
commit
0401a91ef4
@ -42,7 +42,7 @@ func New(ctx context.Context, config *Config) (*Server, error) {
|
|||||||
clients: make([]Client, 0, len(config.NameServers)+len(config.NameServer)),
|
clients: make([]Client, 0, len(config.NameServers)+len(config.NameServer)),
|
||||||
tag: config.Tag,
|
tag: config.Tag,
|
||||||
}
|
}
|
||||||
if len(server.tag) == 0 {
|
if server.tag == "" {
|
||||||
server.tag = generateRandomTag()
|
server.tag = generateRandomTag()
|
||||||
}
|
}
|
||||||
if len(config.ClientIp) > 0 {
|
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) {
|
func (s *Server) lookupIPInternal(domain string, option IPOption) ([]net.IP, error) {
|
||||||
if len(domain) == 0 {
|
if domain == "" {
|
||||||
return nil, newError("empty domain name")
|
return nil, newError("empty domain name")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ func (m *Manager) GetHandler(ctx context.Context, tag string) (inbound.Handler,
|
|||||||
|
|
||||||
// RemoveHandler implements inbound.Manager.
|
// RemoveHandler implements inbound.Manager.
|
||||||
func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
|
func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
|
||||||
if len(tag) == 0 {
|
if tag == "" {
|
||||||
return common.ErrNoClue
|
return common.ErrNoClue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,7 +123,7 @@ func (m *Manager) AddHandler(ctx context.Context, handler outbound.Handler) erro
|
|||||||
|
|
||||||
// RemoveHandler implements outbound.Manager.
|
// RemoveHandler implements outbound.Manager.
|
||||||
func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
|
func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
|
||||||
if len(tag) == 0 {
|
if tag == "" {
|
||||||
return common.ErrNoClue
|
return common.ErrNoClue
|
||||||
}
|
}
|
||||||
m.access.Lock()
|
m.access.Lock()
|
||||||
|
@ -27,10 +27,10 @@ type Bridge struct {
|
|||||||
|
|
||||||
// NewBridge creates a new Bridge instance.
|
// NewBridge creates a new Bridge instance.
|
||||||
func NewBridge(config *BridgeConfig, dispatcher routing.Dispatcher) (*Bridge, error) {
|
func NewBridge(config *BridgeConfig, dispatcher routing.Dispatcher) (*Bridge, error) {
|
||||||
if len(config.Tag) == 0 {
|
if config.Tag == "" {
|
||||||
return nil, newError("bridge tag is empty")
|
return nil, newError("bridge tag is empty")
|
||||||
}
|
}
|
||||||
if len(config.Domain) == 0 {
|
if config.Domain == "" {
|
||||||
return nil, newError("bridge domain is empty")
|
return nil, newError("bridge domain is empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,11 +28,11 @@ type Portal struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewPortal(config *PortalConfig, ohm outbound.Manager) (*Portal, error) {
|
func NewPortal(config *PortalConfig, ohm outbound.Manager) (*Portal, error) {
|
||||||
if len(config.Tag) == 0 {
|
if config.Tag == "" {
|
||||||
return nil, newError("portal tag is empty")
|
return nil, newError("portal tag is empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(config.Domain) == 0 {
|
if config.Domain == "" {
|
||||||
return nil, newError("portal domain is empty")
|
return nil, newError("portal domain is empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ func (b *Balancer) PickOutbound() (string, error) {
|
|||||||
return "", newError("no available outbounds selected")
|
return "", newError("no available outbounds selected")
|
||||||
}
|
}
|
||||||
tag := b.strategy.PickOutbound(tags)
|
tag := b.strategy.PickOutbound(tags)
|
||||||
if len(tag) == 0 {
|
if tag == "" {
|
||||||
return "", newError("balancing strategy returns empty tag")
|
return "", newError("balancing strategy returns empty tag")
|
||||||
}
|
}
|
||||||
return tag, nil
|
return tag, nil
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
// ParseXForwardedFor parses X-Forwarded-For header in http headers, and return the IP list in it.
|
// ParseXForwardedFor parses X-Forwarded-For header in http headers, and return the IP list in it.
|
||||||
func ParseXForwardedFor(header http.Header) []net.Address {
|
func ParseXForwardedFor(header http.Header) []net.Address {
|
||||||
xff := header.Get("X-Forwarded-For")
|
xff := header.Get("X-Forwarded-For")
|
||||||
if len(xff) == 0 {
|
if xff == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
list := strings.Split(xff, ",")
|
list := strings.Split(xff, ",")
|
||||||
@ -38,7 +38,7 @@ func RemoveHopByHopHeaders(header http.Header) {
|
|||||||
|
|
||||||
connections := header.Get("Connection")
|
connections := header.Get("Connection")
|
||||||
header.Del("Connection")
|
header.Del("Connection")
|
||||||
if len(connections) == 0 {
|
if connections == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, h := range strings.Split(connections, ",") {
|
for _, h := range strings.Split(connections, ",") {
|
||||||
|
@ -44,7 +44,7 @@ var (
|
|||||||
|
|
||||||
func beginWithHTTPMethod(b []byte) error {
|
func beginWithHTTPMethod(b []byte) error {
|
||||||
for _, m := range &methods {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,8 +81,7 @@ func ReadClientHello(data []byte, h *SniffHeader) error {
|
|||||||
return errNotClientHello
|
return errNotClientHello
|
||||||
}
|
}
|
||||||
|
|
||||||
switch extension {
|
if extension == 0x00 { /* extensionServerName */
|
||||||
case 0x00: /* extensionServerName */
|
|
||||||
d := data[:length]
|
d := data[:length]
|
||||||
if len(d) < 2 {
|
if len(d) < 2 {
|
||||||
return errNotClientHello
|
return errNotClientHello
|
||||||
|
@ -51,7 +51,7 @@ func (g *DomainMatcherGroup) addMatcher(m domainMatcher, value uint32) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *DomainMatcherGroup) Match(domain string) uint32 {
|
func (g *DomainMatcherGroup) Match(domain string) uint32 {
|
||||||
if len(domain) == 0 {
|
if domain == "" {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ type ApiConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *ApiConfig) Build() (*commander.Config, error) {
|
func (c *ApiConfig) Build() (*commander.Config, error) {
|
||||||
if len(c.Tag) == 0 {
|
if c.Tag == "" {
|
||||||
return nil, newError("Api tag can't be empty.")
|
return nil, newError("Api tag can't be empty.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ type BalancingRule struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *BalancingRule) Build() (*router.BalancingRule, error) {
|
func (r *BalancingRule) Build() (*router.BalancingRule, error) {
|
||||||
if len(r.Tag) == 0 {
|
if r.Tag == "" {
|
||||||
return nil, newError("empty balancer tag")
|
return nil, newError("empty balancer tag")
|
||||||
}
|
}
|
||||||
if len(r.Selectors) == 0 {
|
if len(r.Selectors) == 0 {
|
||||||
|
@ -46,7 +46,7 @@ func (v *ShadowsocksServerConfig) Build() (proto.Message, error) {
|
|||||||
config.UdpEnabled = v.UDP
|
config.UdpEnabled = v.UDP
|
||||||
config.Network = v.NetworkList.Build()
|
config.Network = v.NetworkList.Build()
|
||||||
|
|
||||||
if len(v.Password) == 0 {
|
if v.Password == "" {
|
||||||
return nil, newError("Shadowsocks password is not specified.")
|
return nil, newError("Shadowsocks password is not specified.")
|
||||||
}
|
}
|
||||||
account := &shadowsocks.Account{
|
account := &shadowsocks.Account{
|
||||||
@ -103,7 +103,7 @@ func (v *ShadowsocksClientConfig) Build() (proto.Message, error) {
|
|||||||
if server.Port == 0 {
|
if server.Port == 0 {
|
||||||
return nil, newError("Invalid Shadowsocks port.")
|
return nil, newError("Invalid Shadowsocks port.")
|
||||||
}
|
}
|
||||||
if len(server.Password) == 0 {
|
if server.Password == "" {
|
||||||
return nil, newError("Shadowsocks password is not specified.")
|
return nil, newError("Shadowsocks password is not specified.")
|
||||||
}
|
}
|
||||||
account := &shadowsocks.Account{
|
account := &shadowsocks.Account{
|
||||||
|
@ -134,7 +134,7 @@ type WebSocketConfig struct {
|
|||||||
// Build implements Buildable.
|
// Build implements Buildable.
|
||||||
func (c *WebSocketConfig) Build() (proto.Message, error) {
|
func (c *WebSocketConfig) Build() (proto.Message, error) {
|
||||||
path := c.Path
|
path := c.Path
|
||||||
if len(path) == 0 && len(c.Path2) > 0 {
|
if path == "" && c.Path2 != "" {
|
||||||
path = c.Path2
|
path = c.Path2
|
||||||
}
|
}
|
||||||
header := make([]*websocket.Header, 0, 32)
|
header := make([]*websocket.Header, 0, 32)
|
||||||
@ -380,7 +380,7 @@ func (c *StreamConfig) Build() (*internet.StreamConfig, error) {
|
|||||||
}
|
}
|
||||||
config.ProtocolName = protocol
|
config.ProtocolName = protocol
|
||||||
}
|
}
|
||||||
if strings.ToLower(c.Security) == "tls" {
|
if strings.EqualFold(c.Security, "tls") {
|
||||||
tlsSettings := c.TLSSettings
|
tlsSettings := c.TLSSettings
|
||||||
if tlsSettings == nil {
|
if tlsSettings == nil {
|
||||||
tlsSettings = &TLSConfig{}
|
tlsSettings = &TLSConfig{}
|
||||||
@ -469,7 +469,7 @@ type ProxyConfig struct {
|
|||||||
|
|
||||||
// Build implements Buildable.
|
// Build implements Buildable.
|
||||||
func (v *ProxyConfig) Build() (*internet.ProxyConfig, error) {
|
func (v *ProxyConfig) Build() (*internet.ProxyConfig, error) {
|
||||||
if len(v.Tag) == 0 {
|
if v.Tag == "" {
|
||||||
return nil, newError("Proxy tag is not set.")
|
return nil, newError("Proxy tag is not set.")
|
||||||
}
|
}
|
||||||
return &internet.ProxyConfig{
|
return &internet.ProxyConfig{
|
||||||
|
@ -21,7 +21,7 @@ func (l *stringList) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *stringList) Set(v string) error {
|
func (l *stringList) Set(v string) error {
|
||||||
if len(v) == 0 {
|
if v == "" {
|
||||||
return newError("empty value")
|
return newError("empty value")
|
||||||
}
|
}
|
||||||
*l = append(*l, v)
|
*l = append(*l, v)
|
||||||
|
@ -22,7 +22,7 @@ var (
|
|||||||
|
|
||||||
func RegisterCommand(cmd Command) error {
|
func RegisterCommand(cmd Command) error {
|
||||||
entry := strings.ToLower(cmd.Name())
|
entry := strings.ToLower(cmd.Name())
|
||||||
if len(entry) == 0 {
|
if entry == "" {
|
||||||
return newError("empty command name")
|
return newError("empty command name")
|
||||||
}
|
}
|
||||||
commandRegistry[entry] = cmd
|
commandRegistry[entry] = cmd
|
||||||
|
@ -128,11 +128,11 @@ func (c *VerifyCommand) Execute(args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
target := fs.Arg(0)
|
target := fs.Arg(0)
|
||||||
if len(target) == 0 {
|
if target == "" {
|
||||||
return newError("empty file path.")
|
return newError("empty file path.")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(*sigFile) == 0 {
|
if *sigFile == "" {
|
||||||
*sigFile = target + ".sig"
|
*sigFile = target + ".sig"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,11 +120,11 @@ Start:
|
|||||||
}
|
}
|
||||||
|
|
||||||
defaultPort := net.Port(80)
|
defaultPort := net.Port(80)
|
||||||
if strings.ToLower(request.URL.Scheme) == "https" {
|
if strings.EqualFold(request.URL.Scheme, "https") {
|
||||||
defaultPort = net.Port(443)
|
defaultPort = net.Port(443)
|
||||||
}
|
}
|
||||||
host := request.Host
|
host := request.Host
|
||||||
if len(host) == 0 {
|
if host == "" {
|
||||||
host = request.URL.Host
|
host = request.URL.Host
|
||||||
}
|
}
|
||||||
dest, err := http_proto.ParseHost(host, defaultPort)
|
dest, err := http_proto.ParseHost(host, defaultPort)
|
||||||
@ -137,7 +137,7 @@ Start:
|
|||||||
Status: log.AccessAccepted,
|
Status: log.AccessAccepted,
|
||||||
})
|
})
|
||||||
|
|
||||||
if strings.ToUpper(request.Method) == "CONNECT" {
|
if strings.EqualFold(request.Method, "CONNECT") {
|
||||||
return s.handleConnect(ctx, request, reader, conn, dest, dispatcher)
|
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")
|
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 {
|
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.
|
// RFC 2068 (HTTP/1.1) requires URL to be absolute URL in HTTP proxy.
|
||||||
response := &http.Response{
|
response := &http.Response{
|
||||||
Status: "Bad Request",
|
Status: "Bad Request",
|
||||||
@ -235,7 +235,7 @@ func (s *Server) handlePlainHTTP(ctx context.Context, request *http.Request, wri
|
|||||||
http_proto.RemoveHopByHopHeaders(request.Header)
|
http_proto.RemoveHopByHopHeaders(request.Header)
|
||||||
|
|
||||||
// Prevent UA from being set to golang's default ones
|
// 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", "")
|
request.Header.Set("User-Agent", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 {
|
func (h *Handler) RemoveUser(ctx context.Context, email string) error {
|
||||||
if len(email) == 0 {
|
if email == "" {
|
||||||
return newError("Email must not be empty.")
|
return newError("Email must not be empty.")
|
||||||
}
|
}
|
||||||
if !h.usersByEmail.Remove(email) {
|
if !h.usersByEmail.Remove(email) {
|
||||||
|
@ -149,7 +149,7 @@ func (v *TimedUserValidator) Remove(email string) bool {
|
|||||||
email = strings.ToLower(email)
|
email = strings.ToLower(email)
|
||||||
idx := -1
|
idx := -1
|
||||||
for i, u := range v.users {
|
for i, u := range v.users {
|
||||||
if strings.ToLower(u.user.Email) == email {
|
if strings.EqualFold(u.user.Email, email) {
|
||||||
idx = i
|
idx = i
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ const protocolName = "domainsocket"
|
|||||||
|
|
||||||
func (c *Config) GetUnixAddr() (*net.UnixAddr, error) {
|
func (c *Config) GetUnixAddr() (*net.UnixAddr, error) {
|
||||||
path := c.Path
|
path := c.Path
|
||||||
if len(path) == 0 {
|
if path == "" {
|
||||||
return nil, newError("empty domain socket path")
|
return nil, newError("empty domain socket path")
|
||||||
}
|
}
|
||||||
if c.Abstract && path[0] != '\x00' {
|
if c.Abstract && path[0] != '\x00' {
|
||||||
|
@ -57,7 +57,7 @@ func (v *RequestConfig) GetFullVersion() string {
|
|||||||
func (v *ResponseConfig) HasHeader(header string) bool {
|
func (v *ResponseConfig) HasHeader(header string) bool {
|
||||||
cHeader := strings.ToLower(header)
|
cHeader := strings.ToLower(header)
|
||||||
for _, tHeader := range v.Header {
|
for _, tHeader := range v.Header {
|
||||||
if strings.ToLower(tHeader.Name) == cHeader {
|
if strings.EqualFold(tHeader.Name, cHeader) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ func (c *Config) getRandomHost() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) getNormalizedPath() string {
|
func (c *Config) getNormalizedPath() string {
|
||||||
if len(c.Path) == 0 {
|
if c.Path == "" {
|
||||||
return "/"
|
return "/"
|
||||||
}
|
}
|
||||||
if c.Path[0] != '/' {
|
if c.Path[0] != '/' {
|
||||||
|
@ -236,7 +236,7 @@ type Option func(*tls.Config)
|
|||||||
// WithDestination sets the server name in TLS config.
|
// WithDestination sets the server name in TLS config.
|
||||||
func WithDestination(dest net.Destination) Option {
|
func WithDestination(dest net.Destination) Option {
|
||||||
return func(config *tls.Config) {
|
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()
|
config.ServerName = dest.Address.Domain()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ func (c *conn) HandshakeAddress() net.Address {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
state := c.Conn.ConnectionState()
|
state := c.Conn.ConnectionState()
|
||||||
if len(state.ServerName) == 0 {
|
if state.ServerName == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return net.ParseAddress(state.ServerName)
|
return net.ParseAddress(state.ServerName)
|
||||||
|
@ -13,7 +13,7 @@ const protocolName = "websocket"
|
|||||||
|
|
||||||
func (c *Config) GetNormalizedPath() string {
|
func (c *Config) GetNormalizedPath() string {
|
||||||
path := c.Path
|
path := c.Path
|
||||||
if len(path) == 0 {
|
if path == "" {
|
||||||
return "/"
|
return "/"
|
||||||
}
|
}
|
||||||
if path[0] != '/' {
|
if path[0] != '/' {
|
||||||
|
Loading…
Reference in New Issue
Block a user