separate network and transport protocol

This commit is contained in:
Darien Raymond committed 2017-01-12 12:54:34 +01:00
1 parent f9b3a1446a
commit d24fe9dbfb
41 files changed
+261 -321

No files matched your search

+3 -3
View File
@@ -5,11 +5,11 @@ import (
)
// Apply applies this Config.
func (v *Config) Apply() error {
if v == nil {
func (c *Config) Apply() error {
if c == nil {
return nil
}
if err := internet.ApplyGlobalNetworkSettings(v.NetworkSettings); err != nil {
if err := internet.ApplyGlobalTransportSettings(c.TransportSettings); err != nil {
return err
}
return nil
+11 -12
View File
@@ -18,7 +18,7 @@ const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
// Global transport settings. This affects all type of connections that go through V2Ray.
type Config struct {
NetworkSettings []*v2ray_core_transport_internet.NetworkSettings `protobuf:"bytes,1,rep,name=network_settings,json=networkSettings" json:"network_settings,omitempty"`
TransportSettings []*v2ray_core_transport_internet.TransportSettings `protobuf:"bytes,1,rep,name=transport_settings,json=transportSettings" json:"transport_settings,omitempty"`
}
func (m *Config) Reset() { *m = Config{} }
@@ -26,9 +26,9 @@ func (m *Config) String() string { return proto.CompactTextString(m)
func (*Config) ProtoMessage() {}
func (*Config) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
func (m *Config) GetNetworkSettings() []*v2ray_core_transport_internet.NetworkSettings {
func (m *Config) GetTransportSettings() []*v2ray_core_transport_internet.TransportSettings {
if m != nil {
return m.NetworkSettings
return m.TransportSettings
}
return nil
}
@@ -40,17 +40,16 @@ func init() {
func init() { proto.RegisterFile("v2ray.com/core/transport/config.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 180 bytes of a gzipped FileDescriptorProto
// 174 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x52, 0x2d, 0x33, 0x2a, 0x4a,
0xac, 0xd4, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xce, 0x2f, 0x4a, 0xd5, 0x2f, 0x29,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x83, 0xdb, 0xc8, 0x40, 0x0f, 0x9b, 0x65, 0x7a, 0x30, 0x23, 0xf5, 0x42, 0x60, 0x42,
0xc1, 0x50, 0x7d, 0x41, 0x82, 0x25, 0xe8, 0x42, 0x4e, 0x9e, 0x5c, 0x12, 0xc9, 0xf9, 0xb9, 0x58,
0x4d, 0x72, 0xe2, 0x86, 0x38, 0x22, 0x00, 0xe4, 0xa6, 0x28, 0x4e, 0xb8, 0xf8, 0x2a, 0x26, 0x91,
0x30, 0xa3, 0xa0, 0xc4, 0x4a, 0x3d, 0x67, 0x90, 0x72, 0xb8, 0x2d, 0x49, 0x6c, 0x60, 0xc7, 0x1b,
0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x4b, 0xd3, 0xcc, 0x90, 0x2b, 0x01, 0x00, 0x00,
}
+1 -1
View File
@@ -10,5 +10,5 @@ import "v2ray.com/core/transport/internet/config.proto";
// Global transport settings. This affects all type of connections that go through V2Ray.
message Config {
repeated v2ray.core.transport.internet.NetworkSettings network_settings = 1;
repeated v2ray.core.transport.internet.TransportSettings transport_settings = 1;
}
+31 -25
View File
@@ -2,52 +2,58 @@ package internet
import (
"v2ray.com/core/common/errors"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/serial"
)
type ConfigCreator func() interface{}
var (
globalNetworkConfigCreatorCache = make(map[v2net.Network]ConfigCreator)
globalNetworkSettings []*NetworkSettings
ErrUnconfiguredNetwork = errors.New("Network config creator not set.")
globalTransportConfigCreatorCache = make(map[TransportProtocol]ConfigCreator)
globalTransportSettings []*TransportSettings
)
func RegisterNetworkConfigCreator(network v2net.Network, creator ConfigCreator) error {
func RegisterProtocolConfigCreator(protocol TransportProtocol, creator ConfigCreator) error {
// TODO: check duplicate
globalNetworkConfigCreatorCache[network] = creator
globalTransportConfigCreatorCache[protocol] = creator
return nil
}
func CreateNetworkConfig(network v2net.Network) (interface{}, error) {
creator, ok := globalNetworkConfigCreatorCache[network]
func CreateTransportConfig(protocol TransportProtocol) (interface{}, error) {
creator, ok := globalTransportConfigCreatorCache[protocol]
if !ok {
log.Warning("Internet: Network config creator not found: ", network)
return nil, ErrUnconfiguredNetwork
return nil, errors.New("Internet: Unknown transport protocol: ", protocol)
}
return creator(), nil
}
func (v *NetworkSettings) GetTypedSettings() (interface{}, error) {
func (v *TransportSettings) GetTypedSettings() (interface{}, error) {
return v.Settings.GetInstance()
}
func (v *StreamConfig) GetEffectiveNetworkSettings() (interface{}, error) {
for _, settings := range v.NetworkSettings {
if settings.Network == v.Network {
func (v *StreamConfig) GetEffectiveProtocol() TransportProtocol {
if v == nil {
return TransportProtocol_TCP
}
return v.Protocol
}
func (v *StreamConfig) GetEffectiveTransportSettings() (interface{}, error) {
protocol := v.GetEffectiveProtocol()
if v != nil {
for _, settings := range v.TransportSettings {
if settings.Protocol == protocol {
return settings.GetTypedSettings()
}
}
}
for _, settings := range globalTransportSettings {
if settings.Protocol == protocol {
return settings.GetTypedSettings()
}
}
for _, settings := range globalNetworkSettings {
if settings.Network == v.Network {
return settings.GetTypedSettings()
}
}
return CreateNetworkConfig(v.Network)
return CreateTransportConfig(protocol)
}
func (v *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
@@ -63,8 +69,8 @@ func (v *StreamConfig) HasSecuritySettings() bool {
return len(v.SecurityType) > 0
}
func ApplyGlobalNetworkSettings(settings []*NetworkSettings) error {
globalNetworkSettings = settings
func ApplyGlobalTransportSettings(settings []*TransportSettings) error {
globalTransportSettings = settings
return nil
}
+71 -42
View File
@@ -3,7 +3,6 @@ package internet
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import v2ray_core_common_net "v2ray.com/core/common/net"
import v2ray_core_common_serial "v2ray.com/core/common/serial"
// Reference imports to suppress errors if they are not otherwise used.
@@ -17,26 +16,53 @@ var _ = math.Inf
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type NetworkSettings struct {
type TransportProtocol int32
const (
TransportProtocol_TCP TransportProtocol = 0
TransportProtocol_UDP TransportProtocol = 1
TransportProtocol_MKCP TransportProtocol = 2
TransportProtocol_WebSocket TransportProtocol = 3
)
var TransportProtocol_name = map[int32]string{
0: "TCP",
1: "UDP",
2: "MKCP",
3: "WebSocket",
}
var TransportProtocol_value = map[string]int32{
"TCP": 0,
"UDP": 1,
"MKCP": 2,
"WebSocket": 3,
}
func (x TransportProtocol) String() string {
return proto.EnumName(TransportProtocol_name, int32(x))
}
func (TransportProtocol) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
type TransportSettings struct {
// Type of network that this settings supports.
Network v2ray_core_common_net.Network `protobuf:"varint,1,opt,name=network,enum=v2ray.core.common.net.Network" json:"network,omitempty"`
Protocol TransportProtocol `protobuf:"varint,1,opt,name=protocol,enum=v2ray.core.transport.internet.TransportProtocol" json:"protocol,omitempty"`
// Specific settings.
Settings *v2ray_core_common_serial.TypedMessage `protobuf:"bytes,2,opt,name=settings" json:"settings,omitempty"`
}
func (m *NetworkSettings) Reset() { *m = NetworkSettings{} }
func (m *NetworkSettings) String() string { return proto.CompactTextString(m) }
func (*NetworkSettings) ProtoMessage() {}
func (*NetworkSettings) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
func (m *TransportSettings) Reset() { *m = TransportSettings{} }
func (m *TransportSettings) String() string { return proto.CompactTextString(m) }
func (*TransportSettings) ProtoMessage() {}
func (*TransportSettings) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
func (m *NetworkSettings) GetNetwork() v2ray_core_common_net.Network {
func (m *TransportSettings) GetProtocol() TransportProtocol {
if m != nil {
return m.Network
return m.Protocol
}
return v2ray_core_common_net.Network_Unknown
return TransportProtocol_TCP
}
func (m *NetworkSettings) GetSettings() *v2ray_core_common_serial.TypedMessage {
func (m *TransportSettings) GetSettings() *v2ray_core_common_serial.TypedMessage {
if m != nil {
return m.Settings
}
@@ -45,8 +71,8 @@ func (m *NetworkSettings) GetSettings() *v2ray_core_common_serial.TypedMessage {
type StreamConfig struct {
// Effective network.
Network v2ray_core_common_net.Network `protobuf:"varint,1,opt,name=network,enum=v2ray.core.common.net.Network" json:"network,omitempty"`
NetworkSettings []*NetworkSettings `protobuf:"bytes,2,rep,name=network_settings,json=networkSettings" json:"network_settings,omitempty"`
Protocol TransportProtocol `protobuf:"varint,1,opt,name=protocol,enum=v2ray.core.transport.internet.TransportProtocol" json:"protocol,omitempty"`
TransportSettings []*TransportSettings `protobuf:"bytes,2,rep,name=transport_settings,json=transportSettings" json:"transport_settings,omitempty"`
// Type of security. Must be a message name of the settings proto.
SecurityType string `protobuf:"bytes,3,opt,name=security_type,json=securityType" json:"security_type,omitempty"`
SecuritySettings []*v2ray_core_common_serial.TypedMessage `protobuf:"bytes,4,rep,name=security_settings,json=securitySettings" json:"security_settings,omitempty"`
@@ -57,16 +83,16 @@ func (m *StreamConfig) String() string { return proto.CompactTextStri
func (*StreamConfig) ProtoMessage() {}
func (*StreamConfig) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
func (m *StreamConfig) GetNetwork() v2ray_core_common_net.Network {
func (m *StreamConfig) GetProtocol() TransportProtocol {
if m != nil {
return m.Network
return m.Protocol
}
return v2ray_core_common_net.Network_Unknown
return TransportProtocol_TCP
}
func (m *StreamConfig) GetNetworkSettings() []*NetworkSettings {
func (m *StreamConfig) GetTransportSettings() []*TransportSettings {
if m != nil {
return m.NetworkSettings
return m.TransportSettings
}
return nil
}
@@ -102,35 +128,38 @@ func (m *ProxyConfig) GetTag() string {
}
func init() {
proto.RegisterType((*NetworkSettings)(nil), "v2ray.core.transport.internet.NetworkSettings")
proto.RegisterType((*TransportSettings)(nil), "v2ray.core.transport.internet.TransportSettings")
proto.RegisterType((*StreamConfig)(nil), "v2ray.core.transport.internet.StreamConfig")
proto.RegisterType((*ProxyConfig)(nil), "v2ray.core.transport.internet.ProxyConfig")
proto.RegisterEnum("v2ray.core.transport.internet.TransportProtocol", TransportProtocol_name, TransportProtocol_value)
}
func init() { proto.RegisterFile("v2ray.com/core/transport/internet/config.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 344 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xa4, 0x92, 0xcf, 0x4a, 0xc3, 0x40,
0x10, 0xc6, 0x49, 0x2b, 0xda, 0x6e, 0xab, 0xad, 0x39, 0x95, 0x42, 0xb5, 0x56, 0xd0, 0x9c, 0x26,
0x12, 0x2f, 0x9e, 0xdb, 0x93, 0x87, 0x8a, 0xa4, 0x45, 0xd0, 0x4b, 0x59, 0xe3, 0x18, 0x82, 0x66,
0xb7, 0xec, 0x8e, 0x7f, 0xf2, 0x14, 0x3e, 0x81, 0x2f, 0xe0, 0x53, 0x4a, 0x92, 0xdd, 0x50, 0x4a,
0x29, 0x82, 0xb7, 0x21, 0xf9, 0xe6, 0xcb, 0x6f, 0x7e, 0x84, 0xc1, 0x7b, 0xa0, 0x78, 0x06, 0x91,
0x4c, 0xfd, 0x48, 0x2a, 0xf4, 0x49, 0x71, 0xa1, 0x97, 0x52, 0x91, 0x9f, 0x08, 0x42, 0x25, 0x90,
0xfc, 0x48, 0x8a, 0xe7, 0x24, 0x86, 0xa5, 0x92, 0x24, 0xdd, 0x81, 0xcd, 0x2b, 0x84, 0x2a, 0x0b,
0x36, 0xdb, 0x3f, 0x5f, 0xab, 0x8b, 0x64, 0x9a, 0x4a, 0xe1, 0xe7, 0x35, 0x02, 0xe9, 0x43, 0xaa,
0x97, 0xb2, 0xa7, 0x7f, 0xb1, 0x39, 0xa8, 0x51, 0x25, 0xfc, 0xd5, 0xa7, 0x6c, 0x89, 0x4f, 0x8b,
0x14, 0xb5, 0xe6, 0x31, 0x96, 0x1b, 0xa3, 0x2f, 0x87, 0x75, 0x6e, 0xca, 0x8e, 0x19, 0x12, 0x25,
0x22, 0xd6, 0xee, 0x15, 0xdb, 0x33, 0xb5, 0x3d, 0x67, 0xe8, 0x78, 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_namespace = "V2Ray.Core.Transport.Internet";
option go_package = "internet";
option java_package = "com.v2ray.core.transport.internet";
import "v2ray.com/core/common/net/network.proto";
import "v2ray.com/core/common/serial/typed_message.proto";
message NetworkSettings {
enum TransportProtocol {
TCP = 0;
UDP = 1;
MKCP = 2;
WebSocket = 3;
}
message TransportSettings {
// Type of network that this settings supports.
v2ray.core.common.net.Network network = 1;
TransportProtocol protocol = 1;
// Specific settings.
v2ray.core.common.serial.TypedMessage settings = 2;
@@ -18,9 +24,9 @@ message NetworkSettings {
message StreamConfig {
// Effective network.
v2ray.core.common.net.Network network = 1;
TransportProtocol protocol = 1;
repeated NetworkSettings network_settings = 2;
repeated TransportSettings transport_settings = 2;
// Type of security. Must be a message name of the settings proto.
string security_type = 3;
@@ -30,4 +36,4 @@ message StreamConfig {
message ProxyConfig {
string tag = 1;
}
}
+9 -12
View File
@@ -8,10 +8,6 @@ import (
v2net "v2ray.com/core/common/net"
)
var (
ErrUnsupportedStreamType = errors.New("Unsupported stream type.")
)
type DialerOptions struct {
Stream *StreamConfig
Proxy *ProxyConfig
@@ -20,16 +16,16 @@ type DialerOptions struct {
type Dialer func(src v2net.Address, dest v2net.Destination, options DialerOptions) (Connection, error)
var (
networkDialerCache = make(map[v2net.Network]Dialer)
transportDialerCache = make(map[TransportProtocol]Dialer)
ProxyDialer Dialer
)
func RegisterNetworkDialer(network v2net.Network, dialer Dialer) error {
if _, found := networkDialerCache[network]; found {
return errors.New("Internet|Dialer: ", network, " dialer already registered.")
func RegisterTransportDialer(protocol TransportProtocol, dialer Dialer) error {
if _, found := transportDialerCache[protocol]; found {
return errors.New("Internet|Dialer: ", protocol, " dialer already registered.")
}
networkDialerCache[network] = dialer
transportDialerCache[protocol] = dialer
return nil
}
@@ -40,14 +36,15 @@ func Dial(src v2net.Address, dest v2net.Destination, options DialerOptions) (Con
}
if dest.Network == v2net.Network_TCP {
dialer := networkDialerCache[options.Stream.Network]
protocol := options.Stream.GetEffectiveProtocol()
dialer := transportDialerCache[protocol]
if dialer == nil {
return nil, errors.New("Internet|Dialer: ", options.Stream.Network, " dialer not registered.")
return nil, errors.New("Internet|Dialer: ", options.Stream.Protocol, " dialer not registered.")
}
return dialer(src, dest, options)
}
udpDialer := networkDialerCache[v2net.Network_UDP]
udpDialer := transportDialerCache[TransportProtocol_UDP]
if udpDialer == nil {
return nil, errors.New("Internet|Dialer: UDP dialer not registered.")
}
+3 -3
View File
@@ -3,7 +3,7 @@ package kcp
import (
"crypto/cipher"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common"
"v2ray.com/core/transport/internet"
)
@@ -104,7 +104,7 @@ func (v *Config) IsConnectionReuse() bool {
}
func init() {
internet.RegisterNetworkConfigCreator(v2net.Network_KCP, func() interface{} {
common.Must(internet.RegisterProtocolConfigCreator(internet.TransportProtocol_MKCP, func() interface{} {
return new(Config)
})
}))
}
+2 -2
View File
@@ -129,7 +129,7 @@ func DialKCP(src v2net.Address, dest v2net.Destination, options internet.DialerO
conn = c
}
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
networkSettings, err := options.Stream.GetEffectiveTransportSettings()
if err != nil {
log.Error("KCP|Dialer: Failed to get KCP settings: ", err)
return nil, err
@@ -173,5 +173,5 @@ func DialKCP(src v2net.Address, dest v2net.Destination, options internet.DialerO
}
func init() {
common.Must(internet.RegisterNetworkDialer(v2net.Network_KCP, DialKCP))
common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_MKCP, DialKCP))
}
+6 -6
View File
@@ -20,10 +20,10 @@ func TestDialAndListen(t *testing.T) {
listerner, err := NewListener(v2net.LocalHostIP, v2net.Port(0), internet.ListenOptions{
Stream: &internet.StreamConfig{
Network: v2net.Network_KCP,
NetworkSettings: []*internet.NetworkSettings{
Protocol: internet.TransportProtocol_MKCP,
TransportSettings: []*internet.TransportSettings{
{
Network: v2net.Network_KCP,
Protocol: internet.TransportProtocol_MKCP,
Settings: serial.ToTypedMessage(&Config{}),
},
},
@@ -59,10 +59,10 @@ func TestDialAndListen(t *testing.T) {
for i := 0; i < 10; i++ {
clientConn, err := DialKCP(v2net.LocalHostIP, v2net.UDPDestination(v2net.LocalHostIP, port), internet.DialerOptions{
Stream: &internet.StreamConfig{
Network: v2net.Network_KCP,
NetworkSettings: []*internet.NetworkSettings{
Protocol: internet.TransportProtocol_MKCP,
TransportSettings: []*internet.TransportSettings{
{
Network: v2net.Network_KCP,
Protocol: internet.TransportProtocol_MKCP,
Settings: serial.ToTypedMessage(&Config{}),
},
},
+2 -2
View File
@@ -93,7 +93,7 @@ type Listener struct {
}
func NewListener(address v2net.Address, port v2net.Port, options internet.ListenOptions) (*Listener, error) {
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
networkSettings, err := options.Stream.GetEffectiveTransportSettings()
if err != nil {
log.Error("KCP|Listener: Failed to get KCP settings: ", err)
return nil, err
@@ -298,5 +298,5 @@ func ListenKCP(address v2net.Address, port v2net.Port, options internet.ListenOp
}
func init() {
common.Must(internet.RegisterNetworkListener(v2net.Network_KCP, ListenKCP))
common.Must(internet.RegisterTransportListener(internet.TransportProtocol_MKCP, ListenKCP))
}
+3 -3
View File
@@ -1,7 +1,7 @@
package tcp
import (
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common"
"v2ray.com/core/transport/internet"
)
@@ -13,7 +13,7 @@ func (v *Config) IsConnectionReuse() bool {
}
func init() {
internet.RegisterNetworkConfigCreator(v2net.Network_TCP, func() interface{} {
common.Must(internet.RegisterProtocolConfigCreator(internet.TransportProtocol_TCP, func() interface{} {
return new(Config)
})
}))
}
+2 -2
View File
@@ -22,7 +22,7 @@ func Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOpti
if src == nil {
src = v2net.AnyIP
}
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
networkSettings, err := options.Stream.GetEffectiveTransportSettings()
if err != nil {
return nil, err
}
@@ -70,5 +70,5 @@ func Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOpti
}
func init() {
common.Must(internet.RegisterNetworkDialer(v2net.Network_TCP, Dial))
common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_TCP, Dial))
}
+2 -2
View File
@@ -42,7 +42,7 @@ func ListenTCP(address v2net.Address, port v2net.Port, options internet.ListenOp
if err != nil {
return nil, err
}
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
networkSettings, err := options.Stream.GetEffectiveTransportSettings()
if err != nil {
return nil, err
}
@@ -159,5 +159,5 @@ func (v *TCPListener) Close() error {
}
func init() {
common.Must(internet.RegisterNetworkListener(v2net.Network_TCP, ListenTCP))
common.Must(internet.RegisterTransportListener(internet.TransportProtocol_TCP, ListenTCP))
}
+9 -10
View File
@@ -11,16 +11,14 @@ import (
)
var (
ErrClosedConnection = errors.New("Connection already closed.")
networkListenerCache = make(map[v2net.Network]ListenFunc)
transportListenerCache = make(map[TransportProtocol]ListenFunc)
)
func RegisterNetworkListener(network v2net.Network, listener ListenFunc) error {
if _, found := networkListenerCache[network]; found {
return errors.New("Internet|TCPHub: ", network, " listener already registered.")
func RegisterTransportListener(protocol TransportProtocol, listener ListenFunc) error {
if _, found := transportListenerCache[protocol]; found {
return errors.New("Internet|TCPHub: ", protocol, " listener already registered.")
}
networkListenerCache[network] = listener
transportListenerCache[protocol] = listener
return nil
}
@@ -46,13 +44,14 @@ func ListenTCP(address v2net.Address, port v2net.Port, callback ConnectionHandle
options := ListenOptions{
Stream: settings,
}
listenFunc := networkListenerCache[settings.Network]
protocol := settings.GetEffectiveProtocol()
listenFunc := transportListenerCache[protocol]
if listenFunc == nil {
return nil, errors.New("Internet|TCPHub: ", settings.Network, " listener not registered.")
return nil, errors.New("Internet|TCPHub: ", protocol, " listener not registered.")
}
listener, err := listenFunc(address, port, options)
if err != nil {
return nil, errors.Base(err).Message("Interent|TCPHub: Failed to listen: ")
return nil, errors.Base(err).Message("Interent|TCPHub: Failed to listen on address: ", address, ":", port)
}
hub := &TCPHub{
+1 -1
View File
@@ -8,7 +8,7 @@ import (
)
func init() {
common.Must(internet.RegisterNetworkDialer(v2net.Network_UDP,
common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_UDP,
func(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
conn, err := internet.DialSystem(src, dest)
if err != nil {
+3 -3
View File
@@ -1,7 +1,7 @@
package websocket
import (
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common"
"v2ray.com/core/transport/internet"
)
@@ -13,7 +13,7 @@ func (c *Config) IsConnectionReuse() bool {
}
func init() {
internet.RegisterNetworkConfigCreator(v2net.Network_WebSocket, func() interface{} {
common.Must(internet.RegisterProtocolConfigCreator(internet.TransportProtocol_WebSocket, func() interface{} {
return new(Config)
})
}))
}
+3 -3
View File
@@ -22,7 +22,7 @@ func Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOpti
if src == nil {
src = v2net.AnyIP
}
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
networkSettings, err := options.Stream.GetEffectiveTransportSettings()
if err != nil {
return nil, err
}
@@ -48,11 +48,11 @@ func Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOpti
}
func init() {
common.Must(internet.RegisterNetworkDialer(v2net.Network_WebSocket, Dial))
common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_WebSocket, Dial))
}
func wsDial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (*wsconn, error) {
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
networkSettings, err := options.Stream.GetEffectiveTransportSettings()
if err != nil {
return nil, err
}
+2 -2
View File
@@ -38,7 +38,7 @@ type WSListener struct {
}
func ListenWS(address v2net.Address, port v2net.Port, options internet.ListenOptions) (internet.Listener, error) {
networkSettings, err := options.Stream.GetEffectiveNetworkSettings()
networkSettings, err := options.Stream.GetEffectiveTransportSettings()
if err != nil {
return nil, err
}
@@ -199,5 +199,5 @@ func (v *WSListener) Close() error {
}
func init() {
common.Must(internet.RegisterNetworkListener(v2net.Network_WebSocket, ListenWS))
common.Must(internet.RegisterTransportListener(internet.TransportProtocol_WebSocket, ListenWS))
}
+18 -18
View File
@@ -21,10 +21,10 @@ func Test_listenWSAndDial(t *testing.T) {
assert := assert.On(t)
listen, err := ListenWS(v2net.DomainAddress("localhost"), 13146, internet.ListenOptions{
Stream: &internet.StreamConfig{
Network: v2net.Network_WebSocket,
NetworkSettings: []*internet.NetworkSettings{
Protocol: internet.TransportProtocol_WebSocket,
TransportSettings: []*internet.TransportSettings{
{
Network: v2net.Network_WebSocket,
Protocol: internet.TransportProtocol_WebSocket,
Settings: serial.ToTypedMessage(&Config{
Path: "ws",
}),
@@ -58,10 +58,10 @@ func Test_listenWSAndDial(t *testing.T) {
}()
conn, err := Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13146), internet.DialerOptions{
Stream: &internet.StreamConfig{
Network: v2net.Network_WebSocket,
NetworkSettings: []*internet.NetworkSettings{
Protocol: internet.TransportProtocol_WebSocket,
TransportSettings: []*internet.TransportSettings{
{
Network: v2net.Network_WebSocket,
Protocol: internet.TransportProtocol_WebSocket,
Settings: serial.ToTypedMessage(&Config{
Path: "ws",
}),
@@ -82,10 +82,10 @@ func Test_listenWSAndDial(t *testing.T) {
<-time.After(time.Second * 5)
conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13146), internet.DialerOptions{
Stream: &internet.StreamConfig{
Network: v2net.Network_WebSocket,
NetworkSettings: []*internet.NetworkSettings{
Protocol: internet.TransportProtocol_WebSocket,
TransportSettings: []*internet.TransportSettings{
{
Network: v2net.Network_WebSocket,
Protocol: internet.TransportProtocol_WebSocket,
Settings: serial.ToTypedMessage(&Config{
Path: "ws",
}),
@@ -103,10 +103,10 @@ func Test_listenWSAndDial(t *testing.T) {
<-time.After(time.Second * 15)
conn, err = Dial(v2net.AnyIP, v2net.TCPDestination(v2net.DomainAddress("localhost"), 13146), internet.DialerOptions{
Stream: &internet.StreamConfig{
Network: v2net.Network_WebSocket,
NetworkSettings: []*internet.NetworkSettings{
Protocol: internet.TransportProtocol_WebSocket,
TransportSettings: []*internet.TransportSettings{
{
Network: v2net.Network_WebSocket,
Protocol: internet.TransportProtocol_WebSocket,
Settings: serial.ToTypedMessage(&Config{
Path: "ws",
}),
@@ -145,10 +145,10 @@ func Test_listenWSAndDial_TLS(t *testing.T) {
Stream: &internet.StreamConfig{
SecurityType: serial.GetMessageType(new(v2tls.Config)),
SecuritySettings: []*serial.TypedMessage{serial.ToTypedMessage(tlsSettings)},
Network: v2net.Network_WebSocket,
NetworkSettings: []*internet.NetworkSettings{
Protocol: internet.TransportProtocol_WebSocket,
TransportSettings: []*internet.TransportSettings{
{
Network: v2net.Network_WebSocket,
Protocol: internet.TransportProtocol_WebSocket,
Settings: serial.ToTypedMessage(&Config{
Path: "wss",
ConnectionReuse: &ConnectionReuse{
@@ -170,10 +170,10 @@ func Test_listenWSAndDial_TLS(t *testing.T) {
Stream: &internet.StreamConfig{
SecurityType: serial.GetMessageType(new(v2tls.Config)),
SecuritySettings: []*serial.TypedMessage{serial.ToTypedMessage(tlsSettings)},
Network: v2net.Network_WebSocket,
NetworkSettings: []*internet.NetworkSettings{
Protocol: internet.TransportProtocol_WebSocket,
TransportSettings: []*internet.TransportSettings{
{
Network: v2net.Network_WebSocket,
Protocol: internet.TransportProtocol_WebSocket,
Settings: serial.ToTypedMessage(&Config{
Path: "wss",
ConnectionReuse: &ConnectionReuse{