diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..792cc3f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +inlets-svc.exe +/bin/** diff --git a/beep.go b/beep.go new file mode 100644 index 0000000..dcf2340 --- /dev/null +++ b/beep.go @@ -0,0 +1,22 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build windows + +package main + +import ( + "syscall" +) + +// BUG(brainman): MessageBeep Windows api is broken on Windows 7, +// so this example does not beep when runs as service on Windows 7. + +var ( + beepFunc = syscall.MustLoadDLL("user32.dll").MustFindProc("MessageBeep") +) + +func beep() { + beepFunc.Call(0xffffffff) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..076c58a --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/inlets/inlets-svc + +go 1.13 + +require golang.org/x/sys v0.0.0-20210317091845-390168757d9c diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6ea228f --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +golang.org/x/sys v0.0.0-20210317091845-390168757d9c h1:WGyvPg8lhdtSkb8BiYWdtPlLSommHOmJHFvzWODI7BQ= +golang.org/x/sys v0.0.0-20210317091845-390168757d9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/install.go b/install.go new file mode 100644 index 0000000..39cb00d --- /dev/null +++ b/install.go @@ -0,0 +1,92 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build windows + +package main + +import ( + "fmt" + "os" + "path/filepath" + + "golang.org/x/sys/windows/svc/eventlog" + "golang.org/x/sys/windows/svc/mgr" +) + +func exePath() (string, error) { + prog := os.Args[0] + p, err := filepath.Abs(prog) + if err != nil { + return "", err + } + fi, err := os.Stat(p) + if err == nil { + if !fi.Mode().IsDir() { + return p, nil + } + err = fmt.Errorf("%s is directory", p) + } + if filepath.Ext(p) == "" { + p += ".exe" + fi, err := os.Stat(p) + if err == nil { + if !fi.Mode().IsDir() { + return p, nil + } + err = fmt.Errorf("%s is directory", p) + } + } + return "", err +} + +func installService(name, desc string) error { + exepath, err := exePath() + if err != nil { + return err + } + m, err := mgr.Connect() + if err != nil { + return err + } + defer m.Disconnect() + s, err := m.OpenService(name) + if err == nil { + s.Close() + return fmt.Errorf("service %s already exists", name) + } + s, err = m.CreateService(name, exepath, mgr.Config{DisplayName: desc}, "is", "auto-started") + if err != nil { + return err + } + defer s.Close() + err = eventlog.InstallAsEventCreate(name, eventlog.Error|eventlog.Warning|eventlog.Info) + if err != nil { + s.Delete() + return fmt.Errorf("SetupEventLogSource() failed: %s", err) + } + return nil +} + +func removeService(name string) error { + m, err := mgr.Connect() + if err != nil { + return err + } + defer m.Disconnect() + s, err := m.OpenService(name) + if err != nil { + return fmt.Errorf("service %s is not installed", name) + } + defer s.Close() + err = s.Delete() + if err != nil { + return err + } + err = eventlog.Remove(name) + if err != nil { + return fmt.Errorf("RemoveEventLogSource() failed: %s", err) + } + return nil +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..43c72bd --- /dev/null +++ b/main.go @@ -0,0 +1,78 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build windows + +// Example service program that beeps. +// +// The program demonstrates how to create Windows service and +// install / remove it on a computer. It also shows how to +// stop / start / pause / continue any service, and how to +// write to event log. It also shows how to use debug +// facilities available in debug package. +// +package main + +import ( + "fmt" + "log" + "os" + "strings" + + "golang.org/x/sys/windows/svc" +) + +const svcName = "inlets-client" +const svcDesc = "Inlets client" + +func usage(errmsg string) { + fmt.Fprintf(os.Stderr, + "%s\n\n"+ + "usage: %s \n"+ + " where is one of\n"+ + " install, remove, debug, start, stop, pause or continue.\n", + errmsg, os.Args[0]) + os.Exit(2) +} + +func main() { + + inService, err := svc.IsWindowsService() + if err != nil { + log.Fatalf("failed to determine if we are running in service: %v", err) + } + if inService { + runService(svcName, false) + return + } + + if len(os.Args) < 2 { + usage("no command specified") + } + + cmd := strings.ToLower(os.Args[1]) + switch cmd { + case "debug": + runService(svcName, true) + return + case "install": + err = installService(svcName, svcDesc) + case "remove": + err = removeService(svcName) + case "start": + err = startService(svcName) + case "stop": + err = controlService(svcName, svc.Stop, svc.Stopped) + case "pause": + err = controlService(svcName, svc.Pause, svc.Paused) + case "continue": + err = controlService(svcName, svc.Continue, svc.Running) + default: + usage(fmt.Sprintf("invalid command %s", cmd)) + } + if err != nil { + log.Fatalf("failed to %s %s: %v", cmd, svcName, err) + } + return +} diff --git a/manage.go b/manage.go new file mode 100644 index 0000000..c4cf92c --- /dev/null +++ b/manage.go @@ -0,0 +1,63 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build windows + +package main + +import ( + "fmt" + "time" + + "golang.org/x/sys/windows/svc" + "golang.org/x/sys/windows/svc/mgr" +) + +func startService(name string) error { + m, err := mgr.Connect() + if err != nil { + return err + } + defer m.Disconnect() + s, err := m.OpenService(name) + if err != nil { + return fmt.Errorf("could not access service: %v", err) + } + defer s.Close() + err = s.Start("is", "manual-started") + if err != nil { + return fmt.Errorf("could not start service: %v", err) + } + return nil +} + +func controlService(name string, c svc.Cmd, to svc.State) error { + m, err := mgr.Connect() + if err != nil { + return err + } + + defer m.Disconnect() + s, err := m.OpenService(name) + if err != nil { + return fmt.Errorf("could not access service: %v", err) + } + defer s.Close() + status, err := s.Control(c) + if err != nil { + return fmt.Errorf("could not send control=%d: %v", c, err) + } + timeout := time.Now().Add(10 * time.Second) + for status.State != to { + if timeout.Before(time.Now()) { + return fmt.Errorf("timeout waiting for service to go to state=%d", to) + } + time.Sleep(300 * time.Millisecond) + status, err = s.Query() + if err != nil { + return fmt.Errorf("could not retrieve service status: %v", err) + } + } + return nil +} diff --git a/service.go b/service.go new file mode 100644 index 0000000..2c28cf1 --- /dev/null +++ b/service.go @@ -0,0 +1,155 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build windows + +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "os/exec" + "strconv" + "strings" + "time" + + "golang.org/x/sys/windows/svc" + "golang.org/x/sys/windows/svc/debug" + "golang.org/x/sys/windows/svc/eventlog" +) + +var elog debug.Log + +type InletsClientConfig struct { + Upstreams []string `json":upstreams"` + URL string `json":url"` + Token string `json":token"` + LicenseFile string `json:"license-file"` + AutoTLS bool `json:"auto-tls"` +} + +type myservice struct { + InletsClientConfig + Process *exec.Cmd +} + +func newService() (*myservice, error) { + m := &myservice{} + c := InletsClientConfig{} + + b, err := ioutil.ReadFile("C:\\inlets.json") + if err != nil { + return nil, err + } + err = json.Unmarshal(b, &c) + if err != nil { + return nil, err + } + + elog.Info(1, fmt.Sprintf("Should we start here?")) + m.InletsClientConfig = c + + upstreams := "--upstream=" + strings.TrimRight(strings.Join(m.Upstreams, ","), ",") + args := []string{ + "http", + "client", + "--url=" + m.URL, + upstreams, + "--token=" + m.Token, + "--license-file=" + m.LicenseFile, + "--auto-tls=" + strconv.FormatBool(m.AutoTLS), + } + + elog.Info(1, fmt.Sprintf("inlets-pro %v", args)) + cmd := exec.Command("inlets-pro", args...) + + m.Process = cmd + + err = m.Process.Start() + if err != nil { + elog.Error(1, fmt.Sprintf("Error starting app %s", err.Error())) + } + + elog.Info(1, fmt.Sprintf("PID %d", cmd.Process.Pid)) + return m, nil +} + +func (m *myservice) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { + const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue + changes <- svc.Status{State: svc.StartPending} + fasttick := time.Tick(500 * time.Millisecond) + slowtick := time.Tick(2 * time.Second) + tick := fasttick + changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} +loop: + for { + select { + case <-tick: + beep() + elog.Info(1, fmt.Sprintf("beep, %v", m)) + case c := <-r: + switch c.Cmd { + + case svc.Interrogate: + changes <- c.CurrentStatus + // Testing deadlock from https://code.google.com/p/winsvc/issues/detail?id=4 + time.Sleep(100 * time.Millisecond) + changes <- c.CurrentStatus + case svc.Stop, svc.Shutdown: + // golang.org/x/sys/windows/svc.TestExample is verifying this output. + testOutput := strings.Join(args, "-") + testOutput += fmt.Sprintf("-%d", c.Context) + elog.Info(1, testOutput) + err := m.Process.Process.Kill() + if err != nil { + elog.Info(1, "Error killing process: "+err.Error()) + } + break loop + case svc.Pause: + changes <- svc.Status{State: svc.Paused, Accepts: cmdsAccepted} + tick = slowtick + case svc.Continue: + changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} + tick = fasttick + default: + elog.Error(1, fmt.Sprintf("unexpected control request #%d", c)) + } + } + } + changes <- svc.Status{State: svc.StopPending} + return +} + +func runService(name string, isDebug bool) { + var err error + if isDebug { + elog = debug.New(name) + } else { + elog, err = eventlog.Open(name) + if err != nil { + return + } + } + defer elog.Close() + + elog.Info(1, fmt.Sprintf("starting %s service", name)) + run := svc.Run + if isDebug { + run = debug.Run + } + + m, err := newService() + if err != nil { + elog.Error(1, fmt.Sprintf("%s service failed: %v", name, err)) + return + } + + err = run(name, m) + if err != nil { + elog.Error(1, fmt.Sprintf("%s service failed: %v", name, err)) + return + } + elog.Info(1, fmt.Sprintf("%s service stopped", name)) +}