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 = ["."]
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

View file

@ -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"

View file

@ -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)
}