mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-17 14:57:44 -05:00
socks5 server implementation
This commit is contained in:
parent
361f87df09
commit
e764275c58
@ -9,6 +9,10 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
socksVersion = uint8(5)
|
socksVersion = uint8(5)
|
||||||
|
|
||||||
|
AuthNotRequired = byte(0x00)
|
||||||
|
AuthGssApi = byte(0x01)
|
||||||
|
AuthUserPass = byte(0x02)
|
||||||
)
|
)
|
||||||
|
|
||||||
// Authentication request header of Socks5 protocol
|
// Authentication request header of Socks5 protocol
|
||||||
@ -18,6 +22,15 @@ type Socks5AuthenticationRequest struct {
|
|||||||
authMethods [256]byte
|
authMethods [256]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (request *Socks5AuthenticationRequest) HasAuthMethod(method byte) bool {
|
||||||
|
for i := byte(0); i < request.nMethods; i++ {
|
||||||
|
if request.authMethods[i] == method {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, err error) {
|
func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, err error) {
|
||||||
buffer := make([]byte, 2)
|
buffer := make([]byte, 2)
|
||||||
nBytes, err := reader.Read(buffer)
|
nBytes, err := reader.Read(buffer)
|
||||||
@ -59,6 +72,13 @@ type Socks5AuthenticationResponse struct {
|
|||||||
authMethod byte
|
authMethod byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewAuthenticationResponse(authMethod byte) *Socks5AuthenticationResponse {
|
||||||
|
response := new(Socks5AuthenticationResponse)
|
||||||
|
response.version = socksVersion
|
||||||
|
response.authMethod = authMethod
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Socks5AuthenticationResponse) ToBytes() []byte {
|
func (r *Socks5AuthenticationResponse) ToBytes() []byte {
|
||||||
buffer := make([]byte, 2 /* size of Socks5AuthenticationResponse */)
|
buffer := make([]byte, 2 /* size of Socks5AuthenticationResponse */)
|
||||||
buffer[0] = r.version
|
buffer[0] = r.version
|
||||||
@ -66,7 +86,7 @@ func (r *Socks5AuthenticationResponse) ToBytes() []byte {
|
|||||||
return buffer
|
return buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteAuthentication(writer io.Writer, response Socks5AuthenticationResponse) error {
|
func WriteAuthentication(writer io.Writer, response *Socks5AuthenticationResponse) error {
|
||||||
_, err := writer.Write(response.ToBytes())
|
_, err := writer.Write(response.ToBytes())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -75,16 +95,20 @@ const (
|
|||||||
AddrTypeIPv4 = byte(0x01)
|
AddrTypeIPv4 = byte(0x01)
|
||||||
AddrTypeIPv6 = byte(0x04)
|
AddrTypeIPv6 = byte(0x04)
|
||||||
AddrTypeDomain = byte(0x03)
|
AddrTypeDomain = byte(0x03)
|
||||||
|
|
||||||
|
CmdConnect = byte(0x01)
|
||||||
|
CmdBind = byte(0x02)
|
||||||
|
CmdUdpAssociate = byte(0x03)
|
||||||
)
|
)
|
||||||
|
|
||||||
type Socks5Request struct {
|
type Socks5Request struct {
|
||||||
version byte
|
Version byte
|
||||||
command byte
|
Command byte
|
||||||
addrType byte
|
AddrType byte
|
||||||
ipv4 [4]byte
|
IPv4 [4]byte
|
||||||
domain string
|
Domain string
|
||||||
ipv6 [16]byte
|
IPv6 [16]byte
|
||||||
port uint16
|
Port uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
||||||
@ -99,13 +123,13 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
request.version = buffer[0]
|
request.Version = buffer[0]
|
||||||
request.command = buffer[1]
|
request.Command = buffer[1]
|
||||||
// buffer[2] is a reserved field
|
// buffer[2] is a reserved field
|
||||||
request.addrType = buffer[3]
|
request.AddrType = buffer[3]
|
||||||
switch request.addrType {
|
switch request.AddrType {
|
||||||
case 0x01:
|
case 0x01:
|
||||||
nBytes, err = reader.Read(request.ipv4[:])
|
nBytes, err = reader.Read(request.IPv4[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -124,9 +148,9 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
|||||||
err = fmt.Errorf("Unable to read domain")
|
err = fmt.Errorf("Unable to read domain")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
request.domain = string(buffer[1 : domainLength+1])
|
request.Domain = string(buffer[1 : domainLength+1])
|
||||||
case 0x04:
|
case 0x04:
|
||||||
nBytes, err = reader.Read(request.ipv6[:])
|
nBytes, err = reader.Read(request.IPv6[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -135,7 +159,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("Unexpected address type %d", request.addrType)
|
err = fmt.Errorf("Unexpected address type %d", request.AddrType)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,7 +173,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
request.port = binary.BigEndian.Uint16(buffer)
|
request.Port = binary.BigEndian.Uint16(buffer)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,7 +199,28 @@ type Socks5Response struct {
|
|||||||
Port uint16
|
Port uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Socks5Response) toBytes() []byte {
|
func NewSocks5Response() *Socks5Response {
|
||||||
|
response := new(Socks5Response)
|
||||||
|
response.Version = socksVersion
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Socks5Response) SetIPv4(ipv4 []byte) {
|
||||||
|
r.AddrType = AddrTypeIPv4
|
||||||
|
copy(r.IPv4[:], ipv4)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Socks5Response) SetIPv6(ipv6 []byte) {
|
||||||
|
r.AddrType = AddrTypeIPv6
|
||||||
|
copy(r.IPv6[:], ipv6)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Socks5Response) SetDomain(domain string) {
|
||||||
|
r.AddrType = AddrTypeDomain
|
||||||
|
r.Domain = domain
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Socks5Response) toBytes() []byte {
|
||||||
buffer := make([]byte, 0, 300)
|
buffer := make([]byte, 0, 300)
|
||||||
buffer = append(buffer, r.Version)
|
buffer = append(buffer, r.Version)
|
||||||
buffer = append(buffer, r.Error)
|
buffer = append(buffer, r.Error)
|
||||||
@ -196,7 +241,7 @@ func (r Socks5Response) toBytes() []byte {
|
|||||||
return buffer
|
return buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteResponse(writer io.Writer, response Socks5Response) error {
|
func WriteResponse(writer io.Writer, response *Socks5Response) error {
|
||||||
_, err := writer.Write(response.toBytes())
|
_, err := writer.Write(response.toBytes())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -52,20 +52,20 @@ func TestRequestRead(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error %v", err)
|
t.Errorf("Unexpected error %v", err)
|
||||||
}
|
}
|
||||||
if request.version != 0x05 {
|
if request.Version != 0x05 {
|
||||||
t.Errorf("Expected version 5, but got %d", request.version)
|
t.Errorf("Expected version 5, but got %d", request.Version)
|
||||||
}
|
}
|
||||||
if request.command != 0x01 {
|
if request.Command != 0x01 {
|
||||||
t.Errorf("Expected command 1, but got %d", request.command)
|
t.Errorf("Expected command 1, but got %d", request.Command)
|
||||||
}
|
}
|
||||||
if request.addrType != 0x01 {
|
if request.AddrType != 0x01 {
|
||||||
t.Errorf("Expected addresstype 1, but got %d", request.addrType)
|
t.Errorf("Expected addresstype 1, but got %d", request.AddrType)
|
||||||
}
|
}
|
||||||
if !bytes.Equal([]byte{0x72, 0x72, 0x72, 0x72}, request.ipv4[:]) {
|
if !bytes.Equal([]byte{0x72, 0x72, 0x72, 0x72}, request.IPv4[:]) {
|
||||||
t.Errorf("Expected IPv4 address 114.114.114.114, but got %v", request.ipv4[:])
|
t.Errorf("Expected IPv4 address 114.114.114.114, but got %v", request.IPv4[:])
|
||||||
}
|
}
|
||||||
if request.port != 53 {
|
if request.Port != 53 {
|
||||||
t.Errorf("Expected port 53, but got %d", request.port)
|
t.Errorf("Expected port 53, but got %d", request.Port)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,15 @@
|
|||||||
package socks
|
package socks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
socksio "github.com/v2ray/v2ray-core/io/socks"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrorAuthenticationFailed = errors.New("None of the authentication methods is allowed.")
|
||||||
|
ErrorCommandNotSupported = errors.New("Client requested an unsupported command.")
|
||||||
)
|
)
|
||||||
|
|
||||||
// SocksServer is a SOCKS 5 proxy server
|
// SocksServer is a SOCKS 5 proxy server
|
||||||
@ -31,5 +39,33 @@ func (server *SocksServer) AcceptConnections(listener net.Listener) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
||||||
|
defer connection.Close()
|
||||||
|
|
||||||
|
auth, err := socksio.ReadAuthentication(connection)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if auth.HasAuthMethod(socksio.AuthNotRequired) {
|
||||||
|
return ErrorAuthenticationFailed
|
||||||
|
}
|
||||||
|
|
||||||
|
authResponse := socksio.NewAuthenticationResponse(socksio.AuthNotRequired)
|
||||||
|
socksio.WriteAuthentication(connection, authResponse)
|
||||||
|
|
||||||
|
request, err := socksio.ReadRequest(connection)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if request.Command == socksio.CmdBind || request.Command == socksio.CmdUdpAssociate {
|
||||||
|
response := socksio.NewSocks5Response()
|
||||||
|
response.Error = socksio.ErrorCommandNotSupported
|
||||||
|
socksio.WriteResponse(connection, response)
|
||||||
|
return ErrorCommandNotSupported
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: establish connection with VNext
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package net
|
package vemss
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
9
vsegment.go
Normal file
9
vsegment.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
// VSegment is a connection between 2 VPoints
|
||||||
|
type VSegment struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewVSegment() *VSegment {
|
||||||
|
return new(VSegment)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user