浏览代码

Shorten StringLiteral

v2ray 9 年之前
父节点
当前提交
47b2fafb32

+ 4 - 4
app/router/rules/condition.go

@@ -63,12 +63,12 @@ func (this *AnyCondition) Len() int {
 }
 
 type PlainDomainMatcher struct {
-	pattern serial.StringLiteral
+	pattern serial.StringT
 }
 
 func NewPlainDomainMatcher(pattern string) *PlainDomainMatcher {
 	return &PlainDomainMatcher{
-		pattern: serial.StringLiteral(pattern),
+		pattern: serial.StringT(pattern),
 	}
 }
 
@@ -76,7 +76,7 @@ func (this *PlainDomainMatcher) Apply(dest v2net.Destination) bool {
 	if !dest.Address().IsDomain() {
 		return false
 	}
-	domain := serial.StringLiteral(dest.Address().Domain())
+	domain := serial.StringT(dest.Address().Domain())
 	return domain.Contains(this.pattern)
 }
 
@@ -98,7 +98,7 @@ func (this *RegexpDomainMatcher) Apply(dest v2net.Destination) bool {
 	if !dest.Address().IsDomain() {
 		return false
 	}
-	domain := serial.StringLiteral(dest.Address().Domain())
+	domain := serial.StringT(dest.Address().Domain())
 	return this.pattern.MatchString(domain.ToLower().String())
 }
 

+ 7 - 7
common/log/internal/log_entry_test.go

@@ -13,15 +13,15 @@ func TestAccessLog(t *testing.T) {
 	v2testing.Current(t)
 
 	entry := &AccessLog{
-		From:   serial.StringLiteral("test_from"),
-		To:     serial.StringLiteral("test_to"),
+		From:   serial.StringT("test_from"),
+		To:     serial.StringT("test_to"),
 		Status: "Accepted",
-		Reason: serial.StringLiteral("test_reason"),
+		Reason: serial.StringT("test_reason"),
 	}
 
 	entryStr := entry.String()
-	assert.StringLiteral(entryStr).Contains(serial.StringLiteral("test_from"))
-	assert.StringLiteral(entryStr).Contains(serial.StringLiteral("test_to"))
-	assert.StringLiteral(entryStr).Contains(serial.StringLiteral("test_reason"))
-	assert.StringLiteral(entryStr).Contains(serial.StringLiteral("Accepted"))
+	assert.StringLiteral(entryStr).Contains(serial.StringT("test_from"))
+	assert.StringLiteral(entryStr).Contains(serial.StringT("test_to"))
+	assert.StringLiteral(entryStr).Contains(serial.StringT("test_reason"))
+	assert.StringLiteral(entryStr).Contains(serial.StringT("Accepted"))
 }

+ 2 - 2
common/net/network.go

@@ -13,7 +13,7 @@ const (
 )
 
 // Network represents a communication network on internet.
-type Network serial.StringLiteral
+type Network serial.StringT
 
 func (this Network) AsList() *NetworkList {
 	list := NetworkList([]Network{this})
@@ -24,7 +24,7 @@ func (this Network) AsList() *NetworkList {
 type NetworkList []Network
 
 // NewNetworkList construsts a NetWorklist from the given StringListeralList.
-func NewNetworkList(networks serial.StringLiteralList) NetworkList {
+func NewNetworkList(networks serial.StringTList) NetworkList {
 	list := NetworkList(make([]Network, networks.Len()))
 	for idx, network := range networks {
 		list[idx] = Network(network.TrimSpace().ToLower())

+ 6 - 6
common/net/testing/assert/address.go

@@ -32,36 +32,36 @@ func (subject *AddressSubject) Equals(another v2net.Address) {
 
 func (subject *AddressSubject) IsIPv4() {
 	if !subject.value.IsIPv4() {
-		subject.Fail(subject.DisplayString(), "is", serial.StringLiteral("an IPv4 address"))
+		subject.Fail(subject.DisplayString(), "is", serial.StringT("an IPv4 address"))
 	}
 }
 
 func (subject *AddressSubject) IsNotIPv4() {
 	if subject.value.IsIPv4() {
-		subject.Fail(subject.DisplayString(), "is not", serial.StringLiteral("an IPv4 address"))
+		subject.Fail(subject.DisplayString(), "is not", serial.StringT("an IPv4 address"))
 	}
 }
 
 func (subject *AddressSubject) IsIPv6() {
 	if !subject.value.IsIPv6() {
-		subject.Fail(subject.DisplayString(), "is", serial.StringLiteral("an IPv6 address"))
+		subject.Fail(subject.DisplayString(), "is", serial.StringT("an IPv6 address"))
 	}
 }
 
 func (subject *AddressSubject) IsNotIPv6() {
 	if subject.value.IsIPv6() {
-		subject.Fail(subject.DisplayString(), "is not", serial.StringLiteral("an IPv6 address"))
+		subject.Fail(subject.DisplayString(), "is not", serial.StringT("an IPv6 address"))
 	}
 }
 
 func (subject *AddressSubject) IsDomain() {
 	if !subject.value.IsDomain() {
-		subject.Fail(subject.DisplayString(), "is", serial.StringLiteral("a domain address"))
+		subject.Fail(subject.DisplayString(), "is", serial.StringT("a domain address"))
 	}
 }
 
 func (subject *AddressSubject) IsNotDomain() {
 	if subject.value.IsDomain() {
-		subject.Fail(subject.DisplayString(), "is not", serial.StringLiteral("a domain address"))
+		subject.Fail(subject.DisplayString(), "is not", serial.StringT("a domain address"))
 	}
 }

+ 4 - 4
common/net/testing/assert/destination.go

@@ -26,24 +26,24 @@ func (this *DestinationSubject) DisplayString() string {
 
 func (this *DestinationSubject) IsTCP() {
 	if !this.value.IsTCP() {
-		this.Fail(this.DisplayString(), "is", serial.StringLiteral("a TCP destination"))
+		this.Fail(this.DisplayString(), "is", serial.StringT("a TCP destination"))
 	}
 }
 
 func (this *DestinationSubject) IsNotTCP() {
 	if this.value.IsTCP() {
-		this.Fail(this.DisplayString(), "is not", serial.StringLiteral("a TCP destination"))
+		this.Fail(this.DisplayString(), "is not", serial.StringT("a TCP destination"))
 	}
 }
 
 func (this *DestinationSubject) IsUDP() {
 	if !this.value.IsUDP() {
-		this.Fail(this.DisplayString(), "is", serial.StringLiteral("a UDP destination"))
+		this.Fail(this.DisplayString(), "is", serial.StringT("a UDP destination"))
 	}
 }
 
 func (this *DestinationSubject) IsNotUDP() {
 	if this.value.IsUDP() {
-		this.Fail(this.DisplayString(), "is not", serial.StringLiteral("a UDP destination"))
+		this.Fail(this.DisplayString(), "is not", serial.StringT("a UDP destination"))
 	}
 }

+ 1 - 1
common/net/testing/assert/ip.go

@@ -28,7 +28,7 @@ func (subject *IPSubject) DisplayString() string {
 
 func (subject *IPSubject) IsNil() {
 	if subject.value != nil {
-		subject.Fail(subject.DisplayString(), "is", serial.StringLiteral("nil"))
+		subject.Fail(subject.DisplayString(), "is", serial.StringT("nil"))
 	}
 }
 

+ 1 - 1
common/net/testing/assert/port.go

@@ -44,6 +44,6 @@ func (subject *PortSubject) LessThan(expectation v2net.Port) {
 
 func (subject *PortSubject) IsValid() {
 	if subject.value == 0 {
-		subject.Fail(subject.DisplayString(), "is", serial.StringLiteral("a valid port"))
+		subject.Fail(subject.DisplayString(), "is", serial.StringT("a valid port"))
 	}
 }

+ 11 - 11
common/serial/string.go

@@ -9,28 +9,28 @@ type String interface {
 	String() string
 }
 
-type StringLiteral string
+type StringT string
 
-func NewStringLiteral(str String) StringLiteral {
-	return StringLiteral(str.String())
+func NewStringT(str String) StringT {
+	return StringT(str.String())
 }
 
-func (this StringLiteral) Contains(str String) bool {
+func (this StringT) Contains(str String) bool {
 	return strings.Contains(this.String(), str.String())
 }
 
-func (this StringLiteral) String() string {
+func (this StringT) String() string {
 	return string(this)
 }
 
-func (this StringLiteral) ToLower() StringLiteral {
-	return StringLiteral(strings.ToLower(string(this)))
+func (this StringT) ToLower() StringT {
+	return StringT(strings.ToLower(string(this)))
 }
 
-func (this StringLiteral) ToUpper() StringLiteral {
-	return StringLiteral(strings.ToUpper(string(this)))
+func (this StringT) ToUpper() StringT {
+	return StringT(strings.ToUpper(string(this)))
 }
 
-func (this StringLiteral) TrimSpace() StringLiteral {
-	return StringLiteral(strings.TrimSpace(string(this)))
+func (this StringT) TrimSpace() StringT {
+	return StringT(strings.TrimSpace(string(this)))
 }

+ 2 - 2
common/serial/string_json.go

@@ -6,11 +6,11 @@ import (
 	"encoding/json"
 )
 
-func (this *StringLiteral) UnmarshalJSON(data []byte) error {
+func (this *StringT) UnmarshalJSON(data []byte) error {
 	var str string
 	if err := json.Unmarshal(data, &str); err != nil {
 		return err
 	}
-	*this = StringLiteral(str)
+	*this = StringT(str)
 	return nil
 }

+ 5 - 5
common/serial/string_list.go

@@ -1,15 +1,15 @@
 package serial
 
-type StringLiteralList []StringLiteral
+type StringTList []StringT
 
-func NewStringLiteralList(raw []string) *StringLiteralList {
-	list := StringLiteralList(make([]StringLiteral, len(raw)))
+func NewStringTList(raw []string) *StringTList {
+	list := StringTList(make([]StringT, len(raw)))
 	for idx, str := range raw {
-		list[idx] = StringLiteral(str)
+		list[idx] = StringT(str)
 	}
 	return &list
 }
 
-func (this *StringLiteralList) Len() int {
+func (this *StringTList) Len() int {
 	return len(*this)
 }

+ 3 - 3
common/serial/string_list_json.go

@@ -8,17 +8,17 @@ import (
 	"strings"
 )
 
-func (this *StringLiteralList) UnmarshalJSON(data []byte) error {
+func (this *StringTList) UnmarshalJSON(data []byte) error {
 	var strarray []string
 	if err := json.Unmarshal(data, &strarray); err == nil {
-		*this = *NewStringLiteralList(strarray)
+		*this = *NewStringTList(strarray)
 		return nil
 	}
 
 	var rawstr string
 	if err := json.Unmarshal(data, &rawstr); err == nil {
 		strlist := strings.Split(rawstr, ",")
-		*this = *NewStringLiteralList(strlist)
+		*this = *NewStringTList(strlist)
 		return nil
 	}
 	return errors.New("Unknown format of a string list: " + string(data))

+ 1 - 1
common/serial/string_test.go

@@ -20,5 +20,5 @@ func TestNewStringSerial(t *testing.T) {
 	v2testing.Current(t)
 
 	testString := &TestString{value: "abcd"}
-	assert.String(NewStringLiteral(testString)).Equals("abcd")
+	assert.String(NewStringT(testString)).Equals("abcd")
 }

+ 5 - 5
proxy/shadowsocks/server.go

@@ -106,14 +106,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.StringLiteral(""), log.AccessRejected, serial.StringLiteral(err.Error()))
+		log.Access(source, serial.StringT(""), log.AccessRejected, serial.StringT(err.Error()))
 		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.StringLiteral(""))
+	log.Access(source, dest, log.AccessAccepted, serial.StringT(""))
 	log.Info("Shadowsocks: Tunnelling request to ", dest)
 
 	this.udpServer.Dispatch(source, dest, request.DetachUDPPayload(), func(destination v2net.Destination, payload *alloc.Buffer) {
@@ -172,7 +172,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.StringLiteral(""), log.AccessRejected, serial.StringLiteral(err.Error()))
+		log.Access(conn.RemoteAddr(), serial.StringT(""), log.AccessRejected, serial.StringT(err.Error()))
 		log.Error("Shadowsocks: Failed to read IV: ", err)
 		return
 	}
@@ -190,7 +190,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.StringLiteral(""), log.AccessRejected, serial.StringLiteral(err.Error()))
+		log.Access(conn.RemoteAddr(), serial.StringT(""), log.AccessRejected, serial.StringT(err.Error()))
 		log.Warning("Shadowsocks: Invalid request from ", conn.RemoteAddr(), ": ", err)
 		return
 	}
@@ -201,7 +201,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.StringLiteral(""))
+	log.Access(conn.RemoteAddr(), dest, log.AccessAccepted, serial.StringT(""))
 	log.Info("Shadowsocks: Tunnelling request to ", dest)
 
 	ray := this.packetDispatcher.DispatchToOutbound(dest)

+ 2 - 2
proxy/vmess/inbound/inbound.go

@@ -130,11 +130,11 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.Connection) {
 
 	request, err := session.DecodeRequestHeader(reader)
 	if err != nil {
-		log.Access(connection.RemoteAddr(), serial.StringLiteral(""), log.AccessRejected, serial.StringLiteral(err.Error()))
+		log.Access(connection.RemoteAddr(), serial.StringT(""), log.AccessRejected, serial.StringT(err.Error()))
 		log.Warning("VMessIn: Invalid request from ", connection.RemoteAddr(), ": ", err)
 		return
 	}
-	log.Access(connection.RemoteAddr(), request.Destination(), log.AccessAccepted, serial.StringLiteral(""))
+	log.Access(connection.RemoteAddr(), request.Destination(), log.AccessAccepted, serial.StringT(""))
 	log.Debug("VMessIn: Received request for ", request.Destination())
 
 	ray := this.packetDispatcher.DispatchToOutbound(request.Destination())

+ 3 - 3
testing/assert/stringsubject.go

@@ -7,7 +7,7 @@ import (
 )
 
 func StringLiteral(value string) *StringSubject {
-	return String(serial.StringLiteral((value)))
+	return String(serial.StringT((value)))
 }
 
 func String(value serial.String) *StringSubject {
@@ -30,13 +30,13 @@ func (subject *StringSubject) DisplayString() string {
 
 func (subject *StringSubject) Equals(expectation string) {
 	if subject.value.String() != expectation {
-		subject.Fail(subject.DisplayString(), "is equal to", serial.StringLiteral(expectation))
+		subject.Fail(subject.DisplayString(), "is equal to", serial.StringT(expectation))
 	}
 }
 
 func (subject *StringSubject) NotEquals(expectation string) {
 	if subject.value.String() == expectation {
-		subject.Fail(subject.DisplayString(), "is not equal to ", serial.StringLiteral(expectation))
+		subject.Fail(subject.DisplayString(), "is not equal to ", serial.StringT(expectation))
 	}
 }