From c75d8407063d46709c4afdca3c087ca756bc6f16 Mon Sep 17 00:00:00 2001 From: v2ray Date: Tue, 24 May 2016 22:41:51 +0200 Subject: [PATCH] Remove serial.String --- app/router/rules/config_json.go | 20 ++++---- common/collect/string_list.go | 12 +++++ .../{serial => collect}/string_list_json.go | 8 ++-- .../string_list_json_test.go | 6 +-- common/log/access.go | 4 +- common/log/internal/log_entry.go | 47 ++++++++++++------- common/log/internal/log_entry_test.go | 7 ++- common/net/network.go | 10 ++-- common/net/network_json.go | 4 +- common/serial/string.go | 36 -------------- common/serial/string_json.go | 16 ------- common/serial/string_json_test.go | 28 ----------- common/serial/string_list.go | 15 ------ proxy/freedom/config_json.go | 6 +-- proxy/shadowsocks/config_json.go | 18 +++---- proxy/shadowsocks/server.go | 11 ++--- proxy/vmess/inbound/inbound.go | 5 +- 17 files changed, 89 insertions(+), 164 deletions(-) create mode 100644 common/collect/string_list.go rename common/{serial => collect}/string_list_json.go (71%) rename common/{serial => collect}/string_list_json_test.go (74%) delete mode 100644 common/serial/string.go delete mode 100644 common/serial/string_json.go delete mode 100644 common/serial/string_json_test.go delete mode 100644 common/serial/string_list.go diff --git a/app/router/rules/config_json.go b/app/router/rules/config_json.go index f7a3669e4..e15afc3dc 100644 --- a/app/router/rules/config_json.go +++ b/app/router/rules/config_json.go @@ -8,9 +8,9 @@ import ( "strings" router "github.com/v2ray/v2ray-core/app/router" + "github.com/v2ray/v2ray-core/common/collect" "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/common/serial" ) type JsonRule struct { @@ -21,8 +21,8 @@ type JsonRule struct { func parseFieldRule(msg json.RawMessage) (*Rule, error) { type RawFieldRule struct { JsonRule - Domain *serial.StringTList `json:"domain"` - IP *serial.StringTList `json:"ip"` + Domain *collect.StringList `json:"domain"` + IP *collect.StringList `json:"ip"` Port *v2net.PortRange `json:"port"` Network *v2net.NetworkList `json:"network"` } @@ -37,14 +37,14 @@ func parseFieldRule(msg json.RawMessage) (*Rule, error) { anyCond := NewAnyCondition() for _, rawDomain := range *(rawFieldRule.Domain) { var matcher Condition - if strings.HasPrefix(rawDomain.String(), "regexp:") { - rawMatcher, err := NewRegexpDomainMatcher(rawDomain.String()[7:]) + if strings.HasPrefix(rawDomain, "regexp:") { + rawMatcher, err := NewRegexpDomainMatcher(rawDomain[7:]) if err != nil { return nil, err } matcher = rawMatcher } else { - matcher = NewPlainDomainMatcher(rawDomain.String()) + matcher = NewPlainDomainMatcher(rawDomain) } anyCond.Add(matcher) } @@ -54,7 +54,7 @@ func parseFieldRule(msg json.RawMessage) (*Rule, error) { if rawFieldRule.IP != nil && rawFieldRule.IP.Len() > 0 { anyCond := NewAnyCondition() for _, ipStr := range *(rawFieldRule.IP) { - cidrMatcher, err := NewCIDRMatcher(ipStr.String()) + cidrMatcher, err := NewCIDRMatcher(ipStr) if err != nil { log.Error("Router: Invalid IP range in router rule: ", err) return nil, err @@ -128,10 +128,10 @@ func init() { Rules: make([]*Rule, len(jsonConfig.RuleList)), DomainStrategy: DomainAsIs, } - domainStrategy := serial.StringT(jsonConfig.DomainStrategy).ToLower() - if domainStrategy.String() == "alwaysip" { + domainStrategy := strings.ToLower(jsonConfig.DomainStrategy) + if domainStrategy == "alwaysip" { config.DomainStrategy = AlwaysUseIP - } else if domainStrategy.String() == "ipifnonmatch" { + } else if domainStrategy == "ipifnonmatch" { config.DomainStrategy = UseIPIfNonMatch } for idx, rawRule := range jsonConfig.RuleList { diff --git a/common/collect/string_list.go b/common/collect/string_list.go new file mode 100644 index 000000000..498f50eca --- /dev/null +++ b/common/collect/string_list.go @@ -0,0 +1,12 @@ +package collect + +type StringList []string + +func NewStringList(raw []string) *StringList { + list := StringList(raw) + return &list +} + +func (this *StringList) Len() int { + return len(*this) +} diff --git a/common/serial/string_list_json.go b/common/collect/string_list_json.go similarity index 71% rename from common/serial/string_list_json.go rename to common/collect/string_list_json.go index 7bb76efc1..0233bef2d 100644 --- a/common/serial/string_list_json.go +++ b/common/collect/string_list_json.go @@ -1,6 +1,6 @@ // +build json -package serial +package collect import ( "encoding/json" @@ -8,17 +8,17 @@ import ( "strings" ) -func (this *StringTList) UnmarshalJSON(data []byte) error { +func (this *StringList) UnmarshalJSON(data []byte) error { var strarray []string if err := json.Unmarshal(data, &strarray); err == nil { - *this = *NewStringTList(strarray) + *this = *NewStringList(strarray) return nil } var rawstr string if err := json.Unmarshal(data, &rawstr); err == nil { strlist := strings.Split(rawstr, ",") - *this = *NewStringTList(strlist) + *this = *NewStringList(strlist) return nil } return errors.New("Unknown format of a string list: " + string(data)) diff --git a/common/serial/string_list_json_test.go b/common/collect/string_list_json_test.go similarity index 74% rename from common/serial/string_list_json_test.go rename to common/collect/string_list_json_test.go index 39a561044..beb3d0690 100644 --- a/common/serial/string_list_json_test.go +++ b/common/collect/string_list_json_test.go @@ -1,12 +1,12 @@ // +build json -package serial_test +package collect_test import ( "encoding/json" "testing" - . "github.com/v2ray/v2ray-core/common/serial" + . "github.com/v2ray/v2ray-core/common/collect" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -14,7 +14,7 @@ func TestStringListUnmarshalError(t *testing.T) { assert := assert.On(t) rawJson := `1234` - list := new(StringTList) + list := new(StringList) err := json.Unmarshal([]byte(rawJson), list) assert.Error(err).IsNotNil() } diff --git a/common/log/access.go b/common/log/access.go index 3a2121a54..989adcc28 100644 --- a/common/log/access.go +++ b/common/log/access.go @@ -1,8 +1,6 @@ package log import ( - "fmt" - "github.com/v2ray/v2ray-core/common/log/internal" ) @@ -30,7 +28,7 @@ func InitAccessLogger(file string) error { } // Access writes an access log. -func Access(from, to fmt.Stringer, status AccessStatus, reason fmt.Stringer) { +func Access(from, to interface{}, status AccessStatus, reason interface{}) { accessLoggerInstance.Log(&internal.AccessLog{ From: from, To: to, diff --git a/common/log/internal/log_entry.go b/common/log/internal/log_entry.go index 44bbc1fbf..d7000e9bc 100644 --- a/common/log/internal/log_entry.go +++ b/common/log/internal/log_entry.go @@ -8,6 +8,26 @@ import ( "github.com/v2ray/v2ray-core/common/serial" ) +func InterfaceToString(value interface{}) string { + if value == nil { + return " " + } + switch value := value.(type) { + case string: + return value + case *string: + return *value + case fmt.Stringer: + return value.String() + case error: + return value.Error() + case []byte: + return serial.BytesToHexString(value) + default: + return fmt.Sprint(value) + } +} + type LogEntry interface { common.Releasable fmt.Stringer @@ -32,29 +52,16 @@ func (this *ErrorLog) String() string { b.AppendString(this.Prefix) for _, value := range this.Values { - switch typedVal := value.(type) { - case string: - b.AppendString(typedVal) - case *string: - b.AppendString(*typedVal) - case fmt.Stringer: - b.AppendString(typedVal.String()) - case error: - b.AppendString(typedVal.Error()) - case []byte: - b.AppendString(serial.BytesToHexString(typedVal)) - default: - b.AppendString(fmt.Sprint(value)) - } + b.AppendString(InterfaceToString(value)) } return b.String() } type AccessLog struct { - From fmt.Stringer - To fmt.Stringer + From interface{} + To interface{} Status string - Reason fmt.Stringer + Reason interface{} } func (this *AccessLog) Release() { @@ -67,5 +74,9 @@ func (this *AccessLog) String() string { b := alloc.NewSmallBuffer().Clear() defer b.Release() - return b.AppendString(this.From.String()).AppendString(" ").AppendString(this.Status).AppendString(" ").AppendString(this.To.String()).AppendString(" ").AppendString(this.Reason.String()).String() + b.AppendString(InterfaceToString(this.From)).AppendString(" ") + b.AppendString(this.Status).AppendString(" ") + b.AppendString(InterfaceToString(this.To)).AppendString(" ") + b.AppendString(InterfaceToString(this.Reason)) + return b.String() } diff --git a/common/log/internal/log_entry_test.go b/common/log/internal/log_entry_test.go index c28a6db59..eb9c52ae2 100644 --- a/common/log/internal/log_entry_test.go +++ b/common/log/internal/log_entry_test.go @@ -4,7 +4,6 @@ import ( "testing" . "github.com/v2ray/v2ray-core/common/log/internal" - "github.com/v2ray/v2ray-core/common/serial" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -12,10 +11,10 @@ func TestAccessLog(t *testing.T) { assert := assert.On(t) entry := &AccessLog{ - From: serial.StringT("test_from"), - To: serial.StringT("test_to"), + From: "test_from", + To: "test_to", Status: "Accepted", - Reason: serial.StringT("test_reason"), + Reason: "test_reason", } entryStr := entry.String() diff --git a/common/net/network.go b/common/net/network.go index 860466949..8ad935fd4 100644 --- a/common/net/network.go +++ b/common/net/network.go @@ -1,7 +1,9 @@ package net import ( - "github.com/v2ray/v2ray-core/common/serial" + "strings" + + "github.com/v2ray/v2ray-core/common/collect" ) const ( @@ -13,7 +15,7 @@ const ( ) // Network represents a communication network on internet. -type Network serial.StringT +type Network string func (this Network) AsList() *NetworkList { list := NetworkList([]Network{this}) @@ -24,10 +26,10 @@ func (this Network) AsList() *NetworkList { type NetworkList []Network // NewNetworkList construsts a NetWorklist from the given StringListeralList. -func NewNetworkList(networks serial.StringTList) NetworkList { +func NewNetworkList(networks collect.StringList) NetworkList { list := NetworkList(make([]Network, networks.Len())) for idx, network := range networks { - list[idx] = Network(network.TrimSpace().ToLower()) + list[idx] = Network(strings.ToLower(strings.TrimSpace(network))) } return list } diff --git a/common/net/network_json.go b/common/net/network_json.go index 0779f1b73..f7fd73740 100644 --- a/common/net/network_json.go +++ b/common/net/network_json.go @@ -5,11 +5,11 @@ package net import ( "encoding/json" - "github.com/v2ray/v2ray-core/common/serial" + "github.com/v2ray/v2ray-core/common/collect" ) func (this *NetworkList) UnmarshalJSON(data []byte) error { - var strlist serial.StringTList + var strlist collect.StringList if err := json.Unmarshal(data, &strlist); err != nil { return err } diff --git a/common/serial/string.go b/common/serial/string.go deleted file mode 100644 index c6138b588..000000000 --- a/common/serial/string.go +++ /dev/null @@ -1,36 +0,0 @@ -package serial - -import ( - "strings" -) - -// An interface for any objects that has string presentation. -type String interface { - String() string -} - -type StringT string - -func NewStringT(str String) StringT { - return StringT(str.String()) -} - -func (this StringT) Contains(str String) bool { - return strings.Contains(this.String(), str.String()) -} - -func (this StringT) String() string { - return string(this) -} - -func (this StringT) ToLower() StringT { - return StringT(strings.ToLower(string(this))) -} - -func (this StringT) ToUpper() StringT { - return StringT(strings.ToUpper(string(this))) -} - -func (this StringT) TrimSpace() StringT { - return StringT(strings.TrimSpace(string(this))) -} diff --git a/common/serial/string_json.go b/common/serial/string_json.go deleted file mode 100644 index b7372bfed..000000000 --- a/common/serial/string_json.go +++ /dev/null @@ -1,16 +0,0 @@ -// +build json - -package serial - -import ( - "encoding/json" -) - -func (this *StringT) UnmarshalJSON(data []byte) error { - var str string - if err := json.Unmarshal(data, &str); err != nil { - return err - } - *this = StringT(str) - return nil -} diff --git a/common/serial/string_json_test.go b/common/serial/string_json_test.go deleted file mode 100644 index a39115c34..000000000 --- a/common/serial/string_json_test.go +++ /dev/null @@ -1,28 +0,0 @@ -// +build json - -package serial_test - -import ( - "encoding/json" - "testing" - - . "github.com/v2ray/v2ray-core/common/serial" - "github.com/v2ray/v2ray-core/testing/assert" -) - -func TestInvalidStringTJson(t *testing.T) { - assert := assert.On(t) - - var s StringT - err := json.Unmarshal([]byte("1"), &s) - assert.Error(err).IsNotNil() -} - -func TestStringTParsing(t *testing.T) { - assert := assert.On(t) - - var s StringT - err := json.Unmarshal([]byte("\"1\""), &s) - assert.Error(err).IsNil() - assert.String(s.String()).Equals("1") -} diff --git a/common/serial/string_list.go b/common/serial/string_list.go deleted file mode 100644 index dba42711f..000000000 --- a/common/serial/string_list.go +++ /dev/null @@ -1,15 +0,0 @@ -package serial - -type StringTList []StringT - -func NewStringTList(raw []string) *StringTList { - list := StringTList(make([]StringT, len(raw))) - for idx, str := range raw { - list[idx] = StringT(str) - } - return &list -} - -func (this *StringTList) Len() int { - return len(*this) -} diff --git a/proxy/freedom/config_json.go b/proxy/freedom/config_json.go index 8e9465ed2..9a9f8258e 100644 --- a/proxy/freedom/config_json.go +++ b/proxy/freedom/config_json.go @@ -4,8 +4,8 @@ package freedom import ( "encoding/json" + "strings" - "github.com/v2ray/v2ray-core/common/serial" "github.com/v2ray/v2ray-core/proxy/internal/config" ) @@ -18,8 +18,8 @@ func (this *Config) UnmarshalJSON(data []byte) error { return err } this.DomainStrategy = DomainStrategyAsIs - domainStrategy := serial.StringT(jsonConfig.DomainStrategy).ToLower() - if domainStrategy.String() == "useip" { + domainStrategy := strings.ToLower(jsonConfig.DomainStrategy) + if domainStrategy == "useip" { this.DomainStrategy = DomainStrategyUseIP } return nil diff --git a/proxy/shadowsocks/config_json.go b/proxy/shadowsocks/config_json.go index f31178dd7..865fa3a20 100644 --- a/proxy/shadowsocks/config_json.go +++ b/proxy/shadowsocks/config_json.go @@ -4,21 +4,21 @@ package shadowsocks import ( "encoding/json" + "strings" "github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/protocol" - "github.com/v2ray/v2ray-core/common/serial" "github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/proxy/internal/config" ) func (this *Config) UnmarshalJSON(data []byte) error { type JsonConfig struct { - Cipher serial.StringT `json:"method"` - Password serial.StringT `json:"password"` - UDP bool `json:"udp"` - Level byte `json:"level"` - Email string `json:"email"` + Cipher string `json:"method"` + Password string `json:"password"` + UDP bool `json:"udp"` + Level byte `json:"level"` + Email string `json:"email"` } jsonConfig := new(JsonConfig) if err := json.Unmarshal(data, jsonConfig); err != nil { @@ -26,8 +26,8 @@ func (this *Config) UnmarshalJSON(data []byte) error { } this.UDP = jsonConfig.UDP - jsonConfig.Cipher = jsonConfig.Cipher.ToLower() - switch jsonConfig.Cipher.String() { + jsonConfig.Cipher = strings.ToLower(jsonConfig.Cipher) + switch jsonConfig.Cipher { case "aes-256-cfb": this.Cipher = &AesCfb{ KeyBytes: 32, @@ -53,7 +53,7 @@ func (this *Config) UnmarshalJSON(data []byte) error { log.Error("Shadowsocks: Password is not specified.") return internal.ErrorBadConfiguration } - this.Key = PasswordToCipherKey(jsonConfig.Password.String(), this.Cipher.KeySize()) + this.Key = PasswordToCipherKey(jsonConfig.Password, this.Cipher.KeySize()) this.Level = protocol.UserLevel(jsonConfig.Level) this.Email = jsonConfig.Email diff --git a/proxy/shadowsocks/server.go b/proxy/shadowsocks/server.go index 09c10abec..5763ef9c6 100644 --- a/proxy/shadowsocks/server.go +++ b/proxy/shadowsocks/server.go @@ -14,7 +14,6 @@ import ( "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/common/protocol" - "github.com/v2ray/v2ray-core/common/serial" "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/internal" "github.com/v2ray/v2ray-core/transport/hub" @@ -106,14 +105,14 @@ func (this *Server) handlerUDPPayload(payload *alloc.Buffer, source v2net.Destin request, err := ReadRequest(reader, NewAuthenticator(HeaderKeyGenerator(key, iv)), true) if err != nil { - log.Access(source, serial.StringT(""), log.AccessRejected, serial.StringT(err.Error())) + log.Access(source, "", log.AccessRejected, err) log.Warning("Shadowsocks: Invalid request from ", source, ": ", err) return } //defer request.Release() dest := v2net.UDPDestination(request.Address, request.Port) - log.Access(source, dest, log.AccessAccepted, serial.StringT("")) + log.Access(source, dest, log.AccessAccepted, "") log.Info("Shadowsocks: Tunnelling request to ", dest) this.udpServer.Dispatch(source, dest, request.DetachUDPPayload(), func(destination v2net.Destination, payload *alloc.Buffer) { @@ -172,7 +171,7 @@ func (this *Server) handleConnection(conn *hub.Connection) { ivLen := this.config.Cipher.IVSize() _, err := io.ReadFull(bufferedReader, buffer.Value[:ivLen]) if err != nil { - log.Access(conn.RemoteAddr(), serial.StringT(""), log.AccessRejected, serial.StringT(err.Error())) + log.Access(conn.RemoteAddr(), "", log.AccessRejected, err) log.Error("Shadowsocks: Failed to read IV: ", err) return } @@ -190,7 +189,7 @@ func (this *Server) handleConnection(conn *hub.Connection) { request, err := ReadRequest(reader, NewAuthenticator(HeaderKeyGenerator(key, iv)), false) if err != nil { - log.Access(conn.RemoteAddr(), serial.StringT(""), log.AccessRejected, serial.StringT(err.Error())) + log.Access(conn.RemoteAddr(), "", log.AccessRejected, err) log.Warning("Shadowsocks: Invalid request from ", conn.RemoteAddr(), ": ", err) return } @@ -201,7 +200,7 @@ func (this *Server) handleConnection(conn *hub.Connection) { timedReader.SetTimeOut(userSettings.PayloadReadTimeout) dest := v2net.TCPDestination(request.Address, request.Port) - log.Access(conn.RemoteAddr(), dest, log.AccessAccepted, serial.StringT("")) + log.Access(conn.RemoteAddr(), dest, log.AccessAccepted, "") log.Info("Shadowsocks: Tunnelling request to ", dest) ray := this.packetDispatcher.DispatchToOutbound(dest) diff --git a/proxy/vmess/inbound/inbound.go b/proxy/vmess/inbound/inbound.go index 4f4f96823..4b57e646d 100644 --- a/proxy/vmess/inbound/inbound.go +++ b/proxy/vmess/inbound/inbound.go @@ -12,7 +12,6 @@ import ( v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/common/protocol" "github.com/v2ray/v2ray-core/common/protocol/raw" - "github.com/v2ray/v2ray-core/common/serial" "github.com/v2ray/v2ray-core/common/uuid" "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/internal" @@ -130,11 +129,11 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.Connection) { request, err := session.DecodeRequestHeader(reader) if err != nil { - log.Access(connection.RemoteAddr(), serial.StringT(""), log.AccessRejected, serial.StringT(err.Error())) + log.Access(connection.RemoteAddr(), "", log.AccessRejected, err) log.Warning("VMessIn: Invalid request from ", connection.RemoteAddr(), ": ", err) return } - log.Access(connection.RemoteAddr(), request.Destination(), log.AccessAccepted, serial.StringT("")) + log.Access(connection.RemoteAddr(), request.Destination(), log.AccessAccepted, "") log.Debug("VMessIn: Received request for ", request.Destination()) ray := this.packetDispatcher.DispatchToOutbound(request.Destination())