-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
154 lines (133 loc) · 3.64 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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
}
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("Starting: inlets-pro %v", strings.Replace(strings.Join(args, " "), m.Token, "REDACTED", 1)))
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("inlets-client 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))
}