-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
148 lines (127 loc) · 4.55 KB
/
types.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
package socks
import (
"fmt"
"net"
)
// Header holds a Socks5 header
type Header struct {
Version Version
Methods []byte
}
// Request holds a Socks5 request
type Request struct {
Version Version
Command RequestCmd
Reserved byte
AddressType RequestAddressType
DestinationAddress []byte
DestinationPort uint16
}
func (r Request) GetDestinationString() string {
switch r.AddressType {
case RequestAddressTypeDomainname:
return fmt.Sprintf("%s:%d", r.DestinationAddress, r.DestinationPort)
case RequestAddressTypeIPv4:
ip := net.IP(r.DestinationAddress)
return fmt.Sprintf("%s:%d", ip.String(), r.DestinationPort)
case RequestAddressTypeIPv6:
ip := net.IP(r.DestinationAddress)
return fmt.Sprintf("%s:%d", ip.String(), r.DestinationPort)
default:
return fmt.Sprintf("Address type %d not implemented", r.AddressType)
}
}
// Methods holds the socks5 msethod
type Methods uint8
const (
// MethodNoAuthRequired means the socks proxy requires no auth
MethodNoAuthRequired = 0x00
// MethodGSSAPI means the socks proxy requires authentication with GSSAPI
MethodGSSAPI = 0x01
// MethodUsernamePassword means the socks proxy requires authentication with username and passowrd
MethodUsernamePassword = 0x02
// MethodNoAcceptableMethods means the socks proxy does not implement any of the requested methods
MethodNoAcceptableMethods = 0xff
)
// Version holds the socks5 version
type Version uint8
// Value gets the real value of the Version
func (v Version) Value() uint8 {
return uint8(v)
}
const (
// Version4 represents socks4
Version4 Version = 0x04
// Version5 represents socks5
Version5 Version = 0x05
)
// RequestCmd is the requested socks command
type RequestCmd uint8
const (
// RequestCmdConnect represents the CONNECT command
RequestCmdConnect RequestCmd = 0x01
// RequestCmdBind represents the BIND command
RequestCmdBind RequestCmd = 0x02
// RequestCmdAssociate represents the ASSOCIATE command
RequestCmdAssociate RequestCmd = 0x03
)
// RequestAddressType is the Address Type from the socks communications
type RequestAddressType uint8
const (
// RequestAddressTypeIPv4 represents IPv4
RequestAddressTypeIPv4 RequestAddressType = 0x01
// RequestAddressTypeDomainname represents a domain name
RequestAddressTypeDomainname RequestAddressType = 0x03
// RequestAddressTypeIPv6 represents IPv6
RequestAddressTypeIPv6 RequestAddressType = 0x04
)
// Value gets the real value of the RequestAddressType
func (t RequestAddressType) Value() uint8 {
return uint8(t)
}
// RequestReplyReason is used in replies to the client
type RequestReplyReason uint8
// Value gets the real value of the RequestReplyReason
func (r RequestReplyReason) Value() uint8 {
return uint8(r)
}
const (
// RequestReplySucceeded represents the "succeeded" reply
RequestReplySucceeded RequestReplyReason = 0x00
// RequestReplyGeneralFailure represents the "general SOCKS server failure" reply
RequestReplyGeneralFailure RequestReplyReason = 0x01
// RequestReplyConnectionNotAllowed represents the "connection not allowed by ruleset" reply
RequestReplyConnectionNotAllowed RequestReplyReason = 0x02
// RequestReplyNetworkUnreachable represents the "Network unreachable" reply
RequestReplyNetworkUnreachable RequestReplyReason = 0x03
// RequestReplyHostUnreachable represents the "Host unreachable" reply
RequestReplyHostUnreachable RequestReplyReason = 0x04
// RequestReplyConnectionRefused represents the "Connection refused" reply
RequestReplyConnectionRefused RequestReplyReason = 0x05
// RequestReplyTTLExpired represents the "TTL expired" reply
RequestReplyTTLExpired RequestReplyReason = 0x06
// RequestReplyCommandNotSupported represents the "Command not supported" reply
RequestReplyCommandNotSupported RequestReplyReason = 0x07
// RequestReplyAddressTypeNotSupported represents the "Address type not supported" reply
RequestReplyAddressTypeNotSupported RequestReplyReason = 0x08
// RequestReplyMethodNotSupported represents the "Method not supported" reply
RequestReplyMethodNotSupported RequestReplyReason = 0xff
)
// RequestReply is the struct for the client reply
type RequestReply struct {
Version Version
Reply RequestReplyReason
AddressType RequestAddressType
BindAddress string
BindPort uint16
}
// Error is used to also return a ReplyReason to the client
type Error struct {
Err error
Reason RequestReplyReason
}
// Error returns the underying error string
func (e *Error) Error() string { return e.Err.Error() }
func NewError(reason RequestReplyReason, err error) *Error {
return &Error{Reason: reason, Err: err}
}