mirror of https://github.com/v2ray/v2ray-core
				
				
				
			Fix dokodemo for space
							parent
							
								
									54f98fe11b
								
							
						
					
					
						commit
						ec610494ea
					
				| 
						 | 
				
			
			@ -3,12 +3,14 @@ package dokodemo
 | 
			
		|||
import (
 | 
			
		||||
	"sync"
 | 
			
		||||
 | 
			
		||||
	"github.com/v2ray/v2ray-core/app"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/app/dispatcher"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/common/alloc"
 | 
			
		||||
	v2io "github.com/v2ray/v2ray-core/common/io"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/common/log"
 | 
			
		||||
	v2net "github.com/v2ray/v2ray-core/common/net"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/proxy"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/proxy/internal"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/transport/hub"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -26,13 +28,21 @@ type DokodemoDoor struct {
 | 
			
		|||
	listeningPort    v2net.Port
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewDokodemoDoor(config *Config, packetDispatcher dispatcher.PacketDispatcher) *DokodemoDoor {
 | 
			
		||||
	return &DokodemoDoor{
 | 
			
		||||
		config:           config,
 | 
			
		||||
		packetDispatcher: packetDispatcher,
 | 
			
		||||
		address:          config.Address,
 | 
			
		||||
		port:             config.Port,
 | 
			
		||||
func NewDokodemoDoor(config *Config, space app.Space) *DokodemoDoor {
 | 
			
		||||
	d := &DokodemoDoor{
 | 
			
		||||
		config:  config,
 | 
			
		||||
		address: config.Address,
 | 
			
		||||
		port:    config.Port,
 | 
			
		||||
	}
 | 
			
		||||
	space.InitializeApplication(func() error {
 | 
			
		||||
		if !space.HasApp(dispatcher.APP_ID) {
 | 
			
		||||
			log.Error("Dokodemo: Dispatcher is not found in the space.")
 | 
			
		||||
			return app.ErrorMissingApplication
 | 
			
		||||
		}
 | 
			
		||||
		d.packetDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
 | 
			
		||||
		return nil
 | 
			
		||||
	})
 | 
			
		||||
	return d
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *DokodemoDoor) Port() v2net.Port {
 | 
			
		||||
| 
						 | 
				
			
			@ -153,3 +163,10 @@ func (this *DokodemoDoor) HandleTCPConnection(conn *hub.Connection) {
 | 
			
		|||
	outputFinish.Lock()
 | 
			
		||||
	inputFinish.Lock()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	internal.MustRegisterInboundHandlerCreator("dokodemo-door",
 | 
			
		||||
		func(space app.Space, rawConfig interface{}) (proxy.InboundHandler, error) {
 | 
			
		||||
			return NewDokodemoDoor(rawConfig.(*Config), space), nil
 | 
			
		||||
		})
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,21 +0,0 @@
 | 
			
		|||
package dokodemo
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/v2ray/v2ray-core/app"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/app/dispatcher"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/proxy"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/proxy/internal"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	internal.MustRegisterInboundHandlerCreator("dokodemo-door",
 | 
			
		||||
		func(space app.Space, rawConfig interface{}) (proxy.InboundHandler, error) {
 | 
			
		||||
			config := rawConfig.(*Config)
 | 
			
		||||
			if !space.HasApp(dispatcher.APP_ID) {
 | 
			
		||||
				return nil, internal.ErrorBadConfiguration
 | 
			
		||||
			}
 | 
			
		||||
			return NewDokodemoDoor(
 | 
			
		||||
				config,
 | 
			
		||||
				space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)), nil
 | 
			
		||||
		})
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -4,32 +4,58 @@ import (
 | 
			
		|||
	"net"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	testdispatcher "github.com/v2ray/v2ray-core/app/dispatcher/testing"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/app"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/app/dispatcher"
 | 
			
		||||
	dispatchers "github.com/v2ray/v2ray-core/app/dispatcher/impl"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/app/proxyman"
 | 
			
		||||
	v2net "github.com/v2ray/v2ray-core/common/net"
 | 
			
		||||
	v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
 | 
			
		||||
	netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
 | 
			
		||||
	. "github.com/v2ray/v2ray-core/proxy/dokodemo"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/proxy/freedom"
 | 
			
		||||
	v2testing "github.com/v2ray/v2ray-core/testing"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/testing/assert"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/testing/servers/tcp"
 | 
			
		||||
	"github.com/v2ray/v2ray-core/testing/servers/udp"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestDokodemoTCP(t *testing.T) {
 | 
			
		||||
	v2testing.Current(t)
 | 
			
		||||
 | 
			
		||||
	testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
 | 
			
		||||
	tcpServer := &tcp.Server{
 | 
			
		||||
		Port: v2nettesting.PickPort(),
 | 
			
		||||
		MsgProcessor: func(data []byte) []byte {
 | 
			
		||||
			buffer := make([]byte, 0, 2048)
 | 
			
		||||
			buffer = append(buffer, []byte("Processed: ")...)
 | 
			
		||||
			buffer = append(buffer, data...)
 | 
			
		||||
			return buffer
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	_, err := tcpServer.Start()
 | 
			
		||||
	assert.Error(err).IsNil()
 | 
			
		||||
 | 
			
		||||
	defer tcpServer.Close()
 | 
			
		||||
 | 
			
		||||
	space := app.NewSpace()
 | 
			
		||||
	space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
 | 
			
		||||
	ohm := proxyman.NewDefaultOutboundHandlerManager()
 | 
			
		||||
	ohm.SetDefaultHandler(&freedom.FreedomConnection{})
 | 
			
		||||
	space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, ohm)
 | 
			
		||||
 | 
			
		||||
	data2Send := "Data to be sent to remote."
 | 
			
		||||
 | 
			
		||||
	dokodemo := NewDokodemoDoor(&Config{
 | 
			
		||||
		Address: v2net.IPAddress([]byte{1, 2, 3, 4}),
 | 
			
		||||
		Port:    128,
 | 
			
		||||
		Address: v2net.LocalHostIP,
 | 
			
		||||
		Port:    tcpServer.Port,
 | 
			
		||||
		Network: v2net.TCPNetwork.AsList(),
 | 
			
		||||
		Timeout: 600,
 | 
			
		||||
	}, testPacketDispatcher)
 | 
			
		||||
	}, space)
 | 
			
		||||
	defer dokodemo.Close()
 | 
			
		||||
 | 
			
		||||
	assert.Error(space.Initialize()).IsNil()
 | 
			
		||||
 | 
			
		||||
	port := v2nettesting.PickPort()
 | 
			
		||||
	err := dokodemo.Listen(port)
 | 
			
		||||
	err = dokodemo.Listen(port)
 | 
			
		||||
	assert.Error(err).IsNil()
 | 
			
		||||
	netassert.Port(port).Equals(dokodemo.Port())
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -43,36 +69,51 @@ func TestDokodemoTCP(t *testing.T) {
 | 
			
		|||
	tcpClient.Write([]byte(data2Send))
 | 
			
		||||
	tcpClient.CloseWrite()
 | 
			
		||||
 | 
			
		||||
	destination := <-testPacketDispatcher.Destination
 | 
			
		||||
 | 
			
		||||
	response := make([]byte, 1024)
 | 
			
		||||
	nBytes, err := tcpClient.Read(response)
 | 
			
		||||
	assert.Error(err).IsNil()
 | 
			
		||||
	tcpClient.Close()
 | 
			
		||||
 | 
			
		||||
	assert.StringLiteral("Processed: " + data2Send).Equals(string(response[:nBytes]))
 | 
			
		||||
	assert.Bool(destination.IsTCP()).IsTrue()
 | 
			
		||||
	netassert.Address(destination.Address()).Equals(v2net.IPAddress([]byte{1, 2, 3, 4}))
 | 
			
		||||
	netassert.Port(destination.Port()).Equals(128)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestDokodemoUDP(t *testing.T) {
 | 
			
		||||
	v2testing.Current(t)
 | 
			
		||||
 | 
			
		||||
	testPacketDispatcher := testdispatcher.NewTestPacketDispatcher(nil)
 | 
			
		||||
	udpServer := &udp.Server{
 | 
			
		||||
		Port: v2nettesting.PickPort(),
 | 
			
		||||
		MsgProcessor: func(data []byte) []byte {
 | 
			
		||||
			buffer := make([]byte, 0, 2048)
 | 
			
		||||
			buffer = append(buffer, []byte("Processed: ")...)
 | 
			
		||||
			buffer = append(buffer, data...)
 | 
			
		||||
			return buffer
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	_, err := udpServer.Start()
 | 
			
		||||
	assert.Error(err).IsNil()
 | 
			
		||||
 | 
			
		||||
	defer udpServer.Close()
 | 
			
		||||
 | 
			
		||||
	space := app.NewSpace()
 | 
			
		||||
	space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))
 | 
			
		||||
	ohm := proxyman.NewDefaultOutboundHandlerManager()
 | 
			
		||||
	ohm.SetDefaultHandler(&freedom.FreedomConnection{})
 | 
			
		||||
	space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, ohm)
 | 
			
		||||
 | 
			
		||||
	data2Send := "Data to be sent to remote."
 | 
			
		||||
 | 
			
		||||
	dokodemo := NewDokodemoDoor(&Config{
 | 
			
		||||
		Address: v2net.IPAddress([]byte{5, 6, 7, 8}),
 | 
			
		||||
		Port:    256,
 | 
			
		||||
		Address: v2net.LocalHostIP,
 | 
			
		||||
		Port:    udpServer.Port,
 | 
			
		||||
		Network: v2net.UDPNetwork.AsList(),
 | 
			
		||||
		Timeout: 600,
 | 
			
		||||
	}, testPacketDispatcher)
 | 
			
		||||
	}, space)
 | 
			
		||||
	defer dokodemo.Close()
 | 
			
		||||
 | 
			
		||||
	assert.Error(space.Initialize()).IsNil()
 | 
			
		||||
 | 
			
		||||
	port := v2nettesting.PickPort()
 | 
			
		||||
	err := dokodemo.Listen(port)
 | 
			
		||||
	err = dokodemo.Listen(port)
 | 
			
		||||
	assert.Error(err).IsNil()
 | 
			
		||||
	netassert.Port(port).Equals(dokodemo.Port())
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -82,13 +123,13 @@ func TestDokodemoUDP(t *testing.T) {
 | 
			
		|||
		Zone: "",
 | 
			
		||||
	})
 | 
			
		||||
	assert.Error(err).IsNil()
 | 
			
		||||
	defer udpClient.Close()
 | 
			
		||||
 | 
			
		||||
	udpClient.Write([]byte(data2Send))
 | 
			
		||||
	udpClient.Close()
 | 
			
		||||
 | 
			
		||||
	destination := <-testPacketDispatcher.Destination
 | 
			
		||||
 | 
			
		||||
	assert.Bool(destination.IsUDP()).IsTrue()
 | 
			
		||||
	netassert.Address(destination.Address()).Equals(v2net.IPAddress([]byte{5, 6, 7, 8}))
 | 
			
		||||
	netassert.Port(destination.Port()).Equals(256)
 | 
			
		||||
	response := make([]byte, 1024)
 | 
			
		||||
	nBytes, addr, err := udpClient.ReadFromUDP(response)
 | 
			
		||||
	assert.Error(err).IsNil()
 | 
			
		||||
	netassert.IP(addr.IP).Equals(v2net.LocalHostIP.IP())
 | 
			
		||||
	assert.Bytes(response[:nBytes]).Equals([]byte("Processed: " + data2Send))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue