diff --git a/app/proxyman/config.go b/app/proxyman/config.go index 6f20ba489..6a4140634 100644 --- a/app/proxyman/config.go +++ b/app/proxyman/config.go @@ -1,5 +1,12 @@ package proxyman +import ( + "context" + "errors" + + "v2ray.com/core/proxy" +) + func (s *AllocationStrategy) GetConcurrencyValue() uint32 { if s == nil || s.Concurrency == nil { return 3 @@ -13,3 +20,14 @@ func (s *AllocationStrategy) GetRefreshValue() uint32 { } return s.Refresh.Value } + +func (c *OutboundHandlerConfig) GetProxyHandler(ctx context.Context) (proxy.OutboundHandler, error) { + if c == nil { + return nil, errors.New("Proxyman: OutboundHandlerConfig is nil.") + } + config, err := c.ProxySettings.GetInstance() + if err != nil { + return nil, err + } + return proxy.CreateOutboundHandler(ctx, config) +} diff --git a/app/proxyman/config.pb.go b/app/proxyman/config.pb.go index 6e892a35b..4b51106f5 100644 --- a/app/proxyman/config.pb.go +++ b/app/proxyman/config.pb.go @@ -246,6 +246,96 @@ func (m *OutboundConfig) String() string { return proto.CompactTextSt func (*OutboundConfig) ProtoMessage() {} func (*OutboundConfig) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{5} } +type StreamSenderConfig struct { + // Send traffic through the given IP. Only IP is allowed. + Via *v2ray_core_common_net.IPOrDomain `protobuf:"bytes,1,opt,name=via" json:"via,omitempty"` + StreamSettings *v2ray_core_transport_internet.StreamConfig `protobuf:"bytes,2,opt,name=stream_settings,json=streamSettings" json:"stream_settings,omitempty"` + ProxySettings *v2ray_core_transport_internet.ProxyConfig `protobuf:"bytes,3,opt,name=proxy_settings,json=proxySettings" json:"proxy_settings,omitempty"` +} + +func (m *StreamSenderConfig) Reset() { *m = StreamSenderConfig{} } +func (m *StreamSenderConfig) String() string { return proto.CompactTextString(m) } +func (*StreamSenderConfig) ProtoMessage() {} +func (*StreamSenderConfig) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{6} } + +func (m *StreamSenderConfig) GetVia() *v2ray_core_common_net.IPOrDomain { + if m != nil { + return m.Via + } + return nil +} + +func (m *StreamSenderConfig) GetStreamSettings() *v2ray_core_transport_internet.StreamConfig { + if m != nil { + return m.StreamSettings + } + return nil +} + +func (m *StreamSenderConfig) GetProxySettings() *v2ray_core_transport_internet.ProxyConfig { + if m != nil { + return m.ProxySettings + } + return nil +} + +type DatagramSenderConfig struct { + // Send traffic through the given IP. Only IP is allowed. + Via *v2ray_core_common_net.IPOrDomain `protobuf:"bytes,1,opt,name=via" json:"via,omitempty"` + ProxySettings *v2ray_core_transport_internet.ProxyConfig `protobuf:"bytes,2,opt,name=proxy_settings,json=proxySettings" json:"proxy_settings,omitempty"` +} + +func (m *DatagramSenderConfig) Reset() { *m = DatagramSenderConfig{} } +func (m *DatagramSenderConfig) String() string { return proto.CompactTextString(m) } +func (*DatagramSenderConfig) ProtoMessage() {} +func (*DatagramSenderConfig) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{7} } + +func (m *DatagramSenderConfig) GetVia() *v2ray_core_common_net.IPOrDomain { + if m != nil { + return m.Via + } + return nil +} + +func (m *DatagramSenderConfig) GetProxySettings() *v2ray_core_transport_internet.ProxyConfig { + if m != nil { + return m.ProxySettings + } + return nil +} + +type OutboundHandlerConfig struct { + Tag string `protobuf:"bytes,1,opt,name=tag" json:"tag,omitempty"` + SenderSettings []*v2ray_core_common_serial.TypedMessage `protobuf:"bytes,2,rep,name=sender_settings,json=senderSettings" json:"sender_settings,omitempty"` + ProxySettings *v2ray_core_common_serial.TypedMessage `protobuf:"bytes,3,opt,name=proxy_settings,json=proxySettings" json:"proxy_settings,omitempty"` +} + +func (m *OutboundHandlerConfig) Reset() { *m = OutboundHandlerConfig{} } +func (m *OutboundHandlerConfig) String() string { return proto.CompactTextString(m) } +func (*OutboundHandlerConfig) ProtoMessage() {} +func (*OutboundHandlerConfig) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{8} } + +func (m *OutboundHandlerConfig) GetTag() string { + if m != nil { + return m.Tag + } + return "" +} + +func (m *OutboundHandlerConfig) GetSenderSettings() []*v2ray_core_common_serial.TypedMessage { + if m != nil { + return m.SenderSettings + } + return nil +} + +func (m *OutboundHandlerConfig) GetProxySettings() *v2ray_core_common_serial.TypedMessage { + if m != nil { + return m.ProxySettings + } + return nil +} + func init() { proto.RegisterType((*InboundConfig)(nil), "v2ray.core.app.proxyman.InboundConfig") proto.RegisterType((*AllocationStrategy)(nil), "v2ray.core.app.proxyman.AllocationStrategy") @@ -255,49 +345,57 @@ func init() { proto.RegisterType((*DatagramReceiverConfig)(nil), "v2ray.core.app.proxyman.DatagramReceiverConfig") proto.RegisterType((*InboundHandlerConfig)(nil), "v2ray.core.app.proxyman.InboundHandlerConfig") proto.RegisterType((*OutboundConfig)(nil), "v2ray.core.app.proxyman.OutboundConfig") + proto.RegisterType((*StreamSenderConfig)(nil), "v2ray.core.app.proxyman.StreamSenderConfig") + proto.RegisterType((*DatagramSenderConfig)(nil), "v2ray.core.app.proxyman.DatagramSenderConfig") + proto.RegisterType((*OutboundHandlerConfig)(nil), "v2ray.core.app.proxyman.OutboundHandlerConfig") proto.RegisterEnum("v2ray.core.app.proxyman.AllocationStrategy_Type", AllocationStrategy_Type_name, AllocationStrategy_Type_value) } func init() { proto.RegisterFile("v2ray.com/core/app/proxyman/config.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 601 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe4, 0x54, 0x5f, 0x6f, 0xd3, 0x3e, - 0x14, 0xfd, 0xb5, 0xdd, 0x6f, 0x6c, 0xb7, 0xac, 0x2b, 0x66, 0x62, 0xa5, 0x08, 0xa9, 0x54, 0x08, - 0x2a, 0x81, 0x9c, 0x11, 0xc4, 0x03, 0x4f, 0x68, 0xff, 0x24, 0xf6, 0x30, 0x56, 0xb9, 0x13, 0x0f, - 0x08, 0xa9, 0xba, 0x4b, 0xbc, 0x10, 0x91, 0xd8, 0x96, 0xed, 0x8e, 0xe5, 0x2b, 0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x46, 0x70, 0xbe, 0x3d, 0x4f, 0x61, 0xd3, - 0x14, 0x8e, 0xc7, 0x86, 0x5b, 0x1b, 0x8b, 0xc8, 0x74, 0x56, 0xe6, 0x95, 0xa7, 0x8f, 0x81, 0x56, - 0x8f, 0x81, 0xba, 0x9c, 0x5c, 0x3e, 0xac, 0xe5, 0x34, 0x46, 0xa5, 0x44, 0xff, 0x67, 0x0d, 0xee, - 0x1d, 0xa0, 0xc5, 0x48, 0xff, 0x3b, 0x51, 0xf6, 0x7f, 0xd4, 0x60, 0xab, 0x1c, 0x09, 0x6f, 0x51, - 0x84, 0xc9, 0xd4, 0x72, 0x1b, 0x1a, 0x16, 0xa3, 0xc2, 0xeb, 0x3a, 0xcb, 0x97, 0x64, 0x04, 0x77, - 0x74, 0x19, 0xcb, 0x55, 0xee, 0xf5, 0x5e, 0x63, 0xd0, 0xf4, 0x9f, 0x2c, 0xb0, 0xe3, 0xa6, 0x60, - 0x31, 0x0f, 0xc2, 0x63, 0x37, 0x04, 0x59, 0xbb, 0x12, 0xa8, 0x42, 0x27, 0xc7, 0xd0, 0x2a, 0xae, - 0x7c, 0xa5, 0xe8, 0x8c, 0xfd, 0xa9, 0xe2, 0x46, 0xc1, 0x9e, 0xd6, 0xb0, 0x0d, 0xad, 0x93, 0x89, - 0x9d, 0x99, 0x70, 0x7b, 0xef, 0xe0, 0x41, 0x20, 0xd3, 0x65, 0x31, 0xed, 0x35, 0x1d, 0x6c, 0x98, - 0x8f, 0xc7, 0x0f, 0x6b, 0xd5, 0xf6, 0xd7, 0xfa, 0xf6, 0x7b, 0x9f, 0x61, 0x46, 0xf7, 0x73, 0xc2, - 0xae, 0x52, 0x74, 0x58, 0xfe, 0x39, 0x5b, 0x2d, 0x26, 0xe9, 0xcb, 0x5f, 0x01, 0x00, 0x00, 0xff, - 0xff, 0x94, 0x72, 0xcb, 0xa7, 0x3f, 0x06, 0x00, 0x00, + // 683 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe4, 0x55, 0xdd, 0x6e, 0xd3, 0x30, + 0x18, 0x25, 0xe9, 0x18, 0xdb, 0x57, 0xd6, 0x15, 0x53, 0x58, 0x29, 0x42, 0x2a, 0x15, 0x82, 0x0a, + 0x50, 0x32, 0x3a, 0x71, 0xc1, 0x15, 0xda, 0x9f, 0xc4, 0x2e, 0xc6, 0x8a, 0x3b, 0x71, 0x81, 0x90, + 0x2a, 0x2f, 0xf1, 0x42, 0x44, 0x62, 0x47, 0xb6, 0x5b, 0x96, 0x97, 0xe1, 0x01, 0x78, 0x0a, 0xae, + 0x90, 0x90, 0x78, 0x9a, 0x3d, 0x01, 0x72, 0x9c, 0x74, 0xd5, 0xda, 0xb2, 0x8d, 0x8a, 0x2b, 0xee, + 0x5c, 0xf7, 0x3b, 0xc7, 0x3e, 0xe7, 0x7c, 0xfe, 0x02, 0xed, 0x61, 0x47, 0x90, 0xd4, 0xf1, 0x78, + 0xec, 0x7a, 0x5c, 0x50, 0x97, 0x24, 0x89, 0x9b, 0x08, 0x7e, 0x92, 0xc6, 0x84, 0xb9, 0x1e, 0x67, + 0xc7, 0x61, 0xe0, 0x24, 0x82, 0x2b, 0x8e, 0xd6, 0x8a, 0x4a, 0x41, 0x1d, 0x92, 0x24, 0x4e, 0x51, + 0xd5, 0x58, 0x3f, 0x47, 0xe1, 0xf1, 0x38, 0xe6, 0xcc, 0x95, 0x54, 0x84, 0x24, 0x72, 0x55, 0x9a, + 0x50, 0xbf, 0x1f, 0x53, 0x29, 0x49, 0x40, 0x0d, 0x55, 0xe3, 0xc9, 0x74, 0x04, 0xa3, 0xca, 0x25, + 0xbe, 0x2f, 0xa8, 0x94, 0x79, 0xe1, 0xa3, 0xd9, 0x85, 0x09, 0x17, 0x2a, 0xaf, 0x72, 0xce, 0x55, + 0x29, 0x41, 0x98, 0xd4, 0xff, 0xbb, 0x21, 0x53, 0x54, 0xe8, 0xea, 0x71, 0x25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0x39, 0xa1, 0xdc, 0x69, 0x8e, 0x9b, 0x62, 0x7a, 0xca, 0x61, 0x54, 0x39, 0x5d, + 0x2e, 0x14, 0xd6, 0x75, 0x78, 0x39, 0x29, 0x96, 0xe8, 0x15, 0x2c, 0x46, 0xa1, 0x54, 0x94, 0xe5, + 0xd1, 0x3d, 0x9c, 0x01, 0xde, 0xeb, 0x1e, 0x88, 0x1d, 0x1e, 0x93, 0x90, 0xe1, 0x1c, 0x80, 0x3e, + 0xc2, 0x6d, 0x32, 0x52, 0xdd, 0x97, 0xb9, 0xec, 0x3c, 0x99, 0x67, 0x57, 0x48, 0x06, 0x23, 0x32, + 0xd9, 0x9e, 0x87, 0xb0, 0x2a, 0x33, 0xc5, 0x7d, 0x49, 0x95, 0x0a, 0x59, 0x20, 0xeb, 0x0b, 0x93, + 0xcc, 0xa3, 0xc7, 0xe0, 0x14, 0x8f, 0xc1, 0x31, 0x3e, 0x19, 0x7f, 0x70, 0xc5, 0x70, 0xf4, 0x72, + 0x8a, 0xd6, 0xa9, 0x05, 0x77, 0x77, 0x88, 0x22, 0x81, 0xf8, 0x7f, 0xac, 0x6c, 0xfd, 0xb2, 0xa0, + 0x96, 0x8f, 0x84, 0x37, 0x84, 0xf9, 0xd1, 0x48, 0x72, 0x15, 0x4a, 0x8a, 0x04, 0x99, 0xd6, 0x65, + 0xac, 0x97, 0xa8, 0x07, 0xb7, 0x44, 0x6e, 0xcb, 0x99, 0xef, 0x76, 0xb3, 0xd4, 0x2e, 0x77, 0x1e, + 0x4f, 0x91, 0x63, 0xa6, 0x60, 0x36, 0x0f, 0xfc, 0x7d, 0x33, 0x04, 0x71, 0xb5, 0x20, 0x28, 0x4c, + 0x47, 0xfb, 0x50, 0xc9, 0xae, 0x7c, 0xc6, 0x68, 0x84, 0x5d, 0x96, 0x71, 0x25, 0x43, 0x8f, 0x32, + 0xac, 0x42, 0xe5, 0x60, 0xa0, 0xc6, 0x27, 0xdc, 0xa9, 0x05, 0xa8, 0x97, 0x07, 0xcd, 0xfc, 0x91, + 0xbc, 0x0d, 0x28, 0x0d, 0x43, 0x92, 0x47, 0x79, 0x89, 0x34, 0x74, 0xf5, 0xb4, 0xbe, 0xb3, 0xe7, + 0xee, 0x3b, 0xf4, 0x6e, 0x86, 0x05, 0x4f, 0x2f, 0x20, 0xed, 0x6a, 0x50, 0xce, 0x79, 0xce, 0x86, + 0xaf, 0x16, 0xd4, 0x8a, 0x56, 0x9e, 0x5f, 0xf6, 0xe4, 0x05, 0xed, 0x79, 0x2f, 0xf8, 0xd3, 0x82, + 0x3b, 0x45, 0x50, 0x17, 0xf5, 0xdd, 0x01, 0xac, 0xca, 0x4c, 0xc3, 0xdf, 0x76, 0x5d, 0xc5, 0xc0, + 0xff, 0x51, 0xcf, 0x6d, 0xbd, 0x85, 0xfb, 0x1e, 0x8f, 0x67, 0x3d, 0xc4, 0xad, 0xb2, 0x11, 0xd6, + 0xd5, 0x1f, 0xe0, 0x0f, 0x4b, 0xc5, 0xf6, 0x37, 0x7b, 0xed, 0x7d, 0x07, 0x93, 0xd4, 0xd9, 0xd6, + 0x80, 0xcd, 0x24, 0x31, 0x6e, 0xc5, 0x84, 0x1d, 0x2d, 0x66, 0xdf, 0xea, 0x8d, 0xdf, 0x01, 0x00, + 0x00, 0xff, 0xff, 0x18, 0xce, 0x1c, 0x24, 0xa1, 0x08, 0x00, 0x00, } diff --git a/app/proxyman/config.proto b/app/proxyman/config.proto index 853ab72a2..01862dcc1 100644 --- a/app/proxyman/config.proto +++ b/app/proxyman/config.proto @@ -73,11 +73,13 @@ message StreamSenderConfig { // Send traffic through the given IP. Only IP is allowed. v2ray.core.common.net.IPOrDomain via = 1; v2ray.core.transport.internet.StreamConfig stream_settings = 2; + v2ray.core.transport.internet.ProxyConfig proxy_settings = 3; } message DatagramSenderConfig { // Send traffic through the given IP. Only IP is allowed. v2ray.core.common.net.IPOrDomain via = 1; + v2ray.core.transport.internet.ProxyConfig proxy_settings = 2; } message OutboundHandlerConfig { diff --git a/app/proxyman/outbound/handler.go b/app/proxyman/outbound/handler.go new file mode 100644 index 000000000..3beaf55a6 --- /dev/null +++ b/app/proxyman/outbound/handler.go @@ -0,0 +1,81 @@ +package outbound + +import ( + "context" + + "v2ray.com/core/app/proxyman" + "v2ray.com/core/common/errors" + "v2ray.com/core/common/net" + "v2ray.com/core/proxy" + "v2ray.com/core/transport/internet" +) + +type Handler struct { + config *proxyman.OutboundHandlerConfig + streamSettings *proxyman.StreamSenderConfig + datagramSettings *proxyman.DatagramSenderConfig + proxy proxy.OutboundHandler +} + +func NewHandler(ctx context.Context, config *proxyman.OutboundHandlerConfig) (*Handler, error) { + h := &Handler{ + config: config, + } + for _, rawSettings := range config.SenderSettings { + settings, err := rawSettings.GetInstance() + if err != nil { + return nil, err + } + switch ts := settings.(type) { + case *proxyman.StreamSenderConfig: + h.streamSettings = ts + case *proxyman.DatagramSenderConfig: + h.datagramSettings = ts + default: + return nil, errors.New("Proxyman|DefaultOutboundHandler: Unknown sender settings: ", rawSettings.Type) + } + } + proxyHandler, err := config.GetProxyHandler(proxy.ContextWithDialer(ctx, h)) + if err != nil { + return nil, err + } + + h.proxy = proxyHandler + return h, nil +} + +func (h *Handler) Dial(ctx context.Context, destination net.Destination) (internet.Connection, error) { + switch destination.Network { + case net.Network_TCP: + return h.dialStream(ctx, destination) + case net.Network_UDP: + return h.dialDatagram(ctx, destination) + default: + panic("Proxyman|DefaultOutboundHandler: unexpected network.") + } +} + +func (h *Handler) dialStream(ctx context.Context, destination net.Destination) (internet.Connection, error) { + var src net.Address + if h.streamSettings != nil { + src = h.streamSettings.Via.AsAddress() + } + var options internet.DialerOptions + if h.streamSettings != nil { + options.Proxy = h.streamSettings.ProxySettings + options.Stream = h.streamSettings.StreamSettings + } + return internet.Dial(src, destination, options) +} + +func (h *Handler) dialDatagram(ctx context.Context, destination net.Destination) (internet.Connection, error) { + var src net.Address + if h.datagramSettings != nil { + src = h.datagramSettings.Via.AsAddress() + } + var options internet.DialerOptions + if h.datagramSettings != nil { + options.Proxy = h.datagramSettings.ProxySettings + } + return internet.Dial(src, destination, options) +} diff --git a/app/proxyman/outbound/outbound.go b/app/proxyman/outbound/outbound.go index 8e4ad820f..8d2e38ee3 100644 --- a/app/proxyman/outbound/outbound.go +++ b/app/proxyman/outbound/outbound.go @@ -1,9 +1,8 @@ package outbound import ( - "sync" - "context" + "sync" "v2ray.com/core/app/proxyman" "v2ray.com/core/common" diff --git a/app/proxyman/proxyman.go b/app/proxyman/proxyman.go index 47bc05c15..63b87dae8 100644 --- a/app/proxyman/proxyman.go +++ b/app/proxyman/proxyman.go @@ -2,8 +2,12 @@ package proxyman import ( + "context" + "v2ray.com/core/app" + "v2ray.com/core/common/net" "v2ray.com/core/proxy" + "v2ray.com/core/transport/ray" ) type InboundHandlerManager interface { @@ -11,7 +15,6 @@ type InboundHandlerManager interface { } type InboundHandler interface { - } type OutboundHandlerManager interface { @@ -21,6 +24,10 @@ type OutboundHandlerManager interface { SetHandler(tag string, handler proxy.OutboundHandler) error } +type OutboundHandler interface { + Dispatch(ctx context.Context, destination net.Destination, outboundRay ray.OutboundRay) +} + func InboundHandlerManagerFromSpace(space app.Space) InboundHandlerManager { app := space.GetApplication((*InboundHandlerManager)(nil)) if app == nil { diff --git a/proxy/context.go b/proxy/context.go index 49d7784d0..dae22774b 100644 --- a/proxy/context.go +++ b/proxy/context.go @@ -7,8 +7,9 @@ import ( type key int const ( - inboundMetaKey = key(0) - outboundMetaKey = key(1) + inboundMetaKey key = iota + outboundMetaKey + dialerKey ) func ContextWithInboundMeta(ctx context.Context, meta *InboundHandlerMeta) context.Context { @@ -34,3 +35,15 @@ func OutboundMetaFromContext(ctx context.Context) *OutboundHandlerMeta { } return v.(*OutboundHandlerMeta) } + +func ContextWithDialer(ctx context.Context, dialer Dialer) context.Context { + return context.WithValue(ctx, dialerKey, dialer) +} + +func DialerFromContext(ctx context.Context) Dialer { + v := ctx.Value(dialerKey) + if v == nil { + return nil + } + return v.(Dialer) +}