Skip to content

Commit

Permalink
remove use of context.WithValue in transport
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Nov 21, 2018
1 parent d2d0c69 commit 5279296
Show file tree
Hide file tree
Showing 32 changed files with 212 additions and 297 deletions.
16 changes: 8 additions & 8 deletions proxy/dokodemo/dokodemo.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ func (d *DokodemoDoor) Process(ctx context.Context, network net.Network, conn in
if !d.config.FollowRedirect {
writer = &buf.SequentialWriter{Writer: conn}
} else {
tCtx := internet.ContextWithBindAddress(context.Background(), dest)
tCtx = internet.ContextWithStreamSettings(tCtx, &internet.MemoryStreamConfig{
ProtocolName: "udp",
SocketSettings: &internet.SocketConfig{
Tproxy: internet.SocketConfig_TProxy,
},
})
tConn, err := internet.DialSystem(tCtx, net.DestinationFromAddr(conn.RemoteAddr()))
sockopt := &internet.SocketConfig{
Tproxy: internet.SocketConfig_TProxy,
}
if dest.Address.Family().IsIP() {
sockopt.BindAddress = dest.Address.IP()
sockopt.BindPort = uint32(dest.Port)
}
tConn, err := internet.DialSystem(ctx, net.DestinationFromAddr(conn.RemoteAddr()), sockopt)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions testing/servers/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ type Server struct {
}

func (server *Server) Start() (net.Destination, error) {
return server.StartContext(context.Background())
return server.StartContext(context.Background(), nil)
}

func (server *Server) StartContext(ctx context.Context) (net.Destination, error) {
func (server *Server) StartContext(ctx context.Context, sockopt *internet.SocketConfig) (net.Destination, error) {
listenerAddr := server.Listen
if listenerAddr == nil {
listenerAddr = net.LocalHostIP
}
listener, err := internet.ListenSystem(ctx, &net.TCPAddr{
IP: listenerAddr.IP(),
Port: int(server.Port),
})
}, sockopt)
if err != nil {
return net.Destination{}, err
}
Expand Down
96 changes: 57 additions & 39 deletions transport/internet/config.pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ type SocketConfig struct {
// ReceiveOriginalDestAddress is for enabling IP_RECVORIGDSTADDR socket option.
// This option is for UDP only.
ReceiveOriginalDestAddress bool `protobuf:"varint,4,opt,name=receive_original_dest_address,json=receiveOriginalDestAddress,proto3" json:"receive_original_dest_address,omitempty"`
BindAddress []byte `protobuf:"bytes,5,opt,name=bind_address,json=bindAddress,proto3" json:"bind_address,omitempty"`
BindPort uint32 `protobuf:"varint,6,opt,name=bind_port,json=bindPort,proto3" json:"bind_port,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
Expand Down Expand Up @@ -368,6 +370,20 @@ func (m *SocketConfig) GetReceiveOriginalDestAddress() bool {
return false
}

func (m *SocketConfig) GetBindAddress() []byte {
if m != nil {
return m.BindAddress
}
return nil
}

func (m *SocketConfig) GetBindPort() uint32 {
if m != nil {
return m.BindPort
}
return 0
}

func init() {
proto.RegisterEnum("v2ray.core.transport.internet.TransportProtocol", TransportProtocol_name, TransportProtocol_value)
proto.RegisterEnum("v2ray.core.transport.internet.SocketConfig_TCPFastOpenState", SocketConfig_TCPFastOpenState_name, SocketConfig_TCPFastOpenState_value)
Expand All @@ -383,43 +399,45 @@ func init() {
}

var fileDescriptor_91dbc815c3d97a05 = []byte{
// 607 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x53, 0xdd, 0x6e, 0xd3, 0x4c,
0x10, 0xad, 0xed, 0x34, 0x4d, 0x27, 0x69, 0xea, 0xee, 0x55, 0x54, 0xa9, 0xfa, 0xfa, 0x05, 0x09,
0x45, 0x20, 0xad, 0x2b, 0x23, 0xb8, 0xe2, 0xa6, 0x4d, 0x40, 0x54, 0xd0, 0xc6, 0x72, 0x0c, 0x48,
0x95, 0x90, 0xb5, 0x75, 0x26, 0x91, 0xd5, 0xd8, 0x1b, 0xed, 0x2e, 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2a, 0x66, 0xd3, 0xa9, 0x40, 0xa9, 0x8f, 0x63, 0x0d, 0x5a, 0xe1, 0x61, 0x09, 0x1a, 0x97,
0x98, 0x11, 0x4a, 0x75, 0x5a, 0x20, 0xfa, 0xcf, 0xc1, 0xbd, 0xab, 0x95, 0xb4, 0xa0, 0x71, 0x2a,
0xcf, 0xa5, 0xbb, 0x45, 0x00, 0x9a, 0xaf, 0x72, 0x76, 0xbd, 0x40, 0xd7, 0x22, 0x6d, 0xd8, 0x19,
0xa5, 0xd2, 0x7c, 0xd8, 0x7d, 0x0f, 0xe0, 0x97, 0x1e, 0xb2, 0x03, 0xce, 0x78, 0x36, 0x2b, 0xf0,
0x45, 0xda, 0xb5, 0x48, 0x07, 0x5a, 0x21, 0x4e, 0x53, 0x81, 0x89, 0x72, 0xed, 0x27, 0x57, 0x70,
0xf0, 0xdb, 0x3b, 0xd0, 0x7d, 0xd1, 0x30, 0x70, 0xb7, 0x74, 0xf0, 0x7e, 0x14, 0xb8, 0x96, 0x1e,
0x7d, 0xf1, 0x76, 0x18, 0xb8, 0x36, 0xd9, 0x83, 0xdd, 0x8f, 0x78, 0x5d, 0x6c, 0xc0, 0x75, 0x74,
0xe1, 0x4d, 0x14, 0x05, 0x6e, 0x83, 0xb8, 0xd0, 0x19, 0xf1, 0x8c, 0xa5, 0x79, 0x59, 0xdb, 0x3e,
0x1b, 0xc3, 0xff, 0x09, 0xcf, 0x36, 0xef, 0x32, 0xb0, 0xae, 0x5a, 0x55, 0xfc, 0xd5, 0x3e, 0xfa,
0xe0, 0x87, 0x6c, 0x45, 0x87, 0x1a, 0x5b, 0xcb, 0xa2, 0xe7, 0x65, 0xfd, 0xba, 0x69, 0x9e, 0xde,
0xb3, 0x9f, 0x01, 0x00, 0x00, 0xff, 0xff, 0xf2, 0x99, 0xd5, 0x37, 0x49, 0x05, 0x00, 0x00,
// 636 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00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x8e, 0x7c, 0xbb, 0x41, 0x76, 0xa1, 0xfd, 0x11, 0x67, 0xc5, 0x05, 0x6d, 0x53, 0x15, 0xde, 0x84,
0xa1, 0x6f, 0x37, 0x89, 0x0d, 0xdd, 0x31, 0xcb, 0x68, 0x9a, 0x97, 0xb5, 0xed, 0xb3, 0x09, 0x1c,
0xc7, 0x2c, 0xbb, 0xdb, 0x0b, 0xdf, 0xb8, 0xb2, 0xaa, 0xf8, 0x6b, 0xe3, 0xf0, 0x83, 0x17, 0xd0,
0xb5, 0x33, 0x52, 0xd8, 0x7a, 0x2d, 0xe7, 0xbc, 0xac, 0xcf, 0x5a, 0xfa, 0xe9, 0x3e, 0xfb, 0x19,
0x00, 0x00, 0xff, 0xff, 0xce, 0xe9, 0xc8, 0x20, 0x89, 0x05, 0x00, 0x00,
}
4 changes: 4 additions & 0 deletions transport/internet/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ message SocketConfig {
// ReceiveOriginalDestAddress is for enabling IP_RECVORIGDSTADDR socket option.
// This option is for UDP only.
bool receive_original_dest_address = 4;

bytes bind_address = 5;

uint32 bind_port = 6;
}
37 changes: 0 additions & 37 deletions transport/internet/context.go

This file was deleted.

14 changes: 6 additions & 8 deletions transport/internet/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Dialer interface {
}

// dialFunc is an interface to dial network connection to a specific destination.
type dialFunc func(ctx context.Context, dest net.Destination) (Connection, error)
type dialFunc func(ctx context.Context, dest net.Destination, streamSettings *MemoryStreamConfig) (Connection, error)

var (
transportDialerCache = make(map[string]dialFunc)
Expand All @@ -33,42 +33,40 @@ func RegisterTransportDialer(protocol string, dialer dialFunc) error {
}

// Dial dials a internet connection towards the given destination.
func Dial(ctx context.Context, dest net.Destination) (Connection, error) {
func Dial(ctx context.Context, dest net.Destination, streamSettings *MemoryStreamConfig) (Connection, error) {
if dest.Network == net.Network_TCP {
streamSettings := StreamSettingsFromContext(ctx)
if streamSettings == nil {
s, err := ToMemoryStreamConfig(nil)
if err != nil {
return nil, newError("failed to create default stream settings").Base(err)
}
streamSettings = s
ctx = ContextWithStreamSettings(ctx, streamSettings)
}

protocol := streamSettings.ProtocolName
dialer := transportDialerCache[protocol]
if dialer == nil {
return nil, newError(protocol, " dialer not registered").AtError()
}
return dialer(ctx, dest)
return dialer(ctx, dest, streamSettings)
}

if dest.Network == net.Network_UDP {
udpDialer := transportDialerCache["udp"]
if udpDialer == nil {
return nil, newError("UDP dialer not registered").AtError()
}
return udpDialer(ctx, dest)
return udpDialer(ctx, dest, streamSettings)
}

return nil, newError("unknown network ", dest.Network)
}

// DialSystem calls system dialer to create a network connection.
func DialSystem(ctx context.Context, dest net.Destination) (net.Conn, error) {
func DialSystem(ctx context.Context, dest net.Destination, sockopt *SocketConfig) (net.Conn, error) {
var src net.Address
if outbound := session.OutboundFromContext(ctx); outbound != nil {
src = outbound.Gateway
}
return effectiveSystemDialer.Dial(ctx, src, dest)
return effectiveSystemDialer.Dial(ctx, src, dest, sockopt)
}
2 changes: 1 addition & 1 deletion transport/internet/dialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestDialWithLocalAddr(t *testing.T) {
assert(err, IsNil)
defer server.Close()

conn, err := DialSystem(context.Background(), net.TCPDestination(net.LocalHostIP, dest.Port))
conn, err := DialSystem(context.Background(), net.TCPDestination(net.LocalHostIP, dest.Port), nil)
assert(err, IsNil)
assert(conn.RemoteAddr().String(), Equals, "127.0.0.1:"+dest.Port.String())
conn.Close()
Expand Down
18 changes: 3 additions & 15 deletions transport/internet/domainsocket/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,8 @@ import (
"v2ray.com/core/transport/internet/tls"
)

func getSettingsFromContext(ctx context.Context) *Config {
rawSettings := internet.StreamSettingsFromContext(ctx)
if rawSettings == nil {
return nil
}
return rawSettings.ProtocolSettings.(*Config)
}

func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
settings := getSettingsFromContext(ctx)
if settings == nil {
return nil, newError("domain socket settings is not specified.").AtError()
}

func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (internet.Connection, error) {
settings := streamSettings.ProtocolSettings.(*Config)
addr, err := settings.GetUnixAddr()
if err != nil {
return nil, err
Expand All @@ -36,7 +24,7 @@ func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error
return nil, newError("failed to dial unix: ", settings.Path).Base(err).AtWarning()
}

if config := tls.ConfigFromContext(ctx); config != nil {
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
return tls.Client(conn, config.GetTLSConfig(tls.WithDestination(dest))), nil
}

Expand Down
10 changes: 3 additions & 7 deletions transport/internet/domainsocket/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ type Listener struct {
locker *fileLocker
}

func Listen(ctx context.Context, address net.Address, port net.Port, handler internet.ConnHandler) (internet.Listener, error) {
settings := getSettingsFromContext(ctx)
if settings == nil {
return nil, newError("domain socket settings not specified.")
}

func Listen(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, handler internet.ConnHandler) (internet.Listener, error) {
settings := streamSettings.ProtocolSettings.(*Config)
addr, err := settings.GetUnixAddr()
if err != nil {
return nil, err
Expand Down Expand Up @@ -58,7 +54,7 @@ func Listen(ctx context.Context, address net.Address, port net.Port, handler int
}
}

if config := tls.ConfigFromContext(ctx); config != nil {
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {
ln.tlsConfig = config.GetTLSConfig()
}

Expand Down
18 changes: 10 additions & 8 deletions transport/internet/domainsocket/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ import (
func TestListen(t *testing.T) {
assert := With(t)

ctx := internet.ContextWithStreamSettings(context.Background(), &internet.MemoryStreamConfig{
ctx := context.Background()
streamSettings := &internet.MemoryStreamConfig{
ProtocolName: "domainsocket",
ProtocolSettings: &Config{
Path: "/tmp/ts3",
},
})
listener, err := Listen(ctx, nil, net.Port(0), func(conn internet.Connection) {
}
listener, err := Listen(ctx, nil, net.Port(0), streamSettings, func(conn internet.Connection) {
defer conn.Close()

b := buf.New()
Expand All @@ -36,7 +37,7 @@ func TestListen(t *testing.T) {
assert(err, IsNil)
defer listener.Close()

conn, err := Dial(ctx, net.Destination{})
conn, err := Dial(ctx, net.Destination{}, streamSettings)
assert(err, IsNil)
defer conn.Close()

Expand All @@ -56,14 +57,15 @@ func TestListenAbstract(t *testing.T) {

assert := With(t)

ctx := internet.ContextWithStreamSettings(context.Background(), &internet.MemoryStreamConfig{
ctx := context.Background()
streamSettings := &internet.MemoryStreamConfig{
ProtocolName: "domainsocket",
ProtocolSettings: &Config{
Path: "/tmp/ts3",
Abstract: true,
},
})
listener, err := Listen(ctx, nil, net.Port(0), func(conn internet.Connection) {
}
listener, err := Listen(ctx, nil, net.Port(0), streamSettings, func(conn internet.Connection) {
defer conn.Close()

b := buf.New()
Expand All @@ -75,7 +77,7 @@ func TestListenAbstract(t *testing.T) {
assert(err, IsNil)
defer listener.Close()

conn, err := Dial(ctx, net.Destination{})
conn, err := Dial(ctx, net.Destination{}, streamSettings)
assert(err, IsNil)
defer conn.Close()

Expand Down
Loading

0 comments on commit 5279296

Please sign in to comment.