Shorten StringLiteral

pull/168/head
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 { type PlainDomainMatcher struct {
pattern serial.StringLiteral pattern serial.StringT
} }
func NewPlainDomainMatcher(pattern string) *PlainDomainMatcher { func NewPlainDomainMatcher(pattern string) *PlainDomainMatcher {
return &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() { if !dest.Address().IsDomain() {
return false return false
} }
domain := serial.StringLiteral(dest.Address().Domain()) domain := serial.StringT(dest.Address().Domain())
return domain.Contains(this.pattern) return domain.Contains(this.pattern)
} }
@ -98,7 +98,7 @@ func (this *RegexpDomainMatcher) Apply(dest v2net.Destination) bool {
if !dest.Address().IsDomain() { if !dest.Address().IsDomain() {
return false return false
} }
domain := serial.StringLiteral(dest.Address().Domain()) domain := serial.StringT(dest.Address().Domain())
return this.pattern.MatchString(domain.ToLower().String()) return this.pattern.MatchString(domain.ToLower().String())
} }

View File

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

View File

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

View File

@ -32,36 +32,36 @@ func (subject *AddressSubject) Equals(another v2net.Address) {
func (subject *AddressSubject) IsIPv4() { func (subject *AddressSubject) IsIPv4() {
if !subject.value.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() { func (subject *AddressSubject) IsNotIPv4() {
if subject.value.IsIPv4() { 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() { func (subject *AddressSubject) IsIPv6() {
if !subject.value.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() { func (subject *AddressSubject) IsNotIPv6() {
if subject.value.IsIPv6() { 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() { func (subject *AddressSubject) IsDomain() {
if !subject.value.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() { func (subject *AddressSubject) IsNotDomain() {
if subject.value.IsDomain() { 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() { func (this *DestinationSubject) IsTCP() {
if !this.value.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() { func (this *DestinationSubject) IsNotTCP() {
if this.value.IsTCP() { 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() { func (this *DestinationSubject) IsUDP() {
if !this.value.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() { func (this *DestinationSubject) IsNotUDP() {
if this.value.IsUDP() { 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() { func (subject *IPSubject) IsNil() {
if subject.value != nil { 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() { func (subject *PortSubject) IsValid() {
if subject.value == 0 { 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 String() string
} }
type StringLiteral string type StringT string
func NewStringLiteral(str String) StringLiteral { func NewStringT(str String) StringT {
return StringLiteral(str.String()) 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()) return strings.Contains(this.String(), str.String())
} }
func (this StringLiteral) String() string { func (this StringT) String() string {
return string(this) return string(this)
} }
func (this StringLiteral) ToLower() StringLiteral { func (this StringT) ToLower() StringT {
return StringLiteral(strings.ToLower(string(this))) return StringT(strings.ToLower(string(this)))
} }
func (this StringLiteral) ToUpper() StringLiteral { func (this StringT) ToUpper() StringT {
return StringLiteral(strings.ToUpper(string(this))) return StringT(strings.ToUpper(string(this)))
} }
func (this StringLiteral) TrimSpace() StringLiteral { func (this StringT) TrimSpace() StringT {
return StringLiteral(strings.TrimSpace(string(this))) return StringT(strings.TrimSpace(string(this)))
} }

View File

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

View File

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

View File

@ -8,17 +8,17 @@ import (
"strings" "strings"
) )
func (this *StringLiteralList) UnmarshalJSON(data []byte) error { func (this *StringTList) UnmarshalJSON(data []byte) error {
var strarray []string var strarray []string
if err := json.Unmarshal(data, &strarray); err == nil { if err := json.Unmarshal(data, &strarray); err == nil {
*this = *NewStringLiteralList(strarray) *this = *NewStringTList(strarray)
return nil return nil
} }
var rawstr string var rawstr string
if err := json.Unmarshal(data, &rawstr); err == nil { if err := json.Unmarshal(data, &rawstr); err == nil {
strlist := strings.Split(rawstr, ",") strlist := strings.Split(rawstr, ",")
*this = *NewStringLiteralList(strlist) *this = *NewStringTList(strlist)
return nil return nil
} }
return errors.New("Unknown format of a string list: " + string(data)) 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) v2testing.Current(t)
testString := &TestString{value: "abcd"} 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) request, err := ReadRequest(reader, NewAuthenticator(HeaderKeyGenerator(key, iv)), true)
if err != nil { 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) log.Warning("Shadowsocks: Invalid request from ", source, ": ", err)
return return
} }
//defer request.Release() //defer request.Release()
dest := v2net.UDPDestination(request.Address, request.Port) 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) log.Info("Shadowsocks: Tunnelling request to ", dest)
this.udpServer.Dispatch(source, dest, request.DetachUDPPayload(), func(destination v2net.Destination, payload *alloc.Buffer) { 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() ivLen := this.config.Cipher.IVSize()
_, err := io.ReadFull(bufferedReader, buffer.Value[:ivLen]) _, err := io.ReadFull(bufferedReader, buffer.Value[:ivLen])
if err != nil { 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) log.Error("Shadowsocks: Failed to read IV: ", err)
return return
} }
@ -190,7 +190,7 @@ func (this *Server) handleConnection(conn *hub.Connection) {
request, err := ReadRequest(reader, NewAuthenticator(HeaderKeyGenerator(key, iv)), false) request, err := ReadRequest(reader, NewAuthenticator(HeaderKeyGenerator(key, iv)), false)
if err != nil { 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) log.Warning("Shadowsocks: Invalid request from ", conn.RemoteAddr(), ": ", err)
return return
} }
@ -201,7 +201,7 @@ func (this *Server) handleConnection(conn *hub.Connection) {
timedReader.SetTimeOut(userSettings.PayloadReadTimeout) timedReader.SetTimeOut(userSettings.PayloadReadTimeout)
dest := v2net.TCPDestination(request.Address, request.Port) 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) log.Info("Shadowsocks: Tunnelling request to ", dest)
ray := this.packetDispatcher.DispatchToOutbound(dest) ray := this.packetDispatcher.DispatchToOutbound(dest)

View File

@ -130,11 +130,11 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.Connection) {
request, err := session.DecodeRequestHeader(reader) request, err := session.DecodeRequestHeader(reader)
if err != nil { 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) log.Warning("VMessIn: Invalid request from ", connection.RemoteAddr(), ": ", err)
return 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()) log.Debug("VMessIn: Received request for ", request.Destination())
ray := this.packetDispatcher.DispatchToOutbound(request.Destination()) ray := this.packetDispatcher.DispatchToOutbound(request.Destination())

View File

@ -7,7 +7,7 @@ import (
) )
func StringLiteral(value string) *StringSubject { func StringLiteral(value string) *StringSubject {
return String(serial.StringLiteral((value))) return String(serial.StringT((value)))
} }
func String(value serial.String) *StringSubject { func String(value serial.String) *StringSubject {
@ -30,13 +30,13 @@ func (subject *StringSubject) DisplayString() string {
func (subject *StringSubject) Equals(expectation string) { func (subject *StringSubject) Equals(expectation string) {
if subject.value.String() != expectation { 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) { func (subject *StringSubject) NotEquals(expectation string) {
if subject.value.String() == expectation { 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))
} }
} }