Preliminary support for running as a Windows service

This commit is contained in:
Frank Denis 2018-01-17 10:58:19 +01:00
parent 0db304b4bc
commit 3fe6dbd740
3 changed files with 71 additions and 14 deletions

17
Gopkg.lock generated
View file

@ -70,6 +70,12 @@
packages = ["."] packages = ["."]
revision = "13d65f1d301904c28ff6c3256169cc60dd99c9dd" revision = "13d65f1d301904c28ff6c3256169cc60dd99c9dd"
[[projects]]
name = "github.com/judwhite/go-svc"
packages = ["svc"]
revision = "63c12402f579f0bdf022653c821a1aa5d7544f01"
version = "v1.0.0"
[[projects]] [[projects]]
name = "github.com/miekg/dns" name = "github.com/miekg/dns"
packages = ["."] packages = ["."]
@ -102,9 +108,18 @@
] ]
revision = "5ccada7d0a7ba9aeb5d3aca8d3501b4c2a509fec" revision = "5ccada7d0a7ba9aeb5d3aca8d3501b4c2a509fec"
[[projects]]
branch = "master"
name = "golang.org/x/sys"
packages = [
"windows",
"windows/svc"
]
revision = "fff93fa7cd278d84afc205751523809c464168ab"
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
inputs-digest = "01e41ba3f8bb51f155fbca27baa1f94d4bc8e8d21d094b531941d78e01945fe2" inputs-digest = "f55cf27bd0cdf3a812cf059cadae3be13f9760e19b311628b76498ab4e5c2d58"
solver-name = "gps-cdcl" solver-name = "gps-cdcl"
solver-version = 1 solver-version = 1

View file

@ -41,3 +41,7 @@
[[constraint]] [[constraint]]
branch = "master" branch = "master"
name = "github.com/hashicorp/go-immutable-radix" name = "github.com/hashicorp/go-immutable-radix"
[[constraint]]
name = "github.com/judwhite/go-svc"
version = "1.0.0"

View file

@ -2,12 +2,15 @@ package main
import ( import (
"crypto/rand" "crypto/rand"
"log"
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
"sync"
"time" "time"
"github.com/jedisct1/dlog" "github.com/jedisct1/dlog"
"github.com/judwhite/go-svc/svc"
"golang.org/x/crypto/curve25519" "golang.org/x/crypto/curve25519"
) )
@ -35,9 +38,26 @@ type Proxy struct {
pluginsGlobals PluginsGlobals pluginsGlobals PluginsGlobals
} }
type App struct {
wg sync.WaitGroup
quit chan struct{}
}
func main() { func main() {
dlog.Init("dnscrypt-proxy", dlog.SeverityNotice) dlog.Init("dnscrypt-proxy", dlog.SeverityNotice)
cdLocal() 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{} proxy := Proxy{}
if err := ConfigLoad(&proxy, "dnscrypt-proxy.toml"); err != nil { if err := ConfigLoad(&proxy, "dnscrypt-proxy.toml"); err != nil {
dlog.Fatal(err) dlog.Fatal(err)
@ -48,17 +68,23 @@ func main() {
if proxy.daemonize { if proxy.daemonize {
Daemonize() Daemonize()
} }
app.quit = make(chan struct{})
app.wg.Add(1)
go func() {
proxy.StartProxy() proxy.StartProxy()
<-app.quit
dlog.Notice("Quit signal received...")
app.wg.Done()
}()
return nil
} }
func cdLocal() { func (app *App) Stop() error {
ex, err := os.Executable() dlog.Notice("Stopping...")
if err != nil { close(app.quit)
dlog.Critical(err) app.wg.Wait()
return dlog.Notice("Stopped.")
} return nil
exPath := filepath.Dir(ex)
os.Chdir(exPath)
} }
func (proxy *Proxy) StartProxy() { func (proxy *Proxy) StartProxy() {
@ -87,10 +113,12 @@ func (proxy *Proxy) StartProxy() {
} }
} }
dlog.Notice("dnscrypt-proxy is ready") dlog.Notice("dnscrypt-proxy is ready")
go func() {
for { for {
time.Sleep(proxy.certRefreshDelay) time.Sleep(proxy.certRefreshDelay)
proxy.serversInfo.refresh(proxy) proxy.serversInfo.refresh(proxy)
} }
}()
} }
func (proxy *Proxy) udpListener(listenAddr *net.UDPAddr) error { func (proxy *Proxy) udpListener(listenAddr *net.UDPAddr) error {
@ -240,3 +268,13 @@ func (proxy *Proxy) processIncomingQuery(serverInfo *ServerInfo, clientProto str
} }
serverInfo.noticeSuccess(proxy) serverInfo.noticeSuccess(proxy)
} }
func cdLocal() {
ex, err := os.Executable()
if err != nil {
dlog.Critical(err)
return
}
exPath := filepath.Dir(ex)
os.Chdir(exPath)
}