1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 09:36:34 -05:00

decouple outbound handler manager

This commit is contained in:
Darien Raymond 2016-12-16 23:02:11 +01:00
parent 1bf247da55
commit 095c29fa8c
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
10 changed files with 173 additions and 58 deletions

1
all.go
View File

@ -5,6 +5,7 @@ import (
_ "v2ray.com/core/app/dispatcher/impl"
_ "v2ray.com/core/app/dns/server"
_ "v2ray.com/core/app/proxy"
_ "v2ray.com/core/app/proxyman/outbound"
_ "v2ray.com/core/app/router"
_ "v2ray.com/core/proxy/blackhole"

View File

@ -6,6 +6,7 @@ import (
"v2ray.com/core/app"
. "v2ray.com/core/app/proxy"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/proxyman/outbound"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/proxy/freedom"
@ -18,7 +19,7 @@ func TestProxyDial(t *testing.T) {
assert := assert.On(t)
space := app.NewSpace()
outboundManager := proxyman.NewDefaultOutboundHandlerManager()
outboundManager := outbound.New()
outboundManager.SetHandler("tag", freedom.NewFreedomConnection(&freedom.Config{}, space, &proxy.OutboundHandlerMeta{
Tag: "tag",
StreamSettings: &internet.StreamConfig{

66
app/proxyman/config.pb.go Normal file
View File

@ -0,0 +1,66 @@
// Code generated by protoc-gen-go.
// source: v2ray.com/core/app/proxyman/config.proto
// DO NOT EDIT!
/*
Package proxyman is a generated protocol buffer package.
It is generated from these files:
v2ray.com/core/app/proxyman/config.proto
It has these top-level messages:
InboundConfig
OutboundConfig
*/
package proxyman
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
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type InboundConfig struct {
}
func (m *InboundConfig) Reset() { *m = InboundConfig{} }
func (m *InboundConfig) String() string { return proto.CompactTextString(m) }
func (*InboundConfig) ProtoMessage() {}
func (*InboundConfig) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
type OutboundConfig struct {
}
func (m *OutboundConfig) Reset() { *m = OutboundConfig{} }
func (m *OutboundConfig) String() string { return proto.CompactTextString(m) }
func (*OutboundConfig) ProtoMessage() {}
func (*OutboundConfig) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
func init() {
proto.RegisterType((*InboundConfig)(nil), "v2ray.core.app.proxyman.InboundConfig")
proto.RegisterType((*OutboundConfig)(nil), "v2ray.core.app.proxyman.OutboundConfig")
}
func init() { proto.RegisterFile("v2ray.com/core/app/proxyman/config.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 129 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xd2, 0x28, 0x33, 0x2a, 0x4a,
0xac, 0xd4, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xce, 0x2f, 0x4a, 0xd5, 0x4f, 0x2c, 0x28, 0xd0, 0x2f,
0x28, 0xca, 0xaf, 0xa8, 0xcc, 0x4d, 0xcc, 0xd3, 0x4f, 0xce, 0xcf, 0x4b, 0xcb, 0x4c, 0xd7, 0x2b,
0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x87, 0xa9, 0x2c, 0x4a, 0xd5, 0x4b, 0x2c, 0x28, 0xd0, 0x83,
0xa9, 0x52, 0xe2, 0xe7, 0xe2, 0xf5, 0xcc, 0x4b, 0xca, 0x2f, 0xcd, 0x4b, 0x71, 0x06, 0xab, 0x57,
0x12, 0xe0, 0xe2, 0xf3, 0x2f, 0x2d, 0x41, 0x12, 0x71, 0x32, 0xe1, 0x92, 0x4e, 0xce, 0xcf, 0xd5,
0xc3, 0x61, 0x82, 0x13, 0x37, 0x44, 0x59, 0x00, 0xc8, 0x9e, 0x28, 0x0e, 0x98, 0x70, 0x12, 0x1b,
0xd8, 0x62, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0x41, 0x39, 0xa0, 0x25, 0xa4, 0x00, 0x00,
0x00,
}

13
app/proxyman/config.proto Normal file
View File

@ -0,0 +1,13 @@
syntax = "proto3";
package v2ray.core.app.proxyman;
option go_package = "proxyman";
option java_package = "com.v2ray.core.app.proxyman";
option java_outer_classname = "ConfigProto";
message InboundConfig {
}
message OutboundConfig {
}

View File

@ -0,0 +1,72 @@
package outbound
import (
"sync"
"v2ray.com/core/app"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common/serial"
"v2ray.com/core/proxy"
)
type DefaultOutboundHandlerManager struct {
sync.RWMutex
defaultHandler proxy.OutboundHandler
taggedHandler map[string]proxy.OutboundHandler
}
func New() *DefaultOutboundHandlerManager {
return &DefaultOutboundHandlerManager{
taggedHandler: make(map[string]proxy.OutboundHandler),
}
}
func (v *DefaultOutboundHandlerManager) Release() {
}
func (v *DefaultOutboundHandlerManager) GetDefaultHandler() proxy.OutboundHandler {
v.RLock()
defer v.RUnlock()
if v.defaultHandler == nil {
return nil
}
return v.defaultHandler
}
func (v *DefaultOutboundHandlerManager) SetDefaultHandler(handler proxy.OutboundHandler) error {
v.Lock()
defer v.Unlock()
v.defaultHandler = handler
return nil
}
func (v *DefaultOutboundHandlerManager) GetHandler(tag string) proxy.OutboundHandler {
v.RLock()
defer v.RUnlock()
if handler, found := v.taggedHandler[tag]; found {
return handler
}
return nil
}
func (v *DefaultOutboundHandlerManager) SetHandler(tag string, handler proxy.OutboundHandler) error {
v.Lock()
defer v.Unlock()
v.taggedHandler[tag] = handler
return nil
}
type OutboundHandlerManagerFactory struct{}
func (v OutboundHandlerManagerFactory) Create(space app.Space, config interface{}) (app.Application, error) {
return New(), nil
}
func (v OutboundHandlerManagerFactory) AppId() app.ID {
return proxyman.APP_ID_OUTBOUND_MANAGER
}
func init() {
app.RegisterApplicationFactory(serial.GetMessageType(new(proxyman.OutboundConfig)), OutboundHandlerManagerFactory{})
}

View File

@ -1,8 +1,6 @@
package proxyman
import (
"sync"
"v2ray.com/core/app"
"v2ray.com/core/proxy"
)
@ -19,51 +17,6 @@ type InboundHandlerManager interface {
type OutboundHandlerManager interface {
GetHandler(tag string) proxy.OutboundHandler
GetDefaultHandler() proxy.OutboundHandler
}
type DefaultOutboundHandlerManager struct {
sync.RWMutex
defaultHandler proxy.OutboundHandler
taggedHandler map[string]proxy.OutboundHandler
}
func NewDefaultOutboundHandlerManager() *DefaultOutboundHandlerManager {
return &DefaultOutboundHandlerManager{
taggedHandler: make(map[string]proxy.OutboundHandler),
}
}
func (v *DefaultOutboundHandlerManager) Release() {
}
func (v *DefaultOutboundHandlerManager) GetDefaultHandler() proxy.OutboundHandler {
v.RLock()
defer v.RUnlock()
if v.defaultHandler == nil {
return nil
}
return v.defaultHandler
}
func (v *DefaultOutboundHandlerManager) SetDefaultHandler(handler proxy.OutboundHandler) {
v.Lock()
defer v.Unlock()
v.defaultHandler = handler
}
func (v *DefaultOutboundHandlerManager) GetHandler(tag string) proxy.OutboundHandler {
v.RLock()
defer v.RUnlock()
if handler, found := v.taggedHandler[tag]; found {
return handler
}
return nil
}
func (v *DefaultOutboundHandlerManager) SetHandler(tag string, handler proxy.OutboundHandler) {
v.Lock()
defer v.Unlock()
v.taggedHandler[tag] = handler
SetDefaultHandler(handler proxy.OutboundHandler) error
SetHandler(tag string, handler proxy.OutboundHandler) error
}

View File

@ -7,7 +7,9 @@ import (
"v2ray.com/core/app/dispatcher"
dispatchers "v2ray.com/core/app/dispatcher/impl"
"v2ray.com/core/app/dns"
dnsserver "v2ray.com/core/app/dns/server"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/proxyman/outbound"
. "v2ray.com/core/app/router"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
@ -29,9 +31,9 @@ func TestSimpleRouter(t *testing.T) {
}
space := app.NewSpace()
space.BindApp(dns.APP_ID, dns.NewCacheServer(space, &dns.Config{}))
space.BindApp(dns.APP_ID, dnsserver.NewCacheServer(space, &dns.Config{}))
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager())
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outbound.New())
r := NewRouter(config, space)
space.BindApp(APP_ID, r)
assert.Error(space.Initialize()).IsNil()

View File

@ -8,6 +8,7 @@ import (
"v2ray.com/core/app/dispatcher"
dispatchers "v2ray.com/core/app/dispatcher/impl"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/proxyman/outbound"
"v2ray.com/core/common/dice"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
@ -37,7 +38,7 @@ func TestDokodemoTCP(t *testing.T) {
space := app.NewSpace()
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
ohm := proxyman.NewDefaultOutboundHandlerManager()
ohm := outbound.New()
ohm.SetDefaultHandler(
freedom.NewFreedomConnection(
&freedom.Config{},
@ -108,7 +109,7 @@ func TestDokodemoUDP(t *testing.T) {
space := app.NewSpace()
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
ohm := proxyman.NewDefaultOutboundHandlerManager()
ohm := outbound.New()
ohm.SetDefaultHandler(
freedom.NewFreedomConnection(
&freedom.Config{},

View File

@ -7,7 +7,9 @@ import (
"v2ray.com/core/app/dispatcher"
dispatchers "v2ray.com/core/app/dispatcher/impl"
"v2ray.com/core/app/dns"
dnsserver "v2ray.com/core/app/dns/server"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/proxyman/outbound"
"v2ray.com/core/app/router"
"v2ray.com/core/common/buf"
v2net "v2ray.com/core/common/net"
@ -64,11 +66,11 @@ func TestIPResolution(t *testing.T) {
assert := assert.On(t)
space := app.NewSpace()
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager())
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outbound.New())
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
r := router.NewRouter(&router.Config{}, space)
space.BindApp(router.APP_ID, r)
dnsServer := dns.NewCacheServer(space, &dns.Config{
dnsServer := dnsserver.NewCacheServer(space, &dns.Config{
Hosts: map[string]*v2net.IPOrDomain{
"v2ray.com": v2net.NewIPOrDomain(v2net.LocalHostIP),
},

View File

@ -41,8 +41,12 @@ func NewPoint(pConfig *Config) (*Point, error) {
vpoint.space = space
vpoint.space.BindApp(proxyman.APP_ID_INBOUND_MANAGER, vpoint)
outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager()
vpoint.space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager)
outboundManagerConfig := new(proxyman.OutboundConfig)
if err := space.BindFromConfig(serial.GetMessageType(outboundManagerConfig), outboundManagerConfig); err != nil {
return nil, err
}
outboundHandlerManager := space.GetApp(proxyman.APP_ID_OUTBOUND_MANAGER).(proxyman.OutboundHandlerManager)
proxyDialer := proxydialer.NewOutboundProxy(space)
proxyDialer.RegisterDialer()