func TestIPResolution(t *testing.T) { assert := assert.On(t) space := app.NewSpace() space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outbound.New()) space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) r := router.NewRouter(&router.Config{}, space) space.BindApp(router.APP_ID, r) dnsServer := dnsserver.NewCacheServer(space, &dns.Config{ Hosts: map[string]*v2net.IPOrDomain{ "v2ray.com": v2net.NewIPOrDomain(v2net.LocalHostIP), }, }) space.BindApp(dns.APP_ID, dnsServer) freedom := NewFreedomConnection( &Config{DomainStrategy: Config_USE_IP}, space, &proxy.OutboundHandlerMeta{ Address: v2net.AnyIP, StreamSettings: &internet.StreamConfig{ Network: v2net.Network_RawTCP, }, }) space.Initialize() ipDest := freedom.ResolveIP(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), v2net.Port(80))) assert.Destination(ipDest).IsTCP() assert.Address(ipDest.Address).Equals(v2net.LocalHostIP) }
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 := outbound.New() 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.NewIPOrDomain(v2net.LocalHostIP), 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)) }