-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (63 loc) · 1.58 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/armon/go-socks5"
)
var (
flagDaemon = flag.Bool("daemon", false, "Run as a daemon")
flagInstall = flag.Bool("install", true, "Install the daemon (systemd service)")
)
func main() {
if err := do(); err != nil {
log.Fatal(err)
}
}
func do() error {
flag.Parse()
if *flagDaemon {
*flagInstall = false
}
if *flagInstall && getuid() != 0 {
return fmt.Errorf("you must install this program as root")
}
switch {
case *flagInstall:
if err := installDefaultConfig(); err != nil {
return fmt.Errorf("failed to write default config to %s: %v", configPath, err)
}
if err := installSystemdService(); err != nil {
return fmt.Errorf("failed to install systemd service: %v", err)
}
fmt.Println("gobsips installed and started as a systemd service.")
return nil
case *flagDaemon:
config := loadConfig()
// Create SOCKS5 configuration
conf := &socks5.Config{
Credentials: socks5.StaticCredentials{
config.Username: config.Password,
},
}
// Create SOCKS5 server
server, err := socks5.New(conf)
if err != nil {
return fmt.Errorf("failed to create SOCKS5 server: %v", err)
}
// Start server
addr := fmt.Sprintf("%s:%s", config.ListenHost, config.ListenPort)
log.Printf("Starting gobsips on %s", addr)
if err := listenAndServe(server, "tcp", addr); err != nil {
return fmt.Errorf("failed to start server: %v", err)
}
}
return nil
}
var (
getuid = os.Getuid
listenAndServe = func(server *socks5.Server, network, addr string) error {
return server.ListenAndServe(network, addr)
}
)