forked from nextdns/nextdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
82 lines (77 loc) · 1.55 KB
/
service.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 (
"fmt"
"os"
"github.com/nextdns/nextdns/config"
"github.com/nextdns/nextdns/host"
"github.com/nextdns/nextdns/host/service"
)
func svc(args []string) error {
cmd := args[0]
args = args[1:]
var c config.Config
if cmd == "install" {
c.Parse("nextdns "+cmd, args, true)
}
svcArgs := []string{"run"}
if c.File != "" {
svcArgs = append(svcArgs, "-config-file", c.File)
}
s, err := host.NewService(service.Config{
Name: "nextdns",
DisplayName: "NextDNS Proxy",
Description: "NextDNS DNS53 to DoH proxy.",
Arguments: svcArgs,
})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
switch cmd {
case "install":
_ = s.Stop()
_ = s.Uninstall()
if err := c.Save(); err != nil {
fmt.Printf("Cannot write config: %v\n", err)
os.Exit(1)
}
err := s.Install()
if err == nil {
err = s.Start()
}
fmt.Printf("NextDNS installed and started using %s init\n", service.Name(s))
return err
case "uninstall":
_ = deactivate()
_ = s.Stop()
return s.Uninstall()
case "start":
return s.Start()
case "stop":
return s.Stop()
case "restart":
return s.Restart()
case "status":
status := "unknown"
s, err := s.Status()
if err != nil {
return err
}
switch s {
case service.StatusRunning:
status = "running"
case service.StatusStopped:
status = "stopped"
case service.StatusNotInstalled:
status = "not installed"
}
fmt.Println(status)
return nil
case "log":
l, err := host.ReadLog("nextdns")
fmt.Printf("%s", l)
return err
default:
panic("unknown cmd: " + cmd)
}
}