2016-01-24 20:48:38 +00:00
|
|
|
package scenarios
|
|
|
|
|
|
|
|
import (
|
2017-11-17 15:53:00 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"io"
|
2016-01-24 20:48:38 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
2017-11-17 15:53:00 +00:00
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/buf"
|
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
"v2ray.com/core"
|
|
|
|
"v2ray.com/core/app/proxyman"
|
2017-08-29 10:56:57 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2017-03-31 15:06:16 +00: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 15:53:00 +00:00
|
|
|
"v2ray.com/core/testing/servers/tcp"
|
2017-10-26 19:44:13 +00:00
|
|
|
. "v2ray.com/ext/assert"
|
2016-01-24 20:48:38 +00:00
|
|
|
)
|
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
func TestHttpConformance(t *testing.T) {
|
2017-10-24 14:15:35 +00:00
|
|
|
assert := With(t)
|
2016-01-24 20:48:38 +00:00
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
httpServerPort := pickPort()
|
|
|
|
httpServer := &v2httptest.Server{
|
|
|
|
Port: httpServerPort,
|
2016-01-24 20:48:38 +00:00
|
|
|
PathHandler: make(map[string]http.HandlerFunc),
|
|
|
|
}
|
|
|
|
_, err := httpServer.Start()
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
2016-01-24 20:48:38 +00:00
|
|
|
defer httpServer.Close()
|
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
serverPort := pickPort()
|
|
|
|
serverConfig := &core.Config{
|
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
2017-08-29 10:56:57 +00:00
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
2017-03-31 15:06:16 +00:00
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
2016-01-24 20:48:38 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-05-17 22:39:30 +00:00
|
|
|
servers, err := InitializeServerConfigs(serverConfig)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
2016-06-10 21:01:17 +00:00
|
|
|
|
2017-03-31 15:06:16 +00: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 21:01:17 +00:00
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
client := &http.Client{
|
|
|
|
Transport: transport,
|
|
|
|
}
|
2016-06-10 21:01:17 +00:00
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
resp, err := client.Get("http://127.0.0.1:" + httpServerPort.String())
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(resp.StatusCode, Equals, 200)
|
2016-06-10 21:01:17 +00:00
|
|
|
|
2017-03-31 15:06:16 +00:00
|
|
|
content, err := ioutil.ReadAll(resp.Body)
|
2017-10-24 14:15:35 +00:00
|
|
|
assert(err, IsNil)
|
|
|
|
assert(string(content), Equals, "Home")
|
2016-06-10 21:01:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-17 22:39:30 +00:00
|
|
|
CloseAllServers(servers)
|
2016-06-10 21:01:17 +00:00
|
|
|
}
|
2017-10-26 19:44:13 +00:00
|
|
|
|
2017-11-17 15:53:00 +00:00
|
|
|
func TestHttpConnectMethod(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
|
|
|
tcpServer := tcp.Server{
|
|
|
|
MsgProcessor: xor,
|
|
|
|
}
|
|
|
|
dest, err := tcpServer.Start()
|
|
|
|
assert(err, IsNil)
|
|
|
|
defer tcpServer.Close()
|
|
|
|
|
|
|
|
serverPort := pickPort()
|
|
|
|
serverConfig := &core.Config{
|
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
servers, err := InitializeServerConfigs(serverConfig)
|
|
|
|
assert(err, IsNil)
|
|
|
|
|
|
|
|
{
|
|
|
|
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))
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
assert(err, IsNil)
|
|
|
|
assert(resp.StatusCode, Equals, 200)
|
|
|
|
|
|
|
|
content := make([]byte, len(payload))
|
|
|
|
common.Must2(io.ReadFull(resp.Body, content))
|
|
|
|
assert(err, IsNil)
|
|
|
|
assert(content, Equals, xor(payload))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseAllServers(servers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHttpPost(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
|
|
|
httpServerPort := pickPort()
|
|
|
|
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()
|
|
|
|
assert(err, IsNil)
|
|
|
|
defer httpServer.Close()
|
|
|
|
|
|
|
|
serverPort := pickPort()
|
|
|
|
serverConfig := &core.Config{
|
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
|
|
|
|
PortRange: net.SinglePortRange(serverPort),
|
|
|
|
Listen: net.NewIPOrDomain(net.LocalHostIP),
|
|
|
|
}),
|
|
|
|
ProxySettings: serial.ToTypedMessage(&v2http.ServerConfig{}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
servers, err := InitializeServerConfigs(serverConfig)
|
|
|
|
assert(err, IsNil)
|
|
|
|
|
|
|
|
{
|
|
|
|
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))
|
|
|
|
assert(err, IsNil)
|
|
|
|
assert(resp.StatusCode, Equals, 200)
|
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(resp.Body)
|
|
|
|
assert(err, IsNil)
|
|
|
|
assert(content, Equals, xor(payload))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseAllServers(servers)
|
|
|
|
}
|
|
|
|
|
2017-10-28 12:25:22 +00: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 19:44:13 +00:00
|
|
|
func TestHttpBasicAuth(t *testing.T) {
|
|
|
|
assert := With(t)
|
|
|
|
|
|
|
|
httpServerPort := pickPort()
|
|
|
|
httpServer := &v2httptest.Server{
|
|
|
|
Port: httpServerPort,
|
|
|
|
PathHandler: make(map[string]http.HandlerFunc),
|
|
|
|
}
|
|
|
|
_, err := httpServer.Start()
|
|
|
|
assert(err, IsNil)
|
|
|
|
defer httpServer.Close()
|
|
|
|
|
|
|
|
serverPort := pickPort()
|
|
|
|
serverConfig := &core.Config{
|
|
|
|
Inbound: []*proxyman.InboundHandlerConfig{
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outbound: []*proxyman.OutboundHandlerConfig{
|
|
|
|
{
|
|
|
|
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
servers, err := InitializeServerConfigs(serverConfig)
|
|
|
|
assert(err, IsNil)
|
|
|
|
|
|
|
|
{
|
|
|
|
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())
|
|
|
|
assert(err, IsNil)
|
|
|
|
assert(resp.StatusCode, Equals, 401)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "http://127.0.0.1:"+httpServerPort.String(), nil)
|
|
|
|
assert(err, IsNil)
|
|
|
|
|
2017-10-28 12:25:22 +00:00
|
|
|
setProxyBasicAuth(req, "a", "c")
|
2017-10-26 19:44:13 +00:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
assert(err, IsNil)
|
|
|
|
assert(resp.StatusCode, Equals, 401)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
req, err := http.NewRequest("GET", "http://127.0.0.1:"+httpServerPort.String(), nil)
|
|
|
|
assert(err, IsNil)
|
|
|
|
|
2017-10-28 12:25:22 +00:00
|
|
|
setProxyBasicAuth(req, "a", "b")
|
2017-10-26 19:44:13 +00:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
assert(err, IsNil)
|
|
|
|
assert(resp.StatusCode, Equals, 200)
|
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(resp.Body)
|
|
|
|
assert(err, IsNil)
|
|
|
|
assert(string(content), Equals, "Home")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CloseAllServers(servers)
|
|
|
|
}
|