1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-02 09:17:55 -04:00
v2fly/proxy/shadowsocks/protocol.go

78 lines
2.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/transport"
)
const (
AddrTypeIPv4 = 1
AddrTypeIPv6 = 4
AddrTypeDomain = 3
)
type Request struct {
Address v2net.Address
Port v2net.Port
}
func ReadRequest(reader io.Reader) (*Request, error) {
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.CorruptedPacket
}
request := new(Request)
addrType := buffer.Value[0]
switch addrType {
case AddrTypeIPv4:
_, err := io.ReadFull(reader, buffer.Value[:4])
2016-01-28 06:33:58 -05:00
if err != nil {
log.Error("Shadowsocks: Failed to read IPv4 address: ", err)
return nil, transport.CorruptedPacket
}
request.Address = v2net.IPAddress(buffer.Value[:4])
case AddrTypeIPv6:
_, err := io.ReadFull(reader, buffer.Value[:16])
2016-01-28 06:33:58 -05:00
if err != nil {
log.Error("Shadowsocks: Failed to read IPv6 address: ", err)
return nil, transport.CorruptedPacket
}
request.Address = v2net.IPAddress(buffer.Value[:16])
case AddrTypeDomain:
_, err := io.ReadFull(reader, buffer.Value[:1])
2016-01-28 06:33:58 -05:00
if err != nil {
log.Error("Shadowsocks: Failed to read domain lenth: ", err)
return nil, transport.CorruptedPacket
}
domainLength := int(buffer.Value[0])
_, err = io.ReadFull(reader, buffer.Value[:domainLength])
2016-01-28 06:33:58 -05:00
if err != nil {
log.Error("Shadowsocks: Failed to read domain: ", err)
return nil, transport.CorruptedPacket
}
request.Address = v2net.DomainAddress(string(buffer.Value[:domainLength]))
default:
log.Error("Shadowsocks: Unknown address type: ", addrType)
return nil, transport.CorruptedPacket
}
_, err = io.ReadFull(reader, buffer.Value[:2])
2016-01-28 06:33:58 -05:00
if err != nil {
log.Error("Shadowsocks: Failed to read port: ", err)
return nil, transport.CorruptedPacket
}
request.Port = v2net.PortFromBytes(buffer.Value[:2])
return request, nil
}