dnscrypt-proxy/dnscrypt-proxy/main.go

120 lines
2.6 KiB
Go
Raw Normal View History

package main
import (
crypto_rand "crypto/rand"
"encoding/binary"
2018-01-17 11:28:43 +01:00
"flag"
"fmt"
"math/rand"
2018-02-28 18:11:48 +01:00
"os"
"sync"
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"
)
const (
2019-10-14 12:11:13 +02:00
AppVersion = "2.0.29-beta.1"
2018-01-31 08:38:22 +01:00
DefaultConfigFileName = "dnscrypt-proxy.toml"
)
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")
os.Setenv("GODEBUG", os.Getenv("GODEBUG")+",tls13=1")
seed := make([]byte, 8)
crypto_rand.Read(seed)
rand.Seed(int64(binary.LittleEndian.Uint64(seed[:])))
pwd, err := os.Getwd()
if err != nil {
dlog.Fatal("Unable to find the path to the current directory")
}
2018-01-17 11:28:43 +01:00
svcConfig := &service.Config{
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))
app := &App{}
2018-01-17 11:28:43 +01:00
svc, err := service.New(app, svcConfig)
if err != nil {
svc = nil
dlog.Debug(err)
}
app.proxy = NewProxy()
_ = ServiceManagerStartNotify()
if err := ConfigLoad(&app.proxy, svcFlag); err != nil {
2018-01-17 11:28:43 +01:00
dlog.Fatal(err)
}
if len(*svcFlag) != 0 {
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
}
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 {
dlog.Fatal(err)
}
app.quit = make(chan struct{})
app.wg.Add(1)
if service != nil {
go func() {
2018-03-17 22:47:17 +01:00
app.AppMain(proxy)
}()
} else {
2018-03-17 22:47:17 +01:00
app.AppMain(proxy)
}
return nil
}
func (app *App) AppMain(proxy *Proxy) {
2018-02-28 18:11:48 +01:00
pidfile.Write()
proxy.StartProxy()
<-app.quit
dlog.Notice("Quit signal received...")
app.wg.Done()
2018-02-28 18:11:48 +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)
}
dlog.Notice("Stopped.")
return nil
2018-01-14 00:56:46 +01:00
}