prox5/socks5_server.go

52 lines
1.2 KiB
Go
Raw Normal View History

2022-05-23 01:05:50 +00:00
package prox5
2021-09-30 19:19:09 +00:00
import (
"fmt"
2021-11-25 08:14:28 +00:00
2021-09-30 19:19:09 +00:00
"git.tcp.direct/kayos/go-socks5"
)
type socksLogger struct {
2022-06-26 02:51:42 +00:00
parent *ProxyEngine
2021-09-30 19:19:09 +00:00
}
// Printf is used to handle socks server logging.
func (s socksLogger) Printf(format string, a ...interface{}) {
2021-10-09 20:45:30 +00:00
s.parent.dbgPrint(fmt.Sprintf(format, a...))
2021-09-30 19:19:09 +00:00
}
2021-10-09 20:45:30 +00:00
type socksCreds struct {
2021-09-30 19:19:09 +00:00
username string
password string
}
// Valid implements the socks5.CredentialStore interface.
func (s socksCreds) Valid(username, password string) bool {
if s.username == username && s.password == password {
return true
}
return false
}
// StartSOCKS5Server starts our rotating proxy SOCKS5 server.
// listen is standard Go listen string, e.g: "127.0.0.1:1080".
// username and password are used for authenticatig to the SOCKS5 server.
2022-06-26 02:51:42 +00:00
func (pe *ProxyEngine) StartSOCKS5Server(listen, username, password string) error {
pe.socks5ServerAuth = socksCreds{username: username, password: password}
2021-09-30 19:19:09 +00:00
conf := &socks5.Config{
2022-06-26 02:51:42 +00:00
Credentials: pe.socks5ServerAuth,
Logger: pe.socksServerLogger,
Dial: pe.MysteryDialer,
2021-09-30 19:19:09 +00:00
}
2022-06-26 02:51:42 +00:00
pe.dbgPrint("listening for SOCKS5 connections on " + listen)
2021-09-30 19:19:09 +00:00
server, err := socks5.New(conf)
if err != nil {
return err
}
return server.ListenAndServe("tcp", listen)
}