move port out of address

This commit is contained in:
v2ray
2015-12-16 23:53:38 +01:00
parent 4be7cd7908
commit 34a0cb0b70
34 changed files with 194 additions and 172 deletions
+4 -6
View File
@@ -261,23 +261,21 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
}
func (request *Socks5Request) Destination() v2net.Destination {
var address v2net.Address
switch request.AddrType {
case AddrTypeIPv4:
address = v2net.IPAddress(request.IPv4[:], request.Port)
return v2net.TCPDestination(v2net.IPAddress(request.IPv4[:]), request.Port)
case AddrTypeIPv6:
address = v2net.IPAddress(request.IPv6[:], request.Port)
return v2net.TCPDestination(v2net.IPAddress(request.IPv6[:]), request.Port)
case AddrTypeDomain:
maybeIP := net.ParseIP(request.Domain)
if maybeIP != nil {
address = v2net.IPAddress(maybeIP, request.Port)
return v2net.TCPDestination(v2net.IPAddress(maybeIP), request.Port)
} else {
address = v2net.DomainAddress(request.Domain, request.Port)
return v2net.TCPDestination(v2net.DomainAddress(request.Domain), request.Port)
}
default:
panic("Unknown address type")
}
return v2net.NewTCPDestination(address)
}
const (
+10 -9
View File
@@ -17,11 +17,12 @@ var (
type Socks5UDPRequest struct {
Fragment byte
Address v2net.Address
Port v2net.Port
Data *alloc.Buffer
}
func (request *Socks5UDPRequest) Destination() v2net.Destination {
return v2net.NewUDPDestination(request.Address)
return v2net.UDPDestination(request.Address, request.Port)
}
func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) {
@@ -34,7 +35,7 @@ func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) {
case request.Address.IsDomain():
buffer.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain()))).Append([]byte(request.Address.Domain()))
}
buffer.Append(request.Address.Port().Bytes())
buffer.Append(request.Port.Bytes())
buffer.Append(request.Data.Value)
}
@@ -56,16 +57,16 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
return nil, transport.CorruptedPacket
}
ip := packet[4:8]
port := v2net.PortFromBytes(packet[8:10])
request.Address = v2net.IPAddress(ip, port)
request.Port = v2net.PortFromBytes(packet[8:10])
request.Address = v2net.IPAddress(ip)
dataBegin = 10
case AddrTypeIPv6:
if len(packet) < 22 {
return nil, transport.CorruptedPacket
}
ip := packet[4:20]
port := v2net.PortFromBytes(packet[20:22])
request.Address = v2net.IPAddress(ip, port)
request.Port = v2net.PortFromBytes(packet[20:22])
request.Address = v2net.IPAddress(ip)
dataBegin = 22
case AddrTypeDomain:
domainLength := int(packet[4])
@@ -73,12 +74,12 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
return nil, transport.CorruptedPacket
}
domain := string(packet[5 : 5+domainLength])
port := v2net.PortFromBytes(packet[5+domainLength : 5+domainLength+2])
request.Port = v2net.PortFromBytes(packet[5+domainLength : 5+domainLength+2])
maybeIP := net.ParseIP(domain)
if maybeIP != nil {
request.Address = v2net.IPAddress(maybeIP, port)
request.Address = v2net.IPAddress(maybeIP)
} else {
request.Address = v2net.DomainAddress(domain, port)
request.Address = v2net.DomainAddress(domain)
}
dataBegin = 5 + domainLength + 2
default:
+4 -1
View File
@@ -3,6 +3,8 @@ package protocol
import (
"testing"
v2net "github.com/v2ray/v2ray-core/common/net"
netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
"github.com/v2ray/v2ray-core/transport"
@@ -31,6 +33,7 @@ func TestDomainAddressRequest(t *testing.T) {
assert.Error(err).IsNil()
assert.Byte(request.Fragment).Equals(1)
assert.String(request.Address).Equals("v2ray.com:80")
assert.String(request.Address).Equals("v2ray.com")
netassert.Port(request.Port).Equals(v2net.Port(80))
assert.Bytes(request.Data.Value).Equals([]byte("Actual payload"))
}
+7 -7
View File
@@ -191,12 +191,12 @@ func (this *SocksServer) handleUDP(reader *v2net.TimeOutReader, writer io.Writer
response.Port = udpAddr.Port()
switch {
case udpAddr.IsIPv4():
response.SetIPv4(udpAddr.IP())
case udpAddr.IsIPv6():
response.SetIPv6(udpAddr.IP())
case udpAddr.IsDomain():
response.SetDomain(udpAddr.Domain())
case udpAddr.Address().IsIPv4():
response.SetIPv4(udpAddr.Address().IP())
case udpAddr.Address().IsIPv6():
response.SetIPv6(udpAddr.Address().IP())
case udpAddr.Address().IsDomain():
response.SetDomain(udpAddr.Address().Domain())
}
responseBuffer := alloc.NewSmallBuffer().Clear()
@@ -236,7 +236,7 @@ func (this *SocksServer) handleSocks4(reader io.Reader, writer io.Writer, auth p
return UnsupportedSocksCommand
}
dest := v2net.NewTCPDestination(v2net.IPAddress(auth.IP[:], auth.Port))
dest := v2net.TCPDestination(v2net.IPAddress(auth.IP[:]), auth.Port)
packet := v2net.NewPacket(dest, nil, true)
this.transport(reader, writer, packet)
return nil
+2 -2
View File
@@ -71,7 +71,7 @@ func TestSocksTcpConnect(t *testing.T) {
assert.Bytes([]byte(data2Send)).Equals(connOutput.Bytes())
assert.Bytes(dataReturned).Equals(connInput)
assert.StringLiteral(targetServer).Equals(och.Destination.Address().String())
assert.StringLiteral(targetServer).Equals(och.Destination.NetAddr())
}
func TestSocksTcpConnectWithUserPass(t *testing.T) {
@@ -129,7 +129,7 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) {
assert.Bytes([]byte(data2Send)).Equals(connOutput.Bytes())
assert.Bytes(dataReturned).Equals(connInput)
assert.StringLiteral(targetServer).Equals(och.Destination.Address().String())
assert.StringLiteral(targetServer).Equals(och.Destination.NetAddr())
}
func TestSocksTcpConnectWithWrongUserPass(t *testing.T) {
+6 -5
View File
@@ -9,7 +9,7 @@ import (
"github.com/v2ray/v2ray-core/proxy/socks/protocol"
)
var udpAddress v2net.Address
var udpAddress v2net.Destination
func (this *SocksServer) ListenUDP(port v2net.Port) error {
addr := &net.UDPAddr{
@@ -22,13 +22,13 @@ func (this *SocksServer) ListenUDP(port v2net.Port) error {
log.Error("Socks failed to listen UDP on port %d: %v", port, err)
return err
}
udpAddress = v2net.IPAddress(this.config.IP(), port)
udpAddress = v2net.UDPDestination(v2net.IPAddress(this.config.IP()), port)
go this.AcceptPackets(conn)
return nil
}
func (this *SocksServer) getUDPAddr() v2net.Address {
func (this *SocksServer) getUDPAddr() v2net.Destination {
return udpAddress
}
@@ -60,11 +60,11 @@ func (this *SocksServer) AcceptPackets(conn *net.UDPConn) error {
udpPacket := v2net.NewPacket(request.Destination(), request.Data, false)
log.Info("Send packet to %s with %d bytes", udpPacket.Destination().String(), request.Data.Len())
go this.handlePacket(conn, udpPacket, addr, request.Address)
go this.handlePacket(conn, udpPacket, addr, request.Address, request.Port)
}
}
func (this *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet, clientAddr *net.UDPAddr, targetAddr v2net.Address) {
func (this *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet, clientAddr *net.UDPAddr, targetAddr v2net.Address, port v2net.Port) {
ray := this.space.PacketDispatcher().DispatchToOutbound(packet)
close(ray.InboundInput())
@@ -72,6 +72,7 @@ func (this *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet, cl
response := &protocol.Socks5UDPRequest{
Fragment: 0,
Address: targetAddr,
Port: port,
Data: data,
}
log.Info("Writing back UDP response with %d bytes from %s to %s", data.Len(), targetAddr.String(), clientAddr.String())