-
Notifications
You must be signed in to change notification settings - Fork 6
/
debug.go
242 lines (214 loc) · 5.37 KB
/
debug.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package prox5
import (
"fmt"
"sync"
"sync/atomic"
"git.tcp.direct/kayos/common/pool"
)
var (
debugStatus *uint32
debugHardLock = &sync.RWMutex{}
)
func init() {
dd := debugDisabled
debugStatus = &dd
}
const (
debugEnabled uint32 = iota
debugDisabled
stamp = "[prox5] "
)
type SocksLogger struct {
parent *ProxyEngine
}
// Printf is used to handle socks server logging.
func (s SocksLogger) Printf(format string, a ...interface{}) {
buf := strs.Get()
buf.MustWriteString(fmt.Sprintf(format, a...))
s.parent.dbgPrint(buf)
}
type basicPrinter struct{}
func (b *basicPrinter) Print(a ...any) {
if len(a) == 0 {
return
}
str := fmt.Sprint(a...)
if useDebugChannel {
debugChan <- str
return
}
buf := strs.Get()
buf.MustWriteString(stamp)
buf.MustWriteString(str)
strs.MustPut(buf)
}
func (b *basicPrinter) Printf(format string, items ...any) {
b.Print(fmt.Sprintf(format, items...))
}
func (b *basicPrinter) Errorf(format string, items ...any) {
b.Printf(format, items...)
}
// DebugEnabled returns the current state of our debug switch.
func (p5 *ProxyEngine) DebugEnabled() bool {
debugHardLock.RLock()
defer debugHardLock.RUnlock()
return atomic.CompareAndSwapUint32(debugStatus, debugEnabled, debugEnabled)
}
// EnableDebug enables printing of verbose messages during operation
func (p5 *ProxyEngine) EnableDebug() {
atomic.StoreUint32(debugStatus, debugEnabled)
p5.dbgPrint(simpleString("prox5 debug enabled"))
}
// DisableDebug enables printing of verbose messages during operation.
// WARNING: if you are using a DebugChannel, you must read all of the messages in the channel's cache or this will block.
func (p5 *ProxyEngine) DisableDebug() {
atomic.StoreUint32(debugStatus, debugDisabled)
}
func simpleString(s string) *pool.String {
buf := strs.Get()
buf.MustWriteString(s)
return buf
}
func (p5 *ProxyEngine) dbgPrint(builder *pool.String) {
defer strs.MustPut(builder)
if !p5.DebugEnabled() {
return
}
p5.DebugLogger.Printf(builder.String())
return
}
func (p5 *ProxyEngine) msgUnableToReach(socksString, target string, err error) {
if !p5.DebugEnabled() {
return
}
buf := strs.Get()
buf.MustWriteString("unable to reach ")
if p5.opt.redact {
buf.MustWriteString("[redacted]")
} else {
buf.MustWriteString(target)
}
buf.MustWriteString(" with ")
buf.MustWriteString(socksString)
if !p5.opt.redact {
buf.MustWriteString(": ")
buf.MustWriteString(err.Error())
}
buf.MustWriteString(", cycling...")
p5.dbgPrint(buf)
}
func (p5 *ProxyEngine) msgUsingProxy(socksString string) {
if !p5.DebugEnabled() {
return
}
buf := strs.Get()
if p5.GetDebugRedactStatus() {
socksString = "(redacted)"
}
buf.MustWriteString("mysteryDialer using socks: ")
buf.MustWriteString(socksString)
p5.dbgPrint(buf)
}
func (p5 *ProxyEngine) msgFailedMiddleware(socksString string) {
if !p5.DebugEnabled() {
return
}
buf := strs.Get()
buf.MustWriteString("failed middleware check, ")
buf.MustWriteString(socksString)
buf.MustWriteString(", cycling...")
p5.dbgPrint(buf)
}
func (p5 *ProxyEngine) msgTry(socksString string) {
if !p5.DebugEnabled() {
return
}
if p5.GetDebugRedactStatus() {
socksString = "(redacted)"
}
buf := strs.Get()
buf.MustWriteString("try dial with: ")
buf.MustWriteString(socksString)
p5.dbgPrint(buf)
}
func (p5 *ProxyEngine) msgCantGetLock(socksString string, putback bool) {
if !p5.DebugEnabled() {
return
}
if p5.GetDebugRedactStatus() {
socksString = "(redacted)"
}
buf := strs.Get()
buf.MustWriteString("can't get lock for ")
buf.MustWriteString(socksString)
if putback {
buf.MustWriteString(", putting back in queue")
}
p5.dbgPrint(buf)
}
func (p5 *ProxyEngine) msgGotLock(socksString string) {
if !p5.DebugEnabled() {
return
}
if p5.GetDebugRedactStatus() {
socksString = "(redacted)"
}
buf := strs.Get()
buf.MustWriteString("got lock for ")
buf.MustWriteString(socksString)
p5.dbgPrint(buf)
}
func (p5 *ProxyEngine) msgChecked(sock *Proxy, success bool) {
if !p5.DebugEnabled() {
return
}
pstr := sock.Endpoint
if p5.GetDebugRedactStatus() {
pstr = "(redacted)"
}
buf := strs.Get()
if !success {
buf.MustWriteString("failed to verify: ")
buf.MustWriteString(pstr)
p5.dbgPrint(buf)
return
}
buf.MustWriteString("verified ")
buf.MustWriteString(pstr)
buf.MustWriteString(" as ")
buf.MustWriteString(sock.protocol.Get().String())
buf.MustWriteString(" proxy")
p5.dbgPrint(buf)
}
func (p5 *ProxyEngine) msgBadProxRate(sock *Proxy) {
if !p5.DebugEnabled() {
return
}
if p5.lastBadProxAnnnounced.Load().(string) == sock.Endpoint {
return
}
sockString := sock.Endpoint
if p5.GetDebugRedactStatus() {
sockString = "(redacted)"
}
buf := strs.Get()
buf.MustWriteString("badProx ratelimited: ")
buf.MustWriteString(sockString)
p5.dbgPrint(buf)
p5.lastBadProxAnnnounced.Store(sock.Endpoint)
}
// ------------
var (
debugChan chan string
useDebugChannel bool
)
// DebugChannel will return a channel which will receive debug messages once debug is enabled.
// This will alter the flow of debug messages, they will no longer print to console, they will be pushed into this channel.
// Make sure you pull from the channel eventually to avoid build up of blocked goroutines.
//
// Deprecated: use DebugLogger instead. This will be removed in a future version.
func (p5 *ProxyEngine) DebugChannel() chan string {
debugChan = make(chan string, 100)
useDebugChannel = true
return debugChan
}