1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-12 23:18:36 -04:00

support using specific address

This commit is contained in:
v2ray 2016-06-04 00:38:22 +02:00
parent 6c31ff91e6
commit a4d76dc394
48 changed files with 412 additions and 301 deletions

View File

@ -20,7 +20,7 @@ func TestDnsAdd(t *testing.T) {
space := app.NewSpace()
outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager()
outboundHandlerManager.SetDefaultHandler(freedom.NewFreedomConnection(&freedom.Config{}, space))
outboundHandlerManager.SetDefaultHandler(freedom.NewFreedomConnection(&freedom.Config{}, space, v2net.AnyIP))
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager)
space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))

View File

@ -22,6 +22,10 @@ func (this Network) AsList() *NetworkList {
return &list
}
func (this Network) String() string {
return string(this)
}
// NetworkList is a list of Networks.
type NetworkList []Network

View File

@ -6,5 +6,5 @@ import (
)
func PickPort() v2net.Port {
return v2net.Port(30000 + dice.Roll(5000))
return v2net.Port(30000 + dice.Roll(20000))
}

View File

@ -50,7 +50,7 @@ func NewTimedUserValidator(hasher IDHash) UserValidator {
hasher: hasher,
cancel: signal.NewCloseSignal(),
}
go tus.updateUserHash(time.Tick(updateIntervalSec*time.Second), tus.cancel)
go tus.updateUserHash(updateIntervalSec*time.Second, tus.cancel)
return tus
}
@ -88,11 +88,11 @@ func (this *TimedUserValidator) generateNewHashes(nowSec Timestamp, idx int, ent
}
}
func (this *TimedUserValidator) updateUserHash(tick <-chan time.Time, cancel *signal.CancelSignal) {
func (this *TimedUserValidator) updateUserHash(interval time.Duration, cancel *signal.CancelSignal) {
L:
for {
select {
case now := <-tick:
case now := <-time.After(interval):
nowSec := Timestamp(now.Unix() + cacheDurationSec)
for _, entry := range this.ids {
this.generateNewHashes(nowSec, entry.userIdx, entry)

View File

@ -31,7 +31,7 @@ func (this *BlackHole) Dispatch(destination v2net.Destination, payload *alloc.Bu
func init() {
internal.MustRegisterOutboundHandlerCreator("blackhole",
func(space app.Space, config interface{}) (proxy.OutboundHandler, error) {
func(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
return NewBlackHole(), nil
})
}

View File

@ -29,11 +29,13 @@ type DokodemoDoor struct {
listeningAddress v2net.Address
}
func NewDokodemoDoor(config *Config, space app.Space) *DokodemoDoor {
func NewDokodemoDoor(config *Config, space app.Space, listen v2net.Address, port v2net.Port) *DokodemoDoor {
d := &DokodemoDoor{
config: config,
address: config.Address,
port: config.Port,
config: config,
address: config.Address,
port: config.Port,
listeningAddress: listen,
listeningPort: port,
}
space.InitializeApplication(func() error {
if !space.HasApp(dispatcher.APP_ID) {
@ -66,26 +68,20 @@ func (this *DokodemoDoor) Close() {
}
}
func (this *DokodemoDoor) Listen(address v2net.Address, port v2net.Port) error {
func (this *DokodemoDoor) Start() error {
if this.accepting {
if this.listeningPort == port && this.listeningAddress.Equals(address) {
return nil
} else {
return proxy.ErrorAlreadyListening
}
return nil
}
this.listeningPort = port
this.listeningAddress = address
this.accepting = true
if this.config.Network.HasNetwork(v2net.TCPNetwork) {
err := this.ListenTCP(address, port)
err := this.ListenTCP(this.listeningAddress, this.listeningPort)
if err != nil {
return err
}
}
if this.config.Network.HasNetwork(v2net.UDPNetwork) {
err := this.ListenUDP(address, port)
err := this.ListenUDP(this.listeningAddress, this.listeningPort)
if err != nil {
return err
}
@ -168,7 +164,7 @@ func (this *DokodemoDoor) HandleTCPConnection(conn *hub.Connection) {
func init() {
internal.MustRegisterInboundHandlerCreator("dokodemo-door",
func(space app.Space, rawConfig interface{}) (proxy.InboundHandler, error) {
return NewDokodemoDoor(rawConfig.(*Config), space), nil
func(space app.Space, rawConfig interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) {
return NewDokodemoDoor(rawConfig.(*Config), space, listen, port), nil
})
}

View File

@ -37,23 +37,23 @@ 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))
ohm.SetDefaultHandler(freedom.NewFreedomConnection(&freedom.Config{}, space, v2net.LocalHostIP))
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, ohm)
data2Send := "Data to be sent to remote."
port := v2nettesting.PickPort()
dokodemo := NewDokodemoDoor(&Config{
Address: v2net.LocalHostIP,
Port: tcpServer.Port,
Network: v2net.TCPNetwork.AsList(),
Timeout: 600,
}, space)
}, space, v2net.LocalHostIP, port)
defer dokodemo.Close()
assert.Error(space.Initialize()).IsNil()
port := v2nettesting.PickPort()
err = dokodemo.Listen(v2net.LocalHostIP, port)
err = dokodemo.Start()
assert.Error(err).IsNil()
assert.Port(port).Equals(dokodemo.Port())
@ -95,23 +95,23 @@ 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))
ohm.SetDefaultHandler(freedom.NewFreedomConnection(&freedom.Config{}, space, v2net.AnyIP))
space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, ohm)
data2Send := "Data to be sent to remote."
port := v2nettesting.PickPort()
dokodemo := NewDokodemoDoor(&Config{
Address: v2net.LocalHostIP,
Port: udpServer.Port,
Network: v2net.UDPNetwork.AsList(),
Timeout: 600,
}, space)
}, space, v2net.LocalHostIP, port)
defer dokodemo.Close()
assert.Error(space.Initialize()).IsNil()
port := v2nettesting.PickPort()
err = dokodemo.Listen(v2net.LocalHostIP, port)
err = dokodemo.Start()
assert.Error(err).IsNil()
assert.Port(port).Equals(dokodemo.Port())

View File

@ -23,12 +23,14 @@ type FreedomConnection struct {
domainStrategy DomainStrategy
timeout uint32
dns dns.Server
sendThrough v2net.Address
}
func NewFreedomConnection(config *Config, space app.Space) *FreedomConnection {
func NewFreedomConnection(config *Config, space app.Space, sendThrough v2net.Address) *FreedomConnection {
f := &FreedomConnection{
domainStrategy: config.DomainStrategy,
timeout: config.Timeout,
sendThrough: sendThrough,
}
space.InitializeApplication(func() error {
if config.DomainStrategy == DomainStrategyUseIP {
@ -78,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(destination)
rawConn, err := hub.DialWithoutCache(this.sendThrough, destination)
if err != nil {
return err
}
@ -138,7 +140,7 @@ func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *
func init() {
internal.MustRegisterOutboundHandlerCreator("freedom",
func(space app.Space, config interface{}) (proxy.OutboundHandler, error) {
return NewFreedomConnection(config.(*Config), space), nil
func(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
return NewFreedomConnection(config.(*Config), space, sendThrough), nil
})
}

View File

@ -37,7 +37,7 @@ func TestSinglePacket(t *testing.T) {
assert.Error(err).IsNil()
space := app.NewSpace()
freedom := NewFreedomConnection(&Config{}, space)
freedom := NewFreedomConnection(&Config{}, space, v2net.AnyIP)
space.Initialize()
traffic := ray.NewRay()
@ -57,7 +57,7 @@ func TestSinglePacket(t *testing.T) {
func TestUnreachableDestination(t *testing.T) {
assert := assert.On(t)
freedom := NewFreedomConnection(&Config{}, app.NewSpace())
freedom := NewFreedomConnection(&Config{}, app.NewSpace(), v2net.AnyIP)
traffic := ray.NewRay()
data2Send := "Data to be sent to remote"
payload := alloc.NewSmallBuffer().Clear().Append([]byte(data2Send))
@ -81,7 +81,7 @@ func TestIPResolution(t *testing.T) {
})
space.BindApp(dns.APP_ID, dnsServer)
freedom := NewFreedomConnection(&Config{DomainStrategy: DomainStrategyUseIP}, space)
freedom := NewFreedomConnection(&Config{DomainStrategy: DomainStrategyUseIP}, space, v2net.AnyIP)
space.Initialize()

View File

@ -33,10 +33,12 @@ type Server struct {
listeningAddress v2net.Address
}
func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher) *Server {
func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, listen v2net.Address, port v2net.Port) *Server {
return &Server{
packetDispatcher: packetDispatcher,
config: config,
listeningAddress: listen,
listeningPort: port,
}
}
@ -54,24 +56,18 @@ func (this *Server) Close() {
}
}
func (this *Server) Listen(address v2net.Address, port v2net.Port) error {
func (this *Server) Start() error {
if this.accepting {
if this.listeningPort == port && this.listeningAddress.Equals(address) {
return nil
} else {
return proxy.ErrorAlreadyListening
}
return nil
}
this.listeningPort = port
this.listeningAddress = address
var tlsConfig *tls.Config
if this.config.TLSConfig != nil {
tlsConfig = this.config.TLSConfig.GetConfig()
}
tcpListener, err := hub.ListenTCP(address, port, this.handleConnection, tlsConfig)
tcpListener, err := hub.ListenTCP(this.listeningAddress, this.listeningPort, this.handleConnection, tlsConfig)
if err != nil {
log.Error("HTTP: Failed listen on port ", port, ": ", err)
log.Error("HTTP: Failed listen on port ", this.listeningPort, ": ", err)
return err
}
this.Lock()
@ -276,12 +272,14 @@ func (this *Server) handlePlainHTTP(request *http.Request, dest v2net.Destinatio
func init() {
internal.MustRegisterInboundHandlerCreator("http",
func(space app.Space, rawConfig interface{}) (proxy.InboundHandler, error) {
func(space app.Space, rawConfig interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) {
if !space.HasApp(dispatcher.APP_ID) {
return nil, internal.ErrorBadConfiguration
}
return NewServer(
rawConfig.(*Config),
space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)), nil
space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher),
listen,
port), nil
})
}

View File

@ -52,11 +52,11 @@ func TestNormalGetRequest(t *testing.T) {
testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
httpProxy := NewServer(&Config{}, testPacketDispatcher)
port := v2nettesting.PickPort()
httpProxy := NewServer(&Config{}, testPacketDispatcher, v2net.LocalHostIP, port)
defer httpProxy.Close()
port := v2nettesting.PickPort()
err := httpProxy.Listen(v2net.LocalHostIP, port)
err := httpProxy.Start()
assert.Error(err).IsNil()
assert.Port(port).Equals(httpProxy.Port())

View File

@ -2,8 +2,9 @@ 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{}) (proxy.InboundHandler, error)
type OutboundHandlerCreator func(space app.Space, config interface{}) (proxy.OutboundHandler, error)
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)

View File

@ -4,6 +4,7 @@ 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"
)
@ -45,7 +46,7 @@ func MustRegisterOutboundHandlerCreator(name string, creator OutboundHandlerCrea
}
}
func CreateInboundHandler(name string, space app.Space, rawConfig []byte) (proxy.InboundHandler, error) {
func CreateInboundHandler(name string, space app.Space, rawConfig []byte, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) {
creator, found := inboundFactories[name]
if !found {
return nil, ErrorProxyNotFound
@ -55,12 +56,12 @@ func CreateInboundHandler(name string, space app.Space, rawConfig []byte) (proxy
if err != nil {
return nil, err
}
return creator(space, proxyConfig)
return creator(space, proxyConfig, listen, port)
}
return creator(space, nil)
return creator(space, nil, listen, port)
}
func CreateOutboundHandler(name string, space app.Space, rawConfig []byte) (proxy.OutboundHandler, error) {
func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
creator, found := outboundFactories[name]
if !found {
return nil, ErrorNameExists
@ -71,8 +72,8 @@ func CreateOutboundHandler(name string, space app.Space, rawConfig []byte) (prox
if err != nil {
return nil, err
}
return creator(space, proxyConfig)
return creator(space, proxyConfig, sendThrough)
}
return creator(space, nil)
return creator(space, nil, sendThrough)
}

View File

@ -16,8 +16,8 @@ const (
// An InboundHandler handles inbound network connections to V2Ray.
type InboundHandler interface {
// Listen starts a InboundHandler by listen on a specific port.
Listen(on v2net.Address, port v2net.Port) error
// Listen starts a InboundHandler.
Start() error
// Close stops the handler to accepting anymore inbound connections.
Close()
// Port returns the port that the handler is listening on.

View File

@ -2,14 +2,15 @@ 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) (proxy.InboundHandler, error) {
return internal.CreateInboundHandler(name, space, rawConfig)
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 CreateOutboundHandler(name string, space app.Space, rawConfig []byte) (proxy.OutboundHandler, error) {
return internal.CreateOutboundHandler(name, space, rawConfig)
func CreateOutboundHandler(name string, space app.Space, rawConfig []byte, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
return internal.CreateOutboundHandler(name, space, rawConfig, sendThrough)
}

View File

@ -1,6 +1,7 @@
package shadowsocks_test
import (
"io"
"testing"
"github.com/v2ray/v2ray-core/common/alloc"
@ -29,7 +30,7 @@ func TestEmptyPayload(t *testing.T) {
buffer := alloc.NewSmallBuffer().Clear()
_, err := ReadRequest(buffer, nil, false)
assert.Error(err).Equals(transport.ErrorCorruptedPacket)
assert.Error(err).Equals(io.EOF)
}
func TestSingleBytePayload(t *testing.T) {

View File

@ -30,10 +30,12 @@ type Server struct {
udpServer *hub.UDPServer
}
func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher) *Server {
func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, listen v2net.Address, port v2net.Port) *Server {
return &Server{
config: config,
packetDispatcher: packetDispatcher,
address: listen,
port: port,
}
}
@ -56,34 +58,28 @@ func (this *Server) Close() {
}
func (this *Server) Listen(address v2net.Address, port v2net.Port) error {
func (this *Server) Start() error {
if this.accepting {
if this.port == port && this.address.Equals(address) {
return nil
} else {
return proxy.ErrorAlreadyListening
}
return nil
}
tcpHub, err := hub.ListenTCP(address, port, this.handleConnection, nil)
tcpHub, err := hub.ListenTCP(this.address, this.port, this.handleConnection, nil)
if err != nil {
log.Error("Shadowsocks: Failed to listen TCP on port ", port, ": ", err)
log.Error("Shadowsocks: Failed to listen TCP on port ", this.port, ": ", err)
return err
}
this.tcpHub = tcpHub
if this.config.UDP {
this.udpServer = hub.NewUDPServer(this.packetDispatcher)
udpHub, err := hub.ListenUDP(address, port, this.handlerUDPPayload)
udpHub, err := hub.ListenUDP(this.address, this.port, this.handlerUDPPayload)
if err != nil {
log.Error("Shadowsocks: Failed to listen UDP on port ", port, ": ", err)
log.Error("Shadowsocks: Failed to listen UDP on port ", this.port, ": ", err)
return err
}
this.udpHub = udpHub
}
this.port = port
this.address = address
this.accepting = true
return nil
@ -256,12 +252,14 @@ func (this *Server) handleConnection(conn *hub.Connection) {
func init() {
internal.MustRegisterInboundHandlerCreator("shadowsocks",
func(space app.Space, rawConfig interface{}) (proxy.InboundHandler, error) {
func(space app.Space, rawConfig interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) {
if !space.HasApp(dispatcher.APP_ID) {
return nil, internal.ErrorBadConfiguration
}
return NewServer(
rawConfig.(*Config),
space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)), nil
space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher),
listen,
port), nil
})
}

View File

@ -38,10 +38,12 @@ type Server struct {
}
// NewServer creates a new Server object.
func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher) *Server {
func NewServer(config *Config, packetDispatcher dispatcher.PacketDispatcher, listen v2net.Address, port v2net.Port) *Server {
return &Server{
config: config,
packetDispatcher: packetDispatcher,
listeningAddress: listen,
listeningPort: port,
}
}
@ -68,20 +70,18 @@ func (this *Server) Close() {
}
// Listen implements InboundHandler.Listen().
func (this *Server) Listen(address v2net.Address, port v2net.Port) error {
func (this *Server) Start() error {
if this.accepting {
if this.listeningPort == port && this.listeningAddress.Equals(address) {
return nil
} else {
return proxy.ErrorAlreadyListening
}
return nil
}
this.listeningPort = port
this.listeningAddress = address
listener, err := hub.ListenTCP(address, port, this.handleConnection, nil)
listener, err := hub.ListenTCP(
this.listeningAddress,
this.listeningPort,
this.handleConnection,
nil)
if err != nil {
log.Error("Socks: failed to listen on port ", port, ": ", err)
log.Error("Socks: failed to listen on port ", this.listeningPort, ": ", err)
return err
}
this.accepting = true
@ -89,7 +89,7 @@ func (this *Server) Listen(address v2net.Address, port v2net.Port) error {
this.tcpListener = listener
this.tcpMutex.Unlock()
if this.config.UDPEnabled {
this.listenUDP(address, port)
this.listenUDP(this.listeningAddress, this.listeningPort)
}
return nil
}
@ -301,12 +301,14 @@ func (this *Server) transport(reader io.Reader, writer io.Writer, destination v2
func init() {
internal.MustRegisterInboundHandlerCreator("socks",
func(space app.Space, rawConfig interface{}) (proxy.InboundHandler, error) {
func(space app.Space, rawConfig interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) {
if !space.HasApp(dispatcher.APP_ID) {
return nil, internal.ErrorBadConfiguration
}
return NewServer(
rawConfig.(*Config),
space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)), nil
space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher),
listen,
port), nil
})
}

View File

@ -31,16 +31,17 @@ func TestSocksTcpConnect(t *testing.T) {
ConnInput: bytes.NewReader(connInput),
}
protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (v2proxy.OutboundHandler, error) {
return och, nil
})
protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och",
func(space app.Space, config interface{}, sendThrough v2net.Address) (v2proxy.OutboundHandler, error) {
return och, nil
})
assert.Error(err).IsNil()
config := &point.Config{
Port: port,
ListenOn: v2net.LocalHostIP,
InboundConfig: &point.ConnectionConfig{
Port: port,
InboundConfig: &point.InboundConnectionConfig{
Protocol: "socks",
ListenOn: v2net.LocalHostIP,
Settings: []byte(`
{
"auth": "noauth"
@ -51,7 +52,7 @@ func TestSocksTcpConnect(t *testing.T) {
v2net.UDPDestination(v2net.DomainAddress("localhost"), v2net.Port(53)),
},
},
OutboundConfig: &point.ConnectionConfig{
OutboundConfig: &point.OutboundConnectionConfig{
Protocol: protocol,
Settings: nil,
},
@ -96,16 +97,17 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) {
ConnOutput: connOutput,
}
protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (v2proxy.OutboundHandler, error) {
return och, nil
})
protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och",
func(space app.Space, config interface{}, sendThrough v2net.Address) (v2proxy.OutboundHandler, error) {
return och, nil
})
assert.Error(err).IsNil()
config := &point.Config{
Port: port,
ListenOn: v2net.LocalHostIP,
InboundConfig: &point.ConnectionConfig{
Port: port,
InboundConfig: &point.InboundConnectionConfig{
Protocol: "socks",
ListenOn: v2net.LocalHostIP,
Settings: []byte(`
{
"auth": "password",
@ -119,7 +121,7 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) {
v2net.UDPDestination(v2net.DomainAddress("localhost"), v2net.Port(53)),
},
},
OutboundConfig: &point.ConnectionConfig{
OutboundConfig: &point.OutboundConnectionConfig{
Protocol: protocol,
Settings: nil,
},
@ -164,16 +166,17 @@ func TestSocksTcpConnectWithWrongUserPass(t *testing.T) {
ConnOutput: connOutput,
}
protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (v2proxy.OutboundHandler, error) {
return och, nil
})
protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och",
func(space app.Space, config interface{}, sendThrough v2net.Address) (v2proxy.OutboundHandler, error) {
return och, nil
})
assert.Error(err).IsNil()
config := &point.Config{
Port: port,
ListenOn: v2net.LocalHostIP,
InboundConfig: &point.ConnectionConfig{
Port: port,
InboundConfig: &point.InboundConnectionConfig{
Protocol: "socks",
ListenOn: v2net.LocalHostIP,
Settings: []byte(`
{
"auth": "password",
@ -187,7 +190,7 @@ func TestSocksTcpConnectWithWrongUserPass(t *testing.T) {
v2net.UDPDestination(v2net.DomainAddress("localhost"), v2net.Port(53)),
},
},
OutboundConfig: &point.ConnectionConfig{
OutboundConfig: &point.OutboundConnectionConfig{
Protocol: protocol,
Settings: nil,
},
@ -218,15 +221,16 @@ func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) {
ConnOutput: connOutput,
}
protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (v2proxy.OutboundHandler, error) {
return och, nil
})
protocol, err := proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och",
func(space app.Space, config interface{}, sendThrough v2net.Address) (v2proxy.OutboundHandler, error) {
return och, nil
})
assert.Error(err).IsNil()
config := &point.Config{
Port: port,
ListenOn: v2net.LocalHostIP,
InboundConfig: &point.ConnectionConfig{
Port: port,
InboundConfig: &point.InboundConnectionConfig{
ListenOn: v2net.LocalHostIP,
Protocol: "socks",
Settings: []byte(`
{
@ -241,7 +245,7 @@ func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) {
v2net.UDPDestination(v2net.DomainAddress("localhost"), v2net.Port(53)),
},
},
OutboundConfig: &point.ConnectionConfig{
OutboundConfig: &point.OutboundConnectionConfig{
Protocol: protocol,
Settings: nil,
},

View File

@ -10,21 +10,19 @@ import (
)
type InboundConnectionHandler struct {
port v2net.Port
address v2net.Address
ListeningPort v2net.Port
ListeningAddress v2net.Address
PacketDispatcher dispatcher.PacketDispatcher
ConnInput io.Reader
ConnOutput io.Writer
}
func (this *InboundConnectionHandler) Listen(address v2net.Address, port v2net.Port) error {
this.port = port
this.address = address
func (this *InboundConnectionHandler) Start() error {
return nil
}
func (this *InboundConnectionHandler) Port() v2net.Port {
return this.port
return this.ListeningPort
}
func (this *InboundConnectionHandler) Close() {

View File

@ -50,6 +50,6 @@ func (this *OutboundConnectionHandler) Dispatch(destination v2net.Destination, p
return nil
}
func (this *OutboundConnectionHandler) Create(space app.Space, config interface{}) (proxy.OutboundHandler, error) {
func (this *OutboundConnectionHandler) Create(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
return this, nil
}

View File

@ -88,6 +88,8 @@ func (this *VMessInboundHandler) Close() {
this.Lock()
this.listener.Close()
this.listener = nil
this.clients.Release()
this.clients = nil
this.Unlock()
}
}
@ -100,20 +102,14 @@ func (this *VMessInboundHandler) GetUser(email string) *protocol.User {
return user
}
func (this *VMessInboundHandler) Listen(address v2net.Address, port v2net.Port) error {
func (this *VMessInboundHandler) Start() error {
if this.accepting {
if this.listeningPort == port && this.listeningAddress.Equals(address) {
return nil
} else {
return proxy.ErrorAlreadyListening
}
return nil
}
this.listeningPort = port
this.listeningAddress = address
tcpListener, err := hub.ListenTCP(address, port, this.HandleConnection, nil)
tcpListener, err := hub.ListenTCP(this.listeningAddress, this.listeningPort, this.HandleConnection, nil)
if err != nil {
log.Error("Unable to listen tcp port ", port, ": ", err)
log.Error("Unable to listen tcp port ", this.listeningPort, ": ", err)
return err
}
this.accepting = true
@ -224,7 +220,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.Connection) {
func init() {
internal.MustRegisterInboundHandlerCreator("vmess",
func(space app.Space, rawConfig interface{}) (proxy.InboundHandler, error) {
func(space app.Space, rawConfig interface{}, listen v2net.Address, port v2net.Port) (proxy.InboundHandler, error) {
if !space.HasApp(dispatcher.APP_ID) {
return nil, internal.ErrorBadConfiguration
}
@ -240,6 +236,8 @@ func init() {
clients: allowedClients,
detours: config.DetourConfig,
usersByEmail: NewUserByEmail(config.AllowedUsers, config.Defaults),
listeningAddress: listen,
listeningPort: port,
}
if space.HasApp(proxyman.APP_ID_INBOUND_MANAGER) {

View File

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

View File

@ -38,25 +38,28 @@ func TestVMessInAndOut(t *testing.T) {
ConnOutput: ichConnOutput,
}
protocol, err := proxytesting.RegisterInboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (proxy.InboundHandler, error) {
ich.PacketDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
return ich, nil
})
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
ich.PacketDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
return ich, nil
})
assert.Error(err).IsNil()
configA := &point.Config{
Port: portA,
ListenOn: v2net.LocalHostIP,
Port: portA,
DNSConfig: &dns.Config{
NameServers: []v2net.Destination{
v2net.UDPDestination(v2net.DomainAddress("localhost"), v2net.Port(53)),
},
},
InboundConfig: &point.ConnectionConfig{
InboundConfig: &point.InboundConnectionConfig{
Protocol: protocol,
ListenOn: v2net.LocalHostIP,
Settings: nil,
},
OutboundConfig: &point.ConnectionConfig{
OutboundConfig: &point.OutboundConnectionConfig{
Protocol: "vmess",
Settings: []byte(`{
"vnext": [
@ -85,28 +88,29 @@ func TestVMessInAndOut(t *testing.T) {
ConnOutput: ochConnOutput,
}
protocol, err = proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och", func(space app.Space, config interface{}) (proxy.OutboundHandler, error) {
return och, nil
})
protocol, err = proxytesting.RegisterOutboundConnectionHandlerCreator("mock_och",
func(space app.Space, config interface{}, sendThrough v2net.Address) (proxy.OutboundHandler, error) {
return och, nil
})
assert.Error(err).IsNil()
configB := &point.Config{
Port: portB,
ListenOn: v2net.LocalHostIP,
Port: portB,
DNSConfig: &dns.Config{
NameServers: []v2net.Destination{
v2net.UDPDestination(v2net.DomainAddress("localhost"), v2net.Port(53)),
},
},
InboundConfig: &point.ConnectionConfig{
InboundConfig: &point.InboundConnectionConfig{
Protocol: "vmess",
ListenOn: v2net.LocalHostIP,
Settings: []byte(`{
"clients": [
{"id": "` + testAccount.String() + `"}
]
}`),
},
OutboundConfig: &point.ConnectionConfig{
OutboundConfig: &point.OutboundConnectionConfig{
Protocol: protocol,
Settings: nil,
},

View File

@ -1,9 +1,10 @@
{
"port": 1080,
"log": {
"access": ""
"loglevel": "warning"
},
"inbound": {
"port": 1080,
"listen": "127.0.0.1",
"protocol": "socks",
"settings": {
"auth": "noauth",

View File

@ -1,11 +1,11 @@
{
"port": 10086,
"log" : {
"access": "/var/log/v2ray/access.log",
"error": "/var/log/v2ray/error.log",
"loglevel": "warning"
},
"inbound": {
"port": 10086,
"protocol": "vmess",
"settings": {
"clients": [

View File

@ -8,11 +8,19 @@ import (
"github.com/v2ray/v2ray-core/transport"
)
type ConnectionConfig struct {
type InboundConnectionConfig struct {
Port v2net.Port
ListenOn v2net.Address
Protocol string
Settings []byte
}
type OutboundConnectionConfig struct {
Protocol string
SendThrough v2net.Address
Settings []byte
}
type LogConfig struct {
AccessLog string
ErrorLog string
@ -41,19 +49,19 @@ type InboundDetourConfig struct {
}
type OutboundDetourConfig struct {
Protocol string
Tag string
Settings []byte
Protocol string
SendThrough v2net.Address
Tag string
Settings []byte
}
type Config struct {
Port v2net.Port
ListenOn v2net.Address
LogConfig *LogConfig
RouterConfig *router.Config
DNSConfig *dns.Config
InboundConfig *ConnectionConfig
OutboundConfig *ConnectionConfig
InboundConfig *InboundConnectionConfig
OutboundConfig *OutboundConnectionConfig
InboundDetours []*InboundDetourConfig
OutboundDetours []*OutboundDetourConfig
TransportConfig *transport.Config

View File

@ -22,29 +22,21 @@ const (
func (this *Config) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
Port v2net.Port `json:"port"` // Port of this Point server.
ListenOn *v2net.AddressJson `json:"listen"`
LogConfig *LogConfig `json:"log"`
RouterConfig *router.Config `json:"routing"`
DNSConfig *dns.Config `json:"dns"`
InboundConfig *ConnectionConfig `json:"inbound"`
OutboundConfig *ConnectionConfig `json:"outbound"`
InboundDetours []*InboundDetourConfig `json:"inboundDetour"`
OutboundDetours []*OutboundDetourConfig `json:"outboundDetour"`
Transport *transport.Config `json:"transport"`
Port v2net.Port `json:"port"` // Port of this Point server.
LogConfig *LogConfig `json:"log"`
RouterConfig *router.Config `json:"routing"`
DNSConfig *dns.Config `json:"dns"`
InboundConfig *InboundConnectionConfig `json:"inbound"`
OutboundConfig *OutboundConnectionConfig `json:"outbound"`
InboundDetours []*InboundDetourConfig `json:"inboundDetour"`
OutboundDetours []*OutboundDetourConfig `json:"outboundDetour"`
Transport *transport.Config `json:"transport"`
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
this.Port = jsonConfig.Port
this.ListenOn = v2net.AnyIP
if jsonConfig.ListenOn != nil {
if jsonConfig.ListenOn.Address.IsDomain() {
return errors.New("Point: Unable to listen on domain address: " + jsonConfig.ListenOn.Address.Domain())
}
this.ListenOn = jsonConfig.ListenOn.Address
}
this.LogConfig = jsonConfig.LogConfig
this.RouterConfig = jsonConfig.RouterConfig
this.InboundConfig = jsonConfig.InboundConfig
@ -63,10 +55,37 @@ func (this *Config) UnmarshalJSON(data []byte) error {
return nil
}
func (this *ConnectionConfig) UnmarshalJSON(data []byte) error {
func (this *InboundConnectionConfig) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
Port uint16 `json:"port"`
Listen *v2net.AddressJson `json:"listen"`
Protocol string `json:"protocol"`
Settings json.RawMessage `json:"settings"`
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
this.Port = v2net.Port(jsonConfig.Port)
this.ListenOn = v2net.AnyIP
if jsonConfig.Listen != nil {
if jsonConfig.Listen.Address.IsDomain() {
return errors.New("Point: Unable to listen on domain address: " + jsonConfig.Listen.Address.Domain())
}
this.ListenOn = jsonConfig.Listen.Address
}
this.Protocol = jsonConfig.Protocol
this.Settings = jsonConfig.Settings
return nil
}
func (this *OutboundConnectionConfig) UnmarshalJSON(data []byte) error {
type JsonConnectionConfig struct {
Protocol string `json:"protocol"`
Settings json.RawMessage `json:"settings"`
Protocol string `json:"protocol"`
SendThrough *v2net.AddressJson `json:"sendThrough"`
Settings json.RawMessage `json:"settings"`
}
jsonConfig := new(JsonConnectionConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
@ -74,6 +93,14 @@ func (this *ConnectionConfig) UnmarshalJSON(data []byte) error {
}
this.Protocol = jsonConfig.Protocol
this.Settings = jsonConfig.Settings
if jsonConfig.SendThrough != nil {
address := jsonConfig.SendThrough.Address
if address.IsDomain() {
return errors.New("Point: Unable to send through: " + address.String())
}
this.SendThrough = address
}
return nil
}
@ -173,9 +200,10 @@ func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
type JsonOutboundDetourConfig struct {
Protocol string `json:"protocol"`
Tag string `json:"tag"`
Settings json.RawMessage `json:"settings"`
Protocol string `json:"protocol"`
SendThrough *v2net.AddressJson `json:"sendThrough"`
Tag string `json:"tag"`
Settings json.RawMessage `json:"settings"`
}
jsonConfig := new(JsonOutboundDetourConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
@ -184,6 +212,14 @@ func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
this.Protocol = jsonConfig.Protocol
this.Tag = jsonConfig.Tag
this.Settings = jsonConfig.Settings
if jsonConfig.SendThrough != nil {
address := jsonConfig.SendThrough.Address
if address.IsDomain() {
return errors.New("Point: Unable to send through: " + address.String())
}
this.SendThrough = address
}
return nil
}

View File

@ -23,8 +23,8 @@ func TestClientSampleConfig(t *testing.T) {
pointConfig, err := LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json"))
assert.Error(err).IsNil()
assert.Port(pointConfig.Port).IsValid()
assert.Pointer(pointConfig.InboundConfig).IsNotNil()
assert.Port(pointConfig.InboundConfig.Port).IsValid()
assert.Pointer(pointConfig.OutboundConfig).IsNotNil()
assert.String(pointConfig.InboundConfig.Protocol).Equals("socks")
@ -43,8 +43,8 @@ func TestServerSampleConfig(t *testing.T) {
pointConfig, err := LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json"))
assert.Error(err).IsNil()
assert.Port(pointConfig.Port).IsValid()
assert.Pointer(pointConfig.InboundConfig).IsNotNil()
assert.Port(pointConfig.InboundConfig.Port).IsValid()
assert.Pointer(pointConfig.OutboundConfig).IsNotNil()
assert.String(pointConfig.InboundConfig.Protocol).Equals("vmess")

View File

@ -32,7 +32,7 @@ func NewInboundDetourHandlerAlways(space app.Space, config *InboundDetourConfig)
handler.ich = make([]*InboundConnectionHandlerWithPort, 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)
ich, err := proxyrepo.CreateInboundHandler(config.Protocol, space, ichConfig, config.ListenOn, i)
if err != nil {
log.Error("Failed to create inbound connection handler: ", err)
return nil, err
@ -61,7 +61,7 @@ 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.Listen(ich.listen, ich.port)
err := ich.handler.Start()
if err != nil {
log.Error("Failed to start inbound detour on port ", ich.port, ": ", err)
return err

View File

@ -8,7 +8,6 @@ import (
"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"
)
@ -18,8 +17,7 @@ type InboundDetourHandlerDynamic struct {
space app.Space
config *InboundDetourConfig