1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 17:46:58 -05:00

Port picker

This commit is contained in:
V2Ray 2015-11-01 21:32:08 +01:00
parent 192960b711
commit 03b8c8f9e8
4 changed files with 40 additions and 22 deletions

View File

@ -0,0 +1,13 @@
package testing
import (
"sync/atomic"
)
var (
port = int32(30000)
)
func PickPort() uint16 {
return uint16(atomic.AddInt32(&port, 1))
}

View File

@ -2,6 +2,7 @@ package freedom
import ( import (
"bytes" "bytes"
"fmt"
"io/ioutil" "io/ioutil"
"net" "net"
"testing" "testing"
@ -11,6 +12,7 @@ import (
"github.com/v2ray/v2ray-core/app/point" "github.com/v2ray/v2ray-core/app/point"
"github.com/v2ray/v2ray-core/common/alloc" "github.com/v2ray/v2ray-core/common/alloc"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
"github.com/v2ray/v2ray-core/proxy/common/connhandler" "github.com/v2ray/v2ray-core/proxy/common/connhandler"
_ "github.com/v2ray/v2ray-core/proxy/socks" _ "github.com/v2ray/v2ray-core/proxy/socks"
"github.com/v2ray/v2ray-core/proxy/socks/config/json" "github.com/v2ray/v2ray-core/proxy/socks/config/json"
@ -47,7 +49,7 @@ func TestUDPSend(t *testing.T) {
connhandler.RegisterInboundConnectionHandlerFactory("mock_ich", ich) connhandler.RegisterInboundConnectionHandlerFactory("mock_ich", ich)
pointPort := uint16(38724) pointPort := v2nettesting.PickPort()
config := mocks.Config{ config := mocks.Config{
PortValue: pointPort, PortValue: pointPort,
InboundConfigValue: &mocks.ConnectionConfig{ InboundConfigValue: &mocks.ConnectionConfig{
@ -75,7 +77,7 @@ func TestUDPSend(t *testing.T) {
func TestSocksTcpConnect(t *testing.T) { func TestSocksTcpConnect(t *testing.T) {
assert := unit.Assert(t) assert := unit.Assert(t)
port := uint16(38293) port := v2nettesting.PickPort()
data2Send := "Data to be sent to remote" data2Send := "Data to be sent to remote"
@ -91,7 +93,7 @@ func TestSocksTcpConnect(t *testing.T) {
_, err := tcpServer.Start() _, err := tcpServer.Start()
assert.Error(err).IsNil() assert.Error(err).IsNil()
pointPort := uint16(38724) pointPort := v2nettesting.PickPort()
config := mocks.Config{ config := mocks.Config{
PortValue: pointPort, PortValue: pointPort,
InboundConfigValue: &mocks.ConnectionConfig{ InboundConfigValue: &mocks.ConnectionConfig{
@ -112,10 +114,10 @@ func TestSocksTcpConnect(t *testing.T) {
err = point.Start() err = point.Start()
assert.Error(err).IsNil() assert.Error(err).IsNil()
socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:38724", nil, proxy.Direct) socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", pointPort), nil, proxy.Direct)
assert.Error(err).IsNil() assert.Error(err).IsNil()
targetServer := "127.0.0.1:38293" targetServer := fmt.Sprintf("127.0.0.1:%d", port)
conn, err := socks5Client.Dial("tcp", targetServer) conn, err := socks5Client.Dial("tcp", targetServer)
assert.Error(err).IsNil() assert.Error(err).IsNil()

View File

@ -2,6 +2,7 @@ package socks
import ( import (
"bytes" "bytes"
"fmt"
"io/ioutil" "io/ioutil"
"net" "net"
"testing" "testing"
@ -9,6 +10,7 @@ import (
"golang.org/x/net/proxy" "golang.org/x/net/proxy"
"github.com/v2ray/v2ray-core/app/point" "github.com/v2ray/v2ray-core/app/point"
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
"github.com/v2ray/v2ray-core/proxy/common/connhandler" "github.com/v2ray/v2ray-core/proxy/common/connhandler"
"github.com/v2ray/v2ray-core/proxy/socks/config/json" "github.com/v2ray/v2ray-core/proxy/socks/config/json"
proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks" proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
@ -18,7 +20,7 @@ import (
func TestSocksTcpConnect(t *testing.T) { func TestSocksTcpConnect(t *testing.T) {
assert := unit.Assert(t) assert := unit.Assert(t)
port := uint16(12385) port := v2nettesting.PickPort()
connInput := []byte("The data to be returned to socks server.") connInput := []byte("The data to be returned to socks server.")
connOutput := bytes.NewBuffer(make([]byte, 0, 1024)) connOutput := bytes.NewBuffer(make([]byte, 0, 1024))
@ -49,7 +51,7 @@ func TestSocksTcpConnect(t *testing.T) {
err = point.Start() err = point.Start()
assert.Error(err).IsNil() assert.Error(err).IsNil()
socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:12385", nil, proxy.Direct) socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), nil, proxy.Direct)
assert.Error(err).IsNil() assert.Error(err).IsNil()
targetServer := "google.com:80" targetServer := "google.com:80"
@ -73,7 +75,7 @@ func TestSocksTcpConnect(t *testing.T) {
func TestSocksTcpConnectWithUserPass(t *testing.T) { func TestSocksTcpConnectWithUserPass(t *testing.T) {
assert := unit.Assert(t) assert := unit.Assert(t)
port := uint16(12386) port := v2nettesting.PickPort()
connInput := []byte("The data to be returned to socks server.") connInput := []byte("The data to be returned to socks server.")
connOutput := bytes.NewBuffer(make([]byte, 0, 1024)) connOutput := bytes.NewBuffer(make([]byte, 0, 1024))
@ -110,7 +112,7 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) {
err = point.Start() err = point.Start()
assert.Error(err).IsNil() assert.Error(err).IsNil()
socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:12386", &proxy.Auth{"userx", "passy"}, proxy.Direct) socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), &proxy.Auth{"userx", "passy"}, proxy.Direct)
assert.Error(err).IsNil() assert.Error(err).IsNil()
targetServer := "1.2.3.4:443" targetServer := "1.2.3.4:443"
@ -134,7 +136,7 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) {
func TestSocksTcpConnectWithWrongUserPass(t *testing.T) { func TestSocksTcpConnectWithWrongUserPass(t *testing.T) {
assert := unit.Assert(t) assert := unit.Assert(t)
port := uint16(12389) port := v2nettesting.PickPort()
connInput := []byte("The data to be returned to socks server.") connInput := []byte("The data to be returned to socks server.")
connOutput := bytes.NewBuffer(make([]byte, 0, 1024)) connOutput := bytes.NewBuffer(make([]byte, 0, 1024))
@ -171,7 +173,7 @@ func TestSocksTcpConnectWithWrongUserPass(t *testing.T) {
err = point.Start() err = point.Start()
assert.Error(err).IsNil() assert.Error(err).IsNil()
socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:12389", &proxy.Auth{"userx", "passz"}, proxy.Direct) socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), &proxy.Auth{"userx", "passz"}, proxy.Direct)
assert.Error(err).IsNil() assert.Error(err).IsNil()
targetServer := "1.2.3.4:443" targetServer := "1.2.3.4:443"
@ -181,7 +183,7 @@ func TestSocksTcpConnectWithWrongUserPass(t *testing.T) {
func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) { func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) {
assert := unit.Assert(t) assert := unit.Assert(t)
port := uint16(38405) port := v2nettesting.PickPort()
connInput := []byte("The data to be returned to socks server.") connInput := []byte("The data to be returned to socks server.")
connOutput := bytes.NewBuffer(make([]byte, 0, 1024)) connOutput := bytes.NewBuffer(make([]byte, 0, 1024))
@ -218,7 +220,7 @@ func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) {
err = point.Start() err = point.Start()
assert.Error(err).IsNil() assert.Error(err).IsNil()
socks5Client, err := proxy.SOCKS5("tcp", "127.0.0.1:38405", nil, proxy.Direct) socks5Client, err := proxy.SOCKS5("tcp", fmt.Sprintf("127.0.0.1:%d", port), nil, proxy.Direct)
assert.Error(err).IsNil() assert.Error(err).IsNil()
targetServer := "1.2.3.4:443" targetServer := "1.2.3.4:443"
@ -228,7 +230,7 @@ func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) {
func TestSocksUdpSend(t *testing.T) { func TestSocksUdpSend(t *testing.T) {
assert := unit.Assert(t) assert := unit.Assert(t)
port := uint16(12372) port := v2nettesting.PickPort()
connInput := []byte("The data to be returned to socks server.") connInput := []byte("The data to be returned to socks server.")
connOutput := bytes.NewBuffer(make([]byte, 0, 1024)) connOutput := bytes.NewBuffer(make([]byte, 0, 1024))

View File

@ -7,6 +7,7 @@ import (
"github.com/v2ray/v2ray-core/app/point" "github.com/v2ray/v2ray-core/app/point"
"github.com/v2ray/v2ray-core/common/alloc" "github.com/v2ray/v2ray-core/common/alloc"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
v2nettesting "github.com/v2ray/v2ray-core/common/net/testing"
"github.com/v2ray/v2ray-core/proxy/common/connhandler" "github.com/v2ray/v2ray-core/proxy/common/connhandler"
proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks" proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks"
"github.com/v2ray/v2ray-core/proxy/vmess/config" "github.com/v2ray/v2ray-core/proxy/vmess/config"
@ -21,7 +22,9 @@ func TestVMessInAndOut(t *testing.T) {
testAccount, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51") testAccount, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
assert.Error(err).IsNil() assert.Error(err).IsNil()
portA := uint16(17392) portA := v2nettesting.PickPort()
portB := v2nettesting.PickPort()
ichConnInput := []byte("The data to be send to outbound server.") ichConnInput := []byte("The data to be send to outbound server.")
ichConnOutput := bytes.NewBuffer(make([]byte, 0, 1024)) ichConnOutput := bytes.NewBuffer(make([]byte, 0, 1024))
ich := &proxymocks.InboundConnectionHandler{ ich := &proxymocks.InboundConnectionHandler{
@ -42,7 +45,7 @@ func TestVMessInAndOut(t *testing.T) {
SettingsValue: &json.Outbound{ SettingsValue: &json.Outbound{
[]*json.ConfigTarget{ []*json.ConfigTarget{
&json.ConfigTarget{ &json.ConfigTarget{
Address: v2net.IPAddress([]byte{127, 0, 0, 1}, 13829), Address: v2net.IPAddress([]byte{127, 0, 0, 1}, portB),
TCPEnabled: true, TCPEnabled: true,
Users: []*json.ConfigUser{ Users: []*json.ConfigUser{
&json.ConfigUser{Id: testAccount}, &json.ConfigUser{Id: testAccount},
@ -59,8 +62,6 @@ func TestVMessInAndOut(t *testing.T) {
err = pointA.Start() err = pointA.Start()
assert.Error(err).IsNil() assert.Error(err).IsNil()
portB := uint16(13829)
ochConnInput := []byte("The data to be returned to inbound server.") ochConnInput := []byte("The data to be returned to inbound server.")
ochConnOutput := bytes.NewBuffer(make([]byte, 0, 1024)) ochConnOutput := bytes.NewBuffer(make([]byte, 0, 1024))
och := &proxymocks.OutboundConnectionHandler{ och := &proxymocks.OutboundConnectionHandler{
@ -105,7 +106,9 @@ func TestVMessInAndOutUDP(t *testing.T) {
testAccount, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51") testAccount, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
assert.Error(err).IsNil() assert.Error(err).IsNil()
portA := uint16(17394) portA := v2nettesting.PickPort()
portB := v2nettesting.PickPort()
ichConnInput := []byte("The data to be send to outbound server.") ichConnInput := []byte("The data to be send to outbound server.")
ichConnOutput := bytes.NewBuffer(make([]byte, 0, 1024)) ichConnOutput := bytes.NewBuffer(make([]byte, 0, 1024))
ich := &proxymocks.InboundConnectionHandler{ ich := &proxymocks.InboundConnectionHandler{
@ -126,7 +129,7 @@ func TestVMessInAndOutUDP(t *testing.T) {
SettingsValue: &json.Outbound{ SettingsValue: &json.Outbound{
[]*json.ConfigTarget{ []*json.ConfigTarget{
&json.ConfigTarget{ &json.ConfigTarget{
Address: v2net.IPAddress([]byte{127, 0, 0, 1}, 13841), Address: v2net.IPAddress([]byte{127, 0, 0, 1}, portB),
UDPEnabled: true, UDPEnabled: true,
Users: []*json.ConfigUser{ Users: []*json.ConfigUser{
&json.ConfigUser{Id: testAccount}, &json.ConfigUser{Id: testAccount},
@ -143,8 +146,6 @@ func TestVMessInAndOutUDP(t *testing.T) {
err = pointA.Start() err = pointA.Start()
assert.Error(err).IsNil() assert.Error(err).IsNil()
portB := uint16(13841)
ochConnInput := []byte("The data to be returned to inbound server.") ochConnInput := []byte("The data to be returned to inbound server.")
ochConnOutput := bytes.NewBuffer(make([]byte, 0, 1024)) ochConnOutput := bytes.NewBuffer(make([]byte, 0, 1024))
och := &proxymocks.OutboundConnectionHandler{ och := &proxymocks.OutboundConnectionHandler{