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

organize handler metadata

This commit is contained in:
v2ray 2016-06-04 14:25:13 +02:00
parent 030fca10ba
commit 50ca869929
22 changed files with 153 additions and 138 deletions

View File

@ -10,6 +10,7 @@ import (
. "github.com/v2ray/v2ray-core/app/dns"
"github.com/v2ray/v2ray-core/app/proxyman"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/proxy/freedom"
"github.com/v2ray/v2ray-core/testing/assert"
)
@ -20,7 +21,7 @@ func TestDnsAdd(t *testing.T) {
space := app.NewSpace()
outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager()
outboundHandlerManager.SetDefaultHandler(freedom.NewFreedomConnection(&freedom.Config{}, space, v2net.AnyIP))
outboundHandlerManager.SetDefaultHandler(freedom.NewFreedomConnection(&freedom.Config{}, space, &proxy.OutboundHandlerMeta{Address: v2net.AnyIP}))
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager)
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))

View File

@ -11,10 +11,13 @@ import (
// BlackHole is an outbound connection that sliently swallow the entire payload.
type BlackHole struct {
meta *proxy.OutboundHandlerMeta
}
func NewBlackHole() *BlackHole {
return &BlackHole{}
func NewBlackHole(space app.Space, config *Config, meta *proxy.OutboundHandlerMeta) *BlackHole {
return &BlackHole{
meta: meta,
}
}
func (this *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
@ -31,7 +34,7 @@ func (this *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Bu
func init() {
internal.MustRegisterOutboundHandlerCreator("blackhole",
func(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
return NewBlackHole(), nil
func(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
return NewBlackHole(space, config.(*Config), meta), nil
})
}

View File

@ -25,17 +25,15 @@ type DokodemoDoor struct {
tcpListener *hub.TCPHub
udpHub *hub.UDPHub
udpServer *hub.UDPServer
listeningPort v2net.Port
listeningAddress v2net.Address
meta *proxy.InboundHandlerMeta
}
func NewDokodemoDoor(config *Config, space app.Space, listen v2net.Address, port v2net.Port) *DokodemoDoor {
func NewDokodemoDoor(config *Config, space app.Space, meta *proxy.InboundHandlerMeta) *DokodemoDoor {
d := &DokodemoDoor{
config: config,
address: config.Address,
port: config.Port,
listeningAddress: listen,
listeningPort: port,
meta: meta,
}
space.InitializeApplication(func() error {
if !space.HasApp(dispatcher.APP_ID) {
@ -49,7 +47,7 @@ func NewDokodemoDoor(config *Config, space app.Space, listen v2net.Address, port
}
func (this *DokodemoDoor) Port() v2net.Port {
return this.listeningPort
return this.meta.Port
}
func (this *DokodemoDoor) Close() {
@ -75,13 +73,13 @@ func (this *DokodemoDoor) Start() error {
this.accepting = true
if this.config.Network.HasNetwork(v2net.TCPNetwork) {
err := this.ListenTCP(this.listeningAddress, this.listeningPort)
err := this.ListenTCP()
if err != nil {
return err
}
}
if this.config.Network.HasNetwork(v2net.UDPNetwork) {
err := this.ListenUDP(this.listeningAddress, this.listeningPort)
err := this.ListenUDP()
if err != nil {
return err
}
@ -89,11 +87,11 @@ func (this *DokodemoDoor) Start() error {
return nil
}
func (this *DokodemoDoor) ListenUDP(address v2net.Address, port v2net.Port) error {
func (this *DokodemoDoor) ListenUDP() error {
this.udpServer = hub.NewUDPServer(this.packetDispatcher)
udpHub, err := hub.ListenUDP(address, port, this.handleUDPPackets)
udpHub, err := hub.ListenUDP(this.meta.Address, this.meta.Port, this.handleUDPPackets)
if err != nil {
log.Error("Dokodemo failed to listen on port ", port, ": ", err)
log.Error("Dokodemo failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err)
return err
}
this.udpMutex.Lock()
@ -116,10 +114,10 @@ func (this *DokodemoDoor) handleUDPResponse(dest v2net.Destination, payload *all
this.udpHub.WriteTo(payload.Value, dest)
}
func (this *DokodemoDoor) ListenTCP(address v2net.Address, port v2net.Port) error {
tcpListener, err := hub.ListenTCP(address, port, this.HandleTCPConnection, nil)
func (this *DokodemoDoor) ListenTCP() error {
tcpListener, err := hub.ListenTCP(this.meta.Address, this.meta.Port, this.HandleTCPConnection, nil)
if err != nil {
log.Error("Dokodemo: Failed to listen on port ", port, ": ", err)
log.Error("Dokodemo: Failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err)
return err
}
this.tcpMutex.Lock()
@ -164,7 +162,7 @@ func (this *DokodemoDoor) HandleTCPConnection(conn *hub.Connection) {
func init() {
internal.MustRegisterInboundHandlerCreator("dokodemo-door",
func(space app.Space, rawConfig interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) {
return NewDokodemoDoor(rawConfig.(*Config), space, listen, port), nil
func(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
return NewDokodemoDoor(rawConfig.(*Config), space, meta), nil
})
}

View File

@ -10,6 +10,7 @@ import (
"github.com/v2ray/v2ray-core/app/proxyman"
v2net "github.com/v2ray/v2ray-core/common/net"
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
"github.com/v2ray/v2ray-core/proxy"
. "github.com/v2ray/v2ray-core/proxy/dokodemo"
"github.com/v2ray/v2ray-core/proxy/freedom"
"github.com/v2ray/v2ray-core/testing/assert"
@ -37,7 +38,9 @@ func TestDokodemoTCP(t *testing.T) {
space := app.NewSpace()
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
ohm := proxyman.NewDefaultOutboundHandlerManager()
ohm.SetDefaultHandler(freedom.NewFreedomConnection(&freedom.Config{}, space, v2net.LocalHostIP))
ohm.SetDefaultHandler(
freedom.NewFreedomConnection(
&freedom.Config{}, space, &proxy.OutboundHandlerMeta{Address: v2net.LocalHostIP}))
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, ohm)
data2Send := "Data to be sent to remote."
@ -48,7 +51,7 @@ func TestDokodemoTCP(t *testing.T) {
Port: tcpServer.Port,
Network: v2net.TCPNetwork.AsList(),
Timeout: 600,
}, space, v2net.LocalHostIP, port)
}, space, &proxy.InboundHandlerMeta{Address: v2net.LocalHostIP, Port: port})
defer dokodemo.Close()
assert.Error(space.Initialize()).IsNil()
@ -95,7 +98,9 @@ func TestDokodemoUDP(t *testing.T) {
space := app.NewSpace()
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
ohm := proxyman.NewDefaultOutboundHandlerManager()
ohm.SetDefaultHandler(freedom.NewFreedomConnection(&freedom.Config{}, space, v2net.AnyIP))
ohm.SetDefaultHandler(
freedom.NewFreedomConnection(
&freedom.Config{}, space, &proxy.OutboundHandlerMeta{Address: v2net.AnyIP}))
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, ohm)
data2Send := "Data to be sent to remote."
@ -106,7 +111,7 @@ func TestDokodemoUDP(t *testing.T) {
Port: udpServer.Port,
Network: v2net.UDPNetwork.AsList(),
Timeout: 600,
}, space, v2net.LocalHostIP, port)
}, space, &proxy.InboundHandlerMeta{Address: v2net.LocalHostIP, Port: port})
defer dokodemo.Close()
assert.Error(space.Initialize()).IsNil()

View File

@ -23,14 +23,14 @@ type FreedomConnection struct {
domainStrategy DomainStrategy
timeout uint32
dns dns.Server
sendThrough v2net.Address
meta *proxy.OutboundHandlerMeta
}
func NewFreedomConnection(config *Config, space app.Space, sendThrough v2net.Address) *FreedomConnection {
func NewFreedomConnection(config *Config, space app.Space, meta *proxy.OutboundHandlerMeta) *FreedomConnection {
f := &FreedomConnection{
domainStrategy: config.DomainStrategy,
timeout: config.Timeout,
sendThrough: sendThrough,
meta: meta,
}
space.InitializeApplication(func() error {
if config.DomainStrategy == DomainStrategyUseIP {
@ -80,7 +80,7 @@ func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *
destination = this.ResolveIP(destination)
}
err := retry.Timed(5, 100).On(func() error {
rawConn, err := hub.DialWithoutCache(this.sendThrough, destination)
rawConn, err := hub.DialWithoutCache(this.meta.Address, destination)
if err != nil {
return err
}
@ -140,7 +140,7 @@ func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *
func init() {
internal.MustRegisterOutboundHandlerCreator("freedom",
func(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
return NewFreedomConnection(config.(*Config), space, sendThrough), nil
func(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
return NewFreedomConnection(config.(*Config), space, meta), nil
})
}

View File

@ -14,6 +14,7 @@ import (
"github.com/v2ray/v2ray-core/common/alloc"
v2net "github.com/v2ray/v2ray-core/common/net"
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
"github.com/v2ray/v2ray-core/proxy"
. "github.com/v2ray/v2ray-core/proxy/freedom"
"github.com/v2ray/v2ray-core/testing/assert"
"github.com/v2ray/v2ray-core/testing/servers/tcp"
@ -37,7 +38,7 @@ func TestSinglePacket(t *testing.T) {
assert.Error(err).IsNil()
space := app.NewSpace()
freedom := NewFreedomConnection(&Config{}, space, v2net.AnyIP)
freedom := NewFreedomConnection(&Config{}, space, &proxy.OutboundHandlerMeta{Address: v2net.AnyIP})
space.Initialize()
traffic := ray.NewRay()
@ -57,7 +58,7 @@ func TestSinglePacket(t *testing.T) {
func TestUnreachableDestination(t *testing.T) {
assert := assert.On(t)
freedom := NewFreedomConnection(&Config{}, app.NewSpace(), v2net.AnyIP)
freedom := NewFreedomConnection(&Config{}, app.NewSpace(), &proxy.OutboundHandlerMeta{Address: v2net.AnyIP})
traffic := ray.NewRay()
data2Send := "Data to be sent to remote"
payload := alloc.NewSmallBuffer().Clear().Append([]byte(data2Send))
@ -81,7 +82,10 @@ func TestIPResolution(t *testing.T) {
})
space.BindApp(dns.APP_ID, dnsServer)
freedom := NewFreedomConnection(&Config{DomainStrategy: DomainStrategyUseIP}, space, v2net.AnyIP)
freedom := NewFreedomConnection(
&Config{DomainStrategy: DomainStrategyUseIP},
space,
&proxy.OutboundHandlerMeta{Address: v2net.AnyIP})
space.Initialize()

View File

@ -29,21 +29,19 @@ type Server struct {
packetDispatcher dispatcher.PacketDispatcher
config *Config
tcpListener *hub.TCPHub
listeningPort v2net.Port
listeningAddress v2net.Address
meta *proxy.InboundHandlerMeta
}
func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, listen v2net.Address, port v2net.Port) *Server {
func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, meta *proxy.InboundHandlerMeta) *Server {
return &Server{
packetDispatcher: packetDispatcher,
config: config,
listeningAddress: listen,
listeningPort: port,
meta: meta,
}
}
func (this *Server) Port() v2net.Port {
return this.listeningPort
return this.meta.Port
}
func (this *Server) Close() {
@ -65,9 +63,9 @@ func (this *Server) Start() error {
if this.config.TLSConfig != nil {
tlsConfig = this.config.TLSConfig.GetConfig()
}
tcpListener, err := hub.ListenTCP(this.listeningAddress, this.listeningPort, this.handleConnection, tlsConfig)
tcpListener, err := hub.ListenTCP(this.meta.Address, this.meta.Port, this.handleConnection, tlsConfig)
if err != nil {
log.Error("HTTP: Failed listen on port ", this.listeningPort, ": ", err)
log.Error("HTTP: Failed listen on ", this.meta.Address, ":", this.meta.Port, ": ", err)
return err
}
this.Lock()
@ -272,14 +270,13 @@ func (this *Server) handlePlainHTTP(request *http.Request, dest v2net.Destinatio
func init() {
internal.MustRegisterInboundHandlerCreator("http",
func(space app.Space, rawConfig interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) {
func(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
if !space.HasApp(dispatcher.APP_ID) {
return nil, internal.ErrorBadConfiguration
}
return NewServer(
rawConfig.(*Config),
space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher),
listen,
port), nil
meta), nil
})
}

View File

@ -9,6 +9,7 @@ import (
testdispatcher "github.com/v2ray/v2ray-core/app/dispatcher/testing"
v2net "github.com/v2ray/v2ray-core/common/net"
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
"github.com/v2ray/v2ray-core/proxy"
. "github.com/v2ray/v2ray-core/proxy/http"
"github.com/v2ray/v2ray-core/testing/assert"
)
@ -53,7 +54,7 @@ func TestNormalGetRequest(t *testing.T) {
testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
port := v2nettesting.PickPort()
httpProxy := NewServer(&Config{}, testPacketDispatcher, v2net.LocalHostIP, port)
httpProxy := NewServer(&Config{}, testPacketDispatcher, &proxy.InboundHandlerMeta{Address: v2net.LocalHostIP, Port: port})
defer httpProxy.Close()
err := httpProxy.Start()

View File

@ -2,9 +2,8 @@ package internal
import (
"github.com/v2ray/v2ray-core/app"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy"
)
type InboundHandlerCreator func(space app.Space, config interface{}, listenOn v2net.Address, port v2net.Port) (proxy.InboundHandler, error)
type OutboundHandlerCreator func(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error)
type InboundHandlerCreator func(space app.Space, config interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error)
type OutboundHandlerCreator func(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error)

View File

@ -4,7 +4,6 @@ import (
"errors"
"github.com/v2ray/v2ray-core/app"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/proxy/internal/config"
)
@ -46,7 +45,7 @@ func MustRegisterOutboundHandlerCreator(name string, creator OutboundHandlerCrea
}
}
func CreateInboundHandler(name string, space app.Space, rawConfig []byte, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) {
func CreateInboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
creator, found := inboundFactories[name]
if !found {
return nil, ErrorProxyNotFound
@ -56,12 +55,12 @@ func CreateInboundHandler(name string, space app.Space, rawConfig []byte, listen
if err != nil {
return nil, err
}
return creator(space, proxyConfig, listen, port)
return creator(space, proxyConfig, meta)
}
return creator(space, nil, listen, port)
return creator(space, nil, meta)
}
func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
creator, found := outboundFactories[name]
if !found {
return nil, ErrorNameExists
@ -72,8 +71,8 @@ func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, sendT
if err != nil {
return nil, err
}
return creator(space, proxyConfig, sendThrough)
return creator(space, proxyConfig, meta)
}
return creator(space, nil, sendThrough)
return creator(space, nil, meta)
}

View File

@ -14,6 +14,17 @@ const (
HandlerStateRunning = HandlerState(1)
)
type InboundHandlerMeta struct {
Tag string
Address v2net.Address
Port v2net.Port
}
type OutboundHandlerMeta struct {
Tag string
Address v2net.Address
}
// An InboundHandler handles inbound network connections to V2Ray.
type InboundHandler interface {
// Listen starts a InboundHandler.

View File

@ -2,15 +2,14 @@ package repo
import (
"github.com/v2ray/v2ray-core/app"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/proxy/internal"
)
func CreateInboundHandler(name string, space app.Space, rawConfig []byte, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) {
return internal.CreateInboundHandler(name, space, rawConfig, listen, port)
func CreateInboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
return internal.CreateInboundHandler(name, space, rawConfig, meta)
}
func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
return internal.CreateOutboundHandler(name, space, rawConfig, sendThrough)
func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
return internal.CreateOutboundHandler(name, space, rawConfig, meta)
}

View File

@ -22,25 +22,23 @@ import (
type Server struct {
packetDispatcher dispatcher.PacketDispatcher
config *Config
port v2net.Port
address v2net.Address
meta *proxy.InboundHandlerMeta
accepting bool
tcpHub *hub.TCPHub
udpHub *hub.UDPHub
udpServer *hub.UDPServer
}
func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, listen v2net.Address, port v2net.Port) *Server {
func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, meta *proxy.InboundHandlerMeta) *Server {
return &Server{
config: config,
packetDispatcher: packetDispatcher,
address: listen,
port: port,
meta: meta,
}
}
func (this *Server) Port() v2net.Port {
return this.port
return this.meta.Port
}
func (this *Server) Close() {
@ -63,18 +61,18 @@ func (this *Server) Start() error {
return nil
}
tcpHub, err := hub.ListenTCP(this.address, this.port, this.handleConnection, nil)
tcpHub, err := hub.ListenTCP(this.meta.Address, this.meta.Port, this.handleConnection, nil)
if err != nil {
log.Error("Shadowsocks: Failed to listen TCP on port ", this.port, ": ", err)
log.Error("Shadowsocks: Failed to listen TCP on ", this.meta.Address, ":", this.meta.Port, ": ", err)
return err
}
this.tcpHub = tcpHub
if this.config.UDP {
this.udpServer = hub.NewUDPServer(this.packetDispatcher)
udpHub, err := hub.ListenUDP(this.address, this.port, this.handlerUDPPayload)
udpHub, err := hub.ListenUDP(this.meta.Address, this.meta.Port, this.handlerUDPPayload)
if err != nil {
log.Error("Shadowsocks: Failed to listen UDP on port ", this.port, ": ", err)
log.Error("Shadowsocks: Failed to listen UDP on ", this.meta.Address, ":", this.meta.Port, ": ", err)
return err
}
this.udpHub = udpHub
@ -252,14 +250,13 @@ func (this *Server) handleConnection(conn *hub.Connection) {
func init() {
internal.MustRegisterInboundHandlerCreator("shadowsocks",
func(space app.Space, rawConfig interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) {
func(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
if !space.HasApp(dispatcher.APP_ID) {
return nil, internal.ErrorBadConfiguration
}
return NewServer(
rawConfig.(*Config),
space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher),
listen,
port), nil
meta), nil
})
}

View File

@ -33,23 +33,21 @@ type Server struct {
udpHub *hub.UDPHub
udpAddress v2net.Destination
udpServer *hub.UDPServer
listeningPort v2net.Port
listeningAddress v2net.Address
meta *proxy.InboundHandlerMeta
}
// NewServer creates a new Server object.
func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, listen v2net.Address, port v2net.Port) *Server {
func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, meta *proxy.InboundHandlerMeta) *Server {
return &Server{
config: config,
packetDispatcher: packetDispatcher,
listeningAddress: listen,
listeningPort: port,
meta: meta,
}
}
// Port implements InboundHandler.Port().
func (this *Server) Port() v2net.Port {
return this.listeningPort
return this.meta.Port
}
// Close implements InboundHandler.Close().
@ -76,12 +74,12 @@ func (this *Server) Start() error {
}
listener, err := hub.ListenTCP(
this.listeningAddress,
this.listeningPort,
this.meta.Address,
this.meta.Port,
this.handleConnection,
nil)
if err != nil {
log.Error("Socks: failed to listen on port ", this.listeningPort, ": ", err)
log.Error("Socks: failed to listen on ", this.meta.Address, ":", this.meta.Port, ": ", err)
return err
}
this.accepting = true
@ -89,7 +87,7 @@ func (this *Server) Start() error {
this.tcpListener = listener
this.tcpMutex.Unlock()
if this.config.UDPEnabled {
this.listenUDP(this.listeningAddress, this.listeningPort)
this.listenUDP()
}
return nil
}
@ -301,14 +299,13 @@ func (this *Server) transport(reader io.Reader, writer io.Writer, destination v2
func init() {
internal.MustRegisterInboundHandlerCreator("socks",
func(space app.Space, rawConfig interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) {
func(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
if !space.HasApp(dispatcher.APP_ID) {
return nil, internal.ErrorBadConfiguration
}
return NewServer(
rawConfig.(*Config),
space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher),
listen,
port), nil
meta), nil
})
}

View File

@ -32,7 +32,7 @@ func TestSocksTcpConnect(t *testing.T) {
}
protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och",
func(space app.Space, config interface{}, sendThrough v2net.Address) (v2proxy.OutboundHandler, error) {
func(space app.Space, config interface{}, meta *v2proxy.OutboundHandlerMeta) (v2proxy.OutboundHandler, error) {
return och, nil
})
assert.Error(err).IsNil()
@ -98,7 +98,7 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) {
}
protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och",
func(space app.Space, config interface{}, sendThrough v2net.Address) (v2proxy.OutboundHandler, error) {
func(space app.Space, config interface{}, meta *v2proxy.OutboundHandlerMeta) (v2proxy.OutboundHandler, error) {
return och, nil
})
assert.Error(err).IsNil()
@ -167,7 +167,7 @@ func TestSocksTcpConnectWithWrongUserPass(t *testing.T) {
}
protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och",
func(space app.Space, config interface{}, sendThrough v2net.Address) (v2proxy.OutboundHandler, error) {
func(space app.Space, config interface{}, meta *v2proxy.OutboundHandlerMeta) (v2proxy.OutboundHandler, error) {
return och, nil
})
assert.Error(err).IsNil()
@ -222,7 +222,7 @@ func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) {
}
protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och",
func(space app.Space, config interface{}, sendThrough v2net.Address) (v2proxy.OutboundHandler, error) {
func(space app.Space, config interface{}, meta *v2proxy.OutboundHandlerMeta) (v2proxy.OutboundHandler, error) {
return och, nil
})
assert.Error(err).IsNil()

View File

@ -8,15 +8,15 @@ import (
"github.com/v2ray/v2ray-core/transport/hub"
)
func (this *Server) listenUDP(address v2net.Address, port v2net.Port) error {
func (this *Server) listenUDP() error {
this.udpServer = hub.NewUDPServer(this.packetDispatcher)
udpHub, err := hub.ListenUDP(address, port, this.handleUDPPayload)
udpHub, err := hub.ListenUDP(this.meta.Address, this.meta.Port, this.handleUDPPayload)
if err != nil {
log.Error("Socks: Failed to listen on udp port ", port)
log.Error("Socks: Failed to listen on udp ", this.meta.Address, ":", this.meta.Port)
return err
}
this.udpMutex.Lock()
this.udpAddress = v2net.UDPDestination(this.config.Address, port)
this.udpAddress = v2net.UDPDestination(this.config.Address, this.meta.Port)
this.udpHub = udpHub
this.udpMutex.Unlock()
return nil

View File

@ -74,12 +74,11 @@ type VMessInboundHandler struct {
accepting bool
listener *hub.TCPHub
detours *DetourConfig
listeningPort v2net.Port
listeningAddress v2net.Address
meta *proxy.InboundHandlerMeta
}
func (this *VMessInboundHandler) Port() v2net.Port {
return this.listeningPort
return this.meta.Port
}
func (this *VMessInboundHandler) Close() {
@ -107,9 +106,9 @@ func (this *VMessInboundHandler) Start() error {
return nil
}
tcpListener, err := hub.ListenTCP(this.listeningAddress, this.listeningPort, this.HandleConnection, nil)
tcpListener, err := hub.ListenTCP(this.meta.Address, this.meta.Port, this.HandleConnection, nil)
if err != nil {
log.Error("Unable to listen tcp port ", this.listeningPort, ": ", err)
log.Error("Unable to listen tcp ", this.meta.Address, ":", this.meta.Port, ": ", err)
return err
}
this.accepting = true
@ -220,7 +219,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.Connection) {
func init() {
internal.MustRegisterInboundHandlerCreator("vmess",
func(space app.Space, rawConfig interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) {
func(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
if !space.HasApp(dispatcher.APP_ID) {
return nil, internal.ErrorBadConfiguration
}
@ -236,8 +235,7 @@ func init() {
clients: allowedClients,
detours: config.DetourConfig,
usersByEmail: NewUserByEmail(config.AllowedUsers, config.Defaults),
listeningAddress: listen,
listeningPort: port,
meta: meta,
}
if space.HasApp(proxyman.APP_ID_INBOUND_MANAGER) {

View File

@ -21,7 +21,7 @@ import (
type VMessOutboundHandler struct {
receiverManager *ReceiverManager
sendThrough v2net.Address
meta *proxy.OutboundHandlerMeta
}
func (this *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
@ -43,7 +43,7 @@ func (this *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *al
Option: protocol.RequestOptionChunkStream,
}
conn, err := hub.Dial(this.sendThrough, destination)
conn, err := hub.Dial(this.meta.Address, destination)
if err != nil {
log.Error("Failed to open ", destination, ": ", err)
return err
@ -143,11 +143,11 @@ func (this *VMessOutboundHandler) handleResponse(session *raw.ClientSession, con
func init() {
internal.MustRegisterOutboundHandlerCreator("vmess",
func(space app.Space, rawConfig interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
func(space app.Space, rawConfig interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
vOutConfig := rawConfig.(*Config)
return &VMessOutboundHandler{
receiverManager: NewReceiverManager(vOutConfig.Receivers),
sendThrough: sendThrough,
meta: meta,
}, nil
})
}

View File

@ -39,9 +39,9 @@ func TestVMessInAndOut(t *testing.T) {
}
protocol, err := proxytesting.RegisterInboundConnectionHandlerCreator("mock_ich",
func(space app.Space, config interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) {
ich.ListeningAddress = listen
ich.ListeningPort = port
func(space app.Space, config interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
ich.ListeningAddress = meta.Address
ich.ListeningPort = meta.Port
ich.PacketDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
return ich, nil
})
@ -89,7 +89,7 @@ func TestVMessInAndOut(t *testing.T) {
}
protocol, err = proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och",
func(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
func(space app.Space, config interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
return och, nil
})
assert.Error(err).IsNil()

View File

@ -4,23 +4,16 @@ import (
"github.com/v2ray/v2ray-core/app"
"github.com/v2ray/v2ray-core/common/dice"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/retry"
"github.com/v2ray/v2ray-core/proxy"
proxyrepo "github.com/v2ray/v2ray-core/proxy/repo"
)
type InboundConnectionHandlerWithPort struct {
port v2net.Port
listen v2net.Address
handler proxy.InboundHandler
}
// Handler for inbound detour connections.
type InboundDetourHandlerAlways struct {
space app.Space
config *InboundDetourConfig
ich []*InboundConnectionHandlerWithPort
ich []proxy.InboundHandler
}
func NewInboundDetourHandlerAlways(space app.Space, config *InboundDetourConfig) (*InboundDetourHandlerAlways, error) {
@ -29,31 +22,30 @@ func NewInboundDetourHandlerAlways(space app.Space, config *InboundDetourConfig)
config: config,
}
ports := config.PortRange
handler.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.To-ports.From+1)
handler.ich = make([]proxy.InboundHandler, 0, ports.To-ports.From+1)
for i := ports.From; i <= ports.To; i++ {
ichConfig := config.Settings
ich, err := proxyrepo.CreateInboundHandler(config.Protocol, space, ichConfig, config.ListenOn, i)
ich, err := proxyrepo.CreateInboundHandler(config.Protocol, space, ichConfig, &proxy.InboundHandlerMeta{
Address: config.ListenOn,
Port: i,
Tag: config.Tag})
if err != nil {
log.Error("Failed to create inbound connection handler: ", err)
return nil, err
}
handler.ich = append(handler.ich, &InboundConnectionHandlerWithPort{
port: i,
handler: ich,
listen: config.ListenOn,
})
handler.ich = append(handler.ich, ich)
}
return handler, nil
}
func (this *InboundDetourHandlerAlways) GetConnectionHandler() (proxy.InboundHandler, int) {
ich := this.ich[dice.Roll(len(this.ich))]
return ich.handler, this.config.Allocation.Refresh
return ich, this.config.Allocation.Refresh
}
func (this *InboundDetourHandlerAlways) Close() {
for _, ich := range this.ich {
ich.handler.Close()
ich.Close()
}
}
@ -61,9 +53,9 @@ func (this *InboundDetourHandlerAlways) Close() {
func (this *InboundDetourHandlerAlways) Start() error {
for _, ich := range this.ich {
err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
err := ich.handler.Start()
err := ich.Start()
if err != nil {
log.Error("Failed to start inbound detour on port ", ich.port, ": ", err)
log.Error("Failed to start inbound detour:", err)
return err
}
return nil

View File

@ -30,7 +30,10 @@ func NewInboundDetourHandlerDynamic(space app.Space, config *InboundDetourConfig
handler.ichs = make([]proxy.InboundHandler, config.Allocation.Concurrency)
// To test configuration
ich, err := proxyrepo.CreateInboundHandler(config.Protocol, space, config.Settings, config.ListenOn, 0)
ich, err := proxyrepo.CreateInboundHandler(config.Protocol, space, config.Settings, &proxy.InboundHandlerMeta{
Address: config.ListenOn,
Port: 0,
Tag: config.Tag})
if err != nil {
log.Error("Point: Failed to create inbound connection handler: ", err)
return nil, err
@ -80,7 +83,8 @@ func (this *InboundDetourHandlerDynamic) refresh() error {
for idx, _ := range newIchs {
port := this.pickUnusedPort()
ich, err := proxyrepo.CreateInboundHandler(config.Protocol, this.space, config.Settings, config.ListenOn, port)
ich, err := proxyrepo.CreateInboundHandler(config.Protocol, this.space, config.Settings, &proxy.InboundHandlerMeta{
Address: config.ListenOn, Port: port, Tag: config.Tag})
if err != nil {
log.Error("Point: Failed to create inbound connection handler: ", err)
return err

View File

@ -91,7 +91,11 @@ func NewPoint(pConfig *Config) (*Point, error) {
vpoint.space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(vpoint.space))
ichConfig := pConfig.InboundConfig.Settings
ich, err := proxyrepo.CreateInboundHandler(pConfig.InboundConfig.Protocol, vpoint.space, ichConfig, pConfig.InboundConfig.ListenOn, vpoint.port)
ich, err := proxyrepo.CreateInboundHandler(
pConfig.InboundConfig.Protocol, vpoint.space, ichConfig, &proxy.InboundHandlerMeta{
Tag: "system.inbound",
Address: pConfig.InboundConfig.ListenOn,
Port: vpoint.port})
if err != nil {
log.Error("Failed to create inbound connection handler: ", err)
return nil, err
@ -99,7 +103,10 @@ func NewPoint(pConfig *Config) (*Point, error) {
vpoint.ich = ich
ochConfig := pConfig.OutboundConfig.Settings
och, err := proxyrepo.CreateOutboundHandler(pConfig.OutboundConfig.Protocol, vpoint.space, ochConfig, pConfig.OutboundConfig.SendThrough)
och, err := proxyrepo.CreateOutboundHandler(
pConfig.OutboundConfig.Protocol, vpoint.space, ochConfig, &proxy.OutboundHandlerMeta{
Tag: "system.outbound",
Address: pConfig.OutboundConfig.SendThrough})
if err != nil {
log.Error("Failed to create outbound connection handler: ", err)
return nil, err
@ -144,7 +151,10 @@ func NewPoint(pConfig *Config) (*Point, error) {
if len(outboundDetours) > 0 {
vpoint.odh = make(map[string]proxy.OutboundHandler)
for _, detourConfig := range outboundDetours {
detourHandler, err := proxyrepo.CreateOutboundHandler(detourConfig.Protocol, vpoint.space, detourConfig.Settings, detourConfig.SendThrough)
detourHandler, err := proxyrepo.CreateOutboundHandler(
detourConfig.Protocol, vpoint.space, detourConfig.Settings, &proxy.OutboundHandlerMeta{
Tag: detourConfig.Tag,
Address: detourConfig.SendThrough})
if err != nil {
log.Error("Point: Failed to create detour outbound connection handler: ", err)
return nil, err