2018-01-09 00:24:51 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-01-17 11:28:43 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2018-02-28 18:11:48 +01:00
|
|
|
"os"
|
2018-01-17 10:58:19 +01:00
|
|
|
"sync"
|
2018-01-09 00:24:51 +01:00
|
|
|
|
2018-02-28 18:11:48 +01:00
|
|
|
"github.com/facebookgo/pidfile"
|
2018-01-11 11:50:54 +01:00
|
|
|
"github.com/jedisct1/dlog"
|
2018-01-17 11:28:43 +01:00
|
|
|
"github.com/kardianos/service"
|
2018-01-09 00:24:51 +01:00
|
|
|
)
|
|
|
|
|
2018-01-29 23:57:20 +01:00
|
|
|
const (
|
2019-04-28 23:26:33 +02:00
|
|
|
AppVersion = "2.0.23"
|
2018-01-31 08:38:22 +01:00
|
|
|
DefaultConfigFileName = "dnscrypt-proxy.toml"
|
2018-01-29 23:57:20 +01:00
|
|
|
)
|
2018-01-18 12:22:25 +01:00
|
|
|
|
2018-01-17 10:58:19 +01:00
|
|
|
type App struct {
|
2018-01-17 11:28:43 +01:00
|
|
|
wg sync.WaitGroup
|
|
|
|
quit chan struct{}
|
|
|
|
proxy Proxy
|
2018-01-17 10:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 13:35:10 +01:00
|
|
|
func main() {
|
2018-01-18 14:25:45 +01:00
|
|
|
dlog.Init("dnscrypt-proxy", dlog.SeverityNotice, "DAEMON")
|
2019-02-17 23:56:02 +01:00
|
|
|
os.Setenv("GODEBUG", os.Getenv("GODEBUG")+",tls13=1")
|
2018-04-03 19:42:27 +02:00
|
|
|
|
2018-04-03 20:15:33 +02:00
|
|
|
pwd, err := os.Getwd()
|
2018-04-03 19:42:27 +02:00
|
|
|
if err != nil {
|
2018-04-03 20:15:33 +02:00
|
|
|
dlog.Fatal("Unable to find the path to the current directory")
|
2018-04-03 19:42:27 +02:00
|
|
|
}
|
2018-01-17 11:28:43 +01:00
|
|
|
svcConfig := &service.Config{
|
2018-04-03 20:15:33 +02:00
|
|
|
Name: "dnscrypt-proxy",
|
|
|
|
DisplayName: "DNSCrypt client proxy",
|
|
|
|
Description: "Encrypted/authenticated DNS proxy",
|
|
|
|
WorkingDirectory: pwd,
|
2018-01-17 11:28:43 +01:00
|
|
|
}
|
|
|
|
svcFlag := flag.String("service", "", fmt.Sprintf("Control the system service: %q", service.ControlAction))
|
2018-01-17 10:58:19 +01:00
|
|
|
app := &App{}
|
2018-01-17 11:28:43 +01:00
|
|
|
svc, err := service.New(app, svcConfig)
|
|
|
|
if err != nil {
|
2018-01-18 02:07:31 +01:00
|
|
|
svc = nil
|
|
|
|
dlog.Debug(err)
|
2018-01-17 10:58:19 +01:00
|
|
|
}
|
2018-02-04 21:13:54 +01:00
|
|
|
app.proxy = NewProxy()
|
2018-07-19 19:03:57 +02:00
|
|
|
_ = ServiceManagerStartNotify()
|
2018-04-03 20:15:33 +02:00
|
|
|
if err := ConfigLoad(&app.proxy, svcFlag); err != nil {
|
2018-01-17 11:28:43 +01:00
|
|
|
dlog.Fatal(err)
|
|
|
|
}
|
2018-02-06 19:33:58 +01:00
|
|
|
dlog.Noticef("dnscrypt-proxy %s", AppVersion)
|
2018-01-21 18:02:32 +01:00
|
|
|
|
2018-01-17 11:28:43 +01:00
|
|
|
if len(*svcFlag) != 0 {
|
2018-02-20 00:34:06 +01:00
|
|
|
if svc == nil {
|
|
|
|
dlog.Fatal("Built-in service installation is not supported on this platform")
|
|
|
|
}
|
2018-01-17 11:28:43 +01:00
|
|
|
if err := service.Control(svc, *svcFlag); err != nil {
|
|
|
|
dlog.Fatal(err)
|
|
|
|
}
|
|
|
|
if *svcFlag == "install" {
|
|
|
|
dlog.Notice("Installed as a service. Use `-service start` to start")
|
|
|
|
} else if *svcFlag == "uninstall" {
|
|
|
|
dlog.Notice("Service uninstalled")
|
|
|
|
} else if *svcFlag == "start" {
|
|
|
|
dlog.Notice("Service started")
|
|
|
|
} else if *svcFlag == "stop" {
|
|
|
|
dlog.Notice("Service stopped")
|
|
|
|
} else if *svcFlag == "restart" {
|
|
|
|
dlog.Notice("Service restarted")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-01-18 02:07:31 +01:00
|
|
|
if svc != nil {
|
|
|
|
if err = svc.Run(); err != nil {
|
|
|
|
dlog.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
app.Start(nil)
|
2018-01-10 12:01:49 +01:00
|
|
|
}
|
2018-01-17 11:28:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) Start(service service.Service) error {
|
2018-03-17 22:47:17 +01:00
|
|
|
proxy := &app.proxy
|
|
|
|
if err := InitPluginsGlobals(&proxy.pluginsGlobals, proxy); err != nil {
|
2018-01-15 23:07:41 +01:00
|
|
|
dlog.Fatal(err)
|
|
|
|
}
|
2018-01-17 10:58:19 +01:00
|
|
|
app.quit = make(chan struct{})
|
|
|
|
app.wg.Add(1)
|
2018-01-18 02:07:31 +01:00
|
|
|
if service != nil {
|
|
|
|
go func() {
|
2018-03-17 22:47:17 +01:00
|
|
|
app.AppMain(proxy)
|
2018-01-18 02:07:31 +01:00
|
|
|
}()
|
|
|
|
} else {
|
2018-03-17 22:47:17 +01:00
|
|
|
app.AppMain(proxy)
|
2018-01-18 02:07:31 +01:00
|
|
|
}
|
2018-01-17 10:58:19 +01:00
|
|
|
return nil
|
2018-01-09 00:24:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-18 02:07:31 +01:00
|
|
|
func (app *App) AppMain(proxy *Proxy) {
|
2018-02-28 18:11:48 +01:00
|
|
|
pidfile.Write()
|
2018-08-04 00:57:52 +02:00
|
|
|
proxy.StartProxy()
|
2018-01-18 02:07:31 +01:00
|
|
|
<-app.quit
|
|
|
|
dlog.Notice("Quit signal received...")
|
|
|
|
app.wg.Done()
|
2018-02-28 18:11:48 +01:00
|
|
|
|
2018-01-18 02:07:31 +01:00
|
|
|
}
|
|
|
|
|
2018-01-17 11:28:43 +01:00
|
|
|
func (app *App) Stop(service service.Service) error {
|
2018-02-28 18:11:48 +01:00
|
|
|
if pidFilePath := pidfile.GetPidfilePath(); len(pidFilePath) > 1 {
|
|
|
|
os.Remove(pidFilePath)
|
|
|
|
}
|
2018-01-17 10:58:19 +01:00
|
|
|
dlog.Notice("Stopped.")
|
|
|
|
return nil
|
2018-01-14 00:56:46 +01:00
|
|
|
}
|