dnscrypt-proxy/dnscrypt-proxy/main.go

113 lines
2.3 KiB
Go
Raw Normal View History

package main
import (
2018-01-17 11:28:43 +01:00
"flag"
"fmt"
2018-01-14 00:56:46 +01:00
"os"
"path/filepath"
"sync"
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-27 18:18:28 +01:00
const AppVersion = "2.0.0beta11"
type App struct {
2018-01-17 11:28:43 +01:00
wg sync.WaitGroup
quit chan struct{}
proxy Proxy
}
2018-01-09 13:35:10 +01:00
func main() {
dlog.Init("dnscrypt-proxy", dlog.SeverityNotice, "DAEMON")
2018-01-19 00:06:18 +01:00
2018-01-14 00:56:46 +01:00
cdLocal()
2018-01-17 11:28:43 +01:00
svcConfig := &service.Config{
Name: "dnscrypt-proxy",
DisplayName: "DNSCrypt client proxy",
Description: "Encrypted/authenticated DNS proxy",
}
svcFlag := flag.String("service", "", fmt.Sprintf("Control the system service: %q", service.ControlAction))
app := &App{}
2018-01-17 11:28:43 +01:00
svc, err := service.New(app, svcConfig)
if err != nil {
svc = nil
dlog.Debug(err)
}
2018-01-17 11:28:43 +01:00
app.proxy = Proxy{}
if err := ConfigLoad(&app.proxy, svcFlag, "dnscrypt-proxy.toml"); err != nil {
dlog.Fatal(err)
}
dlog.Noticef("Starting dnscrypt-proxy %s", AppVersion)
2018-01-17 11:28:43 +01:00
if len(*svcFlag) != 0 {
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
}
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 {
proxy := app.proxy
proxy.cachedIPs.cache = make(map[string]string)
if err := InitPluginsGlobals(&proxy.pluginsGlobals, &proxy); err != nil {
dlog.Fatal(err)
}
2018-01-10 13:33:06 +01:00
if proxy.daemonize {
Daemonize()
2018-01-10 13:33:06 +01:00
}
app.quit = make(chan struct{})
app.wg.Add(1)
if service != nil {
go func() {
app.AppMain(&proxy)
}()
} else {
app.AppMain(&proxy)
}
return nil
}
func (app *App) AppMain(proxy *Proxy) {
proxy.StartProxy()
<-app.quit
dlog.Notice("Quit signal received...")
app.wg.Done()
}
2018-01-17 11:28:43 +01:00
func (app *App) Stop(service service.Service) error {
dlog.Notice("Stopped.")
return nil
2018-01-14 00:56:46 +01:00
}
func cdLocal() {
ex, err := os.Executable()
if err != nil {
dlog.Warnf("Unable to determine the executable directory: [%s] -- You will need to specify absolute paths in the configuration file", err)
return
}
exPath := filepath.Dir(ex)
os.Chdir(exPath)
}