prox5/example/main.go

171 lines
3.7 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"os"
"time"
2022-10-16 12:56:40 +00:00
"github.com/gdamore/tcell/v2"
2022-10-19 13:15:16 +00:00
// "github.com/haxii/socks5"
"git.tcp.direct/kayos/prox5"
2022-10-16 12:56:40 +00:00
"github.com/rivo/tview"
)
2022-10-16 12:56:40 +00:00
var swamp *prox5.Swamp
type socksLogger struct{}
var socklog = socksLogger{}
func StartUpstreamProxy(listen string) {
2022-10-19 13:15:16 +00:00
swamp.StartSOCKS5Server(listen, "", "")
// conf := &socks5.Config{Dial: swamp.DialContext, Logger: socklog}
// server, err := socks5.New(conf)
//
// if err != nil {
// socklog.Printf(err.Error())
// return
// }
//
// socklog.Printf("starting proxy server on %s", listen)
//
// if err := server.ListenAndServe("tcp", listen); err != nil {
// socklog.Printf(err.Error())
// return
// }
}
func init() {
2022-10-16 10:53:04 +00:00
swamp = prox5.NewDefaultSwamp()
swamp.SetMaxWorkers(5)
swamp.EnableDebug()
2022-10-16 12:56:40 +00:00
swamp.SetDebugLogger(socklog)
swamp.EnableDebugRedaction()
swamp.EnableAutoScaler()
go StartUpstreamProxy("127.0.0.1:1555")
2021-10-27 02:13:52 +00:00
count := swamp.LoadProxyTXT(os.Args[1])
2021-09-21 12:03:20 +00:00
if count < 1 {
2022-10-16 12:56:40 +00:00
socklog.Printf("file contained no valid SOCKS host:port combos")
os.Exit(1)
}
if err := swamp.Start(); err != nil {
panic(err)
}
2021-09-15 05:09:43 +00:00
2022-10-16 12:56:40 +00:00
socklog.Printf("[USAGE] q: quit | d: debug | p: pause/unpause")
}
2022-10-16 13:14:52 +00:00
const statsFmt = ">>>>>-------<<<<<\n>>>>> Prox5 <<<<<\n>>>>>-------<<<<<\n\nUptime: %s\n\nValidated: %d\nDispensed: %d\n\nMaximum Workers: %d\nActive Workers: %d\nAsleep Workers: %d\n\nAutoScale: %s\n\n----------\n%s"
2022-10-16 12:56:40 +00:00
var (
background *tview.TextView
window *tview.Modal
app *tview.Application
)
func currentString(lastMessage string) string {
if swamp == nil {
return ""
}
2022-10-16 12:56:40 +00:00
stats := swamp.GetStatistics()
wMax, wRun, wIdle := swamp.GetWorkers()
return fmt.Sprintf(statsFmt,
stats.GetUptime().Round(time.Second), int(stats.Valid4+stats.Valid4a+stats.Valid5),
stats.Dispensed, wMax, wRun, wIdle, swamp.GetAutoScalerStateString(), lastMessage)
}
2022-10-19 12:36:31 +00:00
func (s socksLogger) Errorf(format string, a ...interface{}) {
s.Printf(format, a...)
}
2022-10-16 12:56:40 +00:00
func (s socksLogger) Printf(format string, a ...interface{}) {
if app == nil {
return
}
2022-10-16 12:56:40 +00:00
msg := fmt.Sprintf(format, a...)
if msg == "" {
return
}
app.QueueUpdateDraw(func() {
window.SetText(currentString(msg))
})
}
2022-10-16 12:56:40 +00:00
func (s socksLogger) Print(str string) {
if app == nil {
return
}
app.QueueUpdateDraw(func() {
window.SetText(currentString(str))
})
}
func buttons(buttonIndex int, buttonLabel string) {
go func() {
switch buttonIndex {
case 0:
app.Stop()
case 1:
if swamp.IsRunning() {
2021-09-21 12:03:20 +00:00
err := swamp.Pause()
if err != nil {
2022-10-16 12:56:40 +00:00
socklog.Printf(err.Error())
2021-09-21 12:03:20 +00:00
}
2021-09-13 19:34:27 +00:00
} else {
if err := swamp.Resume(); err != nil {
2022-10-16 12:56:40 +00:00
socklog.Printf(err.Error())
}
}
2022-10-16 12:56:40 +00:00
case 2:
swamp.SetMaxWorkers(swamp.GetMaxWorkers() + 1)
case 3:
swamp.SetMaxWorkers(swamp.GetMaxWorkers() - 1)
2021-09-13 19:34:27 +00:00
default:
2022-10-16 12:56:40 +00:00
app.Stop()
}
2022-10-16 12:56:40 +00:00
}()
2021-09-13 19:34:27 +00:00
}
2022-10-16 12:56:40 +00:00
func main() {
app = tview.NewApplication()
window = tview.NewModal().
SetText(currentString("Initialize")).
AddButtons([]string{"Quit", "Pause", "+", "-"}).
SetDoneFunc(buttons).
SetBackgroundColor(tcell.ColorBlack).SetTextColor(tcell.ColorWhite)
modal := func(p tview.Primitive, width, height int) tview.Primitive {
return tview.NewFlex().
AddItem(nil, 0, 1, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false).
AddItem(p, height, 1, true).
AddItem(nil, 0, 1, false), width, 1, true).
AddItem(nil, 0, 1, false)
}
2022-10-16 12:56:40 +00:00
background = tview.NewTextView().
SetTextColor(tcell.ColorGray).SetTextAlign(tview.AlignLeft)
pages := tview.NewPages().
AddPage("background", background, true, true).
AddPage("window", modal(window, 150, 50), true, true)
2021-10-27 02:51:14 +00:00
2022-10-16 12:56:40 +00:00
if err := app.SetRoot(pages, false).Run(); err != nil {
panic(err)
}
swamp.SetDebugLogger(socklog)
go func() {
for {
2022-10-16 12:56:40 +00:00
time.Sleep(time.Second)
app.Sync()
}
}()
2022-10-16 12:56:40 +00:00
// Initialize()
}