mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-13 14:44:15 +00:00
Full implementation of Socks5 protocol, proxying is on the way
This commit is contained in:
1 parent
5317b7d112
commit
fb1ad8b81b
7 files changed
+121
-10
No files matched your search
+55
-4
@@ -68,12 +68,15 @@ func (r *Socks5AuthenticationResponse) ToBytes() []byte {
|
||||
|
||||
func WriteAuthentication(writer io.Writer, response Socks5AuthenticationResponse) error {
|
||||
_, err := writer.Write(response.ToBytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
const (
|
||||
AddrTypeIPv4 = byte(0x01)
|
||||
AddrTypeIPv6 = byte(0x04)
|
||||
AddrTypeDomain = byte(0x03)
|
||||
)
|
||||
|
||||
type Socks5Request struct {
|
||||
version byte
|
||||
command byte
|
||||
@@ -149,3 +152,51 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
||||
request.port = binary.BigEndian.Uint16(buffer)
|
||||
return
|
||||
}
|
||||
|
||||
const (
|
||||
ErrorSuccess = byte(0x00)
|
||||
ErrorGeneralFailure = byte(0x01)
|
||||
ErrorConnectionNotAllowed = byte(0x02)
|
||||
ErrorNetworkUnreachable = byte(0x03)
|
||||
ErrorHostUnUnreachable = byte(0x04)
|
||||
ErrorConnectionRefused = byte(0x05)
|
||||
ErrorTTLExpired = byte(0x06)
|
||||
ErrorCommandNotSupported = byte(0x07)
|
||||
ErrorAddressTypeNotSupported = byte(0x08)
|
||||
)
|
||||
|
||||
type Socks5Response struct {
|
||||
Version byte
|
||||
Error byte
|
||||
AddrType byte
|
||||
IPv4 [4]byte
|
||||
Domain string
|
||||
IPv6 [16]byte
|
||||
Port uint16
|
||||
}
|
||||
|
||||
func (r Socks5Response) toBytes() []byte {
|
||||
buffer := make([]byte, 0, 300)
|
||||
buffer = append(buffer, r.Version)
|
||||
buffer = append(buffer, r.Error)
|
||||
buffer = append(buffer, 0x00) // reserved
|
||||
buffer = append(buffer, r.AddrType)
|
||||
switch r.AddrType {
|
||||
case 0x01:
|
||||
buffer = append(buffer, r.IPv4[:]...)
|
||||
case 0x03:
|
||||
buffer = append(buffer, byte(len(r.Domain)))
|
||||
buffer = append(buffer, []byte(r.Domain)...)
|
||||
case 0x04:
|
||||
buffer = append(buffer, r.IPv6[:]...)
|
||||
}
|
||||
portBuffer := make([]byte, 2)
|
||||
binary.BigEndian.PutUint16(portBuffer, r.Port)
|
||||
buffer = append(buffer, portBuffer...)
|
||||
return buffer
|
||||
}
|
||||
|
||||
func WriteResponse(writer io.Writer, response Socks5Response) error {
|
||||
_, err := writer.Write(response.toBytes())
|
||||
return err
|
||||
}
|
||||
@@ -68,3 +68,27 @@ func TestRequestRead(t *testing.T) {
|
||||
t.Errorf("Expected port 53, but got %d", request.port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponseToBytes(t *testing.T) {
|
||||
response := Socks5Response{
|
||||
socksVersion,
|
||||
ErrorSuccess,
|
||||
AddrTypeIPv4,
|
||||
[4]byte{0x72, 0x72, 0x72, 0x72},
|
||||
"",
|
||||
[16]byte{},
|
||||
uint16(53),
|
||||
}
|
||||
rawResponse := response.toBytes()
|
||||
expectedBytes := []byte{
|
||||
socksVersion,
|
||||
ErrorSuccess,
|
||||
byte(0x00),
|
||||
AddrTypeIPv4,
|
||||
0x72, 0x72, 0x72, 0x72,
|
||||
byte(0x00), byte(0x035),
|
||||
}
|
||||
if !bytes.Equal(rawResponse, expectedBytes) {
|
||||
t.Errorf("Expected response %v, but got %v", expectedBytes, rawResponse)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user