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

fix udp handling

This commit is contained in:
Darien Raymond 2017-01-27 14:10:36 +01:00
parent 55fb18b6e1
commit 866a4d879c
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 86 additions and 0 deletions

View File

@ -188,6 +188,7 @@ func (w *udpWorker) getConnection(src v2net.Destination) (*udpConn, bool) {
Port: int(w.port),
},
}
w.activeConn[src] = conn
conn.updateActivity()
return conn, false
@ -223,6 +224,7 @@ func (w *udpWorker) removeConn(src v2net.Destination) {
}
func (w *udpWorker) Start() error {
w.activeConn = make(map[v2net.Destination]*udpConn)
ctx, cancel := context.WithCancel(context.Background())
w.ctx = ctx
w.cancel = cancel

View File

@ -3,6 +3,7 @@ package scenarios
import (
"net"
"testing"
"time"
xproxy "golang.org/x/net/proxy"
"v2ray.com/core"
@ -21,6 +22,7 @@ import (
"v2ray.com/core/proxy/vmess/outbound"
"v2ray.com/core/testing/assert"
"v2ray.com/core/testing/servers/tcp"
"v2ray.com/core/testing/servers/udp"
"v2ray.com/core/transport/internet"
)
@ -565,3 +567,85 @@ func TestForward(t *testing.T) {
CloseAllServers()
}
func TestUDPConnection(t *testing.T) {
assert := assert.On(t)
udpServer := udp.Server{
MsgProcessor: xor,
}
dest, err := udpServer.Start()
assert.Error(err).IsNil()
defer udpServer.Close()
clientPort := pickPort()
clientConfig := &core.Config{
Inbound: []*proxyman.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortRange: v2net.SinglePortRange(clientPort),
Listen: v2net.NewIPOrDomain(v2net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: v2net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &v2net.NetworkList{
Network: []v2net.Network{v2net.Network_UDP},
},
}),
},
},
Outbound: []*proxyman.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}
assert.Error(InitializeServerConfig(clientConfig)).IsNil()
{
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
assert.Error(err).IsNil()
payload := "dokodemo request."
for i := 0; i < 5; i++ {
nBytes, err := conn.Write([]byte(payload))
assert.Error(err).IsNil()
assert.Int(nBytes).Equals(len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
assert.Error(err).IsNil()
assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload)))
}
assert.Error(conn.Close()).IsNil()
}
time.Sleep(20 * time.Second)
{
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: []byte{127, 0, 0, 1},
Port: int(clientPort),
})
assert.Error(err).IsNil()
payload := "dokodemo request."
nBytes, err := conn.Write([]byte(payload))
assert.Error(err).IsNil()
assert.Int(nBytes).Equals(len(payload))
response := make([]byte, 1024)
nBytes, err = conn.Read(response)
assert.Error(err).IsNil()
assert.Bytes(response[:nBytes]).Equals(xor([]byte(payload)))
assert.Error(conn.Close()).IsNil()
}
CloseAllServers()
}