-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
199 lines (170 loc) · 5.1 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package socksy5
import (
"errors"
"fmt"
"io"
"net"
)
// ErrMalformed represents protocol format violation.
// Usually a more specific error type is used:
// [VerIncorrectError], [RsvViolationError], [CmdNotSupportedError],
// [ATYPNotSupportedError] and [UsrPwdVerIncorrectErr].
var ErrMalformed = errors.New("malformed")
type VerIncorrectError byte
func (e VerIncorrectError) Error() string {
return fmt.Sprintf("VER incorrect (0x%02X)", byte(e))
}
// Is returns true if target is [ErrMalformed].
func (e VerIncorrectError) Is(target error) bool {
return target == ErrMalformed
}
type RsvViolationError byte
func (e RsvViolationError) Error() string {
return fmt.Sprintf("RSV violation (0x%02X)", byte(e))
}
// Is returns true if target is [ErrMalformed].
func (e RsvViolationError) Is(target error) bool {
return target == ErrMalformed
}
type CmdNotSupportedError byte
func (e CmdNotSupportedError) Error() string {
return fmt.Sprintf("CMD 0x%02X not supported", byte(e))
}
// Is returns true if target is [ErrMalformed].
func (e CmdNotSupportedError) Is(target error) bool {
return target == ErrMalformed
}
// Unwrap returns [errors.ErrUnsupported].
func (e CmdNotSupportedError) Unwrap() error {
return errors.ErrUnsupported
}
type ATYPNotSupportedError byte
func (e ATYPNotSupportedError) Error() string {
return fmt.Sprintf("ATYP 0x%02X not supported", byte(e))
}
// Is returns true if target is [ErrMalformed].
func (e ATYPNotSupportedError) Is(target error) bool {
return target == ErrMalformed
}
// Unwrap returns [errors.ErrUnsupported].
func (e ATYPNotSupportedError) Unwrap() error {
return errors.ErrUnsupported
}
// ErrAcceptOrDenyFailed is used by [Connect], [Binder] and [Associator].
// It indicates that the accept and deny methods of the request returned not ok.
var ErrAcceptOrDenyFailed = errors.New("request already handled")
// ErrDuplicatedRequest is returned by [Associator.Handle] and [Binder.Handle]
// indicating that another request with same parameters is being handled,
// e.g. the [Binder] is already listening the address stated by the BIND request.
var ErrDuplicatedRequest = errors.New("duplicated request with same parameters")
// An OpError contains Op string describing in which operation has the error occured.
// Currently the error util of socksy5 is not well designed, so OpError is for now
// just for the convenience of converting errors to strings.
type OpError struct {
Op string // E.g. "read handshake", "serve", "reply".
LocalAddr net.Addr
RemoteAddr net.Addr
Err error // Inner error
}
func newOpErr(op string, addrSrc any, err error) *OpError {
e := &OpError{
Op: op,
Err: err,
}
switch addrSrc := addrSrc.(type) {
case net.Conn:
e.LocalAddr = addrSrc.LocalAddr()
e.RemoteAddr = addrSrc.RemoteAddr()
case net.Listener:
e.LocalAddr = addrSrc.Addr()
e.RemoteAddr = nil
default:
e.LocalAddr = nil
e.RemoteAddr = nil
}
return e
}
func (e *OpError) Error() string {
// Yeah this is mostly copy-pasted from net.go, thanks Google!
if e == nil {
return "<nil>"
}
s := e.Op
// Skip addr if inner err is net.OpError,
// because net.OpError usually already contains addr info
if _, ok := e.Err.(*net.OpError); !ok {
if e.LocalAddr != nil {
s += " " + e.LocalAddr.String()
}
if e.RemoteAddr != nil {
if e.LocalAddr != nil {
s += "->"
} else {
s += " "
}
s += e.RemoteAddr.String()
}
}
if e.Err != nil {
s += ": " + e.Err.Error()
}
return s
}
func (e *OpError) Unwrap() error {
if e == nil {
return nil
}
return e.Err
}
// A RequestNotHandledError can be received from the error channel when a handshake
// or request is not handled by external code.
type RequestNotHandledError struct {
Type string // One of "handshake", "CONNECT", "BIND", "UDP ASSOCIATE"
Timeout bool // If the request is not handled in a duration of PeriodAutoDeny
}
func (e *RequestNotHandledError) Error() string {
if e == nil {
return "<nil>"
}
var reason string
if e.Timeout {
reason = "timeout"
} else {
reason = "not sent"
}
return fmt.Sprintf("%s request not handled (%s)", e.Type, reason)
}
// A RelayError represents errors and address info of TCP traffic relaying
// for CONNECT and BIND requests.
type RelayError struct {
ClientRemoteAddr net.Addr
ClientLocalAddr net.Addr
HostRemoteAddr net.Addr
HostLocalAddr net.Addr
Client2HostErr error
Host2ClientErr error
}
func newRelayErr(clientConn, hostConn net.Conn, chErr, hcErr error) *RelayError {
if errors.Is(chErr, io.EOF) && errors.Is(hcErr, io.EOF) {
return nil
}
return &RelayError{
ClientRemoteAddr: clientConn.RemoteAddr(),
ClientLocalAddr: clientConn.LocalAddr(),
HostRemoteAddr: hostConn.RemoteAddr(),
HostLocalAddr: hostConn.LocalAddr(),
Client2HostErr: chErr,
Host2ClientErr: hcErr,
}
}
func (e *RelayError) Error() string {
return fmt.Sprintf(
"%s, client to host: %s, host to client: %s",
relayAddr2str(e.ClientRemoteAddr, e.ClientLocalAddr, e.HostLocalAddr, e.HostRemoteAddr),
e.Client2HostErr, e.Host2ClientErr,
)
}
func (e *RelayError) Unwrap() (errs []error) {
errs = []error{e.Client2HostErr, e.Host2ClientErr}
return
}