Skip to content

Commit

Permalink
Sync from ray
Browse files Browse the repository at this point in the history
  • Loading branch information
kslr committed Jul 12, 2019
2 parents 0a7d998 + aa3c337 commit 2c4c5c5
Show file tree
Hide file tree
Showing 31 changed files with 90 additions and 58 deletions.
4 changes: 2 additions & 2 deletions app/dns/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}

Expand Down
2 changes: 1 addition & 1 deletion app/proxyman/inbound/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion app/proxyman/outbound/outbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions app/reverse/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
4 changes: 2 additions & 2 deletions app/reverse/portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
2 changes: 1 addition & 1 deletion app/router/balancing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions common/buf/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ func NewReader(reader io.Reader) Reader {
if err != nil {
newError("failed to get sysconn").Base(err).WriteToLog()
} else {
/* Check if ReadVReader Can be used on this reader first
Fix https://github.com/v2ray/v2ray-core/issues/1666
/*
Check if ReadVReader Can be used on this reader first
Fix https://github.com/v2ray/v2ray-core/issues/1666
*/
if ok, _ := checkReadVConstraint(rawConn); ok {
return NewReadVReader(reader, rawConn)
Expand Down
6 changes: 1 addition & 5 deletions common/buf/readv_constraint_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,5 @@ func checkReadVConstraint(conn syscall.RawConn) (bool, error) {
reason = err
})

if err != nil {
return false, err
}

return isSocketReady, reason
return isSocketReady, err
}
4 changes: 2 additions & 2 deletions common/protocol/http/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ",")
Expand All @@ -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, ",") {
Expand Down
2 changes: 1 addition & 1 deletion common/protocol/http/sniff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions common/protocol/tls/sniff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion common/strmatcher/domain_matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion infra/conf/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}

Expand Down
8 changes: 7 additions & 1 deletion infra/conf/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,11 @@ func (list *PortList) Build() *net.PortList {
// UnmarshalJSON implements encoding/json.Unmarshaler.UnmarshalJSON
func (list *PortList) UnmarshalJSON(data []byte) error {
var listStr string
var number uint32
if err := json.Unmarshal(data, &listStr); err != nil {
return newError("invalid port list: ", string(data)).Base(err)
if err2 := json.Unmarshal(data, &number); err2 != nil {
return newError("invalid port: ", string(data)).Base(err2)
}
}
rangelist := strings.Split(listStr, ",")
for _, rangeStr := range rangelist {
Expand All @@ -217,6 +220,9 @@ func (list *PortList) UnmarshalJSON(data []byte) error {
}
}
}
if number != 0 {
list.Range = append(list.Range, PortRange{From: uint32(number), To: uint32(number)})
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion infra/conf/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions infra/conf/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func TestRouterConfig(t *testing.T) {
"type": "field",
"port": "53, 443, 1000-2000",
"outboundTag": "test"
},{
"type": "field",
"port": 123,
"outboundTag": "test"
}
]
},
Expand Down Expand Up @@ -114,6 +118,16 @@ func TestRouterConfig(t *testing.T) {
Tag: "test",
},
},
{
PortList: &net.PortList{
Range: []*net.PortRange{
{From: 123, To: 123},
},
},
TargetTag: &router.RoutingRule_Tag{
Tag: "test",
},
},
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions infra/conf/shadowsocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
6 changes: 3 additions & 3 deletions infra/conf/transport_internet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion infra/control/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion infra/control/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions infra/control/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
10 changes: 5 additions & 5 deletions proxy/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down Expand Up @@ -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",
Expand All @@ -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", "")
}

Expand Down
2 changes: 1 addition & 1 deletion proxy/vmess/inbound/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion proxy/vmess/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion transport/internet/domainsocket/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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' {
Expand Down
2 changes: 1 addition & 1 deletion transport/internet/headers/http/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion transport/internet/http/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] != '/' {
Expand Down
Loading

0 comments on commit 2c4c5c5

Please sign in to comment.