1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-16 00:48:14 -04:00
v2fly/testing/scenarios/socks5_helper.go

173 lines
4.4 KiB
Go
Raw Normal View History

2015-11-06 07:08:20 -05:00
package scenarios
import (
2015-11-06 07:45:41 -05:00
"net"
2015-12-02 18:41:34 -05:00
routerconfig "github.com/v2ray/v2ray-core/app/router/config/testing"
_ "github.com/v2ray/v2ray-core/app/router/rules"
rulesconfig "github.com/v2ray/v2ray-core/app/router/rules/config/testing"
2015-11-06 07:08:20 -05:00
v2net "github.com/v2ray/v2ray-core/common/net"
2015-11-06 07:45:41 -05:00
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
_ "github.com/v2ray/v2ray-core/proxy/freedom"
_ "github.com/v2ray/v2ray-core/proxy/socks"
socksjson "github.com/v2ray/v2ray-core/proxy/socks/config/json"
_ "github.com/v2ray/v2ray-core/proxy/vmess"
"github.com/v2ray/v2ray-core/proxy/vmess/config"
vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/config/json"
2015-11-29 08:45:32 -05:00
"github.com/v2ray/v2ray-core/shell/point"
"github.com/v2ray/v2ray-core/shell/point/config/testing/mocks"
2015-11-06 07:08:20 -05:00
)
const (
socks5Version = byte(0x05)
)
func socks5AuthMethodRequest(methods ...byte) []byte {
request := []byte{socks5Version, byte(len(methods))}
request = append(request, methods...)
return request
}
2015-11-06 07:45:41 -05:00
func appendAddress(request []byte, address v2net.Address) []byte {
2015-11-06 07:08:20 -05:00
switch {
case address.IsIPv4():
request = append(request, byte(0x01))
request = append(request, address.IP()...)
case address.IsIPv6():
request = append(request, byte(0x04))
request = append(request, address.IP()...)
case address.IsDomain():
request = append(request, byte(0x03), byte(len(address.Domain())))
request = append(request, []byte(address.Domain())...)
}
2015-12-02 06:47:54 -05:00
request = append(request, address.Port().Bytes()...)
2015-11-06 07:08:20 -05:00
return request
}
2015-11-06 07:45:41 -05:00
func socks5Request(command byte, address v2net.Address) []byte {
request := []byte{socks5Version, command, 0}
request = appendAddress(request, address)
return request
}
func socks5UDPRequest(address v2net.Address, payload []byte) []byte {
request := make([]byte, 0, 1024)
request = append(request, 0, 0, 0)
request = appendAddress(request, address)
request = append(request, payload...)
return request
}
2015-12-02 18:41:34 -05:00
func setUpV2Ray(routing func(v2net.Destination) bool) (v2net.Port, v2net.Port, error) {
2015-11-06 07:45:41 -05:00
id1, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
if err != nil {
2015-12-02 18:41:34 -05:00
return 0, 0, err
2015-11-06 07:45:41 -05:00
}
id2, err := config.NewID("93ccfc71-b136-4015-ac85-e037bd1ead9e")
if err != nil {
2015-12-02 18:41:34 -05:00
return 0, 0, err
2015-11-06 07:45:41 -05:00
}
users := []*vmessjson.ConfigUser{
&vmessjson.ConfigUser{Id: id1},
&vmessjson.ConfigUser{Id: id2},
}
portB := v2nettesting.PickPort()
configB := mocks.Config{
PortValue: portB,
InboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "vmess",
SettingsValue: &vmessjson.Inbound{
AllowedClients: users,
},
},
OutboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "freedom",
SettingsValue: nil,
},
}
pointB, err := point.NewPoint(&configB)
if err != nil {
2015-12-02 18:41:34 -05:00
return 0, 0, err
2015-11-06 07:45:41 -05:00
}
err = pointB.Start()
if err != nil {
2015-12-02 18:41:34 -05:00
return 0, 0, err
2015-11-06 07:45:41 -05:00
}
portA := v2nettesting.PickPort()
2015-12-02 18:41:34 -05:00
portA2 := v2nettesting.PickPort()
2015-11-06 07:45:41 -05:00
configA := mocks.Config{
PortValue: portA,
InboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "socks",
SettingsValue: &socksjson.SocksConfig{
AuthMethod: "noauth",
2015-12-03 16:41:06 -05:00
UDP: true,
2015-11-06 07:45:41 -05:00
HostIP: socksjson.IPAddress(net.IPv4(127, 0, 0, 1)),
},
},
OutboundConfigValue: &mocks.ConnectionConfig{
ProtocolValue: "vmess",
SettingsValue: &vmessjson.Outbound{
[]*vmessjson.ConfigTarget{
&vmessjson.ConfigTarget{
Address: v2net.IPAddress([]byte{127, 0, 0, 1}, portB),
Users: users,
},
},
},
},
2015-12-02 18:41:34 -05:00
InboundDetoursValue: []*mocks.InboundDetourConfig{
&mocks.InboundDetourConfig{
PortRangeValue: &mocks.PortRange{
FromValue: portA2,
ToValue: portA2,
},
ConnectionConfig: &mocks.ConnectionConfig{
ProtocolValue: "socks",
SettingsValue: &socksjson.SocksConfig{
AuthMethod: "noauth",
2015-12-03 16:41:06 -05:00
UDP: false,
2015-12-02 18:41:34 -05:00
HostIP: socksjson.IPAddress(net.IPv4(127, 0, 0, 1)),
},
},
},
},
OutboundDetoursValue: []*mocks.OutboundDetourConfig{
&mocks.OutboundDetourConfig{
TagValue: "direct",
ConnectionConfig: &mocks.ConnectionConfig{
ProtocolValue: "freedom",
SettingsValue: nil,
},
},
},
RouterConfigValue: &routerconfig.RouterConfig{
StrategyValue: "rules",
SettingsValue: &rulesconfig.RouterRuleConfig{
RuleList: []*rulesconfig.TestRule{
&rulesconfig.TestRule{
TagValue: "direct",
Function: routing,
},
},
},
},
2015-11-06 07:45:41 -05:00
}
pointA, err := point.NewPoint(&configA)
if err != nil {
2015-12-02 18:41:34 -05:00
return 0, 0, err
2015-11-06 07:45:41 -05:00
}
err = pointA.Start()
if err != nil {
2015-12-02 18:41:34 -05:00
return 0, 0, err
2015-11-06 07:45:41 -05:00
}
2015-12-02 18:41:34 -05:00
return portA, portA2, nil
2015-11-06 07:45:41 -05:00
}