mirror of https://github.com/v2ray/v2ray-core
Unify error checking by introducing error codes
parent
1995594b98
commit
08f85fc9b7
|
@ -4,55 +4,86 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func HasCode(err error, code int) bool {
|
||||||
|
if errWithCode, ok := err.(ErrorWithCode); ok {
|
||||||
|
return errWithCode.Code() == code
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
type ErrorWithCode interface {
|
||||||
|
error
|
||||||
|
Code() int
|
||||||
|
}
|
||||||
|
|
||||||
|
type ErrorCode int
|
||||||
|
|
||||||
|
func (code ErrorCode) Code() int {
|
||||||
|
return int(code)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (code ErrorCode) Prefix() string {
|
||||||
|
return fmt.Sprintf("[Error 0x%04X] ", code.Code())
|
||||||
|
}
|
||||||
|
|
||||||
type AuthenticationError struct {
|
type AuthenticationError struct {
|
||||||
|
ErrorCode
|
||||||
AuthDetail interface{}
|
AuthDetail interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAuthenticationError(detail interface{}) AuthenticationError {
|
func NewAuthenticationError(detail interface{}) AuthenticationError {
|
||||||
return AuthenticationError{
|
return AuthenticationError{
|
||||||
|
ErrorCode: 1,
|
||||||
AuthDetail: detail,
|
AuthDetail: detail,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (err AuthenticationError) Error() string {
|
func (err AuthenticationError) Error() string {
|
||||||
return fmt.Sprintf("[Error 0x0001] Invalid auth %v", err.AuthDetail)
|
return fmt.Sprintf("%sInvalid auth %v", err.Prefix(), err.AuthDetail)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProtocolVersionError struct {
|
type ProtocolVersionError struct {
|
||||||
|
ErrorCode
|
||||||
Version int
|
Version int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewProtocolVersionError(version int) ProtocolVersionError {
|
func NewProtocolVersionError(version int) ProtocolVersionError {
|
||||||
return ProtocolVersionError{
|
return ProtocolVersionError{
|
||||||
Version: version,
|
ErrorCode: 2,
|
||||||
|
Version: version,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (err ProtocolVersionError) Error() string {
|
func (err ProtocolVersionError) Error() string {
|
||||||
return fmt.Sprintf("[Error 0x0002] Invalid version %d", err.Version)
|
return fmt.Sprintf("%sInvalid version %d", err.Prefix(), err.Version)
|
||||||
}
|
}
|
||||||
|
|
||||||
type CorruptedPacketError struct {
|
type CorruptedPacketError struct {
|
||||||
|
ErrorCode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var corruptedPacketErrorInstance = CorruptedPacketError{ErrorCode: 3}
|
||||||
|
|
||||||
func NewCorruptedPacketError() CorruptedPacketError {
|
func NewCorruptedPacketError() CorruptedPacketError {
|
||||||
return CorruptedPacketError{}
|
return corruptedPacketErrorInstance
|
||||||
}
|
}
|
||||||
|
|
||||||
func (err CorruptedPacketError) Error() string {
|
func (err CorruptedPacketError) Error() string {
|
||||||
return "[Error 0x0003] Corrupted packet."
|
return err.Prefix() + "Corrupted packet."
|
||||||
}
|
}
|
||||||
|
|
||||||
type IPFormatError struct {
|
type IPFormatError struct {
|
||||||
|
ErrorCode
|
||||||
IP []byte
|
IP []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIPFormatError(ip []byte) IPFormatError {
|
func NewIPFormatError(ip []byte) IPFormatError {
|
||||||
return IPFormatError{
|
return IPFormatError{
|
||||||
IP: ip,
|
ErrorCode: 4,
|
||||||
|
IP: ip,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (err IPFormatError) Error() string {
|
func (err IPFormatError) Error() string {
|
||||||
return fmt.Sprintf("[Error 0x0004] Invalid IP %v", err.IP)
|
return fmt.Sprintf("%sInvalid IP %v", err.Prefix(), err.IP)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package errors_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/v2ray/v2ray-core/common/errors"
|
||||||
|
"github.com/v2ray/v2ray-core/testing/unit"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MockError struct {
|
||||||
|
errors.ErrorCode
|
||||||
|
}
|
||||||
|
|
||||||
|
func (err MockError) Error() string {
|
||||||
|
return "This is a fake error."
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHasCode(t *testing.T) {
|
||||||
|
assert := unit.Assert(t)
|
||||||
|
|
||||||
|
err := MockError{ErrorCode: 101}
|
||||||
|
assert.Error(err).HasCode(101)
|
||||||
|
}
|
|
@ -2,10 +2,9 @@ package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"github.com/v2ray/v2ray-core/common/errors"
|
||||||
"github.com/v2ray/v2ray-core/common/log"
|
"github.com/v2ray/v2ray-core/common/log"
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
)
|
)
|
||||||
|
@ -23,10 +22,6 @@ const (
|
||||||
Socks4RequestRejected = byte(91)
|
Socks4RequestRejected = byte(91)
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
ErrorSocksVersion4 = errors.New("Using SOCKS version 4.")
|
|
||||||
)
|
|
||||||
|
|
||||||
// Authentication request header of Socks5 protocol
|
// Authentication request header of Socks5 protocol
|
||||||
type Socks5AuthenticationRequest struct {
|
type Socks5AuthenticationRequest struct {
|
||||||
version byte
|
version byte
|
||||||
|
@ -34,13 +29,6 @@ type Socks5AuthenticationRequest struct {
|
||||||
authMethods [256]byte
|
authMethods [256]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
type Socks4AuthenticationRequest struct {
|
|
||||||
Version byte
|
|
||||||
Command byte
|
|
||||||
Port uint16
|
|
||||||
IP [4]byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (request *Socks5AuthenticationRequest) HasAuthMethod(method byte) bool {
|
func (request *Socks5AuthenticationRequest) HasAuthMethod(method byte) bool {
|
||||||
for i := 0; i < int(request.nMethods); i++ {
|
for i := 0; i < int(request.nMethods); i++ {
|
||||||
if request.authMethods[i] == method {
|
if request.authMethods[i] == method {
|
||||||
|
@ -54,11 +42,11 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut
|
||||||
buffer := make([]byte, 256)
|
buffer := make([]byte, 256)
|
||||||
nBytes, err := reader.Read(buffer)
|
nBytes, err := reader.Read(buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to read socks authentication: %v", err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if nBytes < 2 {
|
if nBytes < 2 {
|
||||||
err = fmt.Errorf("Expected 2 bytes read, but actaully %d bytes read", nBytes)
|
log.Info("Socks expected 2 bytes read, but only %d bytes read", nBytes)
|
||||||
|
err = errors.NewCorruptedPacketError()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,24 +55,26 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut
|
||||||
auth4.Command = buffer[1]
|
auth4.Command = buffer[1]
|
||||||
auth4.Port = binary.BigEndian.Uint16(buffer[2:4])
|
auth4.Port = binary.BigEndian.Uint16(buffer[2:4])
|
||||||
copy(auth4.IP[:], buffer[4:8])
|
copy(auth4.IP[:], buffer[4:8])
|
||||||
err = ErrorSocksVersion4
|
err = NewSocksVersion4Error()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
auth.version = buffer[0]
|
auth.version = buffer[0]
|
||||||
if auth.version != socksVersion {
|
if auth.version != socksVersion {
|
||||||
err = fmt.Errorf("Unknown SOCKS version %d", auth.version)
|
err = errors.NewProtocolVersionError(int(auth.version))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
auth.nMethods = buffer[1]
|
auth.nMethods = buffer[1]
|
||||||
if auth.nMethods <= 0 {
|
if auth.nMethods <= 0 {
|
||||||
err = fmt.Errorf("Zero length of authentication methods")
|
log.Info("Zero length of authentication methods")
|
||||||
|
err = errors.NewCorruptedPacketError()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if nBytes-2 != int(auth.nMethods) {
|
if nBytes-2 != int(auth.nMethods) {
|
||||||
err = fmt.Errorf("Unmatching number of auth methods, expecting %d, but got %d", auth.nMethods, nBytes)
|
log.Info("Unmatching number of auth methods, expecting %d, but got %d", auth.nMethods, nBytes)
|
||||||
|
err = errors.NewCorruptedPacketError()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
copy(auth.authMethods[:], buffer[2:nBytes])
|
copy(auth.authMethods[:], buffer[2:nBytes])
|
||||||
|
@ -207,14 +197,13 @@ type Socks5Request struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
||||||
|
buffer := make([]byte, 256)
|
||||||
buffer := make([]byte, 4)
|
nBytes, err := reader.Read(buffer[:4])
|
||||||
nBytes, err := reader.Read(buffer)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if nBytes < len(buffer) {
|
if nBytes < 4 {
|
||||||
err = fmt.Errorf("Unable to read request.")
|
err = errors.NewCorruptedPacketError()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
request = &Socks5Request{
|
request = &Socks5Request{
|
||||||
|
@ -230,11 +219,10 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if nBytes != 4 {
|
if nBytes != 4 {
|
||||||
err = fmt.Errorf("Unable to read IPv4 address.")
|
err = errors.NewCorruptedPacketError()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case AddrTypeDomain:
|
case AddrTypeDomain:
|
||||||
buffer = make([]byte, 256)
|
|
||||||
nBytes, err = reader.Read(buffer[0:1])
|
nBytes, err = reader.Read(buffer[0:1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
@ -246,7 +234,8 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if nBytes != int(domainLength) {
|
if nBytes != int(domainLength) {
|
||||||
err = fmt.Errorf("Unable to read domain with %d bytes, expecting %d bytes", nBytes, domainLength)
|
log.Info("Unable to read domain with %d bytes, expecting %d bytes", nBytes, domainLength)
|
||||||
|
err = errors.NewCorruptedPacketError()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
request.Domain = string(buffer[:domainLength])
|
request.Domain = string(buffer[:domainLength])
|
||||||
|
@ -256,21 +245,21 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if nBytes != 16 {
|
if nBytes != 16 {
|
||||||
err = fmt.Errorf("Unable to read IPv4 address.")
|
err = errors.NewCorruptedPacketError()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("Unexpected address type %d", request.AddrType)
|
log.Info("Unexpected address type %d", request.AddrType)
|
||||||
|
err = errors.NewCorruptedPacketError()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer = make([]byte, 2)
|
nBytes, err = reader.Read(buffer[:2])
|
||||||
nBytes, err = reader.Read(buffer)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if nBytes != 2 {
|
if nBytes != 2 {
|
||||||
err = fmt.Errorf("Unable to read port.")
|
err = errors.NewCorruptedPacketError()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -351,9 +340,7 @@ func (r *Socks5Response) toBytes() []byte {
|
||||||
case 0x04:
|
case 0x04:
|
||||||
buffer = append(buffer, r.IPv6[:]...)
|
buffer = append(buffer, r.IPv6[:]...)
|
||||||
}
|
}
|
||||||
portBuffer := make([]byte, 2)
|
buffer = append(buffer, byte(r.Port>>8), byte(r.Port))
|
||||||
binary.BigEndian.PutUint16(portBuffer, r.Port)
|
|
||||||
buffer = append(buffer, portBuffer...)
|
|
||||||
return buffer
|
return buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package protocol
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "fmt"
|
||||||
|
|
||||||
|
"github.com/v2ray/v2ray-core/common/errors"
|
||||||
|
_ "github.com/v2ray/v2ray-core/common/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
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."
|
||||||
|
}
|
||||||
|
|
||||||
|
type Socks4AuthenticationRequest struct {
|
||||||
|
Version byte
|
||||||
|
Command byte
|
||||||
|
Port uint16
|
||||||
|
IP [4]byte
|
||||||
|
}
|
|
@ -47,7 +47,7 @@ func TestAuthentication4RequestRead(t *testing.T) {
|
||||||
0x72, 0x72, 0x72, 0x72,
|
0x72, 0x72, 0x72, 0x72,
|
||||||
}
|
}
|
||||||
_, request4, err := ReadAuthentication(bytes.NewReader(rawRequest))
|
_, request4, err := ReadAuthentication(bytes.NewReader(rawRequest))
|
||||||
assert.Error(err).Equals(ErrorSocksVersion4)
|
assert.Error(err).HasCode(1000)
|
||||||
assert.Byte(request4.Version).Named("Version").Equals(0x04)
|
assert.Byte(request4.Version).Named("Version").Equals(0x04)
|
||||||
assert.Byte(request4.Command).Named("Command").Equals(0x01)
|
assert.Byte(request4.Command).Named("Command").Equals(0x01)
|
||||||
assert.Uint16(request4.Port).Named("Port").Equals(53)
|
assert.Uint16(request4.Port).Named("Port").Equals(53)
|
||||||
|
|
|
@ -2,22 +2,23 @@ package socks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_ "bufio"
|
_ "bufio"
|
||||||
"errors"
|
e2 "errors"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core"
|
"github.com/v2ray/v2ray-core"
|
||||||
|
"github.com/v2ray/v2ray-core/common/errors"
|
||||||
"github.com/v2ray/v2ray-core/common/log"
|
"github.com/v2ray/v2ray-core/common/log"
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
protocol "github.com/v2ray/v2ray-core/proxy/socks/protocol"
|
"github.com/v2ray/v2ray-core/proxy/socks/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrorAuthenticationFailed = errors.New("None of the authentication methods is allowed.")
|
ErrorAuthenticationFailed = e2.New("None of the authentication methods is allowed.")
|
||||||
ErrorCommandNotSupported = errors.New("Client requested an unsupported command.")
|
ErrorCommandNotSupported = e2.New("Client requested an unsupported command.")
|
||||||
ErrorInvalidUser = errors.New("Invalid username or password.")
|
ErrorInvalidUser = e2.New("Invalid username or password.")
|
||||||
)
|
)
|
||||||
|
|
||||||
// SocksServer is a SOCKS 5 proxy server
|
// SocksServer is a SOCKS 5 proxy server
|
||||||
|
@ -66,7 +67,7 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
||||||
reader := v2net.NewTimeOutReader(4, connection)
|
reader := v2net.NewTimeOutReader(4, connection)
|
||||||
|
|
||||||
auth, auth4, err := protocol.ReadAuthentication(reader)
|
auth, auth4, err := protocol.ReadAuthentication(reader)
|
||||||
if err != nil && err != protocol.ErrorSocksVersion4 {
|
if err != nil && !errors.HasCode(err, 1000) {
|
||||||
log.Error("Error on reading authentication: %v", err)
|
log.Error("Error on reading authentication: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -74,7 +75,7 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
||||||
var dest v2net.Destination
|
var dest v2net.Destination
|
||||||
|
|
||||||
// TODO refactor this part
|
// TODO refactor this part
|
||||||
if err == protocol.ErrorSocksVersion4 {
|
if errors.HasCode(err, 1000) {
|
||||||
result := protocol.Socks4RequestGranted
|
result := protocol.Socks4RequestGranted
|
||||||
if auth4.Command == protocol.CmdBind {
|
if auth4.Command == protocol.CmdBind {
|
||||||
result = protocol.Socks4RequestRejected
|
result = protocol.Socks4RequestRejected
|
||||||
|
|
|
@ -2,7 +2,8 @@ package unit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
"github.com/v2ray/v2ray-core/common/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ErrorSubject struct {
|
type ErrorSubject struct {
|
||||||
|
@ -43,8 +44,7 @@ func (subject *ErrorSubject) IsNil() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (subject *ErrorSubject) HasCode(code int) {
|
func (subject *ErrorSubject) HasCode(code int) {
|
||||||
errorPrefix := fmt.Sprintf("[Error 0x%04X]", code)
|
if !errors.HasCode(subject.value, code) {
|
||||||
if !strings.Contains(subject.value.Error(), errorPrefix) {
|
|
||||||
subject.FailWithMessage(fmt.Sprintf("Not ture that %s has error code 0x%04X.", subject.DisplayString(), code))
|
subject.FailWithMessage(fmt.Sprintf("Not ture that %s has error code 0x%04X.", subject.DisplayString(), code))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue