2016-12-20 13:09:34 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-05-12 09:14:42 +03:00
|
|
|
"context"
|
2016-12-20 13:09:34 +03:00
|
|
|
"log"
|
2020-05-12 09:14:42 +03:00
|
|
|
"net"
|
2018-04-15 21:08:32 +03:00
|
|
|
"os"
|
2020-05-12 09:14:42 +03:00
|
|
|
"time"
|
2018-04-15 21:08:32 +03:00
|
|
|
|
|
|
|
"github.com/armon/go-socks5"
|
2018-06-13 22:34:35 +03:00
|
|
|
"github.com/caarlos0/env"
|
2016-12-20 13:09:34 +03:00
|
|
|
)
|
|
|
|
|
2018-06-13 22:34:35 +03:00
|
|
|
type params struct {
|
2020-05-12 09:14:42 +03:00
|
|
|
User string `env:"PROXY_USER" envDefault:""`
|
|
|
|
Password string `env:"PROXY_PASSWORD" envDefault:""`
|
|
|
|
Port string `env:"PROXY_PORT" envDefault:"1080"`
|
|
|
|
Timeout time.Duration `env:"DIAL_TIMEOUT" envDefault:"3s"`
|
2018-06-13 22:34:35 +03:00
|
|
|
}
|
|
|
|
|
2016-12-20 13:09:34 +03:00
|
|
|
func main() {
|
2018-06-13 22:34:35 +03:00
|
|
|
// Working with app params
|
|
|
|
cfg := params{}
|
|
|
|
err := env.Parse(&cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("%+v\n", err)
|
|
|
|
}
|
2018-04-15 21:08:32 +03:00
|
|
|
|
2018-06-13 22:34:35 +03:00
|
|
|
//Initialize socks5 config
|
|
|
|
socsk5conf := &socks5.Config{
|
2018-04-15 21:08:32 +03:00
|
|
|
Logger: log.New(os.Stdout, "", log.LstdFlags),
|
2020-05-12 09:14:42 +03:00
|
|
|
Dial: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
d := net.Dialer{
|
|
|
|
Timeout: cfg.Timeout,
|
|
|
|
}
|
|
|
|
return d.DialContext(ctx, network, addr)
|
|
|
|
},
|
2016-12-20 13:09:34 +03:00
|
|
|
}
|
2018-04-15 21:08:32 +03:00
|
|
|
|
2018-06-13 22:34:35 +03:00
|
|
|
if cfg.User+cfg.Password != "" {
|
2018-04-15 21:08:32 +03:00
|
|
|
creds := socks5.StaticCredentials{
|
|
|
|
os.Getenv("PROXY_USER"): os.Getenv("PROXY_PASSWORD"),
|
|
|
|
}
|
|
|
|
cator := socks5.UserPassAuthenticator{Credentials: creds}
|
2018-06-13 22:34:35 +03:00
|
|
|
socsk5conf.AuthMethods = []socks5.Authenticator{cator}
|
2018-04-15 21:08:32 +03:00
|
|
|
}
|
|
|
|
|
2018-06-13 22:34:35 +03:00
|
|
|
server, err := socks5.New(socsk5conf)
|
2016-12-20 13:09:34 +03:00
|
|
|
if err != nil {
|
2018-04-15 21:08:32 +03:00
|
|
|
log.Fatal(err)
|
2016-12-20 13:09:34 +03:00
|
|
|
}
|
|
|
|
|
2018-06-13 22:34:35 +03:00
|
|
|
log.Printf("Start listening proxy service on port %s\n", cfg.Port)
|
|
|
|
if err := server.ListenAndServe("tcp", ":"+cfg.Port); err != nil {
|
2018-04-15 21:08:32 +03:00
|
|
|
log.Fatal(err)
|
2016-12-20 13:09:34 +03:00
|
|
|
}
|
2018-04-15 21:08:32 +03:00
|
|
|
}
|