prox5/debug.go

162 lines
4.1 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"
2022-07-25 06:23:12 +00:00
"strings"
"sync"
)
2022-06-26 02:11:01 +00:00
func init() {
debugMutex = &sync.RWMutex{}
}
var (
useDebugChannel = false
debugChan chan string
debugMutex *sync.RWMutex
)
2022-06-26 02:11:01 +00:00
type DebugPrinter interface {
Print(str string)
Printf(format string, items ...any)
}
2022-06-26 02:11:01 +00:00
type basicPrinter struct{}
func (b basicPrinter) Print(str string) {
2022-06-28 02:27:52 +00:00
println("[prox5] " + str)
2022-06-26 02:11:01 +00:00
}
func (b basicPrinter) Printf(format string, items ...any) {
println(fmt.Sprintf("prox5: "+format, items))
}
// 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.
2022-06-26 02:51:42 +00:00
func (pe *ProxyEngine) DebugChannel() chan string {
2022-06-26 02:11:01 +00:00
debugChan = make(chan string, 1000000)
useDebugChannel = true
return debugChan
}
2022-06-26 02:11:01 +00:00
// DebugEnabled returns the current state of our debug switch.
2022-06-26 02:51:42 +00:00
func (pe *ProxyEngine) DebugEnabled() bool {
return pe.swampopt.debug
}
// DisableDebugChannel redirects debug messages back to the console.
// DisableProxyChannel does not disable debug, use DisableDebug().
2022-06-26 02:51:42 +00:00
func (pe *ProxyEngine) DisableDebugChannel() {
2021-09-18 11:11:09 +00:00
debugMutex.Lock()
defer debugMutex.Unlock()
useDebugChannel = false
}
// EnableDebug enables printing of verbose messages during operation
2022-06-26 02:51:42 +00:00
func (pe *ProxyEngine) EnableDebug() {
pe.swampopt.debug = true
}
// 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-06-26 02:51:42 +00:00
func (pe *ProxyEngine) DisableDebug() {
pe.swampopt.debug = false
}
2022-07-25 06:23:12 +00:00
func simpleString(s string) *strings.Builder {
buf := copABuffer.Get().(*strings.Builder)
buf.WriteString(s)
return buf
}
2022-07-25 06:23:12 +00:00
func (pe *ProxyEngine) dbgPrint(builder *strings.Builder) {
defer discardBuffer(builder)
2022-06-26 02:51:42 +00:00
if !pe.swampopt.debug {
return
}
2022-06-26 02:51:42 +00:00
if !useDebugChannel {
2022-07-25 06:23:12 +00:00
pe.Debug.Print(builder.String())
2022-06-26 02:51:42 +00:00
return
}
select {
2022-07-25 06:23:12 +00:00
case debugChan <- builder.String():
2022-06-26 02:51:42 +00:00
return
default:
2022-07-25 06:23:12 +00:00
buf := copABuffer.Get().(*strings.Builder)
buf.WriteString("overflow: ")
buf.WriteString(builder.String())
pe.Debug.Print(buf.String())
discardBuffer(buf)
}
}
2022-07-25 07:14:26 +00:00
func (pe *ProxyEngine) msgUnableToReach(socksString string) {
buf := copABuffer.Get().(*strings.Builder)
buf.WriteString("unable to reach [redacted] with ")
buf.WriteString(socksString)
buf.WriteString(", cycling...")
pe.dbgPrint(buf)
}
func (pe *ProxyEngine) msgUsingProxy(socksString string) {
buf := copABuffer.Get().(*strings.Builder)
buf.WriteString("MysteryDialer using socks: ")
buf.WriteString(socksString)
pe.dbgPrint(buf)
}
func (pe *ProxyEngine) msgFailedMiddleware(socksString string) {
buf := copABuffer.Get().(*strings.Builder)
buf.WriteString("failed middleware check, ")
buf.WriteString(socksString)
buf.WriteString(", cycling...")
pe.dbgPrint(buf)
}
func (pe *ProxyEngine) msgTry(socksString string) {
buf := copABuffer.Get().(*strings.Builder)
buf.WriteString("try dial with: ")
buf.WriteString(socksString)
pe.dbgPrint(buf)
}
func (pe *ProxyEngine) msgCantGetLock(socksString string, putback bool) {
buf := copABuffer.Get().(*strings.Builder)
buf.WriteString("can't get lock for ")
buf.WriteString(socksString)
if putback {
buf.WriteString(", putting back in queue")
}
pe.dbgPrint(buf)
}
func (pe *ProxyEngine) msgGotLock(socksString string) {
buf := copABuffer.Get().(*strings.Builder)
buf.WriteString("got lock for ")
buf.WriteString(socksString)
pe.dbgPrint(buf)
}
func (pe *ProxyEngine) msgChecked(sock *Proxy, success bool) {
buf := copABuffer.Get().(*strings.Builder)
if success {
buf.WriteString("verified ")
buf.WriteString(sock.Endpoint)
buf.WriteString(" as SOCKS")
buf.WriteString(getProtoStr(sock.proto))
pe.dbgPrint(buf)
return
}
buf.WriteString("failed to verify: ")
buf.WriteString(sock.Endpoint)
pe.dbgPrint(buf)
}
func (pe *ProxyEngine) msgBadProxRate(sock *Proxy) {
buf := copABuffer.Get().(*strings.Builder)
buf.WriteString("badProx ratelimited: ")
buf.WriteString(sock.Endpoint)
pe.dbgPrint(buf)
}