func TestDnsAdd(t *testing.T) { assert := assert.On(t) space := app.NewSpace() outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager() outboundHandlerManager.SetDefaultHandler( freedom.NewFreedomConnection( &freedom.Config{}, space, &proxy.OutboundHandlerMeta{ Address: v2net.AnyIP, StreamSettings: &internet.StreamSettings{ Type: internet.StreamConnectionTypeRawTCP, }, })) space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager) space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) domain := "local.v2ray.com" server := NewCacheServer(space, &Config{ NameServers: []v2net.Destination{ v2net.UDPDestination(v2net.IPAddress([]byte{8, 8, 8, 8}), v2net.Port(53)), }, }) space.BindApp(APP_ID, server) space.Initialize() ips := server.Get(domain) assert.Int(len(ips)).Equals(1) assert.IP(ips[0].To4()).Equals(net.IP([]byte{127, 0, 0, 1})) }
func TestProxyDial(t *testing.T) { assert := assert.On(t) space := app.NewSpace() outboundManager := outbound.New() outboundManager.SetHandler("tag", freedom.NewFreedomConnection(&freedom.Config{}, space, &proxy.OutboundHandlerMeta{ Tag: "tag", StreamSettings: &internet.StreamConfig{ Network: v2net.Network_RawTCP, }, })) space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundManager) proxy := NewOutboundProxy(space) space.BindApp(APP_ID, proxy) assert.Error(space.Initialize()).IsNil() xor := func(b []byte) []byte { for idx, x := range b { b[idx] = x ^ 'c' } return b } tcpServer := &tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() assert.Error(err).IsNil() conn, err := proxy.Dial(v2net.LocalHostIP, dest, internet.DialerOptions{ Stream: &internet.StreamConfig{ Network: v2net.Network_RawTCP, }, Proxy: &internet.ProxyConfig{ Tag: "tag", }, }) assert.Error(err).IsNil() _, err = conn.Write([]byte{'a', 'b', 'c', 'd'}) assert.Error(err).IsNil() b := make([]byte, 10) nBytes, err := conn.Read(b) assert.Error(err).IsNil() assert.Bytes(xor(b[:nBytes])).Equals([]byte{'a', 'b', 'c', 'd'}) conn.Close() tcpServer.Close() }
func TestDokodemoUDP(t *testing.T) { assert := assert.On(t) udpServer := &udp.Server{ 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.NewFreedomConnection( &freedom.Config{}, space, &proxy.OutboundHandlerMeta{ Address: v2net.AnyIP, StreamSettings: &internet.StreamConfig{ Network: v2net.Network_RawTCP, }})) space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, ohm) data2Send := "Data to be sent to remote." port := v2net.Port(dice.Roll(20000) + 10000) dokodemo := NewDokodemoDoor(&Config{ Address: &v2net.IPOrDomain{ Address: &v2net.IPOrDomain_Ip{ Ip: v2net.LocalHostIP.IP(), }, }, Port: uint32(udpServer.Port), NetworkList: v2net.Network_UDP.AsList(), Timeout: 600, }, space, &proxy.InboundHandlerMeta{ Address: v2net.LocalHostIP, Port: port, StreamSettings: &internet.StreamConfig{ Network: v2net.Network_RawTCP, }}) defer dokodemo.Close() assert.Error(space.Initialize()).IsNil() err = dokodemo.Start() assert.Error(err).IsNil() assert.Port(port).Equals(dokodemo.Port()) udpClient, err := net.DialUDP("udp", nil, &net.UDPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(port), Zone: "", }) assert.Error(err).IsNil() defer udpClient.Close() udpClient.Write([]byte(data2Send)) response := make([]byte, 1024) nBytes, addr, err := udpClient.ReadFromUDP(response) assert.Error(err).IsNil() assert.IP(addr.IP).Equals(v2net.LocalHostIP.IP()) assert.Bytes(response[:nBytes]).Equals([]byte("Processed: " + data2Send)) }