2016-01-28 06:33:58 -05:00
|
|
|
package shadowsocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2016-01-29 10:43:45 -05:00
|
|
|
"github.com/v2ray/v2ray-core/common/serial"
|
2016-01-28 06:33:58 -05:00
|
|
|
"github.com/v2ray/v2ray-core/transport"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
AddrTypeIPv4 = 1
|
|
|
|
AddrTypeIPv6 = 4
|
|
|
|
AddrTypeDomain = 3
|
|
|
|
)
|
|
|
|
|
|
|
|
type Request struct {
|
2016-01-29 15:55:42 -05:00
|
|
|
Address v2net.Address
|
|
|
|
Port v2net.Port
|
|
|
|
OTA bool
|
|
|
|
UDPPayload *alloc.Buffer
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
|
|
|
|
2016-01-29 15:55:42 -05:00
|
|
|
func ReadRequest(reader io.Reader, auth *Authenticator, udp bool) (*Request, error) {
|
2016-01-28 06:33:58 -05:00
|
|
|
buffer := alloc.NewSmallBuffer()
|
|
|
|
defer buffer.Release()
|
|
|
|
|
2016-01-28 17:58:23 -05:00
|
|
|
_, err := io.ReadFull(reader, buffer.Value[:1])
|
2016-01-28 06:33:58 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Shadowsocks: Failed to read address type: ", err)
|
2016-01-30 11:20:30 -05:00
|
|
|
return nil, transport.ErrorCorruptedPacket
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
2016-01-29 10:43:45 -05:00
|
|
|
lenBuffer := 1
|
2016-01-28 06:33:58 -05:00
|
|
|
|
|
|
|
request := new(Request)
|
|
|
|
|
2016-01-29 09:09:51 -05:00
|
|
|
addrType := (buffer.Value[0] & 0x0F)
|
|
|
|
if (buffer.Value[0] & 0x10) == 0x10 {
|
|
|
|
request.OTA = true
|
|
|
|
}
|
2016-01-28 06:33:58 -05:00
|
|
|
switch addrType {
|
|
|
|
case AddrTypeIPv4:
|
2016-01-29 10:43:45 -05:00
|
|
|
_, err := io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+4])
|
2016-01-28 06:33:58 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Shadowsocks: Failed to read IPv4 address: ", err)
|
2016-01-30 11:20:30 -05:00
|
|
|
return nil, transport.ErrorCorruptedPacket
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
2016-01-29 10:43:45 -05:00
|
|
|
request.Address = v2net.IPAddress(buffer.Value[lenBuffer : lenBuffer+4])
|
|
|
|
lenBuffer += 4
|
2016-01-28 06:33:58 -05:00
|
|
|
case AddrTypeIPv6:
|
2016-01-29 10:43:45 -05:00
|
|
|
_, err := io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+16])
|
2016-01-28 06:33:58 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Shadowsocks: Failed to read IPv6 address: ", err)
|
2016-01-30 11:20:30 -05:00
|
|
|
return nil, transport.ErrorCorruptedPacket
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
2016-01-29 10:43:45 -05:00
|
|
|
request.Address = v2net.IPAddress(buffer.Value[lenBuffer : lenBuffer+16])
|
|
|
|
lenBuffer += 16
|
2016-01-28 06:33:58 -05:00
|
|
|
case AddrTypeDomain:
|
2016-01-29 10:43:45 -05:00
|
|
|
_, err := io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+1])
|
2016-01-28 06:33:58 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Shadowsocks: Failed to read domain lenth: ", err)
|
2016-01-30 11:20:30 -05:00
|
|
|
return nil, transport.ErrorCorruptedPacket
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
2016-01-29 10:43:45 -05:00
|
|
|
domainLength := int(buffer.Value[lenBuffer])
|
|
|
|
lenBuffer++
|
|
|
|
_, err = io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+domainLength])
|
2016-01-28 06:33:58 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Shadowsocks: Failed to read domain: ", err)
|
2016-01-30 11:20:30 -05:00
|
|
|
return nil, transport.ErrorCorruptedPacket
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
2016-01-29 10:43:45 -05:00
|
|
|
request.Address = v2net.DomainAddress(string(buffer.Value[lenBuffer : lenBuffer+domainLength]))
|
|
|
|
lenBuffer += domainLength
|
2016-01-28 06:33:58 -05:00
|
|
|
default:
|
|
|
|
log.Error("Shadowsocks: Unknown address type: ", addrType)
|
2016-01-30 11:20:30 -05:00
|
|
|
return nil, transport.ErrorCorruptedPacket
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
|
|
|
|
2016-01-29 10:43:45 -05:00
|
|
|
_, err = io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+2])
|
2016-01-28 06:33:58 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Shadowsocks: Failed to read port: ", err)
|
2016-01-30 11:20:30 -05:00
|
|
|
return nil, transport.ErrorCorruptedPacket
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
|
|
|
|
2016-01-29 10:43:45 -05:00
|
|
|
request.Port = v2net.PortFromBytes(buffer.Value[lenBuffer : lenBuffer+2])
|
|
|
|
lenBuffer += 2
|
|
|
|
|
2016-01-29 15:55:42 -05:00
|
|
|
var authBytes []byte
|
|
|
|
|
|
|
|
if udp {
|
|
|
|
nBytes, err := reader.Read(buffer.Value[lenBuffer:])
|
2016-01-29 10:43:45 -05:00
|
|
|
if err != nil {
|
2016-01-29 15:55:42 -05:00
|
|
|
log.Error("Shadowsocks: Failed to read UDP payload: ", err)
|
2016-02-03 15:44:20 -05:00
|
|
|
return nil, transport.ErrorCorruptedPacket
|
2016-01-29 15:55:42 -05:00
|
|
|
}
|
|
|
|
buffer.Slice(0, lenBuffer+nBytes)
|
|
|
|
if request.OTA {
|
|
|
|
authBytes = buffer.Value[lenBuffer+nBytes-AuthSize:]
|
|
|
|
request.UDPPayload = alloc.NewSmallBuffer().Clear().Append(buffer.Value[lenBuffer : lenBuffer+nBytes-AuthSize])
|
|
|
|
lenBuffer = lenBuffer + nBytes - AuthSize
|
|
|
|
} else {
|
|
|
|
request.UDPPayload = alloc.NewSmallBuffer().Clear().Append(buffer.Value[lenBuffer:])
|
2016-01-29 10:43:45 -05:00
|
|
|
}
|
2016-01-29 15:55:42 -05:00
|
|
|
} else {
|
|
|
|
if request.OTA {
|
|
|
|
authBytes = buffer.Value[lenBuffer : lenBuffer+AuthSize]
|
|
|
|
_, err = io.ReadFull(reader, authBytes)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Shadowsocks: Failed to read OTA: ", err)
|
2016-01-30 11:20:30 -05:00
|
|
|
return nil, transport.ErrorCorruptedPacket
|
2016-01-29 15:55:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-29 10:43:45 -05:00
|
|
|
|
2016-01-29 15:55:42 -05:00
|
|
|
if request.OTA {
|
2016-01-29 10:43:45 -05:00
|
|
|
actualAuth := auth.Authenticate(nil, buffer.Value[0:lenBuffer])
|
|
|
|
if !serial.BytesLiteral(actualAuth).Equals(serial.BytesLiteral(authBytes)) {
|
|
|
|
log.Error("Shadowsocks: Invalid OTA: ", actualAuth)
|
2016-01-30 11:20:30 -05:00
|
|
|
return nil, transport.ErrorCorruptedPacket
|
2016-01-29 10:43:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-28 06:33:58 -05:00
|
|
|
return request, nil
|
|
|
|
}
|