1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-26 09:25:23 +00:00

Shorten StringLiteral

This commit is contained in:
v2ray 2016-05-23 20:23:40 +02:00
parent cfdda19834
commit 47b2fafb32
15 changed files with 57 additions and 57 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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