prox5/socks5_server.go

56 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 (
2022-10-19 12:36:31 +00:00
"sync"
2022-07-25 07:14:26 +00:00
"git.tcp.direct/kayos/go-socks5"
2021-09-30 19:19:09 +00:00
"git.tcp.direct/kayos/common/pool"
)
2021-09-30 19:19:09 +00:00
var strs = pool.NewStringFactory()
2022-10-19 12:36:31 +00:00
type cpool struct {
*sync.Pool
}
var bufs = cpool{
Pool: &sync.Pool{
New: func() interface{} {
return make([]byte, 32*1024)
},
},
}
func (c cpool) Get() []byte {
return c.Pool.Get().([]byte)
}
func (c cpool) Put(cc []byte) {
c.Pool.Put(cc)
}
2021-09-30 19:19:09 +00:00
// 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-10-16 10:53:04 +00:00
func (p5 *ProxyEngine) StartSOCKS5Server(listen, username, password string) error {
2022-10-19 13:15:16 +00:00
opts := []socks5.Option{
2022-10-19 12:36:31 +00:00
socks5.WithBufferPool(bufs),
socks5.WithLogger(p5.DebugLogger),
2023-01-31 09:21:29 +00:00
socks5.WithDial(p5.DialContext),
2022-10-19 13:15:16 +00:00
}
if username != "" && password != "" {
cator := socks5.UserPassAuthenticator{Credentials: socks5.StaticCredentials{username: password}}
opts = append(opts, socks5.WithAuthMethods([]socks5.Authenticator{cator}))
}
server := socks5.NewServer(opts...)
2021-09-30 19:19:09 +00:00
buf := strs.Get()
buf.MustWriteString("listening for SOCKS5 connections on ")
buf.MustWriteString(listen)
2022-09-22 23:48:08 +00:00
p5.dbgPrint(buf)
2021-09-30 19:19:09 +00:00
return server.ListenAndServe("tcp", listen)
}