Ver Fonte

network proto

Darien Raymond há 9 anos atrás
pai
commit
c518726910

+ 1 - 1
app/router/rules/router.go

@@ -49,7 +49,7 @@ func (this *Router) ResolveIP(dest v2net.Destination) []v2net.Destination {
 	}
 	dests := make([]v2net.Destination, len(ips))
 	for idx, ip := range ips {
-		if dest.Network() == v2net.TCPNetwork {
+		if dest.Network() == v2net.Network_TCP {
 			dests[idx] = v2net.TCPDestination(v2net.IPAddress(ip), dest.Port())
 		} else {
 			dests[idx] = v2net.UDPDestination(v2net.IPAddress(ip), dest.Port())

+ 1 - 1
app/router/rules/router_test.go

@@ -21,7 +21,7 @@ func TestSimpleRouter(t *testing.T) {
 		Rules: []*Rule{
 			{
 				Tag:       "test",
-				Condition: NewNetworkMatcher(v2net.Network("tcp").AsList()),
+				Condition: NewNetworkMatcher(v2net.Network_TCP.AsList()),
 			},
 		},
 	}

+ 2 - 0
common/net/address.pb.go

@@ -7,6 +7,8 @@ Package net is a generated protocol buffer package.
 
 It is generated from these files:
 	v2ray.com/core/common/net/address.proto
+	v2ray.com/core/common/net/destination.proto
+	v2ray.com/core/common/net/network.proto
 	v2ray.com/core/common/net/port.proto
 
 It has these top-level messages:

+ 4 - 4
common/net/destination.go

@@ -44,7 +44,7 @@ type tcpDestination struct {
 }
 
 func (dest *tcpDestination) Network() Network {
-	return TCPNetwork
+	return Network_TCP
 }
 
 func (dest *tcpDestination) Address() Address {
@@ -78,7 +78,7 @@ func (dest *tcpDestination) Equals(another Destination) bool {
 	if dest == nil || another == nil {
 		return false
 	}
-	if another.Network() != TCPNetwork {
+	if another.Network() != Network_TCP {
 		return false
 	}
 	return dest.Port() == another.Port() && dest.Address().Equals(another.Address())
@@ -90,7 +90,7 @@ type udpDestination struct {
 }
 
 func (dest *udpDestination) Network() Network {
-	return UDPNetwork
+	return Network_UDP
 }
 
 func (dest *udpDestination) Address() Address {
@@ -124,7 +124,7 @@ func (dest *udpDestination) Equals(another Destination) bool {
 	if dest == nil || another == nil {
 		return false
 	}
-	if another.Network() != UDPNetwork {
+	if another.Network() != Network_UDP {
 		return false
 	}
 	return dest.Port() == another.Port() && dest.Address().Equals(another.Address())

+ 27 - 0
common/net/destination.pb.go

@@ -0,0 +1,27 @@
+// Code generated by protoc-gen-go.
+// source: v2ray.com/core/common/net/destination.proto
+// DO NOT EDIT!
+
+package net
+
+import proto "github.com/golang/protobuf/proto"
+import fmt "fmt"
+import math "math"
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ = proto.Marshal
+var _ = fmt.Errorf
+var _ = math.Inf
+
+func init() { proto.RegisterFile("v2ray.com/core/common/net/destination.proto", fileDescriptor1) }
+
+var fileDescriptor1 = []byte{
+	// 98 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xd2, 0x2e, 0x33, 0x2a, 0x4a,
+	0xac, 0xd4, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xce, 0x2f, 0x4a, 0xd5, 0x4f, 0xce, 0xcf, 0xcd, 0xcd,
+	0xcf, 0xd3, 0xcf, 0x4b, 0x2d, 0xd1, 0x4f, 0x49, 0x2d, 0x2e, 0xc9, 0xcc, 0x4b, 0x2c, 0xc9, 0xcc,
+	0xcf, 0xd3, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x92, 0x4c, 0xce, 0xcf, 0xd5, 0x83, 0x69, 0x28,
+	0x4a, 0xd5, 0x83, 0x28, 0xd6, 0xcb, 0x4b, 0x2d, 0x71, 0x62, 0x8d, 0x62, 0xce, 0x4b, 0x2d, 0x49,
+	0x62, 0x03, 0x2b, 0x34, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0x95, 0x64, 0x1f, 0x93, 0x57, 0x00,
+	0x00, 0x00,
+}

+ 4 - 0
common/net/destination.proto

@@ -0,0 +1,4 @@
+syntax = "proto3";
+
+package com.v2ray.core.common.net;
+option go_package = "net";

+ 27 - 19
common/net/network.go

@@ -6,30 +6,38 @@ import (
 	"v2ray.com/core/common/collect"
 )
 
-const (
-	// TCPNetwork represents the TCP network.
-	TCPNetwork = Network("tcp")
-
-	// UDPNetwork represents the UDP network.
-	UDPNetwork = Network("udp")
-
-	// KCPNetwork represents the KCP network.
-	KCPNetwork = Network("kcp")
-
-	// WSNetwork represents the Websocket over HTTP network.
-	WSNetwork = Network("ws")
-)
-
-// Network represents a communication network on internet.
-type Network string
+func ParseNetwork(nwStr string) Network {
+	if network, found := Network_value[nwStr]; found {
+		return Network(network)
+	}
+	switch strings.ToLower(nwStr) {
+	case "tcp":
+		return Network_TCP
+	case "udp":
+		return Network_UDP
+	case "kcp":
+		return Network_KCP
+	case "ws":
+		return Network_WebSocket
+	default:
+		return Network_Unknown
+	}
+}
 
 func (this Network) AsList() *NetworkList {
 	list := NetworkList([]Network{this})
 	return &list
 }
 
-func (this Network) String() string {
-	return string(this)
+func (this Network) SystemString() string {
+	switch this {
+	case Network_TCP, Network_RawTCP:
+		return "tcp"
+	case Network_UDP, Network_KCP:
+		return "udp"
+	default:
+		return "unknown"
+	}
 }
 
 // NetworkList is a list of Networks.
@@ -39,7 +47,7 @@ type NetworkList []Network
 func NewNetworkList(networks collect.StringList) NetworkList {
 	list := NetworkList(make([]Network, networks.Len()))
 	for idx, network := range networks {
-		list[idx] = Network(strings.ToLower(strings.TrimSpace(network)))
+		list[idx] = ParseNetwork(network)
 	}
 	return list
 }

+ 67 - 0
common/net/network.pb.go

@@ -0,0 +1,67 @@
+// Code generated by protoc-gen-go.
+// source: v2ray.com/core/common/net/network.proto
+// DO NOT EDIT!
+
+package net
+
+import proto "github.com/golang/protobuf/proto"
+import fmt "fmt"
+import math "math"
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ = proto.Marshal
+var _ = fmt.Errorf
+var _ = math.Inf
+
+type Network int32
+
+const (
+	Network_Unknown   Network = 0
+	Network_RawTCP    Network = 1
+	Network_TCP       Network = 2
+	Network_UDP       Network = 3
+	Network_KCP       Network = 4
+	Network_WebSocket Network = 5
+)
+
+var Network_name = map[int32]string{
+	0: "Unknown",
+	1: "RawTCP",
+	2: "TCP",
+	3: "UDP",
+	4: "KCP",
+	5: "WebSocket",
+}
+var Network_value = map[string]int32{
+	"Unknown":   0,
+	"RawTCP":    1,
+	"TCP":       2,
+	"UDP":       3,
+	"KCP":       4,
+	"WebSocket": 5,
+}
+
+func (x Network) String() string {
+	return proto.EnumName(Network_name, int32(x))
+}
+func (Network) EnumDescriptor() ([]byte, []int) { return fileDescriptor2, []int{0} }
+
+func init() {
+	proto.RegisterEnum("com.v2ray.core.common.net.Network", Network_name, Network_value)
+}
+
+func init() { proto.RegisterFile("v2ray.com/core/common/net/network.proto", fileDescriptor2) }
+
+var fileDescriptor2 = []byte{
+	// 159 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x52, 0x2f, 0x33, 0x2a, 0x4a,
+	0xac, 0xd4, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xce, 0x2f, 0x4a, 0xd5, 0x4f, 0xce, 0xcf, 0xcd, 0xcd,
+	0xcf, 0xd3, 0xcf, 0x4b, 0x2d, 0x01, 0xe1, 0xf2, 0xfc, 0xa2, 0x6c, 0xbd, 0x82, 0xa2, 0xfc, 0x92,
+	0x7c, 0x21, 0xc9, 0xe4, 0xfc, 0x5c, 0x3d, 0x98, 0xe2, 0xa2, 0x54, 0x3d, 0x88, 0x42, 0xbd, 0xbc,
+	0xd4, 0x12, 0x2d, 0x1f, 0x2e, 0x76, 0x3f, 0x88, 0x5a, 0x21, 0x6e, 0x2e, 0xf6, 0xd0, 0xbc, 0xec,
+	0xbc, 0xfc, 0xf2, 0x3c, 0x01, 0x06, 0x21, 0x2e, 0x2e, 0xb6, 0xa0, 0xc4, 0xf2, 0x10, 0xe7, 0x00,
+	0x01, 0x46, 0x21, 0x76, 0x2e, 0x66, 0x10, 0x83, 0x09, 0xc4, 0x08, 0x75, 0x09, 0x10, 0x60, 0x06,
+	0x31, 0xbc, 0x9d, 0x03, 0x04, 0x58, 0x84, 0x78, 0xb9, 0x38, 0xc3, 0x53, 0x93, 0x82, 0xf3, 0x93,
+	0xb3, 0x53, 0x4b, 0x04, 0x58, 0x9d, 0x58, 0xa3, 0x98, 0xf3, 0x52, 0x4b, 0x92, 0xd8, 0xc0, 0xd6,
+	0x1a, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x83, 0xa9, 0x75, 0xb3, 0xa1, 0x00, 0x00, 0x00,
+}

+ 13 - 0
common/net/network.proto

@@ -0,0 +1,13 @@
+syntax = "proto3";
+
+package com.v2ray.core.common.net;
+option go_package = "net";
+
+enum Network {
+  Unknown = 0;
+  RawTCP = 1;
+  TCP = 2;
+  UDP = 3;
+  KCP = 4;
+  WebSocket = 5;
+}

+ 4 - 4
common/net/network_json_test.go

@@ -16,8 +16,8 @@ func TestArrayNetworkList(t *testing.T) {
 	var list NetworkList
 	err := json.Unmarshal([]byte("[\"Tcp\"]"), &list)
 	assert.Error(err).IsNil()
-	assert.Bool(list.HasNetwork(Network("tcp"))).IsTrue()
-	assert.Bool(list.HasNetwork(Network("udp"))).IsFalse()
+	assert.Bool(list.HasNetwork(ParseNetwork("tcp"))).IsTrue()
+	assert.Bool(list.HasNetwork(ParseNetwork("udp"))).IsFalse()
 }
 
 func TestStringNetworkList(t *testing.T) {
@@ -26,8 +26,8 @@ func TestStringNetworkList(t *testing.T) {
 	var list NetworkList
 	err := json.Unmarshal([]byte("\"TCP, ip\""), &list)
 	assert.Error(err).IsNil()
-	assert.Bool(list.HasNetwork(Network("tcp"))).IsTrue()
-	assert.Bool(list.HasNetwork(Network("udp"))).IsFalse()
+	assert.Bool(list.HasNetwork(ParseNetwork("tcp"))).IsTrue()
+	assert.Bool(list.HasNetwork(ParseNetwork("udp"))).IsFalse()
 }
 
 func TestInvalidNetworkJson(t *testing.T) {

+ 3 - 3
common/net/port.pb.go

@@ -22,15 +22,15 @@ type PortRange struct {
 func (m *PortRange) Reset()                    { *m = PortRange{} }
 func (m *PortRange) String() string            { return proto.CompactTextString(m) }
 func (*PortRange) ProtoMessage()               {}
-func (*PortRange) Descriptor() ([]byte, []int) { return fileDescriptor1, []int{0} }
+func (*PortRange) Descriptor() ([]byte, []int) { return fileDescriptor3, []int{0} }
 
 func init() {
 	proto.RegisterType((*PortRange)(nil), "com.v2ray.core.common.net.PortRange")
 }
 
-func init() { proto.RegisterFile("v2ray.com/core/common/net/port.proto", fileDescriptor1) }
+func init() { proto.RegisterFile("v2ray.com/core/common/net/port.proto", fileDescriptor3) }
 
-var fileDescriptor1 = []byte{
+var fileDescriptor3 = []byte{
 	// 137 bytes of a gzipped FileDescriptorProto
 	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x34, 0xcc, 0xb1, 0x0a, 0xc2, 0x40,
 	0x0c, 0x87, 0x71, 0x7a, 0x56, 0xc1, 0x03, 0x1d, 0x6e, 0xaa, 0x9b, 0x88, 0x83, 0x53, 0x02, 0xfa,

+ 2 - 2
proxy/dokodemo/dokodemo.go

@@ -73,13 +73,13 @@ func (this *DokodemoDoor) Start() error {
 	}
 	this.accepting = true
 
-	if this.config.Network.HasNetwork(v2net.TCPNetwork) {
+	if this.config.Network.HasNetwork(v2net.Network_TCP) {
 		err := this.ListenTCP()
 		if err != nil {
 			return err
 		}
 	}
-	if this.config.Network.HasNetwork(v2net.UDPNetwork) {
+	if this.config.Network.HasNetwork(v2net.Network_UDP) {
 		err := this.ListenUDP()
 		if err != nil {
 			return err

+ 2 - 2
proxy/dokodemo/dokodemo_test.go

@@ -56,7 +56,7 @@ func TestDokodemoTCP(t *testing.T) {
 	dokodemo := NewDokodemoDoor(&Config{
 		Address: v2net.LocalHostIP,
 		Port:    tcpServer.Port,
-		Network: v2net.TCPNetwork.AsList(),
+		Network: v2net.Network_TCP.AsList(),
 		Timeout: 600,
 	}, space, &proxy.InboundHandlerMeta{
 		Address: v2net.LocalHostIP,
@@ -126,7 +126,7 @@ func TestDokodemoUDP(t *testing.T) {
 	dokodemo := NewDokodemoDoor(&Config{
 		Address: v2net.LocalHostIP,
 		Port:    udpServer.Port,
-		Network: v2net.UDPNetwork.AsList(),
+		Network: v2net.Network_UDP.AsList(),
 		Timeout: 600,
 	}, space, &proxy.InboundHandlerMeta{
 		Address: v2net.LocalHostIP,

+ 2 - 2
proxy/freedom/freedom.go

@@ -58,7 +58,7 @@ func (this *FreedomConnection) ResolveIP(destination v2net.Destination) v2net.De
 
 	ip := ips[dice.Roll(len(ips))]
 	var newDest v2net.Destination
-	if destination.Network() == v2net.TCPNetwork {
+	if destination.Network() == v2net.Network_TCP {
 		newDest = v2net.TCPDestination(v2net.IPAddress(ip), destination.Port())
 	} else {
 		newDest = v2net.UDPDestination(v2net.IPAddress(ip), destination.Port())
@@ -112,7 +112,7 @@ func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *
 	var reader io.Reader = conn
 
 	timeout := this.timeout
-	if destination.Network() == v2net.UDPNetwork {
+	if destination.Network() == v2net.Network_UDP {
 		timeout = 16
 	}
 	if timeout > 0 {

+ 1 - 1
proxy/vmess/outbound/outbound.go

@@ -49,7 +49,7 @@ func (this *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *al
 	log.Info("VMess|Outbound: Tunneling request to ", target, " via ", rec.Destination())
 
 	command := protocol.RequestCommandTCP
-	if target.Network() == v2net.UDPNetwork {
+	if target.Network() == v2net.Network_UDP {
 		command = protocol.RequestCommandUDP
 	}
 	request := &protocol.RequestHeader{

+ 4 - 4
testing/assert/destination.go

@@ -20,25 +20,25 @@ type DestinationSubject struct {
 }
 
 func (this *DestinationSubject) IsTCP() {
-	if this.value.Network() != v2net.TCPNetwork {
+	if this.value.Network() != v2net.Network_TCP {
 		this.Fail("is", "a TCP destination")
 	}
 }
 
 func (this *DestinationSubject) IsNotTCP() {
-	if this.value.Network() == v2net.TCPNetwork {
+	if this.value.Network() == v2net.Network_TCP {
 		this.Fail("is not", "a TCP destination")
 	}
 }
 
 func (this *DestinationSubject) IsUDP() {
-	if this.value.Network() != v2net.UDPNetwork {
+	if this.value.Network() != v2net.Network_UDP {
 		this.Fail("is", "a UDP destination")
 	}
 }
 
 func (this *DestinationSubject) IsNotUDP() {
-	if this.value.Network() == v2net.UDPNetwork {
+	if this.value.Network() == v2net.Network_UDP {
 		this.Fail("is not", "a UDP destination")
 	}
 }

+ 3 - 3
transport/internet/connection_json.go

@@ -47,13 +47,13 @@ func (this *StreamSettings) UnmarshalJSON(data []byte) error {
 	if err := json.Unmarshal(data, jsonConfig); err != nil {
 		return err
 	}
-	if jsonConfig.Network.HasNetwork(v2net.KCPNetwork) {
+	if jsonConfig.Network.HasNetwork(v2net.Network_KCP) {
 		this.Type |= StreamConnectionTypeKCP
 	}
-	if jsonConfig.Network.HasNetwork(v2net.WSNetwork) {
+	if jsonConfig.Network.HasNetwork(v2net.Network_WebSocket) {
 		this.Type |= StreamConnectionTypeWebSocket
 	}
-	if jsonConfig.Network.HasNetwork(v2net.TCPNetwork) {
+	if jsonConfig.Network.HasNetwork(v2net.Network_TCP) {
 		this.Type |= StreamConnectionTypeTCP
 	}
 	this.Security = StreamSecurityTypeNone

+ 1 - 1
transport/internet/dialer.go

@@ -27,7 +27,7 @@ func Dial(src v2net.Address, dest v2net.Destination, settings *StreamSettings) (
 
 	var connection Connection
 	var err error
-	if dest.Network() == v2net.TCPNetwork {
+	if dest.Network() == v2net.Network_TCP {
 		switch {
 		case settings.IsCapableOf(StreamConnectionTypeTCP):
 			connection, err = TCPDialer(src, dest)

+ 3 - 3
transport/internet/system_dialer.go

@@ -25,7 +25,7 @@ func (this *DefaultSystemDialer) Dial(src v2net.Address, dest v2net.Destination)
 	}
 	if src != nil && src != v2net.AnyIP {
 		var addr net.Addr
-		if dest.Network() == v2net.TCPNetwork {
+		if dest.Network() == v2net.Network_TCP {
 			addr = &net.TCPAddr{
 				IP:   src.IP(),
 				Port: 0,
@@ -38,7 +38,7 @@ func (this *DefaultSystemDialer) Dial(src v2net.Address, dest v2net.Destination)
 		}
 		dialer.LocalAddr = addr
 	}
-	return dialer.Dial(dest.Network().String(), dest.NetAddr())
+	return dialer.Dial(dest.Network().SystemString(), dest.NetAddr())
 }
 
 type SystemDialerAdapter interface {
@@ -56,7 +56,7 @@ func WithAdapter(dialer SystemDialerAdapter) SystemDialer {
 }
 
 func (this *SimpleSystemDialer) Dial(src v2net.Address, dest v2net.Destination) (net.Conn, error) {
-	return this.adapter.Dial(dest.Network().String(), dest.NetAddr())
+	return this.adapter.Dial(dest.Network().SystemString(), dest.NetAddr())
 }
 
 // UseAlternativeSystemDialer replaces the current system dialer with a given one.

+ 1 - 1
transport/internet/tcp/dialer.go

@@ -19,7 +19,7 @@ func Dial(src v2net.Address, dest v2net.Destination) (internet.Connection, error
 	}
 	id := src.String() + "-" + dest.NetAddr()
 	var conn net.Conn
-	if dest.Network() == v2net.TCPNetwork && effectiveConfig.ConnectionReuse {
+	if dest.Network() == v2net.Network_TCP && effectiveConfig.ConnectionReuse {
 		conn = globalCache.Get(id)
 	}
 	if conn == nil {

+ 1 - 1
transport/internet/ws/dialer.go

@@ -23,7 +23,7 @@ func Dial(src v2net.Address, dest v2net.Destination) (internet.Connection, error
 	}
 	id := src.String() + "-" + dest.NetAddr()
 	var conn *wsconn
-	if dest.Network() == v2net.TCPNetwork && effectiveConfig.ConnectionReuse {
+	if dest.Network() == v2net.Network_TCP && effectiveConfig.ConnectionReuse {
 		connt := globalCache.Get(id)
 		if connt != nil {
 			conn = connt.(*wsconn)