-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.go
82 lines (68 loc) · 1.62 KB
/
install.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
81
82
package main
import (
"crypto/md5"
"fmt"
"os"
"os/exec"
"strings"
)
var (
pathMachineID = "/etc/machine-id"
serviceName = "gobsips.service"
systemdPath = "/etc/systemd/system/" + serviceName
)
func installDefaultConfig() error {
config := Config{}
config.Username = "api"
config.Password = md5sum(getMachineID())
config.ListenHost = "0.0.0.0"
config.ListenPort = "1080"
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return saveConfig(config)
}
return nil
}
func installSystemdService() error {
exePath, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to get executable path: %v", err)
}
serviceContent := fmt.Sprintf(`[Unit]
Description=Go Binary Systemd-Integrated Proxy Server
After=network.target
[Service]
ExecStart=%s -daemon
Restart=always
User=nobody
[Install]
WantedBy=multi-user.target
`, exePath)
if err = os.WriteFile(systemdPath, []byte(serviceContent), 0644); err != nil {
return err
}
// Enable and start the service
if err = runCommand("systemctl", "daemon-reload"); err == nil {
if err = runCommand("systemctl", "enable", serviceName); err == nil {
err = runCommand("systemctl", "start", serviceName)
}
}
return err
}
var run = func(cmd *exec.Cmd) error {
return cmd.Run()
}
func runCommand(name string, args ...string) error {
cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return run(cmd)
}
func getMachineID() string {
if data, err := os.ReadFile(pathMachineID); err == nil {
return strings.TrimSpace(string(data))
}
return ""
}
func md5sum(data string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(data)))
}