mirror of https://github.com/v2ray/v2ray-core
MemoryStreamSettings
parent
49a5f23c0c
commit
b3847fb7c0
|
@ -11,6 +11,7 @@ import (
|
||||||
"v2ray.com/core/common/net"
|
"v2ray.com/core/common/net"
|
||||||
"v2ray.com/core/common/serial"
|
"v2ray.com/core/common/serial"
|
||||||
"v2ray.com/core/proxy"
|
"v2ray.com/core/proxy"
|
||||||
|
"v2ray.com/core/transport/internet"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getStatCounter(v *core.Instance, tag string) (core.StatCounter, core.StatCounter) {
|
func getStatCounter(v *core.Instance, tag string) (core.StatCounter, core.StatCounter) {
|
||||||
|
@ -71,11 +72,16 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *
|
||||||
for port := pr.From; port <= pr.To; port++ {
|
for port := pr.From; port <= pr.To; port++ {
|
||||||
if nl.HasNetwork(net.Network_TCP) {
|
if nl.HasNetwork(net.Network_TCP) {
|
||||||
newError("creating stream worker on ", address, ":", port).AtDebug().WriteToLog()
|
newError("creating stream worker on ", address, ":", port).AtDebug().WriteToLog()
|
||||||
|
mss, err := internet.ToMemoryStreamConfig(receiverConfig.StreamSettings)
|
||||||
|
if err != nil {
|
||||||
|
return nil, newError("failed to parse stream config").Base(err).AtWarning()
|
||||||
|
}
|
||||||
|
|
||||||
worker := &tcpWorker{
|
worker := &tcpWorker{
|
||||||
address: address,
|
address: address,
|
||||||
port: net.Port(port),
|
port: net.Port(port),
|
||||||
proxy: p,
|
proxy: p,
|
||||||
stream: receiverConfig.StreamSettings,
|
stream: mss,
|
||||||
recvOrigDest: receiverConfig.ReceiveOriginalDestination,
|
recvOrigDest: receiverConfig.ReceiveOriginalDestination,
|
||||||
tag: tag,
|
tag: tag,
|
||||||
dispatcher: h.mux,
|
dispatcher: h.mux,
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"v2ray.com/core/common/net"
|
"v2ray.com/core/common/net"
|
||||||
"v2ray.com/core/common/task"
|
"v2ray.com/core/common/task"
|
||||||
"v2ray.com/core/proxy"
|
"v2ray.com/core/proxy"
|
||||||
|
"v2ray.com/core/transport/internet"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DynamicInboundHandler struct {
|
type DynamicInboundHandler struct {
|
||||||
|
@ -19,6 +20,7 @@ type DynamicInboundHandler struct {
|
||||||
v *core.Instance
|
v *core.Instance
|
||||||
proxyConfig interface{}
|
proxyConfig interface{}
|
||||||
receiverConfig *proxyman.ReceiverConfig
|
receiverConfig *proxyman.ReceiverConfig
|
||||||
|
streamSettings *internet.MemoryStreamConfig
|
||||||
portMutex sync.Mutex
|
portMutex sync.Mutex
|
||||||
portsInUse map[net.Port]bool
|
portsInUse map[net.Port]bool
|
||||||
workerMutex sync.RWMutex
|
workerMutex sync.RWMutex
|
||||||
|
@ -39,6 +41,13 @@ func NewDynamicInboundHandler(ctx context.Context, tag string, receiverConfig *p
|
||||||
v: v,
|
v: v,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mss, err := internet.ToMemoryStreamConfig(receiverConfig.StreamSettings)
|
||||||
|
if err != nil {
|
||||||
|
return nil, newError("failed to parse stream settings").Base(err).AtWarning()
|
||||||
|
}
|
||||||
|
|
||||||
|
h.streamSettings = mss
|
||||||
|
|
||||||
h.task = &task.Periodic{
|
h.task = &task.Periodic{
|
||||||
Interval: time.Minute * time.Duration(h.receiverConfig.AllocationStrategy.GetRefreshValue()),
|
Interval: time.Minute * time.Duration(h.receiverConfig.AllocationStrategy.GetRefreshValue()),
|
||||||
Execute: h.refresh,
|
Execute: h.refresh,
|
||||||
|
@ -110,7 +119,7 @@ func (h *DynamicInboundHandler) refresh() error {
|
||||||
address: address,
|
address: address,
|
||||||
port: port,
|
port: port,
|
||||||
proxy: p,
|
proxy: p,
|
||||||
stream: h.receiverConfig.StreamSettings,
|
stream: h.streamSettings,
|
||||||
recvOrigDest: h.receiverConfig.ReceiveOriginalDestination,
|
recvOrigDest: h.receiverConfig.ReceiveOriginalDestination,
|
||||||
dispatcher: h.mux,
|
dispatcher: h.mux,
|
||||||
sniffingConfig: h.receiverConfig.GetEffectiveSniffingSettings(),
|
sniffingConfig: h.receiverConfig.GetEffectiveSniffingSettings(),
|
||||||
|
|
|
@ -33,7 +33,7 @@ type tcpWorker struct {
|
||||||
address net.Address
|
address net.Address
|
||||||
port net.Port
|
port net.Port
|
||||||
proxy proxy.Inbound
|
proxy proxy.Inbound
|
||||||
stream *internet.StreamConfig
|
stream *internet.MemoryStreamConfig
|
||||||
recvOrigDest bool
|
recvOrigDest bool
|
||||||
tag string
|
tag string
|
||||||
dispatcher core.Dispatcher
|
dispatcher core.Dispatcher
|
||||||
|
|
|
@ -17,6 +17,7 @@ import (
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
config *core.OutboundHandlerConfig
|
config *core.OutboundHandlerConfig
|
||||||
senderSettings *proxyman.SenderConfig
|
senderSettings *proxyman.SenderConfig
|
||||||
|
streamSettings *internet.MemoryStreamConfig
|
||||||
proxy proxy.Outbound
|
proxy proxy.Outbound
|
||||||
outboundManager core.OutboundHandlerManager
|
outboundManager core.OutboundHandlerManager
|
||||||
mux *mux.ClientManager
|
mux *mux.ClientManager
|
||||||
|
@ -37,6 +38,11 @@ func NewHandler(ctx context.Context, config *core.OutboundHandlerConfig) (core.O
|
||||||
switch s := senderSettings.(type) {
|
switch s := senderSettings.(type) {
|
||||||
case *proxyman.SenderConfig:
|
case *proxyman.SenderConfig:
|
||||||
h.senderSettings = s
|
h.senderSettings = s
|
||||||
|
mss, err := internet.ToMemoryStreamConfig(s.StreamSettings)
|
||||||
|
if err != nil {
|
||||||
|
return nil, newError("failed to parse stream settings").Base(err).AtWarning()
|
||||||
|
}
|
||||||
|
h.streamSettings = mss
|
||||||
default:
|
default:
|
||||||
return nil, newError("settings is not SenderConfig")
|
return nil, newError("settings is not SenderConfig")
|
||||||
}
|
}
|
||||||
|
@ -118,9 +124,7 @@ func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Conn
|
||||||
ctx = internet.ContextWithDialerSource(ctx, h.senderSettings.Via.AsAddress())
|
ctx = internet.ContextWithDialerSource(ctx, h.senderSettings.Via.AsAddress())
|
||||||
}
|
}
|
||||||
|
|
||||||
if h.senderSettings.StreamSettings != nil {
|
ctx = internet.ContextWithStreamSettings(ctx, h.streamSettings)
|
||||||
ctx = internet.ContextWithStreamSettings(ctx, h.senderSettings.StreamSettings)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return internet.Dial(ctx, dest)
|
return internet.Dial(ctx, dest)
|
||||||
|
|
|
@ -15,16 +15,16 @@ const (
|
||||||
securitySettingsKey
|
securitySettingsKey
|
||||||
)
|
)
|
||||||
|
|
||||||
func ContextWithStreamSettings(ctx context.Context, streamSettings *StreamConfig) context.Context {
|
func ContextWithStreamSettings(ctx context.Context, streamSettings *MemoryStreamConfig) context.Context {
|
||||||
return context.WithValue(ctx, streamSettingsKey, streamSettings)
|
return context.WithValue(ctx, streamSettingsKey, streamSettings)
|
||||||
}
|
}
|
||||||
|
|
||||||
func StreamSettingsFromContext(ctx context.Context) *StreamConfig {
|
func StreamSettingsFromContext(ctx context.Context) *MemoryStreamConfig {
|
||||||
ss := ctx.Value(streamSettingsKey)
|
ss := ctx.Value(streamSettingsKey)
|
||||||
if ss == nil {
|
if ss == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return ss.(*StreamConfig)
|
return ss.(*MemoryStreamConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ContextWithDialerSource(ctx context.Context, addr net.Address) context.Context {
|
func ContextWithDialerSource(ctx context.Context, addr net.Address) context.Context {
|
||||||
|
@ -37,19 +37,3 @@ func DialerSourceFromContext(ctx context.Context) net.Address {
|
||||||
}
|
}
|
||||||
return net.AnyIP
|
return net.AnyIP
|
||||||
}
|
}
|
||||||
|
|
||||||
func ContextWithTransportSettings(ctx context.Context, transportSettings interface{}) context.Context {
|
|
||||||
return context.WithValue(ctx, transportSettingsKey, transportSettings)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TransportSettingsFromContext(ctx context.Context) interface{} {
|
|
||||||
return ctx.Value(transportSettingsKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ContextWithSecuritySettings(ctx context.Context, securitySettings interface{}) context.Context {
|
|
||||||
return context.WithValue(ctx, securitySettingsKey, securitySettings)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SecuritySettingsFromContext(ctx context.Context) interface{} {
|
|
||||||
return ctx.Value(securitySettingsKey)
|
|
||||||
}
|
|
||||||
|
|
|
@ -24,18 +24,19 @@ func RegisterTransportDialer(protocol string, dialer Dialer) error {
|
||||||
func Dial(ctx context.Context, dest net.Destination) (Connection, error) {
|
func Dial(ctx context.Context, dest net.Destination) (Connection, error) {
|
||||||
if dest.Network == net.Network_TCP {
|
if dest.Network == net.Network_TCP {
|
||||||
streamSettings := StreamSettingsFromContext(ctx)
|
streamSettings := StreamSettingsFromContext(ctx)
|
||||||
protocol := streamSettings.GetEffectiveProtocol()
|
var protocol string
|
||||||
transportSettings, err := streamSettings.GetEffectiveTransportSettings()
|
if streamSettings != nil {
|
||||||
|
protocol = streamSettings.ProtocolName
|
||||||
|
} else {
|
||||||
|
protocol = "tcp"
|
||||||
|
pSettings, err := CreateTransportConfigByName(protocol)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, newError("failed to create default config for protocol: ", protocol).Base(err)
|
||||||
}
|
}
|
||||||
ctx = ContextWithTransportSettings(ctx, transportSettings)
|
ctx = ContextWithStreamSettings(ctx, &MemoryStreamConfig{
|
||||||
if streamSettings != nil && streamSettings.HasSecuritySettings() {
|
ProtocolName: protocol,
|
||||||
securitySettings, err := streamSettings.GetEffectiveSecuritySettings()
|
ProtocolSettings: pSettings,
|
||||||
if err != nil {
|
})
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ctx = ContextWithSecuritySettings(ctx, securitySettings)
|
|
||||||
}
|
}
|
||||||
dialer := transportDialerCache[protocol]
|
dialer := transportDialerCache[protocol]
|
||||||
if dialer == nil {
|
if dialer == nil {
|
||||||
|
|
|
@ -13,11 +13,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func getSettingsFromContext(ctx context.Context) *Config {
|
func getSettingsFromContext(ctx context.Context) *Config {
|
||||||
rawSettings := internet.TransportSettingsFromContext(ctx)
|
rawSettings := internet.StreamSettingsFromContext(ctx)
|
||||||
if rawSettings == nil {
|
if rawSettings == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return rawSettings.(*Config)
|
return rawSettings.ProtocolSettings.(*Config)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
|
func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
|
||||||
|
|
|
@ -18,8 +18,11 @@ import (
|
||||||
func TestListen(t *testing.T) {
|
func TestListen(t *testing.T) {
|
||||||
assert := With(t)
|
assert := With(t)
|
||||||
|
|
||||||
ctx := internet.ContextWithTransportSettings(context.Background(), &Config{
|
ctx := internet.ContextWithStreamSettings(context.Background(), &internet.MemoryStreamConfig{
|
||||||
|
ProtocolName: "domainsocket",
|
||||||
|
ProtocolSettings: &Config{
|
||||||
Path: "/tmp/ts3",
|
Path: "/tmp/ts3",
|
||||||
|
},
|
||||||
})
|
})
|
||||||
listener, err := Listen(ctx, nil, net.Port(0), func(conn internet.Connection) {
|
listener, err := Listen(ctx, nil, net.Port(0), func(conn internet.Connection) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
@ -53,9 +56,12 @@ func TestListenAbstract(t *testing.T) {
|
||||||
|
|
||||||
assert := With(t)
|
assert := With(t)
|
||||||
|
|
||||||
ctx := internet.ContextWithTransportSettings(context.Background(), &Config{
|
ctx := internet.ContextWithStreamSettings(context.Background(), &internet.MemoryStreamConfig{
|
||||||
|
ProtocolName: "domainsocket",
|
||||||
|
ProtocolSettings: &Config{
|
||||||
Path: "/tmp/ts3",
|
Path: "/tmp/ts3",
|
||||||
Abstract: true,
|
Abstract: true,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
listener, err := Listen(ctx, nil, net.Port(0), func(conn internet.Connection) {
|
listener, err := Listen(ctx, nil, net.Port(0), func(conn internet.Connection) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
|
@ -72,8 +72,8 @@ func getHTTPClient(ctx context.Context, dest net.Destination) (*http.Client, err
|
||||||
|
|
||||||
// Dial dials a new TCP connection to the given destination.
|
// Dial dials a new TCP connection to the given destination.
|
||||||
func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
|
func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
|
||||||
rawSettings := internet.TransportSettingsFromContext(ctx)
|
rawSettings := internet.StreamSettingsFromContext(ctx)
|
||||||
httpSettings, ok := rawSettings.(*Config)
|
httpSettings, ok := rawSettings.ProtocolSettings.(*Config)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, newError("HTTP config is not set.").AtError()
|
return nil, newError("HTTP config is not set.").AtError()
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,11 +22,14 @@ func TestHTTPConnection(t *testing.T) {
|
||||||
|
|
||||||
port := tcp.PickPort()
|
port := tcp.PickPort()
|
||||||
|
|
||||||
lctx := context.Background()
|
lctx := internet.ContextWithStreamSettings(context.Background(), &internet.MemoryStreamConfig{
|
||||||
lctx = internet.ContextWithSecuritySettings(lctx, &tls.Config{
|
ProtocolName: "http",
|
||||||
|
ProtocolSettings: &Config{},
|
||||||
|
SecurityType: "tls",
|
||||||
|
SecuritySettings: &tls.Config{
|
||||||
Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("www.v2ray.com")))},
|
Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("www.v2ray.com")))},
|
||||||
|
},
|
||||||
})
|
})
|
||||||
lctx = internet.ContextWithTransportSettings(lctx, &Config{})
|
|
||||||
|
|
||||||
listener, err := Listen(lctx, net.LocalHostIP, port, func(conn internet.Connection) {
|
listener, err := Listen(lctx, net.LocalHostIP, port, func(conn internet.Connection) {
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -51,12 +54,15 @@ func TestHTTPConnection(t *testing.T) {
|
||||||
|
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
dctx := context.Background()
|
dctx := internet.ContextWithStreamSettings(context.Background(), &internet.MemoryStreamConfig{
|
||||||
dctx = internet.ContextWithSecuritySettings(dctx, &tls.Config{
|
ProtocolName: "http",
|
||||||
|
ProtocolSettings: &Config{},
|
||||||
|
SecurityType: "tls",
|
||||||
|
SecuritySettings: &tls.Config{
|
||||||
ServerName: "www.v2ray.com",
|
ServerName: "www.v2ray.com",
|
||||||
AllowInsecure: true,
|
AllowInsecure: true,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
dctx = internet.ContextWithTransportSettings(dctx, &Config{})
|
|
||||||
conn, err := Dial(dctx, net.TCPDestination(net.LocalHostIP, port))
|
conn, err := Dial(dctx, net.TCPDestination(net.LocalHostIP, port))
|
||||||
assert(err, IsNil)
|
assert(err, IsNil)
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
|
@ -88,8 +88,8 @@ func (l *Listener) ServeHTTP(writer http.ResponseWriter, request *http.Request)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Listen(ctx context.Context, address net.Address, port net.Port, handler internet.ConnHandler) (internet.Listener, error) {
|
func Listen(ctx context.Context, address net.Address, port net.Port, handler internet.ConnHandler) (internet.Listener, error) {
|
||||||
rawSettings := internet.TransportSettingsFromContext(ctx)
|
rawSettings := internet.StreamSettingsFromContext(ctx)
|
||||||
httpSettings, ok := rawSettings.(*Config)
|
httpSettings, ok := rawSettings.ProtocolSettings.(*Config)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, newError("HTTP config is not set.").AtError()
|
return nil, newError("HTTP config is not set.").AtError()
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ func DialKCP(ctx context.Context, dest net.Destination) (internet.Connection, er
|
||||||
return nil, newError("failed to dial to dest: ", err).AtWarning().Base(err)
|
return nil, newError("failed to dial to dest: ", err).AtWarning().Base(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
kcpSettings := internet.TransportSettingsFromContext(ctx).(*Config)
|
kcpSettings := internet.StreamSettingsFromContext(ctx).ProtocolSettings.(*Config)
|
||||||
|
|
||||||
header, err := kcpSettings.GetPackerHeader()
|
header, err := kcpSettings.GetPackerHeader()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -17,7 +17,11 @@ import (
|
||||||
func TestDialAndListen(t *testing.T) {
|
func TestDialAndListen(t *testing.T) {
|
||||||
assert := With(t)
|
assert := With(t)
|
||||||
|
|
||||||
listerner, err := NewListener(internet.ContextWithTransportSettings(context.Background(), &Config{}), net.LocalHostIP, net.Port(0), func(conn internet.Connection) {
|
lctx := internet.ContextWithStreamSettings(context.Background(), &internet.MemoryStreamConfig{
|
||||||
|
ProtocolName: "mkcp",
|
||||||
|
ProtocolSettings: &Config{},
|
||||||
|
})
|
||||||
|
listerner, err := NewListener(lctx, net.LocalHostIP, net.Port(0), func(conn internet.Connection) {
|
||||||
go func(c internet.Connection) {
|
go func(c internet.Connection) {
|
||||||
payload := make([]byte, 4096)
|
payload := make([]byte, 4096)
|
||||||
for {
|
for {
|
||||||
|
@ -36,7 +40,10 @@ func TestDialAndListen(t *testing.T) {
|
||||||
assert(err, IsNil)
|
assert(err, IsNil)
|
||||||
port := net.Port(listerner.Addr().(*net.UDPAddr).Port)
|
port := net.Port(listerner.Addr().(*net.UDPAddr).Port)
|
||||||
|
|
||||||
ctx := internet.ContextWithTransportSettings(context.Background(), &Config{})
|
ctx := internet.ContextWithStreamSettings(context.Background(), &internet.MemoryStreamConfig{
|
||||||
|
ProtocolName: "mkcp",
|
||||||
|
ProtocolSettings: &Config{},
|
||||||
|
})
|
||||||
wg := new(sync.WaitGroup)
|
wg := new(sync.WaitGroup)
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
clientConn, err := DialKCP(ctx, net.UDPDestination(net.LocalHostIP, port))
|
clientConn, err := DialKCP(ctx, net.UDPDestination(net.LocalHostIP, port))
|
||||||
|
|
|
@ -34,8 +34,8 @@ type Listener struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewListener(ctx context.Context, address net.Address, port net.Port, addConn internet.ConnHandler) (*Listener, error) {
|
func NewListener(ctx context.Context, address net.Address, port net.Port, addConn internet.ConnHandler) (*Listener, error) {
|
||||||
networkSettings := internet.TransportSettingsFromContext(ctx)
|
networkSettings := internet.StreamSettingsFromContext(ctx)
|
||||||
kcpSettings := networkSettings.(*Config)
|
kcpSettings := networkSettings.ProtocolSettings.(*Config)
|
||||||
|
|
||||||
header, err := kcpSettings.GetPackerHeader()
|
header, err := kcpSettings.GetPackerHeader()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
package internet
|
||||||
|
|
||||||
|
type MemoryStreamConfig struct {
|
||||||
|
ProtocolName string
|
||||||
|
ProtocolSettings interface{}
|
||||||
|
SecurityType string
|
||||||
|
SecuritySettings interface{}
|
||||||
|
SocketSettings *SocketConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToMemoryStreamConfig(s *StreamConfig) (*MemoryStreamConfig, error) {
|
||||||
|
ets, err := s.GetEffectiveTransportSettings()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
mss := &MemoryStreamConfig{
|
||||||
|
ProtocolName: s.GetEffectiveProtocol(),
|
||||||
|
ProtocolSettings: ets,
|
||||||
|
SocketSettings: s.SocketSettings,
|
||||||
|
}
|
||||||
|
|
||||||
|
if s != nil && s.HasSecuritySettings() {
|
||||||
|
ess, err := s.GetEffectiveSecuritySettings()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
mss.SecurityType = s.SecurityType
|
||||||
|
mss.SecuritySettings = ess
|
||||||
|
}
|
||||||
|
|
||||||
|
return mss, nil
|
||||||
|
}
|
|
@ -25,7 +25,7 @@ func TestSockOptMark(t *testing.T) {
|
||||||
|
|
||||||
const mark = 1
|
const mark = 1
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx = ContextWithStreamSettings(ctx, &StreamConfig{
|
ctx = ContextWithStreamSettings(ctx, &MemoryStreamConfig{
|
||||||
SocketSettings: &SocketConfig{
|
SocketSettings: &SocketConfig{
|
||||||
Mark: mark,
|
Mark: mark,
|
||||||
},
|
},
|
||||||
|
|
|
@ -11,11 +11,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func getTCPSettingsFromContext(ctx context.Context) *Config {
|
func getTCPSettingsFromContext(ctx context.Context) *Config {
|
||||||
rawTCPSettings := internet.TransportSettingsFromContext(ctx)
|
rawTCPSettings := internet.StreamSettingsFromContext(ctx)
|
||||||
if rawTCPSettings == nil {
|
if rawTCPSettings == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return rawTCPSettings.(*Config)
|
return rawTCPSettings.ProtocolSettings.(*Config)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dial dials a new TCP connection to the given destination.
|
// Dial dials a new TCP connection to the given destination.
|
||||||
|
|
|
@ -29,19 +29,7 @@ type Listener interface {
|
||||||
|
|
||||||
func ListenTCP(ctx context.Context, address net.Address, port net.Port, handler ConnHandler) (Listener, error) {
|
func ListenTCP(ctx context.Context, address net.Address, port net.Port, handler ConnHandler) (Listener, error) {
|
||||||
settings := StreamSettingsFromContext(ctx)
|
settings := StreamSettingsFromContext(ctx)
|
||||||
protocol := settings.GetEffectiveProtocol()
|
protocol := settings.ProtocolName
|
||||||
transportSettings, err := settings.GetEffectiveTransportSettings()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ctx = ContextWithTransportSettings(ctx, transportSettings)
|
|
||||||
if settings != nil && settings.HasSecuritySettings() {
|
|
||||||
securitySettings, err := settings.GetEffectiveSecuritySettings()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ctx = ContextWithSecuritySettings(ctx, securitySettings)
|
|
||||||
}
|
|
||||||
listenFunc := transportListenerCache[protocol]
|
listenFunc := transportListenerCache[protocol]
|
||||||
if listenFunc == nil {
|
if listenFunc == nil {
|
||||||
return nil, newError(protocol, " listener not registered.").AtError()
|
return nil, newError(protocol, " listener not registered.").AtError()
|
||||||
|
|
|
@ -217,11 +217,11 @@ func WithNextProto(protocol ...string) Option {
|
||||||
|
|
||||||
// ConfigFromContext fetches Config from context. Nil if not found.
|
// ConfigFromContext fetches Config from context. Nil if not found.
|
||||||
func ConfigFromContext(ctx context.Context) *Config {
|
func ConfigFromContext(ctx context.Context) *Config {
|
||||||
securitySettings := internet.SecuritySettingsFromContext(ctx)
|
streamSettings := internet.StreamSettingsFromContext(ctx)
|
||||||
if securitySettings == nil {
|
if streamSettings == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
config, ok := securitySettings.(*Config)
|
config, ok := streamSettings.SecuritySettings.(*Config)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ func init() {
|
||||||
|
|
||||||
func dialWebsocket(ctx context.Context, dest net.Destination) (net.Conn, error) {
|
func dialWebsocket(ctx context.Context, dest net.Destination) (net.Conn, error) {
|
||||||
src := internet.DialerSourceFromContext(ctx)
|
src := internet.DialerSourceFromContext(ctx)
|
||||||
wsSettings := internet.TransportSettingsFromContext(ctx).(*Config)
|
wsSettings := internet.StreamSettingsFromContext(ctx).ProtocolSettings.(*Config)
|
||||||
|
|
||||||
dialer := &websocket.Dialer{
|
dialer := &websocket.Dialer{
|
||||||
NetDial: func(network, addr string) (net.Conn, error) {
|
NetDial: func(network, addr string) (net.Conn, error) {
|
||||||
|
|
|
@ -57,8 +57,8 @@ type Listener struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListenWS(ctx context.Context, address net.Address, port net.Port, addConn internet.ConnHandler) (internet.Listener, error) {
|
func ListenWS(ctx context.Context, address net.Address, port net.Port, addConn internet.ConnHandler) (internet.Listener, error) {
|
||||||
networkSettings := internet.TransportSettingsFromContext(ctx)
|
networkSettings := internet.StreamSettingsFromContext(ctx)
|
||||||
wsSettings := networkSettings.(*Config)
|
wsSettings := networkSettings.ProtocolSettings.(*Config)
|
||||||
|
|
||||||
l := &Listener{
|
l := &Listener{
|
||||||
config: wsSettings,
|
config: wsSettings,
|
||||||
|
|
|
@ -17,9 +17,14 @@ import (
|
||||||
|
|
||||||
func Test_listenWSAndDial(t *testing.T) {
|
func Test_listenWSAndDial(t *testing.T) {
|
||||||
assert := With(t)
|
assert := With(t)
|
||||||
listen, err := ListenWS(internet.ContextWithTransportSettings(context.Background(), &Config{
|
|
||||||
|
lctx := internet.ContextWithStreamSettings(context.Background(), &internet.MemoryStreamConfig{
|
||||||
|
ProtocolName: "websocket",
|
||||||
|
ProtocolSettings: &Config{
|
||||||
Path: "ws",
|
Path: "ws",
|
||||||
}), net.DomainAddress("localhost"), 13146, func(conn internet.Connection) {
|
},
|
||||||
|
})
|
||||||
|
listen, err := ListenWS(lctx, net.DomainAddress("localhost"), 13146, func(conn internet.Connection) {
|
||||||
go func(c internet.Connection) {
|
go func(c internet.Connection) {
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
|
|
||||||
|
@ -37,7 +42,10 @@ func Test_listenWSAndDial(t *testing.T) {
|
||||||
})
|
})
|
||||||
assert(err, IsNil)
|
assert(err, IsNil)
|
||||||
|
|
||||||
ctx := internet.ContextWithTransportSettings(context.Background(), &Config{Path: "ws"})
|
ctx := internet.ContextWithStreamSettings(context.Background(), &internet.MemoryStreamConfig{
|
||||||
|
ProtocolName: "websocket",
|
||||||
|
ProtocolSettings: &Config{Path: "ws"},
|
||||||
|
})
|
||||||
conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
|
conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13146))
|
||||||
|
|
||||||
assert(err, IsNil)
|
assert(err, IsNil)
|
||||||
|
@ -65,9 +73,13 @@ func Test_listenWSAndDial(t *testing.T) {
|
||||||
|
|
||||||
func TestDialWithRemoteAddr(t *testing.T) {
|
func TestDialWithRemoteAddr(t *testing.T) {
|
||||||
assert := With(t)
|
assert := With(t)
|
||||||
listen, err := ListenWS(internet.ContextWithTransportSettings(context.Background(), &Config{
|
lctx := internet.ContextWithStreamSettings(context.Background(), &internet.MemoryStreamConfig{
|
||||||
|
ProtocolName: "websocket",
|
||||||
|
ProtocolSettings: &Config{
|
||||||
Path: "ws",
|
Path: "ws",
|
||||||
}), net.DomainAddress("localhost"), 13148, func(conn internet.Connection) {
|
},
|
||||||
|
})
|
||||||
|
listen, err := ListenWS(lctx, net.DomainAddress("localhost"), 13148, func(conn internet.Connection) {
|
||||||
go func(c internet.Connection) {
|
go func(c internet.Connection) {
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
|
|
||||||
|
@ -87,7 +99,10 @@ func TestDialWithRemoteAddr(t *testing.T) {
|
||||||
})
|
})
|
||||||
assert(err, IsNil)
|
assert(err, IsNil)
|
||||||
|
|
||||||
ctx := internet.ContextWithTransportSettings(context.Background(), &Config{Path: "ws", Header: []*Header{{Key: "X-Forwarded-For", Value: "1.1.1.1"}}})
|
ctx := internet.ContextWithStreamSettings(context.Background(), &internet.MemoryStreamConfig{
|
||||||
|
ProtocolName: "websocket",
|
||||||
|
ProtocolSettings: &Config{Path: "ws", Header: []*Header{{Key: "X-Forwarded-For", Value: "1.1.1.1"}}},
|
||||||
|
})
|
||||||
conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13148))
|
conn, err := Dial(ctx, net.TCPDestination(net.DomainAddress("localhost"), 13148))
|
||||||
|
|
||||||
assert(err, IsNil)
|
assert(err, IsNil)
|
||||||
|
@ -111,13 +126,18 @@ func Test_listenWSAndDial_TLS(t *testing.T) {
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
ctx := internet.ContextWithTransportSettings(context.Background(), &Config{
|
ctx := internet.ContextWithStreamSettings(context.Background(), &internet.MemoryStreamConfig{
|
||||||
|
ProtocolName: "websocket",
|
||||||
|
ProtocolSettings: &Config{
|
||||||
Path: "wss",
|
Path: "wss",
|
||||||
})
|
},
|
||||||
ctx = internet.ContextWithSecuritySettings(ctx, &tls.Config{
|
SecurityType: "tls",
|
||||||
|
SecuritySettings: &tls.Config{
|
||||||
AllowInsecure: true,
|
AllowInsecure: true,
|
||||||
Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("localhost")))},
|
Certificate: []*tls.Certificate{tls.ParseCertificate(cert.MustGenerate(nil, cert.CommonName("localhost")))},
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
listen, err := ListenWS(ctx, net.DomainAddress("localhost"), 13143, func(conn internet.Connection) {
|
listen, err := ListenWS(ctx, net.DomainAddress("localhost"), 13143, func(conn internet.Connection) {
|
||||||
go func() {
|
go func() {
|
||||||
_ = conn.Close()
|
_ = conn.Close()
|
||||||
|
|
Loading…
Reference in New Issue