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 TestSimpleRouter(t *testing.T) { assert := assert.On(t) config := &Config{ Rule: []*RoutingRule{ { Tag: "test", NetworkList: &net.NetworkList{ Network: []net.Network{net.Network_TCP}, }, }, }, } space := app.NewSpace() ctx := app.ContextWithSpace(context.Background(), space) assert.Error(app.AddApplicationToSpace(ctx, new(dns.Config))).IsNil() assert.Error(app.AddApplicationToSpace(ctx, new(dispatcher.Config))).IsNil() assert.Error(app.AddApplicationToSpace(ctx, new(proxyman.OutboundConfig))).IsNil() assert.Error(app.AddApplicationToSpace(ctx, config)).IsNil() assert.Error(space.Initialize()).IsNil() r := FromSpace(space) ctx = proxy.ContextWithDestination(ctx, net.TCPDestination(net.DomainAddress("v2ray.com"), 80)) tag, err := r.TakeDetour(ctx) assert.Error(err).IsNil() assert.String(tag).Equals("test") }
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 TestIPResolution(t *testing.T) { assert := assert.On(t) space := app.NewSpace() space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager()) space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) r, _ := router.CreateRouter("rules", &rules.RouterRuleConfig{}, space) space.BindApp(router.APP_ID, r) dnsServer := dns.NewCacheServer(space, &dns.Config{ Hosts: map[string]net.IP{ "v2ray.com": net.IP([]byte{127, 0, 0, 1}), }, }) space.BindApp(dns.APP_ID, dnsServer) freedom := NewFreedomConnection( &Config{DomainStrategy: DomainStrategyUseIP}, space, &proxy.OutboundHandlerMeta{ Address: v2net.AnyIP, StreamSettings: &internet.StreamSettings{ Type: internet.StreamConnectionTypeRawTCP, }, }) 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 TestSimpleRouter(t *testing.T) { assert := assert.On(t) config := &Config{ Rule: []*RoutingRule{ { Tag: "test", NetworkList: &v2net.NetworkList{ Network: []v2net.Network{v2net.Network_TCP}, }, }, }, } space := app.NewSpace() space.BindApp(dns.APP_ID, dns.NewCacheServer(space, &dns.Config{})) space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager()) r := NewRouter(config, space) space.BindApp(APP_ID, r) assert.Error(space.Initialize()).IsNil() tag, err := r.TakeDetour(&proxy.SessionInfo{Destination: v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80)}) assert.Error(err).IsNil() assert.String(tag).Equals("test") }
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 TestUnreachableDestination(t *testing.T) { assert := assert.On(t) freedom := NewFreedomConnection( &Config{}, app.NewSpace(), &proxy.OutboundHandlerMeta{ Address: v2net.AnyIP, StreamSettings: &internet.StreamConfig{ Network: v2net.Network_RawTCP, }, }) traffic := ray.NewRay() data2Send := "Data to be sent to remote" payload := alloc.NewLocalBuffer(2048).Clear().Append([]byte(data2Send)) err := freedom.Dispatch(v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), 128), payload, traffic) assert.Error(err).IsNotNil() }
func TestSinglePacket(t *testing.T) { assert := assert.On(t) tcpServer := &tcp.Server{ 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() space := app.NewSpace() freedom := NewFreedomConnection( &Config{}, space, &proxy.OutboundHandlerMeta{ Address: v2net.AnyIP, StreamSettings: &internet.StreamConfig{ Network: v2net.Network_RawTCP, }, }) space.Initialize() traffic := ray.NewRay() data2Send := "Data to be sent to remote" payload := buf.NewLocal(2048) payload.Append([]byte(data2Send)) go freedom.Dispatch(v2net.TCPDestination(v2net.LocalHostIP, tcpServer.Port), payload, traffic) traffic.InboundInput().Close() respPayload, err := traffic.InboundOutput().Read() assert.Error(err).IsNil() assert.String(respPayload.String()).Equals("Processed: Data to be sent to remote") tcpServer.Close() }
func TestSimpleRouter(t *testing.T) { assert := assert.On(t) config := &RouterRuleConfig{ Rules: []*Rule{ { Tag: "test", Condition: NewNetworkMatcher(v2net.Network("tcp").AsList()), }, }, } space := app.NewSpace() space.BindApp(dns.APP_ID, dns.NewCacheServer(space, &dns.Config{})) space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space)) space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, proxyman.NewDefaultOutboundHandlerManager()) r := NewRouter(config, space) space.BindApp(router.APP_ID, r) assert.Error(space.Initialize()).IsNil() tag, err := r.TakeDetour(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80)) assert.Error(err).IsNil() assert.String(tag).Equals("test") }
// NewPoint returns a new Point server based on given configuration. // The server is not started at this point. func NewPoint(pConfig *Config) (*Point, error) { var vpoint = new(Point) if err := pConfig.Transport.Apply(); err != nil { return nil, err } if err := pConfig.Log.Apply(); err != nil { return nil, err } space := app.NewSpace() vpoint.space = space vpoint.space.BindApp(proxyman.APP_ID_INBOUND_MANAGER, vpoint) outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager() vpoint.space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager) proxyDialer := proxydialer.NewOutboundProxy(space) proxyDialer.RegisterDialer() space.BindApp(proxydialer.APP_ID, proxyDialer) for _, app := range pConfig.App { settings, err := app.GetInstance() if err != nil { return nil, err } if err := space.BindFromConfig(app.Type, settings); err != nil { return nil, err } } if !space.HasApp(dns.APP_ID) { dnsServer := dns.NewCacheServer(space, &dns.Config{ NameServers: []*v2net.Endpoint{ { Address: &v2net.IPOrDomain{ Address: &v2net.IPOrDomain_Domain{ Domain: "localhost", }, }, }, }, }) space.BindApp(dns.APP_ID, dnsServer) } vpoint.space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(vpoint.space)) vpoint.inboundHandlers = make([]InboundDetourHandler, 0, 8) vpoint.taggedInboundHandlers = make(map[string]InboundDetourHandler) for _, inbound := range pConfig.Inbound { allocConfig := inbound.GetAllocationStrategyValue() var inboundHandler InboundDetourHandler switch allocConfig.Type { case AllocationStrategy_Always: dh, err := NewInboundDetourHandlerAlways(vpoint.space, inbound) if err != nil { log.Error("V2Ray: Failed to create detour handler: ", err) return nil, common.ErrBadConfiguration } inboundHandler = dh case AllocationStrategy_Random: dh, err := NewInboundDetourHandlerDynamic(vpoint.space, inbound) if err != nil { log.Error("V2Ray: Failed to create detour handler: ", err) return nil, common.ErrBadConfiguration } inboundHandler = dh default: log.Error("V2Ray: Unknown allocation strategy: ", allocConfig.Type) return nil, common.ErrBadConfiguration } vpoint.inboundHandlers = append(vpoint.inboundHandlers, inboundHandler) if len(inbound.Tag) > 0 { vpoint.taggedInboundHandlers[inbound.Tag] = inboundHandler } } vpoint.outboundHandlers = make([]proxy.OutboundHandler, 0, 8) vpoint.taggedOutboundHandlers = make(map[string]proxy.OutboundHandler) for idx, outbound := range pConfig.Outbound { outboundSettings, err := outbound.GetTypedSettings() if err != nil { return nil, err } outboundHandler, err := proxyregistry.CreateOutboundHandler( outbound.Settings.Type, vpoint.space, outboundSettings, &proxy.OutboundHandlerMeta{ Tag: outbound.Tag, Address: outbound.GetSendThroughValue(), StreamSettings: outbound.StreamSettings, ProxySettings: outbound.ProxySettings, }) if err != nil { log.Error("V2Ray: Failed to create detour outbound connection handler: ", err) return nil, err } if idx == 0 { outboundHandlerManager.SetDefaultHandler(outboundHandler) } if len(outbound.Tag) > 0 { outboundHandlerManager.SetHandler(outbound.Tag, outboundHandler) vpoint.taggedOutboundHandlers[outbound.Tag] = outboundHandler } vpoint.outboundHandlers = append(vpoint.outboundHandlers, outboundHandler) } if err := vpoint.space.Initialize(); err != nil { return nil, err } return vpoint, nil }
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)) }
// NewPoint returns a new Point server based on given configuration. // The server is not started at this point. func NewPoint(pConfig *Config) (*Point, error) { var vpoint = new(Point) if err := pConfig.Transport.Apply(); err != nil { return nil, err } if err := pConfig.Log.Apply(); err != nil { return nil, err } space := app.NewSpace() ctx := app.ContextWithSpace(context.Background(), space) vpoint.space = space outboundHandlerManager := proxyman.OutboundHandlerManagerFromSpace(space) if outboundHandlerManager == nil { o, err := app.CreateAppFromConfig(ctx, new(proxyman.OutboundConfig)) if err != nil { return nil, err } space.AddApplication(o) outboundHandlerManager = o.(proxyman.OutboundHandlerManager) } inboundHandlerManager := proxyman.InboundHandlerManagerFromSpace(space) if inboundHandlerManager == nil { o, err := app.CreateAppFromConfig(ctx, new(proxyman.InboundConfig)) if err != nil { return nil, err } space.AddApplication(o) inboundHandlerManager = o.(proxyman.InboundHandlerManager) } for _, appSettings := range pConfig.App { settings, err := appSettings.GetInstance() if err != nil { return nil, err } application, err := app.CreateAppFromConfig(ctx, settings) if err != nil { return nil, err } if err := space.AddApplication(application); err != nil { return nil, err } } dnsServer := dns.FromSpace(space) if dnsServer == nil { dnsConfig := &dns.Config{ NameServers: []*v2net.Endpoint{{ Address: v2net.NewIPOrDomain(v2net.LocalHostDomain), }}, } d, err := app.CreateAppFromConfig(ctx, dnsConfig) if err != nil { return nil, err } space.AddApplication(d) dnsServer = d.(dns.Server) } disp := dispatcher.FromSpace(space) if disp == nil { d, err := app.CreateAppFromConfig(ctx, new(dispatcher.Config)) if err != nil { return nil, err } space.AddApplication(d) disp = d.(dispatcher.Interface) } for _, inbound := range pConfig.Inbound { if err := inboundHandlerManager.AddHandler(ctx, inbound); err != nil { return nil, err } } for _, outbound := range pConfig.Outbound { if err := outboundHandlerManager.AddHandler(ctx, outbound); err != nil { return nil, err } } if err := vpoint.space.Initialize(); err != nil { return nil, err } return vpoint, nil }
// NewPoint returns a new Point server based on given configuration. // The server is not started at this point. func NewPoint(pConfig *Config) (*Point, error) { var vpoint = new(Point) vpoint.port = pConfig.InboundConfig.Port if vpoint.port == 0 { vpoint.port = pConfig.Port // Backward compatibility } vpoint.listen = pConfig.InboundConfig.ListenOn if pConfig.TransportConfig != nil { pConfig.TransportConfig.Apply() } if pConfig.LogConfig != nil { logConfig := pConfig.LogConfig if len(logConfig.AccessLog) > 0 { err := log.InitAccessLogger(logConfig.AccessLog) if err != nil { return nil, err } } if len(logConfig.ErrorLog) > 0 { err := log.InitErrorLogger(logConfig.ErrorLog) if err != nil { return nil, err } } log.SetLogLevel(logConfig.LogLevel) } vpoint.space = app.NewSpace() vpoint.space.BindApp(proxyman.APP_ID_INBOUND_MANAGER, vpoint) outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager() vpoint.space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager) dnsConfig := pConfig.DNSConfig if dnsConfig != nil { dnsServer := dns.NewCacheServer(vpoint.space, dnsConfig) vpoint.space.BindApp(dns.APP_ID, dnsServer) } routerConfig := pConfig.RouterConfig if routerConfig != nil { r, err := router.CreateRouter(routerConfig.Strategy, routerConfig.Settings, vpoint.space) if err != nil { log.Error("Failed to create router: ", err) return nil, common.ErrBadConfiguration } vpoint.space.BindApp(router.APP_ID, r) vpoint.router = r } vpoint.space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(vpoint.space)) ichConfig := pConfig.InboundConfig.Settings ich, err := proxyregistry.CreateInboundHandler( pConfig.InboundConfig.Protocol, vpoint.space, ichConfig, &proxy.InboundHandlerMeta{ Tag: "system.inbound", Address: pConfig.InboundConfig.ListenOn, Port: vpoint.port, StreamSettings: pConfig.InboundConfig.StreamSettings, AllowPassiveConnection: pConfig.InboundConfig.AllowPassiveConnection, }) if err != nil { log.Error("Failed to create inbound connection handler: ", err) return nil, err } vpoint.ich = ich ochConfig := pConfig.OutboundConfig.Settings och, err := proxyregistry.CreateOutboundHandler( pConfig.OutboundConfig.Protocol, vpoint.space, ochConfig, &proxy.OutboundHandlerMeta{ Tag: "system.outbound", Address: pConfig.OutboundConfig.SendThrough, StreamSettings: pConfig.OutboundConfig.StreamSettings, }) if err != nil { log.Error("Failed to create outbound connection handler: ", err) return nil, err } vpoint.och = och outboundHandlerManager.SetDefaultHandler(och) vpoint.taggedIdh = make(map[string]InboundDetourHandler) detours := pConfig.InboundDetours if len(detours) > 0 { vpoint.idh = make([]InboundDetourHandler, len(detours)) for idx, detourConfig := range detours { allocConfig := detourConfig.Allocation var detourHandler InboundDetourHandler switch allocConfig.Strategy { case AllocationStrategyAlways: dh, err := NewInboundDetourHandlerAlways(vpoint.space, detourConfig) if err != nil { log.Error("Point: Failed to create detour handler: ", err) return nil, common.ErrBadConfiguration } detourHandler = dh case AllocationStrategyRandom: dh, err := NewInboundDetourHandlerDynamic(vpoint.space, detourConfig) if err != nil { log.Error("Point: Failed to create detour handler: ", err) return nil, common.ErrBadConfiguration } detourHandler = dh default: log.Error("Point: Unknown allocation strategy: ", allocConfig.Strategy) return nil, common.ErrBadConfiguration } vpoint.idh[idx] = detourHandler if len(detourConfig.Tag) > 0 { vpoint.taggedIdh[detourConfig.Tag] = detourHandler } } } outboundDetours := pConfig.OutboundDetours if len(outboundDetours) > 0 { vpoint.odh = make(map[string]proxy.OutboundHandler) for _, detourConfig := range outboundDetours { detourHandler, err := proxyregistry.CreateOutboundHandler( detourConfig.Protocol, vpoint.space, detourConfig.Settings, &proxy.OutboundHandlerMeta{ Tag: detourConfig.Tag, Address: detourConfig.SendThrough, StreamSettings: detourConfig.StreamSettings, }) if err != nil { log.Error("Point: Failed to create detour outbound connection handler: ", err) return nil, err } vpoint.odh[detourConfig.Tag] = detourHandler outboundHandlerManager.SetHandler(detourConfig.Tag, detourHandler) } } if err := vpoint.space.Initialize(); err != nil { return nil, err } return vpoint, nil }