move port out of address

This commit is contained in:
v2ray committed 2015-12-16 23:53:38 +01:00
1 parent 4be7cd7908
commit 34a0cb0b70
34 files changed
+194 -172

No files matched your search

+6 -6
View File
@@ -14,8 +14,8 @@ import (
)
type ConfigTarget struct {
Address v2net.Address
Users []*vmessjson.ConfigUser
Destination v2net.Destination
Users []*vmessjson.ConfigUser
}
func (t *ConfigTarget) UnmarshalJSON(data []byte) error {
@@ -38,9 +38,9 @@ func (t *ConfigTarget) UnmarshalJSON(data []byte) error {
return proxyconfig.BadConfiguration
}
if rawConfig.Address.IsIP() {
t.Address = v2net.IPAddress(rawConfig.Address.IP(), rawConfig.Port)
t.Destination = v2net.TCPDestination(v2net.IPAddress(rawConfig.Address.IP()), rawConfig.Port)
} else {
t.Address = v2net.DomainAddress(rawConfig.Address.Domain(), rawConfig.Port)
t.Destination = v2net.TCPDestination(v2net.DomainAddress(rawConfig.Address.Domain()), rawConfig.Port)
}
return nil
}
@@ -74,8 +74,8 @@ func (o *Outbound) Receivers() []*outbound.Receiver {
users = append(users, rawUser)
}
targets = append(targets, &outbound.Receiver{
Address: rawTarget.Address,
Accounts: users,
Destination: rawTarget.Destination,
Accounts: users,
})
}
return targets
+1 -1
View File
@@ -27,7 +27,7 @@ func TestConfigTargetParsing(t *testing.T) {
var target *jsonconfig.ConfigTarget
err := json.Unmarshal([]byte(rawJson), &target)
assert.Error(err).IsNil()
assert.String(target.Address).Equals("127.0.0.1:80")
assert.String(target.Destination).Equals("tcp:127.0.0.1:80")
assert.Int(len(target.Users)).Equals(1)
assert.String(target.Users[0].ID()).Equals("e641f5ad-9397-41e3-bf1a-e8740dfed019")
}
+17 -5
View File
@@ -34,6 +34,7 @@ func (this *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.Out
User: vNextUser,
Command: command,
Address: firstPacket.Destination().Address(),
Port: firstPacket.Destination().Port(),
}
buffer := alloc.NewSmallBuffer()
@@ -46,8 +47,21 @@ func (this *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.Out
return startCommunicate(request, vNextAddress, ray, firstPacket)
}
func startCommunicate(request *protocol.VMessRequest, dest v2net.Address, ray ray.OutboundRay, firstPacket v2net.Packet) error {
conn, err := net.Dial("tcp", dest.String())
func startCommunicate(request *protocol.VMessRequest, dest v2net.Destination, ray ray.OutboundRay, firstPacket v2net.Packet) error {
var destIp net.IP
if dest.Address().IsIPv4() || dest.Address().IsIPv6() {
destIp = dest.Address().IP()
} else {
ips, err := net.LookupIP(dest.Address().Domain())
if err != nil {
return err
}
destIp = ips[0]
}
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: destIp,
Port: int(dest.Port()),
})
if err != nil {
log.Error("Failed to open %s: %v", dest.String(), err)
if ray != nil {
@@ -69,9 +83,7 @@ func startCommunicate(request *protocol.VMessRequest, dest v2net.Address, ray ra
go handleResponse(conn, request, output, &responseFinish, (request.Command == protocol.CmdUDP))
requestFinish.Lock()
if tcpConn, ok := conn.(*net.TCPConn); ok {
tcpConn.CloseWrite()
}
conn.CloseWrite()
responseFinish.Lock()
return nil
}
+4 -4
View File
@@ -8,8 +8,8 @@ import (
)
type Receiver struct {
Address v2net.Address
Accounts []vmess.User
Destination v2net.Destination
Accounts []vmess.User
}
type ReceiverManager struct {
@@ -22,7 +22,7 @@ func NewReceiverManager(receivers []*Receiver) *ReceiverManager {
}
}
func (this *ReceiverManager) PickReceiver() (v2net.Address, vmess.User) {
func (this *ReceiverManager) PickReceiver() (v2net.Destination, vmess.User) {
receiverLen := len(this.receivers)
receiverIdx := 0
if receiverLen > 1 {
@@ -37,5 +37,5 @@ func (this *ReceiverManager) PickReceiver() (v2net.Address, vmess.User) {
userIdx = rand.Intn(userLen)
}
user := receiver.Accounts[userIdx]
return receiver.Address, user
return receiver.Destination, user
}
+8 -7
View File
@@ -41,14 +41,15 @@ type VMessRequest struct {
ResponseHeader []byte
Command byte
Address v2net.Address
Port v2net.Port
}
// Destination is the final destination of this request.
func (this *VMessRequest) Destination() v2net.Destination {
if this.Command == CmdTCP {
return v2net.NewTCPDestination(this.Address)
return v2net.TCPDestination(this.Address, this.Port)
} else {
return v2net.NewUDPDestination(this.Address)
return v2net.UDPDestination(this.Address, this.Port)
}
}
@@ -106,7 +107,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
request.ResponseHeader = buffer.Value[33:37] // 4 bytes
request.Command = buffer.Value[37]
port := v2net.PortFromBytes(buffer.Value[38:40])
request.Port = v2net.PortFromBytes(buffer.Value[38:40])
switch buffer.Value[40] {
case addrTypeIPv4:
@@ -115,14 +116,14 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
if err != nil {
return nil, err
}
request.Address = v2net.IPAddress(buffer.Value[41:45], port)
request.Address = v2net.IPAddress(buffer.Value[41:45])
case addrTypeIPv6:
_, err = v2net.ReadAllBytes(decryptor, buffer.Value[41:57]) // 16 bytes
bufferLen += 16
if err != nil {
return nil, err
}
request.Address = v2net.IPAddress(buffer.Value[41:57], port)
request.Address = v2net.IPAddress(buffer.Value[41:57])
case addrTypeDomain:
_, err = v2net.ReadAllBytes(decryptor, buffer.Value[41:42])
if err != nil {
@@ -134,7 +135,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
return nil, err
}
bufferLen += 1 + domainLength
request.Address = v2net.DomainAddress(string(buffer.Value[42:42+domainLength]), port)
request.Address = v2net.DomainAddress(string(buffer.Value[42 : 42+domainLength]))
}
_, err = v2net.ReadAllBytes(decryptor, buffer.Value[bufferLen:bufferLen+4])
@@ -172,7 +173,7 @@ func (this *VMessRequest) ToBytes(idHash user.CounterHash, randomRangeInt64 user
buffer.Append(this.RequestKey)
buffer.Append(this.ResponseHeader)
buffer.AppendBytes(this.Command)
buffer.Append(this.Address.Port().Bytes())
buffer.Append(this.Port.Bytes())
switch {
case this.Address.IsIPv4():
+4 -2
View File
@@ -55,7 +55,8 @@ func TestVMessSerialization(t *testing.T) {
request.ResponseHeader = randBytes[32:]
request.Command = byte(0x01)
request.Address = v2net.DomainAddress("v2ray.com", 80)
request.Address = v2net.DomainAddress("v2ray.com")
request.Port = v2net.Port(80)
mockTime := int64(1823730)
@@ -113,7 +114,8 @@ func BenchmarkVMessRequestWriting(b *testing.B) {
request.ResponseHeader = randBytes[32:]
request.Command = byte(0x01)
request.Address = v2net.DomainAddress("v2ray.com", 80)
request.Address = v2net.DomainAddress("v2ray.com")
request.Port = v2net.Port(80)
for i := 0; i < b.N; i++ {
request.ToBytes(user.NewTimeHash(user.HMACHash{}), user.GenerateRandomInt64InRange, nil)
+2 -2
View File
@@ -52,7 +52,7 @@ func TestVMessInAndOut(t *testing.T) {
SettingsValue: &outboundjson.Outbound{
[]*outboundjson.ConfigTarget{
&outboundjson.ConfigTarget{
Address: v2net.IPAddress([]byte{127, 0, 0, 1}, portB),
Destination: v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), portB),
Users: []*vmessjson.ConfigUser{
&vmessjson.ConfigUser{Id: testAccount},
},
@@ -99,7 +99,7 @@ func TestVMessInAndOut(t *testing.T) {
err = pointB.Start()
assert.Error(err).IsNil()
dest := v2net.NewTCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}, 80))
dest := v2net.TCPDestination(v2net.IPAddress([]byte{1, 2, 3, 4}), 80)
ich.Communicate(v2net.NewPacket(dest, nil, true))
assert.Bytes(ichConnInput).Equals(ochConnOutput.Bytes())
assert.Bytes(ichConnOutput.Bytes()).Equals(ochConnInput)