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
+2 -1
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) {
+13 -15
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
})
}