-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathserver.go
63 lines (52 loc) · 1.17 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"log"
"net"
"os"
"os/exec"
"github.com/armon/go-socks5"
"github.com/caarlos0/env"
)
type params struct {
User string `env:"SOCKS5_USER" envDefault:""`
Password string `env:"SOCKS5_PASS" envDefault:""`
Port string `env:"SOCKS5_PORT" envDefault:"1080"`
Up string `env:"SOCKS5_UP" envDefault:""`
}
func main() {
// Working with app params
cfg := params{}
err := env.Parse(&cfg)
if err != nil {
log.Printf("%+v\n", err)
}
//Initialize socks5 config
socsk5conf := &socks5.Config{
Logger: log.New(os.Stdout, "", log.LstdFlags),
}
if cfg.User+cfg.Password != "" {
creds := socks5.StaticCredentials{
cfg.User: cfg.Password,
}
cator := socks5.UserPassAuthenticator{Credentials: creds}
socsk5conf.AuthMethods = []socks5.Authenticator{cator}
}
server, err := socks5.New(socsk5conf)
if err != nil {
log.Fatal(err)
}
l, err := net.Listen("tcp", ":"+cfg.Port)
if err != nil {
log.Fatal(err)
}
log.Printf("Start listening proxy service on port %s\n", cfg.Port)
if cfg.Up != "" {
err = exec.Command(cfg.Up).Start()
if err != nil {
log.Fatal(err)
}
}
if err := server.Serve(l); err != nil {
log.Fatal(err)
}
}