2016-01-24 15:48:38 -05:00
|
|
|
package scenarios
|
|
|
|
|
|
|
|
import (
|
2017-11-17 10:53:00 -05:00
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"io"
|
2016-01-24 15:48:38 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
2018-04-18 17:57:28 -04:00
|
|
|
"time"
|
2016-01-24 15:48:38 -05:00
|
|
|
|
2019-02-09 09:46:48 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
2017-03-31 11:06:16 -04:00
|
|
|
"v2ray.com/core"
|
|
|
|
"v2ray.com/core/app/proxyman"
|
2018-11-12 13:51:29 -05:00
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/buf"
|
2017-08-29 06:56:57 -04:00
|
|
|
"v2ray.com/core/common/net"
|
2017-03-31 11:06:16 -04:00
|
|
|
"v2ray.com/core/common/serial"
|
|
|
|
"v2ray.com/core/proxy/freedom"
|
|
|
|
v2http "v2ray.com/core/proxy/http"
|
|
|
|
v2httptest "v2ray.com/core/testing/servers/http"
|
2017-11-17 10:53:00 -05:00
|
|
|
"v2ray.com/core/testing/servers/tcp"
|
2016-01-24 15:48:38 -05:00
|
|
|
)
|
|
|
|
|
2017-03-31 11:06:16 -04:00
|
|
|
func TestHttpConformance(t *testing.T) {
|
2018-03-01 05:53:39 -05:00
|
|
|
httpServerPort := tcp.PickPort()
|
2017-03-31 11:06:16 -04:00
|
|
|
httpServer := &v2httptest.Server{
|
|
|
|
Port: httpServerPort,
|
2016-01-24 15:48:38 -05:00
|
|
|
PathHandler: make(map[string]http.HandlerFunc),
|
|
|
|
}
|
|
|
|
_, err := httpServer.Start()
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2016-01-24 15:48:38 -05:00
|
|
|
defer httpServer.Close()
|
|
|
|
|
2018-03-01 05:53:39 -05:00
|
|
|
serverPort := tcp.PickPort()
|
2017-03-31 11:06:16 -04:00
|
|
|
serverConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-03-31 11:06:16 -04:00
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 06:56:57 -04:00
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-03-31 11:06:16 -04:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-03-31 11:06:16 -04:00
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
2016-01-24 15:48:38 -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-09 09:46:48 -05:00
|
|
|
defer CloseAllServers(servers)
|
2016-06-10 17:01:17 -04:00
|
|
|
|
2017-03-31 11:06:16 -04:00
|
|
|
{
|
|
|
|
transport := &http.Transport{
|
|
|
|
Proxy: func(req *http.Request) (*url.URL, error) {
|
|
|
|
return url.Parse("http://127.0.0.1:" + serverPort.String())
|
|
|
|
},
|
|
|
|
}
|
2016-06-10 17:01:17 -04:00
|
|
|
|
2017-03-31 11:06:16 -04:00
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
2016-06-10 17:01:17 -04:00
|
|
|
|
2017-03-31 11:06:16 -04:00
|
|
|
resp, err := client.Get("http://127.0.0.1:" + httpServerPort.String())
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-09 09:46:48 -05:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
t.Fatal("status: ", resp.StatusCode)
|
|
|
|
}
|
2016-06-10 17:01:17 -04:00
|
|
|
|
2017-03-31 11:06:16 -04:00
|
|
|
content, err := ioutil.ReadAll(resp.Body)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-09 09:46:48 -05:00
|
|
|
if string(content) != "Home" {
|
|
|
|
t.Fatal("body: ", string(content))
|
|
|
|
}
|
2016-06-10 17:01:17 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-26 15:44:13 -04:00
|
|
|
|
2018-04-18 16:41:47 -04:00
|
|
|
func TestHttpError(t *testing.T) {
|
2018-04-18 17:57:28 -04:00
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: func(msg []byte) []byte {
|
|
|
|
return []byte{}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2018-04-18 17:57:28 -04:00
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
time.AfterFunc(time.Second*2, func() {
|
|
|
|
tcpServer.ShouldClose = true
|
|
|
|
})
|
|
|
|
|
2018-04-18 16:41:47 -04:00
|
|
|
serverPort := tcp.PickPort()
|
|
|
|
serverConfig := &core.Config{
|
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
|
|
|
{
|
2018-04-18 17:57:28 -04:00
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
2018-04-18 16:41:47 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
servers, err := InitializeServerConfigs(serverConfig)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-09 09:46:48 -05:00
|
|
|
defer CloseAllServers(servers)
|
2018-04-18 16:41:47 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
transport := &http.Transport{
|
|
|
|
Proxy: func(req *http.Request) (*url.URL, error) {
|
|
|
|
return url.Parse("http://127.0.0.1:" + serverPort.String())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
2018-04-18 17:57:28 -04:00
|
|
|
resp, err := client.Get("http://127.0.0.1:" + dest.Port.String())
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-09 09:46:48 -05:00
|
|
|
if resp.StatusCode != 503 {
|
|
|
|
t.Error("status: ", resp.StatusCode)
|
|
|
|
}
|
2018-04-18 16:41:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 10:53:00 -05:00
|
|
|
func TestHttpConnectMethod(t *testing.T) {
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2017-11-17 10:53:00 -05:00
|
|
|
defer tcpServer.Close()
|
|
|
|
|
2018-03-01 05:53:39 -05:00
|
|
|
serverPort := tcp.PickPort()
|
2017-11-17 10:53:00 -05:00
|
|
|
serverConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-11-17 10:53:00 -05:00
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-11-17 10:53:00 -05:00
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
servers, err := InitializeServerConfigs(serverConfig)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-09 09:46:48 -05:00
|
|
|
defer CloseAllServers(servers)
|
2017-11-17 10:53:00 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
transport := &http.Transport{
|
|
|
|
Proxy: func(req *http.Request) (*url.URL, error) {
|
|
|
|
return url.Parse("http://127.0.0.1:" + serverPort.String())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := make([]byte, 1024*64)
|
|
|
|
common.Must2(rand.Read(payload))
|
|
|
|
req, err := http.NewRequest("Connect", "http://"+dest.NetAddr()+"/", bytes.NewReader(payload))
|
2017-11-26 09:15:03 -05:00
|
|
|
req.Header.Set("X-a", "b")
|
|
|
|
req.Header.Set("X-b", "d")
|
2017-11-17 10:53:00 -05:00
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-09 09:46:48 -05:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
t.Fatal("status: ", resp.StatusCode)
|
|
|
|
}
|
2017-11-17 10:53:00 -05:00
|
|
|
|
|
|
|
content := make([]byte, len(payload))
|
|
|
|
common.Must2(io.ReadFull(resp.Body, content))
|
2019-02-09 09:46:48 -05:00
|
|
|
if r := cmp.Diff(content, xor(payload)); r != "" {
|
|
|
|
t.Fatal(r)
|
|
|
|
}
|
2017-11-17 10:53:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHttpPost(t *testing.T) {
|
2018-03-01 05:53:39 -05:00
|
|
|
httpServerPort := tcp.PickPort()
|
2017-11-17 10:53:00 -05:00
|
|
|
httpServer := &v2httptest.Server{
|
|
|
|
Port: httpServerPort,
|
|
|
|
PathHandler: map[string]http.HandlerFunc{
|
|
|
|
"/testpost": func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
payload, err := buf.ReadAllToBytes(r.Body)
|
|
|
|
r.Body.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte("Unable to read all payload"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
payload = xor(payload)
|
|
|
|
w.Write(payload)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := httpServer.Start()
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2017-11-17 10:53:00 -05:00
|
|
|
defer httpServer.Close()
|
|
|
|
|
2018-03-01 05:53:39 -05:00
|
|
|
serverPort := tcp.PickPort()
|
2017-11-17 10:53:00 -05:00
|
|
|
serverConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-11-17 10:53:00 -05:00
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-11-17 10:53:00 -05:00
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
servers, err := InitializeServerConfigs(serverConfig)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-09 09:46:48 -05:00
|
|
|
defer CloseAllServers(servers)
|
2017-11-17 10:53:00 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
transport := &http.Transport{
|
|
|
|
Proxy: func(req *http.Request) (*url.URL, error) {
|
|
|
|
return url.Parse("http://127.0.0.1:" + serverPort.String())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := make([]byte, 1024*64)
|
|
|
|
common.Must2(rand.Read(payload))
|
|
|
|
|
|
|
|
resp, err := client.Post("http://127.0.0.1:"+httpServerPort.String()+"/testpost", "application/x-www-form-urlencoded", bytes.NewReader(payload))
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-09 09:46:48 -05:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
t.Fatal("status: ", resp.StatusCode)
|
|
|
|
}
|
2017-11-17 10:53:00 -05:00
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(resp.Body)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-09 09:46:48 -05:00
|
|
|
if r := cmp.Diff(content, xor(payload)); r != "" {
|
|
|
|
t.Fatal(r)
|
|
|
|
}
|
2017-11-17 10:53:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 08:25:22 -04:00
|
|
|
func setProxyBasicAuth(req *http.Request, user, pass string) {
|
|
|
|
req.SetBasicAuth(user, pass)
|
|
|
|
req.Header.Set("Proxy-Authorization", req.Header.Get("Authorization"))
|
|
|
|
req.Header.Del("Authorization")
|
|
|
|
}
|
|
|
|
|
2017-10-26 15:44:13 -04:00
|
|
|
func TestHttpBasicAuth(t *testing.T) {
|
2018-03-01 05:53:39 -05:00
|
|
|
httpServerPort := tcp.PickPort()
|
2017-10-26 15:44:13 -04:00
|
|
|
httpServer := &v2httptest.Server{
|
|
|
|
Port: httpServerPort,
|
|
|
|
PathHandler: make(map[string]http.HandlerFunc),
|
|
|
|
}
|
|
|
|
_, err := httpServer.Start()
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2017-10-26 15:44:13 -04:00
|
|
|
defer httpServer.Close()
|
|
|
|
|
2018-03-01 05:53:39 -05:00
|
|
|
serverPort := tcp.PickPort()
|
2017-10-26 15:44:13 -04:00
|
|
|
serverConfig := &core.Config{
|
2018-01-10 06:22:37 -05:00
|
|
|
Inbound: []*core.InboundHandlerConfig{
|
2017-10-26 15:44:13 -04:00
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{
|
|
|
|
Accounts: map[string]string{
|
|
|
|
"a": "b",
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2018-01-10 06:22:37 -05:00
|
|
|
Outbound: []*core.OutboundHandlerConfig{
|
2017-10-26 15:44:13 -04:00
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
servers, err := InitializeServerConfigs(serverConfig)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-09 09:46:48 -05:00
|
|
|
defer CloseAllServers(servers)
|
2017-10-26 15:44:13 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
transport := &http.Transport{
|
|
|
|
Proxy: func(req *http.Request) (*url.URL, error) {
|
|
|
|
return url.Parse("http://127.0.0.1:" + serverPort.String())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
resp, err := client.Get("http://127.0.0.1:" + httpServerPort.String())
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-09 09:46:48 -05:00
|
|
|
if resp.StatusCode != 407 {
|
|
|
|
t.Fatal("status: ", resp.StatusCode)
|
|
|
|
}
|
2017-10-26 15:44:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "http://127.0.0.1:"+httpServerPort.String(), nil)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2017-10-26 15:44:13 -04:00
|
|
|
|
2017-10-28 08:25:22 -04:00
|
|
|
setProxyBasicAuth(req, "a", "c")
|
2017-10-26 15:44:13 -04:00
|
|
|
resp, err := client.Do(req)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-09 09:46:48 -05:00
|
|
|
if resp.StatusCode != 407 {
|
|
|
|
t.Fatal("status: ", resp.StatusCode)
|
|
|
|
}
|
2017-10-26 15:44:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "http://127.0.0.1:"+httpServerPort.String(), nil)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2017-10-26 15:44:13 -04:00
|
|
|
|
2017-10-28 08:25:22 -04:00
|
|
|
setProxyBasicAuth(req, "a", "b")
|
2017-10-26 15:44:13 -04:00
|
|
|
resp, err := client.Do(req)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-09 09:46:48 -05:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
t.Fatal("status: ", resp.StatusCode)
|
|
|
|
}
|
2017-10-26 15:44:13 -04:00
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(resp.Body)
|
2019-02-02 16:19:30 -05:00
|
|
|
common.Must(err)
|
2019-02-09 09:46:48 -05:00
|
|
|
if string(content) != "Home" {
|
|
|
|
t.Fatal("body: ", string(content))
|
|
|
|
}
|
2017-10-26 15:44:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|