2017-01-08 16:32:41 -05:00
|
|
|
package scenarios
|
|
|
|
|
|
|
|
import (
|
2018-02-20 18:18:53 -05:00
|
|
|
"context"
|
2021-09-29 04:25:23 -04:00
|
|
|
"io"
|
2017-04-24 19:40:34 -04:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-01-08 16:32:41 -05:00
|
|
|
"testing"
|
2017-01-27 08:10:36 -05:00
|
|
|
"time"
|
2017-01-08 16:32:41 -05:00
|
|
|
|
2017-01-26 17:05:24 -05:00
|
|
|
xproxy "golang.org/x/net/proxy"
|
2021-10-28 06:34:19 -04:00
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
2021-02-16 15:31:50 -05:00
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v5"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/app/dispatcher"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/app/log"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/app/proxyman"
|
|
|
|
_ "github.com/v2fly/v2ray-core/v5/app/proxyman/inbound"
|
|
|
|
_ "github.com/v2fly/v2ray-core/v5/app/proxyman/outbound"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/app/router"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common"
|
|
|
|
clog "github.com/v2fly/v2ray-core/v5/common/log"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/protocol"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/serial"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/uuid"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/proxy/blackhole"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/proxy/dokodemo"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/proxy/freedom"
|
|
|
|
v2http "github.com/v2fly/v2ray-core/v5/proxy/http"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/proxy/socks"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/proxy/vmess"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/proxy/vmess/inbound"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/proxy/vmess/outbound"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/testing/servers/tcp"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/testing/servers/udp"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/transport/internet"
|
2017-01-08 16:32:41 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPassiveConnection(t *testing.T) {
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
SendFirst: []byte("send first"),
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2017-01-08 16:32:41 -05:00
|
|
|
defer tcpServer.Close()
|
|
|
|
|
2018-03-01 05:53:39 -05:00
|
|
|
serverPort := tcp.PickPort()
|
2017-01-08 16:32:41 -05:00
|
|
|
serverConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-08 16:32:41 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(dest.Address),
|
2017-01-08 16:32:41 -05:00
|
|
|
Port: uint32(dest.Port),
|
2017-08-29 06:56:57 -04:00
|
|
|
NetworkList: &net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
2017-01-08 16:32:41 -05:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-08 16:32:41 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
2017-01-08 16:32:41 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-17 18:39:30 -04:00
|
|
|
servers, err := InitializeServerConfigs(serverConfig)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-03 13:46:53 -05:00
|
|
|
defer CloseAllServers(servers)
|
2017-01-08 16:32:41 -05:00
|
|
|
|
|
|
|
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
|
|
|
|
IP: []byte{127, 0, 0, 1},
|
|
|
|
Port: int(serverPort),
|
|
|
|
})
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2017-01-08 16:32:41 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
response := make([]byte, 1024)
|
|
|
|
nBytes, err := conn.Read(response)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-03 13:46:53 -05:00
|
|
|
if string(response[:nBytes]) != "send first" {
|
|
|
|
t.Error("unexpected first response: ", string(response[:nBytes]))
|
|
|
|
}
|
2017-01-08 16:32:41 -05:00
|
|
|
}
|
|
|
|
|
2019-02-03 13:46:53 -05:00
|
|
|
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
|
|
|
|
t.Error(err)
|
2017-01-08 16:32:41 -05:00
|
|
|
}
|
|
|
|
}
|
2017-01-10 08:25:31 -05:00
|
|
|
|
|
|
|
func TestProxy(t *testing.T) {
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
2019-02-01 08:38:14 -05:00
|
|
|
common.Must(err)
|
2017-01-10 08:25:31 -05:00
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
serverUserID := protocol.NewID(uuid.New())
|
2018-03-01 05:53:39 -05:00
|
|
|
serverPort := tcp.PickPort()
|
2017-01-10 08:25:31 -05:00
|
|
|
serverConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-10 08:25:31 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&inbound.Config{
|
2017-01-10 08:25:31 -05:00
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: serverUserID.String(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-10 08:25:31 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
2017-01-10 08:25:31 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyUserID := protocol.NewID(uuid.New())
|
2018-03-01 05:53:39 -05:00
|
|
|
proxyPort := tcp.PickPort()
|
2017-01-10 08:25:31 -05:00
|
|
|
proxyConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-10 08:25:31 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(proxyPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&inbound.Config{
|
2017-01-10 08:25:31 -05:00
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: proxyUserID.String(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-10 08:25:31 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
2017-01-10 08:25:31 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-03-01 05:53:39 -05:00
|
|
|
clientPort := tcp.PickPort()
|
2017-01-10 08:25:31 -05:00
|
|
|
clientConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-10 08:25:31 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(clientPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(dest.Address),
|
2017-01-10 08:25:31 -05:00
|
|
|
Port: uint32(dest.Port),
|
2017-08-29 06:56:57 -04:00
|
|
|
NetworkList: &net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
2017-01-10 08:25:31 -05:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-10 08:25:31 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&outbound.Config{
|
2017-01-10 08:25:31 -05:00
|
|
|
Receiver: []*protocol.ServerEndpoint{
|
|
|
|
{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-10 08:25:31 -05:00
|
|
|
Port: uint32(serverPort),
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: serverUserID.String(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2017-01-26 14:46:44 -05:00
|
|
|
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
|
|
|
|
ProxySettings: &internet.ProxyConfig{
|
|
|
|
Tag: "proxy",
|
|
|
|
},
|
|
|
|
}),
|
2017-01-10 08:25:31 -05:00
|
|
|
},
|
2017-01-12 06:59:57 -05:00
|
|
|
{
|
|
|
|
Tag: "proxy",
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&outbound.Config{
|
2017-01-12 06:59:57 -05:00
|
|
|
Receiver: []*protocol.ServerEndpoint{
|
|
|
|
{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-12 06:59:57 -05:00
|
|
|
Port: uint32(proxyPort),
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: proxyUserID.String(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-17 18:39:30 -04:00
|
|
|
servers, err := InitializeServerConfigs(serverConfig, proxyConfig, clientConfig)
|
2019-02-01 08:38:14 -05:00
|
|
|
common.Must(err)
|
|
|
|
defer CloseAllServers(servers)
|
2017-01-12 06:59:57 -05:00
|
|
|
|
2019-02-01 08:38:14 -05:00
|
|
|
if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-01-12 06:59:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestProxyOverKCP(t *testing.T) {
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2017-01-12 06:59:57 -05:00
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
serverUserID := protocol.NewID(uuid.New())
|
2018-03-01 05:53:39 -05:00
|
|
|
serverPort := tcp.PickPort()
|
2017-01-12 06:59:57 -05:00
|
|
|
serverConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-12 06:59:57 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
StreamSettings: &internet.StreamConfig{
|
|
|
|
Protocol: internet.TransportProtocol_MKCP,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&inbound.Config{
|
2017-01-12 06:59:57 -05:00
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: serverUserID.String(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-12 06:59:57 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
2017-01-12 06:59:57 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
proxyUserID := protocol.NewID(uuid.New())
|
2018-03-01 05:53:39 -05:00
|
|
|
proxyPort := tcp.PickPort()
|
2017-01-12 06:59:57 -05:00
|
|
|
proxyConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-12 06:59:57 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(proxyPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&inbound.Config{
|
2017-01-12 06:59:57 -05:00
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: proxyUserID.String(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-12 06:59:57 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
|
|
|
|
StreamSettings: &internet.StreamConfig{
|
|
|
|
Protocol: internet.TransportProtocol_MKCP,
|
|
|
|
},
|
|
|
|
}),
|
2017-01-12 06:59:57 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-03-01 05:53:39 -05:00
|
|
|
clientPort := tcp.PickPort()
|
2017-01-12 06:59:57 -05:00
|
|
|
clientConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-12 06:59:57 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(clientPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(dest.Address),
|
2017-01-12 06:59:57 -05:00
|
|
|
Port: uint32(dest.Port),
|
2017-08-29 06:56:57 -04:00
|
|
|
NetworkList: &net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
2017-01-12 06:59:57 -05:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-12 06:59:57 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&outbound.Config{
|
2017-01-12 06:59:57 -05:00
|
|
|
Receiver: []*protocol.ServerEndpoint{
|
|
|
|
{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-12 06:59:57 -05:00
|
|
|
Port: uint32(serverPort),
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: serverUserID.String(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2017-01-26 14:46:44 -05:00
|
|
|
SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{
|
|
|
|
ProxySettings: &internet.ProxyConfig{
|
|
|
|
Tag: "proxy",
|
|
|
|
},
|
|
|
|
StreamSettings: &internet.StreamConfig{
|
|
|
|
Protocol: internet.TransportProtocol_MKCP,
|
|
|
|
},
|
|
|
|
}),
|
2017-01-12 06:59:57 -05:00
|
|
|
},
|
2017-01-10 08:25:31 -05:00
|
|
|
{
|
|
|
|
Tag: "proxy",
|
2017-01-26 14:46:44 -05:00
|
|
|
ProxySettings: serial.ToTypedMessage(&outbound.Config{
|
2017-01-10 08:25:31 -05:00
|
|
|
Receiver: []*protocol.ServerEndpoint{
|
|
|
|
{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-10 08:25:31 -05:00
|
|
|
Port: uint32(proxyPort),
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: proxyUserID.String(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-17 18:39:30 -04:00
|
|
|
servers, err := InitializeServerConfigs(serverConfig, proxyConfig, clientConfig)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-03 13:46:53 -05:00
|
|
|
defer CloseAllServers(servers)
|
2017-01-10 08:25:31 -05:00
|
|
|
|
2019-02-03 13:46:53 -05:00
|
|
|
if err := testTCPConn(clientPort, 1024, time.Second*5)(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-01-10 08:25:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlackhole(t *testing.T) {
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2017-01-10 08:25:31 -05:00
|
|
|
defer tcpServer.Close()
|
|
|
|
|
2017-01-10 18:09:33 -05:00
|
|
|
tcpServer2 := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest2, err := tcpServer2.Start()
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2017-01-10 18:09:33 -05:00
|
|
|
defer tcpServer2.Close()
|
|
|
|
|
2018-03-01 05:53:39 -05:00
|
|
|
serverPort := tcp.PickPort()
|
|
|
|
serverPort2 := tcp.PickPort()
|
2017-01-10 08:25:31 -05:00
|
|
|
serverConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-10 08:25:31 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(dest.Address),
|
2017-01-10 08:25:31 -05:00
|
|
|
Port: uint32(dest.Port),
|
2017-08-29 06:56:57 -04:00
|
|
|
NetworkList: &net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
2017-01-10 08:25:31 -05:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
2017-01-10 18:09:33 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(serverPort2),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 14:46:44 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(dest2.Address),
|
2017-01-10 18:09:33 -05:00
|
|
|
Port: uint32(dest2.Port),
|
2017-08-29 06:56:57 -04:00
|
|
|
NetworkList: &net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
2017-01-10 18:09:33 -05:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
2017-01-10 08:25:31 -05:00
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-10 08:25:31 -05:00
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
Tag: "direct",
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
2017-01-10 18:09:33 -05:00
|
|
|
},
|
|
|
|
{
|
2017-01-26 14:46:44 -05:00
|
|
|
Tag: "blocked",
|
|
|
|
ProxySettings: serial.ToTypedMessage(&blackhole.Config{}),
|
2017-01-10 08:25:31 -05:00
|
|
|
},
|
|
|
|
},
|
2021-06-19 09:36:54 -04:00
|
|
|
App: []*anypb.Any{
|
2017-01-10 18:09:33 -05:00
|
|
|
serial.ToTypedMessage(&router.Config{
|
|
|
|
Rule: []*router.RoutingRule{
|
|
|
|
{
|
2018-11-07 19:01:53 -05:00
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "blocked",
|
|
|
|
},
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(dest2.Port),
|
2017-01-10 18:09:33 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
2017-01-10 08:25:31 -05:00
|
|
|
}
|
|
|
|
|
2017-05-17 18:39:30 -04:00
|
|
|
servers, err := InitializeServerConfigs(serverConfig)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-03 13:46:53 -05:00
|
|
|
defer CloseAllServers(servers)
|
2017-01-10 08:25:31 -05:00
|
|
|
|
2019-02-07 13:29:32 -05:00
|
|
|
if err := testTCPConn(serverPort2, 1024, time.Second*5)(); err == nil {
|
|
|
|
t.Error("nil error")
|
2017-01-10 08:25:31 -05:00
|
|
|
}
|
|
|
|
}
|
2017-01-26 17:05:24 -05:00
|
|
|
|
|
|
|
func TestForward(t *testing.T) {
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2017-01-26 17:05:24 -05:00
|
|
|
defer tcpServer.Close()
|
|
|
|
|
2018-03-01 05:53:39 -05:00
|
|
|
serverPort := tcp.PickPort()
|
2017-01-26 17:05:24 -05:00
|
|
|
serverConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-26 17:05:24 -05:00
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 17:05:24 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
|
|
|
|
AuthType: socks.AuthType_NO_AUTH,
|
|
|
|
Accounts: map[string]string{
|
|
|
|
"Test Account": "Test Password",
|
|
|
|
},
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 17:05:24 -05:00
|
|
|
UdpEnabled: false,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-26 17:05:24 -05:00
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{
|
|
|
|
DestinationOverride: &freedom.DestinationOverride{
|
|
|
|
Server: &protocol.ServerEndpoint{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-26 17:05:24 -05:00
|
|
|
Port: uint32(dest.Port),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-17 18:39:30 -04:00
|
|
|
servers, err := InitializeServerConfigs(serverConfig)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-03 13:46:53 -05:00
|
|
|
defer CloseAllServers(servers)
|
2017-01-26 17:05:24 -05:00
|
|
|
|
|
|
|
{
|
2017-08-29 06:56:57 -04:00
|
|
|
noAuthDialer, err := xproxy.SOCKS5("tcp", net.TCPDestination(net.LocalHostIP, serverPort).NetAddr(), nil, xproxy.Direct)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2017-01-26 17:05:24 -05:00
|
|
|
conn, err := noAuthDialer.Dial("tcp", "google.com:80")
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-03 13:46:53 -05:00
|
|
|
defer conn.Close()
|
2017-01-26 17:05:24 -05:00
|
|
|
|
2019-02-03 13:46:53 -05:00
|
|
|
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2017-01-26 17:05:24 -05:00
|
|
|
}
|
|
|
|
}
|
2017-01-27 08:10:36 -05:00
|
|
|
|
|
|
|
func TestUDPConnection(t *testing.T) {
|
|
|
|
udpServer := udp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := udpServer.Start()
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2017-01-27 08:10:36 -05:00
|
|
|
defer udpServer.Close()
|
|
|
|
|
2021-09-26 19:16:07 -04:00
|
|
|
clientPort := udp.PickPort()
|
2017-01-27 08:10:36 -05:00
|
|
|
clientConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-01-27 08:10:36 -05:00
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(clientPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-01-27 08:10:36 -05:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(dest.Address),
|
2017-01-27 08:10:36 -05:00
|
|
|
Port: uint32(dest.Port),
|
2017-08-29 06:56:57 -04:00
|
|
|
NetworkList: &net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_UDP},
|
2017-01-27 08:10:36 -05:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-01-27 08:10:36 -05:00
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-17 18:39:30 -04:00
|
|
|
servers, err := InitializeServerConfigs(clientConfig)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-06 05:53:33 -05:00
|
|
|
defer CloseAllServers(servers)
|
2017-01-27 08:10:36 -05:00
|
|
|
|
2019-02-03 13:46:53 -05:00
|
|
|
if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
|
|
|
|
t.Error(err)
|
2017-01-27 08:10:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(20 * time.Second)
|
|
|
|
|
2019-02-03 13:46:53 -05:00
|
|
|
if err := testUDPConn(clientPort, 1024, time.Second*5)(); err != nil {
|
|
|
|
t.Error(err)
|
2017-01-27 08:10:36 -05:00
|
|
|
}
|
|
|
|
}
|
2017-04-24 19:40:34 -04:00
|
|
|
|
|
|
|
func TestDomainSniffing(t *testing.T) {
|
2018-03-01 05:53:39 -05:00
|
|
|
sniffingPort := tcp.PickPort()
|
|
|
|
httpPort := tcp.PickPort()
|
2017-04-24 19:40:34 -04:00
|
|
|
serverConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-04-24 19:40:34 -04:00
|
|
|
{
|
|
|
|
Tag: "snif",
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(sniffingPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-04-24 19:40:34 -04:00
|
|
|
DomainOverride: []proxyman.KnownProtocols{
|
|
|
|
proxyman.KnownProtocols_TLS,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-04-24 19:40:34 -04:00
|
|
|
Port: 443,
|
2017-08-29 06:56:57 -04:00
|
|
|
NetworkList: &net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
2017-04-24 19:40:34 -04:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Tag: "http",
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(httpPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-04-24 19:40:34 -04:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-04-24 19:40:34 -04:00
|
|
|
{
|
|
|
|
Tag: "redir",
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{
|
|
|
|
DestinationOverride: &freedom.DestinationOverride{
|
|
|
|
Server: &protocol.ServerEndpoint{
|
2017-08-29 06:56:57 -04:00
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
2017-04-24 19:40:34 -04:00
|
|
|
Port: uint32(sniffingPort),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Tag: "direct",
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
|
|
|
},
|
2021-06-19 09:36:54 -04:00
|
|
|
App: []*anypb.Any{
|
2017-04-24 19:40:34 -04:00
|
|
|
serial.ToTypedMessage(&router.Config{
|
|
|
|
Rule: []*router.RoutingRule{
|
|
|
|
{
|
2018-11-07 19:01:53 -05:00
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "direct",
|
|
|
|
},
|
2017-04-24 19:40:34 -04:00
|
|
|
InboundTag: []string{"snif"},
|
|
|
|
}, {
|
2018-11-07 19:01:53 -05:00
|
|
|
TargetTag: &router.RoutingRule_Tag{
|
|
|
|
Tag: "redir",
|
|
|
|
},
|
2017-04-24 19:40:34 -04:00
|
|
|
InboundTag: []string{"http"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
serial.ToTypedMessage(&log.Config{
|
2021-10-02 17:29:37 -04:00
|
|
|
Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
|
2017-04-24 19:40:34 -04:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-17 18:39:30 -04:00
|
|
|
servers, err := InitializeServerConfigs(serverConfig)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-03 13:46:53 -05:00
|
|
|
defer CloseAllServers(servers)
|
2017-04-24 19:40:34 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
transport := &http.Transport{
|
|
|
|
Proxy: func(req *http.Request) (*url.URL, error) {
|
|
|
|
return url.Parse("http://127.0.0.1:" + httpPort.String())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Get("https://www.github.com/")
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2020-11-21 16:05:01 -05:00
|
|
|
defer resp.Body.Close()
|
2019-02-03 13:46:53 -05:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
t.Error("unexpected status code: ", resp.StatusCode)
|
|
|
|
}
|
2021-09-27 18:02:19 -04:00
|
|
|
common.Must(resp.Write(io.Discard))
|
2017-04-24 19:40:34 -04:00
|
|
|
}
|
|
|
|
}
|
2018-02-20 18:18:53 -05:00
|
|
|
|
|
|
|
func TestDialV2Ray(t *testing.T) {
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2018-02-20 18:18:53 -05:00
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
userID := protocol.NewID(uuid.New())
|
2018-03-01 05:53:39 -05:00
|
|
|
serverPort := tcp.PickPort()
|
2018-02-20 18:18:53 -05:00
|
|
|
serverConfig := &core.Config{
|
2021-06-19 09:36:54 -04:00
|
|
|
App: []*anypb.Any{
|
2018-02-20 18:18:53 -05:00
|
|
|
serial.ToTypedMessage(&log.Config{
|
2021-10-02 17:29:37 -04:00
|
|
|
Error: &log.LogSpecification{Level: clog.Severity_Debug, Type: log.LogType_Console},
|
2018-02-20 18:18:53 -05:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&inbound.Config{
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: userID.String(),
|
2021-12-31 22:10:09 -05:00
|
|
|
AlterId: 0,
|
2018-02-20 18:18:53 -05:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
clientConfig := &core.Config{
|
2021-06-19 09:36:54 -04:00
|
|
|
App: []*anypb.Any{
|
2018-02-20 18:18:53 -05:00
|
|
|
serial.ToTypedMessage(&dispatcher.Config{}),
|
|
|
|
serial.ToTypedMessage(&proxyman.InboundConfig{}),
|
|
|
|
serial.ToTypedMessage(&proxyman.OutboundConfig{}),
|
|
|
|
},
|
|
|
|
Inbound: []*core.InboundHandlerConfig{},
|
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&outbound.Config{
|
|
|
|
Receiver: []*protocol.ServerEndpoint{
|
|
|
|
{
|
|
|
|
Address: net.NewIPOrDomain(net.LocalHostIP),
|
|
|
|
Port: uint32(serverPort),
|
|
|
|
User: []*protocol.User{
|
|
|
|
{
|
|
|
|
Account: serial.ToTypedMessage(&vmess.Account{
|
|
|
|
Id: userID.String(),
|
2021-12-31 22:10:09 -05:00
|
|
|
AlterId: 0,
|
2018-02-20 18:18:53 -05:00
|
|
|
SecuritySettings: &protocol.SecurityConfig{
|
|
|
|
Type: protocol.SecurityType_AES128_GCM,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
servers, err := InitializeServerConfigs(serverConfig)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-03 13:46:53 -05:00
|
|
|
defer CloseAllServers(servers)
|
2018-02-20 18:18:53 -05:00
|
|
|
|
|
|
|
client, err := core.New(clientConfig)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2018-02-20 18:18:53 -05:00
|
|
|
|
|
|
|
conn, err := core.Dial(context.Background(), client, dest)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-03 13:46:53 -05:00
|
|
|
defer conn.Close()
|
2018-02-20 18:18:53 -05:00
|
|
|
|
2019-02-03 13:46:53 -05:00
|
|
|
if err := testTCPConn2(conn, 1024, time.Second*5)(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-02-20 18:18:53 -05:00
|
|
|
}
|