prox5/debug.go

235 lines
5.2 KiB
Go
Raw Normal View History

2022-05-23 01:05:50 +00:00
package prox5
import (
2022-06-26 02:11:01 +00:00
"fmt"
"sync"
"sync/atomic"
"git.tcp.direct/kayos/common/pool"
)
var (
debugStatus *uint32
debugHardLock = &sync.RWMutex{}
)
2022-06-26 02:11:01 +00:00
func init() {
dd := debugDisabled
debugStatus = &dd
2022-06-26 02:11:01 +00:00
}
const (
debugEnabled uint32 = iota
debugDisabled
stamp = "[prox5] "
)
type SocksLogger struct {
2022-10-16 10:53:04 +00:00
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)
}
2022-06-26 02:11:01 +00:00
type basicPrinter struct{}
2022-08-31 18:38:44 +00:00
func (b *basicPrinter) Print(str string) {
if useDebugChannel {
2022-09-22 23:56:28 +00:00
debugChan <- str
return
2022-09-22 23:56:28 +00:00
}
buf := strs.Get()
buf.MustWriteString(stamp)
buf.MustWriteString(str)
println(buf.String())
strs.MustPut(buf)
2022-06-26 02:11:01 +00:00
}
2022-08-31 18:38:44 +00:00
func (b *basicPrinter) Printf(format string, items ...any) {
b.Print(fmt.Sprintf(format, items...))
2022-06-26 02:11:01 +00:00
}
2022-10-19 12:36:31 +00:00
func (b *basicPrinter) Errorf(format string, items ...any) {
b.Printf(format, items...)
}
2022-06-26 02:11:01 +00:00
// DebugEnabled returns the current state of our debug switch.
2022-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) DebugEnabled() bool {
debugHardLock.RLock()
defer debugHardLock.RUnlock()
return atomic.CompareAndSwapUint32(debugStatus, debugEnabled, debugEnabled)
}
// EnableDebug enables printing of verbose messages during operation
2022-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) EnableDebug() {
atomic.StoreUint32(debugStatus, debugEnabled)
}
// 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.
2022-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) DisableDebug() {
atomic.StoreUint32(debugStatus, debugDisabled)
}
func simpleString(s string) *pool.String {
buf := strs.Get()
buf.MustWriteString(s)
2022-07-25 06:23:12 +00:00
return buf
}
2022-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) dbgPrint(builder *pool.String) {
defer strs.MustPut(builder)
2022-09-22 23:48:08 +00:00
if !p5.DebugEnabled() {
return
}
2022-09-22 23:48:08 +00:00
p5.DebugLogger.Print(builder.String())
return
}
2022-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) msgUnableToReach(socksString, target string, err error) {
2022-09-22 23:48:08 +00:00
if !p5.DebugEnabled() {
2022-06-26 02:51:42 +00:00
return
}
buf := strs.Get()
buf.MustWriteString("unable to reach ")
2022-10-16 10:53:04 +00:00
if p5.opt.redact {
buf.MustWriteString("[redacted]")
} else {
buf.MustWriteString(target)
}
buf.MustWriteString(" with ")
buf.MustWriteString(socksString)
2022-10-16 10:53:04 +00:00
if !p5.opt.redact {
buf.MustWriteString(": ")
buf.MustWriteString(err.Error())
}
buf.MustWriteString(", cycling...")
2022-09-22 23:48:08 +00:00
p5.dbgPrint(buf)
2022-07-25 07:14:26 +00:00
}
2022-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) msgUsingProxy(socksString string) {
2022-09-22 23:48:08 +00:00
if !p5.DebugEnabled() {
return
}
buf := strs.Get()
2022-10-16 12:56:40 +00:00
if p5.GetDebugRedactStatus() {
socksString = "(redacted)"
}
2022-12-27 12:55:03 +00:00
buf.MustWriteString("mysteryDialer using socks: ")
buf.MustWriteString(socksString)
2022-09-22 23:48:08 +00:00
p5.dbgPrint(buf)
2022-07-25 07:14:26 +00:00
}
2022-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) msgFailedMiddleware(socksString string) {
2022-09-22 23:48:08 +00:00
if !p5.DebugEnabled() {
return
}
buf := strs.Get()
buf.MustWriteString("failed middleware check, ")
buf.MustWriteString(socksString)
buf.MustWriteString(", cycling...")
2022-09-22 23:48:08 +00:00
p5.dbgPrint(buf)
2022-07-25 07:14:26 +00:00
}
2022-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) msgTry(socksString string) {
2022-09-22 23:48:08 +00:00
if !p5.DebugEnabled() {
return
}
2022-10-16 12:56:40 +00:00
if p5.GetDebugRedactStatus() {
socksString = "(redacted)"
}
buf := strs.Get()
buf.MustWriteString("try dial with: ")
buf.MustWriteString(socksString)
2022-09-22 23:48:08 +00:00
p5.dbgPrint(buf)
2022-07-25 07:14:26 +00:00
}
2022-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) msgCantGetLock(socksString string, putback bool) {
2022-09-22 23:48:08 +00:00
if !p5.DebugEnabled() {
return
}
2022-10-16 12:56:40 +00:00
if p5.GetDebugRedactStatus() {
socksString = "(redacted)"
}
buf := strs.Get()
buf.MustWriteString("can't get lock for ")
buf.MustWriteString(socksString)
2022-07-25 07:14:26 +00:00
if putback {
buf.MustWriteString(", putting back in queue")
2022-07-25 07:14:26 +00:00
}
2022-09-22 23:48:08 +00:00
p5.dbgPrint(buf)
2022-07-25 07:14:26 +00:00
}
2022-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) msgGotLock(socksString string) {
2022-09-22 23:48:08 +00:00
if !p5.DebugEnabled() {
return
}
2022-10-16 12:56:40 +00:00
if p5.GetDebugRedactStatus() {
socksString = "(redacted)"
}
buf := strs.Get()
buf.MustWriteString("got lock for ")
buf.MustWriteString(socksString)
2022-09-22 23:48:08 +00:00
p5.dbgPrint(buf)
2022-07-25 07:14:26 +00:00
}
2022-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) msgChecked(sock *Proxy, success bool) {
2022-09-22 23:48:08 +00:00
if !p5.DebugEnabled() {
return
}
2022-10-16 12:56:40 +00:00
pstr := sock.Endpoint
if p5.GetDebugRedactStatus() {
pstr = "(redacted)"
}
buf := strs.Get()
2022-09-22 23:56:28 +00:00
if !success {
buf.MustWriteString("failed to verify: ")
2022-10-16 12:56:40 +00:00
buf.MustWriteString(pstr)
2022-09-22 23:48:08 +00:00
p5.dbgPrint(buf)
2022-07-25 07:14:26 +00:00
return
}
buf.MustWriteString("verified ")
2022-10-16 12:56:40 +00:00
buf.MustWriteString(pstr)
buf.MustWriteString(" as ")
buf.MustWriteString(sock.protocol.Get().String())
buf.MustWriteString(" proxy")
2022-09-22 23:48:08 +00:00
p5.dbgPrint(buf)
2022-07-25 07:14:26 +00:00
}
2022-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) msgBadProxRate(sock *Proxy) {
2022-09-22 23:48:08 +00:00
if !p5.DebugEnabled() {
return
}
2022-10-16 12:56:40 +00:00
sockString := sock.Endpoint
if p5.GetDebugRedactStatus() {
sockString = "(redacted)"
}
buf := strs.Get()
buf.MustWriteString("badProx ratelimited: ")
2022-10-16 12:56:40 +00:00
buf.MustWriteString(sockString)
2022-09-22 23:48:08 +00:00
p5.dbgPrint(buf)
2022-07-25 07:14:26 +00:00
}
2022-09-22 23:56:28 +00:00
// ------------
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.
2022-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) DebugChannel() chan string {
2022-09-22 23:56:28 +00:00
debugChan = make(chan string, 100)
useDebugChannel = true
return debugChan
}