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 (v *FreedomConfig) Build() (*serial.TypedMessage, error) { config := new(freedom.Config) config.DomainStrategy = freedom.Config_AS_IS domainStrategy := strings.ToLower(v.DomainStrategy) if domainStrategy == "useip" || domainStrategy == "use_ip" { config.DomainStrategy = freedom.Config_USE_IP } config.Timeout = 600 if v.Timeout != nil { config.Timeout = *v.Timeout } if len(v.Redirect) > 0 { host, portStr, err := net.SplitHostPort(v.Redirect) if err != nil { return nil, errors.Base(err).Message("Config: Invalid redirect address: ", v.Redirect, ": ", err) } port, err := v2net.PortFromString(portStr) if err != nil { return nil, errors.Base(err).Message("Config: Invalid redirect port: ", v.Redirect, ": ", err) } if len(host) == 0 { host = "127.0.0.1" } config.DestinationOverride = &freedom.DestinationOverride{ Server: &protocol.ServerEndpoint{ Address: v2net.NewIPOrDomain(v2net.ParseAddress(host)), Port: uint32(port), }, } } return serial.ToTypedMessage(config), nil }
func TestSocksConformance(t *testing.T) { assert := assert.On(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() assert.Error(err).IsNil() defer tcpServer.Close() authPort := pickPort() noAuthPort := pickPort() serverConfig := &core.Config{ Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(authPort), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{ AuthType: socks.AuthType_PASSWORD, Accounts: map[string]string{ "Test Account": "Test Password", }, Address: v2net.NewIPOrDomain(v2net.LocalHostIP), UdpEnabled: false, }), }, { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(noAuthPort), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{ AuthType: socks.AuthType_NO_AUTH, Accounts: map[string]string{ "Test Account": "Test Password", }, Address: v2net.NewIPOrDomain(v2net.LocalHostIP), UdpEnabled: false, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { ProxySettings: serial.ToTypedMessage(&freedom.Config{}), }, }, } assert.Error(InitializeServerConfig(serverConfig)).IsNil() { noAuthDialer, err := xproxy.SOCKS5("tcp", v2net.TCPDestination(v2net.LocalHostIP, noAuthPort).NetAddr(), nil, xproxy.Direct) assert.Error(err).IsNil() conn, err := noAuthDialer.Dial("tcp", dest.NetAddr()) assert.Error(err).IsNil() payload := "test payload" nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) assert.Error(conn.Close()).IsNil() } { authDialer, err := xproxy.SOCKS5("tcp", v2net.TCPDestination(v2net.LocalHostIP, authPort).NetAddr(), &xproxy.Auth{User: "******", Password: "******"}, xproxy.Direct) assert.Error(err).IsNil() conn, err := authDialer.Dial("tcp", dest.NetAddr()) assert.Error(err).IsNil() payload := "test payload" nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) assert.Error(conn.Close()).IsNil() } { dialer := socks4.DialSocksProxy(socks4.SOCKS4, v2net.TCPDestination(v2net.LocalHostIP, noAuthPort).NetAddr()) conn, err := dialer("tcp", dest.NetAddr()) assert.Error(err).IsNil() payload := "test payload" nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) assert.Error(conn.Close()).IsNil() } { dialer := socks4.DialSocksProxy(socks4.SOCKS4A, v2net.TCPDestination(v2net.LocalHostIP, noAuthPort).NetAddr()) conn, err := dialer("tcp", v2net.TCPDestination(v2net.LocalHostDomain, tcpServer.Port).NetAddr()) assert.Error(err).IsNil() payload := "test payload" nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) assert.Error(conn.Close()).IsNil() } CloseAllServers() }
func TestSocksBridgeTCP(t *testing.T) { assert := assert.On(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() assert.Error(err).IsNil() defer tcpServer.Close() serverPort := pickPort() serverConfig := &core.Config{ Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(serverPort), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{ AuthType: socks.AuthType_PASSWORD, Accounts: map[string]string{ "Test Account": "Test Password", }, Address: v2net.NewIPOrDomain(v2net.LocalHostIP), UdpEnabled: false, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { ProxySettings: serial.ToTypedMessage(&freedom.Config{}), }, }, } clientPort := pickPort() clientConfig := &core.Config{ Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(clientPort), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ Address: v2net.NewIPOrDomain(dest.Address), Port: uint32(dest.Port), NetworkList: &v2net.NetworkList{ Network: []v2net.Network{v2net.Network_TCP}, }, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { ProxySettings: serial.ToTypedMessage(&socks.ClientConfig{ Server: []*protocol.ServerEndpoint{ { Address: v2net.NewIPOrDomain(v2net.LocalHostIP), Port: uint32(serverPort), User: []*protocol.User{ { Account: serial.ToTypedMessage(&socks.Account{ Username: "******", Password: "******", }), }, }, }, }, }), }, }, } assert.Error(InitializeServerConfig(serverConfig)).IsNil() assert.Error(InitializeServerConfig(clientConfig)).IsNil() conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) assert.Error(err).IsNil() payload := "test payload" nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) assert.Error(conn.Close()).IsNil() CloseAllServers() }
func TestNoOpConnectionHeader(t *testing.T) { assert := assert.On(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() assert.Error(err).IsNil() defer tcpServer.Close() userID := protocol.NewID(uuid.New()) serverPort := pickPort() serverConfig := &core.Config{ Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(serverPort), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), StreamSettings: &internet.StreamConfig{ TransportSettings: []*internet.TransportConfig{ { Protocol: internet.TransportProtocol_TCP, Settings: serial.ToTypedMessage(&tcptransport.Config{ HeaderSettings: serial.ToTypedMessage(&http.Config{}), }), }, }, }, }), ProxySettings: serial.ToTypedMessage(&inbound.Config{ User: []*protocol.User{ { Account: serial.ToTypedMessage(&vmess.Account{ Id: userID.String(), }), }, }, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { ProxySettings: serial.ToTypedMessage(&freedom.Config{}), }, }, } clientPort := pickPort() clientConfig := &core.Config{ Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(clientPort), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ Address: v2net.NewIPOrDomain(dest.Address), Port: uint32(dest.Port), NetworkList: &v2net.NetworkList{ Network: []v2net.Network{v2net.Network_TCP}, }, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { ProxySettings: serial.ToTypedMessage(&outbound.Config{ Receiver: []*protocol.ServerEndpoint{ { Address: v2net.NewIPOrDomain(v2net.LocalHostIP), Port: uint32(serverPort), User: []*protocol.User{ { Account: serial.ToTypedMessage(&vmess.Account{ Id: userID.String(), }), }, }, }, }, }), SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{ StreamSettings: &internet.StreamConfig{ TransportSettings: []*internet.TransportConfig{ { Protocol: internet.TransportProtocol_TCP, Settings: serial.ToTypedMessage(&tcptransport.Config{ HeaderSettings: serial.ToTypedMessage(&http.Config{}), }), }, }, }, }), }, }, } assert.Error(InitializeServerConfig(serverConfig)).IsNil() assert.Error(InitializeServerConfig(clientConfig)).IsNil() conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) assert.Error(err).IsNil() payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) response := readFrom(conn, time.Second*2, len(payload)) assert.Bytes(response).Equals(xor([]byte(payload))) assert.Error(conn.Close()).IsNil() CloseAllServers() }
func TestResolveIP(t *testing.T) { assert := assert.On(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() assert.Error(err).IsNil() defer tcpServer.Close() serverPort := pickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&dns.Config{ Hosts: map[string]*v2net.IPOrDomain{ "google.com": v2net.NewIPOrDomain(dest.Address), }, }), serial.ToTypedMessage(&router.Config{ DomainStrategy: router.Config_IpIfNonMatch, Rule: []*router.RoutingRule{ { Cidr: []*router.CIDR{ { Ip: []byte{127, 0, 0, 0}, Prefix: 8, }, }, Tag: "direct", }, }, }), }, Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(serverPort), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{ AuthType: socks.AuthType_NO_AUTH, Accounts: map[string]string{ "Test Account": "Test Password", }, Address: v2net.NewIPOrDomain(v2net.LocalHostIP), UdpEnabled: false, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { ProxySettings: serial.ToTypedMessage(&blackhole.Config{}), }, { Tag: "direct", ProxySettings: serial.ToTypedMessage(&freedom.Config{ DomainStrategy: freedom.Config_USE_IP, }), }, }, } assert.Error(InitializeServerConfig(serverConfig)).IsNil() { noAuthDialer, err := xproxy.SOCKS5("tcp", v2net.TCPDestination(v2net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct) assert.Error(err).IsNil() conn, err := noAuthDialer.Dial("tcp", fmt.Sprintf("google.com:%d", dest.Port)) assert.Error(err).IsNil() payload := "test payload" nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) assert.Error(conn.Close()).IsNil() } CloseAllServers() }
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)) }
// 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 }
func TestDokodemoTCP(t *testing.T) { assert := assert.On(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() assert.Error(err).IsNil() defer tcpServer.Close() userID := protocol.NewID(uuid.New()) serverPort := pickPort() serverConfig := &core.Config{ Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(serverPort), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&inbound.Config{ User: []*protocol.User{ { Account: serial.ToTypedMessage(&vmess.Account{ Id: userID.String(), }), }, }, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { ProxySettings: serial.ToTypedMessage(&freedom.Config{}), }, }, } clientPort := uint32(pickPort()) clientPortRange := uint32(5) clientConfig := &core.Config{ Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: &v2net.PortRange{From: clientPort, To: clientPort + clientPortRange}, Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ Address: v2net.NewIPOrDomain(dest.Address), Port: uint32(dest.Port), NetworkList: &v2net.NetworkList{ Network: []v2net.Network{v2net.Network_TCP}, }, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { ProxySettings: serial.ToTypedMessage(&outbound.Config{ Receiver: []*protocol.ServerEndpoint{ { Address: v2net.NewIPOrDomain(v2net.LocalHostIP), Port: uint32(serverPort), User: []*protocol.User{ { Account: serial.ToTypedMessage(&vmess.Account{ Id: userID.String(), }), }, }, }, }, }), }, }, } assert.Error(InitializeServerConfig(serverConfig)).IsNil() assert.Error(InitializeServerConfig(clientConfig)).IsNil() for port := clientPort; port <= clientPort+clientPortRange; port++ { conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(port), }) assert.Error(err).IsNil() payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) assert.Error(conn.Close()).IsNil() } CloseAllServers() }
package scenarios import ( "v2ray.com/core" v2net "v2ray.com/core/common/net" ) var clientConfig = &core.Config{ Inbound: []*core.InboundConnectionConfig{ { PortRange: v2net.SinglePortRange(pickPort()), ListenOn: v2net.NewIPOrDomain(v2net.LocalHostIP), }, }, }
func TestUDPConnection(t *testing.T) { assert := assert.On(t) udpServer := udp.Server{ MsgProcessor: xor, } dest, err := udpServer.Start() assert.Error(err).IsNil() defer udpServer.Close() clientPort := pickPort() clientConfig := &core.Config{ Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(clientPort), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ Address: v2net.NewIPOrDomain(dest.Address), Port: uint32(dest.Port), NetworkList: &v2net.NetworkList{ Network: []v2net.Network{v2net.Network_UDP}, }, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { ProxySettings: serial.ToTypedMessage(&freedom.Config{}), }, }, } assert.Error(InitializeServerConfig(clientConfig)).IsNil() { conn, err := net.DialUDP("udp", nil, &net.UDPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) assert.Error(err).IsNil() payload := "dokodemo request." for i := 0; i < 5; i++ { nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) } assert.Error(conn.Close()).IsNil() } time.Sleep(20 * time.Second) { conn, err := net.DialUDP("udp", nil, &net.UDPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(clientPort), }) assert.Error(err).IsNil() payload := "dokodemo request." nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) assert.Error(conn.Close()).IsNil() } CloseAllServers() }
func TestForward(t *testing.T) { assert := assert.On(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() assert.Error(err).IsNil() defer tcpServer.Close() serverPort := pickPort() serverConfig := &core.Config{ Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(serverPort), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{ AuthType: socks.AuthType_NO_AUTH, Accounts: map[string]string{ "Test Account": "Test Password", }, Address: v2net.NewIPOrDomain(v2net.LocalHostIP), UdpEnabled: false, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { ProxySettings: serial.ToTypedMessage(&freedom.Config{ DestinationOverride: &freedom.DestinationOverride{ Server: &protocol.ServerEndpoint{ Address: v2net.NewIPOrDomain(v2net.LocalHostIP), Port: uint32(dest.Port), }, }, }), }, }, } assert.Error(InitializeServerConfig(serverConfig)).IsNil() { noAuthDialer, err := xproxy.SOCKS5("tcp", v2net.TCPDestination(v2net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct) assert.Error(err).IsNil() conn, err := noAuthDialer.Dial("tcp", "google.com:80") assert.Error(err).IsNil() payload := "test payload" nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) response := make([]byte, 1024) nBytes, err = conn.Read(response) assert.Error(err).IsNil() assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) assert.Error(conn.Close()).IsNil() } CloseAllServers() }
func TestBlackhole(t *testing.T) { assert := assert.On(t) tcpServer := tcp.Server{ MsgProcessor: xor, } dest, err := tcpServer.Start() assert.Error(err).IsNil() defer tcpServer.Close() tcpServer2 := tcp.Server{ MsgProcessor: xor, } dest2, err := tcpServer2.Start() assert.Error(err).IsNil() defer tcpServer2.Close() serverPort := pickPort() serverPort2 := pickPort() serverConfig := &core.Config{ Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(serverPort), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ Address: v2net.NewIPOrDomain(dest.Address), Port: uint32(dest.Port), NetworkList: &v2net.NetworkList{ Network: []v2net.Network{v2net.Network_TCP}, }, }), }, { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(serverPort2), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), }), ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ Address: v2net.NewIPOrDomain(dest2.Address), Port: uint32(dest2.Port), NetworkList: &v2net.NetworkList{ Network: []v2net.Network{v2net.Network_TCP}, }, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { Tag: "direct", ProxySettings: serial.ToTypedMessage(&freedom.Config{}), }, { Tag: "blocked", ProxySettings: serial.ToTypedMessage(&blackhole.Config{}), }, }, App: []*serial.TypedMessage{ serial.ToTypedMessage(&router.Config{ Rule: []*router.RoutingRule{ { Tag: "blocked", PortRange: v2net.SinglePortRange(dest2.Port), }, }, }), }, } assert.Error(InitializeServerConfig(serverConfig)).IsNil() conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(serverPort2), }) assert.Error(err).IsNil() payload := "dokodemo request." { nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) } { response := make([]byte, 1024) _, err := conn.Read(response) assert.Error(err).IsNotNil() } assert.Error(conn.Close()).IsNil() CloseAllServers() }
func TestPassiveConnection(t *testing.T) { assert := assert.On(t) tcpServer := tcp.Server{ MsgProcessor: xor, SendFirst: []byte("send first"), } dest, err := tcpServer.Start() assert.Error(err).IsNil() defer tcpServer.Close() serverPort := pickPort() serverConfig := &core.Config{ Inbound: []*proxyman.InboundHandlerConfig{ { ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{ PortRange: v2net.SinglePortRange(serverPort), Listen: v2net.NewIPOrDomain(v2net.LocalHostIP), AllowPassiveConnection: true, }), ProxySettings: serial.ToTypedMessage(&dokodemo.Config{ Address: v2net.NewIPOrDomain(dest.Address), Port: uint32(dest.Port), NetworkList: &v2net.NetworkList{ Network: []v2net.Network{v2net.Network_TCP}, }, }), }, }, Outbound: []*proxyman.OutboundHandlerConfig{ { ProxySettings: serial.ToTypedMessage(&freedom.Config{}), }, }, } assert.Error(InitializeServerConfig(serverConfig)).IsNil() conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{ IP: []byte{127, 0, 0, 1}, Port: int(serverPort), }) assert.Error(err).IsNil() { response := make([]byte, 1024) nBytes, err := conn.Read(response) assert.Error(err).IsNil() assert.String(string(response[:nBytes])).Equals("send first") } payload := "dokodemo request." { nBytes, err := conn.Write([]byte(payload)) assert.Error(err).IsNil() assert.Int(nBytes).Equals(len(payload)) } { response := make([]byte, 1024) nBytes, err := conn.Read(response) assert.Error(err).IsNil() assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload))) } assert.Error(conn.Close()).IsNil() CloseAllServers() }
func (v *Address) Build() *v2net.IPOrDomain { return v2net.NewIPOrDomain(v.Address) }