diff --git a/Gopkg.lock b/Gopkg.lock index 1e11a33e..37b57f48 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -70,6 +70,12 @@ packages = ["."] revision = "13d65f1d301904c28ff6c3256169cc60dd99c9dd" +[[projects]] + name = "github.com/judwhite/go-svc" + packages = ["svc"] + revision = "63c12402f579f0bdf022653c821a1aa5d7544f01" + version = "v1.0.0" + [[projects]] name = "github.com/miekg/dns" packages = ["."] @@ -102,9 +108,18 @@ ] revision = "5ccada7d0a7ba9aeb5d3aca8d3501b4c2a509fec" +[[projects]] + branch = "master" + name = "golang.org/x/sys" + packages = [ + "windows", + "windows/svc" + ] + revision = "fff93fa7cd278d84afc205751523809c464168ab" + [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "01e41ba3f8bb51f155fbca27baa1f94d4bc8e8d21d094b531941d78e01945fe2" + inputs-digest = "f55cf27bd0cdf3a812cf059cadae3be13f9760e19b311628b76498ab4e5c2d58" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 232eaafa..2eb2392e 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -41,3 +41,7 @@ [[constraint]] branch = "master" name = "github.com/hashicorp/go-immutable-radix" + +[[constraint]] + name = "github.com/judwhite/go-svc" + version = "1.0.0" diff --git a/dnscrypt-proxy/main.go b/dnscrypt-proxy/main.go index 23b68421..9facd73d 100644 --- a/dnscrypt-proxy/main.go +++ b/dnscrypt-proxy/main.go @@ -2,12 +2,15 @@ package main import ( "crypto/rand" + "log" "net" "os" "path/filepath" + "sync" "time" "github.com/jedisct1/dlog" + "github.com/judwhite/go-svc/svc" "golang.org/x/crypto/curve25519" ) @@ -35,9 +38,26 @@ type Proxy struct { pluginsGlobals PluginsGlobals } +type App struct { + wg sync.WaitGroup + quit chan struct{} +} + func main() { dlog.Init("dnscrypt-proxy", dlog.SeverityNotice) cdLocal() + app := &App{} + if err := svc.Run(app); err != nil { + dlog.Fatal(err) + } +} + +func (app *App) Init(env svc.Environment) error { + log.Printf("is win service? %v\n", env.IsWindowsService()) + return nil +} + +func (app *App) Start() error { proxy := Proxy{} if err := ConfigLoad(&proxy, "dnscrypt-proxy.toml"); err != nil { dlog.Fatal(err) @@ -48,17 +68,23 @@ func main() { if proxy.daemonize { Daemonize() } - proxy.StartProxy() + app.quit = make(chan struct{}) + app.wg.Add(1) + go func() { + proxy.StartProxy() + <-app.quit + dlog.Notice("Quit signal received...") + app.wg.Done() + }() + return nil } -func cdLocal() { - ex, err := os.Executable() - if err != nil { - dlog.Critical(err) - return - } - exPath := filepath.Dir(ex) - os.Chdir(exPath) +func (app *App) Stop() error { + dlog.Notice("Stopping...") + close(app.quit) + app.wg.Wait() + dlog.Notice("Stopped.") + return nil } func (proxy *Proxy) StartProxy() { @@ -87,10 +113,12 @@ func (proxy *Proxy) StartProxy() { } } dlog.Notice("dnscrypt-proxy is ready") - for { - time.Sleep(proxy.certRefreshDelay) - proxy.serversInfo.refresh(proxy) - } + go func() { + for { + time.Sleep(proxy.certRefreshDelay) + proxy.serversInfo.refresh(proxy) + } + }() } func (proxy *Proxy) udpListener(listenAddr *net.UDPAddr) error { @@ -240,3 +268,13 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str } serverInfo.noticeSuccess(proxy) } + +func cdLocal() { + ex, err := os.Executable() + if err != nil { + dlog.Critical(err) + return + } + exPath := filepath.Dir(ex) + os.Chdir(exPath) +}