1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2026-02-19 06:35:19 -05:00

Remove error defination

This commit is contained in:
V2Ray
2015-10-13 13:55:06 +02:00
parent 93625dd656
commit 1d40220d31
13 changed files with 74 additions and 214 deletions

10
proxy/errors.go Normal file
View File

@@ -0,0 +1,10 @@
package proxy
import (
"errors"
)
var (
InvalidAuthentication = errors.New("Invalid authentication.")
InvalidProtocolVersion = errors.New("Invalid protocol version.")
)

View File

@@ -5,9 +5,10 @@ import (
"io"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/errors"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/transport"
)
const (
@@ -49,7 +50,7 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut
}
if nBytes < 2 {
log.Info("Socks expected 2 bytes read, but only %d bytes read", nBytes)
err = errors.NewCorruptedPacketError()
err = transport.CorruptedPacket
return
}
@@ -58,26 +59,27 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut
auth4.Command = buffer.Value[1]
auth4.Port = binary.BigEndian.Uint16(buffer.Value[2:4])
copy(auth4.IP[:], buffer.Value[4:8])
err = NewSocksVersion4Error()
err = Socks4Downgrade
return
}
auth.version = buffer.Value[0]
if auth.version != socksVersion {
err = errors.NewProtocolVersionError(int(auth.version))
log.Warning("Unknown protocol version %d", auth.version)
err = proxy.InvalidProtocolVersion
return
}
auth.nMethods = buffer.Value[1]
if auth.nMethods <= 0 {
log.Info("Zero length of authentication methods")
err = errors.NewCorruptedPacketError()
err = transport.CorruptedPacket
return
}
if nBytes-2 != int(auth.nMethods) {
log.Info("Unmatching number of auth methods, expecting %d, but got %d", auth.nMethods, nBytes)
err = errors.NewCorruptedPacketError()
err = transport.CorruptedPacket
return
}
copy(auth.authMethods[:], buffer.Value[2:nBytes])
@@ -194,7 +196,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
return
}
if nBytes < 4 {
err = errors.NewCorruptedPacketError()
err = transport.CorruptedPacket
return
}
request = &Socks5Request{
@@ -210,7 +212,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
return
}
if nBytes != 4 {
err = errors.NewCorruptedPacketError()
err = transport.CorruptedPacket
return
}
case AddrTypeDomain:
@@ -226,7 +228,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
if nBytes != int(domainLength) {
log.Info("Unable to read domain with %d bytes, expecting %d bytes", nBytes, domainLength)
err = errors.NewCorruptedPacketError()
err = transport.CorruptedPacket
return
}
request.Domain = string(buffer.Value[:domainLength])
@@ -236,12 +238,12 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
return
}
if nBytes != 16 {
err = errors.NewCorruptedPacketError()
err = transport.CorruptedPacket
return
}
default:
log.Info("Unexpected address type %d", request.AddrType)
err = errors.NewCorruptedPacketError()
err = transport.CorruptedPacket
return
}
@@ -250,7 +252,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
return
}
if nBytes != 2 {
err = errors.NewCorruptedPacketError()
err = transport.CorruptedPacket
return
}

View File

@@ -1,23 +1,14 @@
package protocol
import (
"errors"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/errors"
)
type SocksVersion4Error struct {
errors.ErrorCode
}
var socksVersion4ErrorInstance = SocksVersion4Error{ErrorCode: 1000}
func NewSocksVersion4Error() SocksVersion4Error {
return socksVersion4ErrorInstance
}
func (err SocksVersion4Error) Error() string {
return err.Prefix() + "Request is socks version 4."
}
var (
Socks4Downgrade = errors.New("Downgraded to Socks 4.")
)
type Socks4AuthenticationRequest struct {
Version byte

View File

@@ -18,7 +18,7 @@ func TestSocks4AuthenticationRequestRead(t *testing.T) {
0x72, 0x72, 0x72, 0x72,
}
_, request4, err := ReadAuthentication(bytes.NewReader(rawRequest))
assert.Error(err).HasCode(1000)
assert.Error(err).Equals(Socks4Downgrade)
assert.Byte(request4.Version).Named("Version").Equals(0x04)
assert.Byte(request4.Command).Named("Command").Equals(0x01)
assert.Uint16(request4.Port).Named("Port").Equals(53)

View File

@@ -1,6 +1,7 @@
package socks
import (
"errors"
"io"
"net"
"strconv"
@@ -9,13 +10,18 @@ import (
"github.com/v2ray/v2ray-core"
"github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/errors"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy"
jsonconfig "github.com/v2ray/v2ray-core/proxy/socks/config/json"
"github.com/v2ray/v2ray-core/proxy/socks/protocol"
)
var (
UnsupportedSocksCommand = errors.New("Unsupported socks command.")
UnsupportedAuthMethod = errors.New("Unsupported auth method.")
)
// SocksServer is a SOCKS 5 proxy server
type SocksServer struct {
accepting bool
@@ -61,12 +67,12 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
reader := v2net.NewTimeOutReader(120, connection)
auth, auth4, err := protocol.ReadAuthentication(reader)
if err != nil && !errors.HasCode(err, 1000) {
if err != nil && err != protocol.Socks4Downgrade {
log.Error("Socks failed to read authentication: %v", err)
return err
}
if err != nil && errors.HasCode(err, 1000) {
if err != nil && err == protocol.Socks4Downgrade {
return server.handleSocks4(reader, connection, auth4)
} else {
return server.handleSocks5(reader, connection, auth)
@@ -87,7 +93,7 @@ func (server *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.W
return err
}
log.Warning("Socks client doesn't support allowed any auth methods.")
return errors.NewInvalidOperationError("Unsupported auth methods.")
return UnsupportedAuthMethod
}
authResponse := protocol.NewAuthenticationResponse(expectedAuthMethod)
@@ -113,9 +119,8 @@ func (server *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.W
return err
}
if status != byte(0) {
err = errors.NewAuthenticationError(upRequest.AuthDetail())
log.Warning(err.Error())
return err
log.Warning("Invalid user account: %s", upRequest.AuthDetail())
return proxy.InvalidAuthentication
}
}
@@ -143,7 +148,7 @@ func (server *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.W
return err
}
log.Warning("Unsupported socks command %d", request.Command)
return errors.NewInvalidOperationError("Socks command " + strconv.Itoa(int(request.Command)))
return UnsupportedSocksCommand
}
response.Error = protocol.ErrorSuccess
@@ -228,7 +233,8 @@ func (server *SocksServer) handleSocks4(reader io.Reader, writer io.Writer, auth
responseBuffer.Release()
if result == protocol.Socks4RequestRejected {
return errors.NewInvalidOperationError("Socks4 command " + strconv.Itoa(int(auth.Command)))
log.Warning("Unsupported socks 4 command %d", auth.Command)
return UnsupportedSocksCommand
}
dest := v2net.NewTCPDestination(v2net.IPAddress(auth.IP[:], auth.Port))

View File

@@ -9,10 +9,12 @@ import (
"io"
"time"
"github.com/v2ray/v2ray-core/common/errors"
v2io "github.com/v2ray/v2ray-core/common/io"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
"github.com/v2ray/v2ray-core/transport"
)
const (
@@ -73,7 +75,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
userId, timeSec, valid := r.vUserSet.GetUser(buffer[:nBytes])
if !valid {
return nil, errors.NewAuthenticationError(buffer[:nBytes])
return nil, proxy.InvalidAuthentication
}
aesCipher, err := aes.NewCipher(userId.CmdKey())
@@ -99,7 +101,8 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
}
if request.Version != Version {
return nil, errors.NewProtocolVersionError(int(request.Version))
log.Warning("Invalid protocol version %d", request.Version)
return nil, proxy.InvalidProtocolVersion
}
request.RequestIV = buffer[1:17] // 16 bytes
@@ -149,7 +152,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
expectedHash := binary.BigEndian.Uint32(buffer[bufferLen : bufferLen+4])
if actualHash != expectedHash {
return nil, errors.NewCorruptedPacketError()
return nil, transport.CorruptedPacket
}
return request, nil