1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-07 10:47:48 -05:00
v2fly/proxy/shadowsocks/protocol.go

141 lines
4.0 KiB
Go
Raw Normal View History

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"
"github.com/v2ray/v2ray-core/common/serial"
"github.com/v2ray/v2ray-core/proxy"
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-05-01 11:18:02 -04:00
func (this *Request) Release() {
this.Address = nil
if this.UDPPayload != nil {
this.UDPPayload.Release()
}
}
func (this *Request) DetachUDPPayload() *alloc.Buffer {
payload := this.UDPPayload
this.UDPPayload = nil
return payload
}
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()
_, 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)
return nil, transport.ErrorCorruptedPacket
2016-01-28 06:33:58 -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:
_, 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)
return nil, transport.ErrorCorruptedPacket
2016-01-28 06:33:58 -05:00
}
request.Address = v2net.IPAddress(buffer.Value[lenBuffer : lenBuffer+4])
lenBuffer += 4
2016-01-28 06:33:58 -05:00
case AddrTypeIPv6:
_, 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)
return nil, transport.ErrorCorruptedPacket
2016-01-28 06:33:58 -05:00
}
request.Address = v2net.IPAddress(buffer.Value[lenBuffer : lenBuffer+16])
lenBuffer += 16
2016-01-28 06:33:58 -05:00
case AddrTypeDomain:
_, 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)
return nil, transport.ErrorCorruptedPacket
2016-01-28 06:33:58 -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)
return nil, transport.ErrorCorruptedPacket
2016-01-28 06:33:58 -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)
return nil, transport.ErrorCorruptedPacket
2016-01-28 06:33:58 -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)
return nil, transport.ErrorCorruptedPacket
2016-01-28 06:33:58 -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:])
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 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)
return nil, transport.ErrorCorruptedPacket
2016-01-29 15:55:42 -05:00
}
}
}
2016-01-29 15:55:42 -05:00
if request.OTA {
actualAuth := auth.Authenticate(nil, buffer.Value[0:lenBuffer])
if !serial.BytesLiteral(actualAuth).Equals(serial.BytesLiteral(authBytes)) {
2016-02-28 15:02:03 -05:00
log.Debug("Shadowsocks: Invalid OTA. Expecting ", actualAuth, ", but got ", authBytes)
log.Warning("Shadowsocks: Invalid OTA.")
return nil, proxy.ErrorInvalidAuthentication
}
}
2016-01-28 06:33:58 -05:00
return request, nil
}