-
Notifications
You must be signed in to change notification settings - Fork 16
/
ngrok.go
300 lines (265 loc) · 7.61 KB
/
ngrok.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
"syscall"
"time"
. "ytd/constants"
. "ytd/models"
"github.com/pkg/errors"
"github.com/wailsapp/wails/v2"
)
type NgrokService struct {
runtime *wails.Runtime
pid int
publicUrl string
*os.ProcessState
context.Context
}
type NgrokProcessResult struct {
err error
output string
errCode string
status string
publicUrl string
monitorStatus chan string
}
type NgrokTunnelInfo struct {
PublicUrl string `json:"url"`
Err error
}
type ngrokTunnels struct {
Tunnels []ngrokTunnel `json:"tunnels"`
}
type ngrokTunnel struct {
Name string `json:"name"`
URI string `json:"uri"`
PublicURL string `json:"public_url"`
Proto string `json:"proto"`
Config ngrokTunnelConfig `json:"config"`
}
type ngrokTunnelConfig struct {
Addr string `json:"addr"`
Inspect bool `json:"inspect"`
}
type ngrokCmdError struct {
err error
errCode string
}
func newProcessResultWithError(err error, output string, errCode string) NgrokProcessResult {
return NgrokProcessResult{
status: NgrokStatusError,
err: err,
output: output,
errCode: errCode,
monitorStatus: make(chan string),
}
}
func newProcessResultWithUrl(url string) NgrokProcessResult {
return NgrokProcessResult{
status: NgrokStatusRunning,
publicUrl: url,
}
}
func (n *NgrokService) isRunning() bool {
return n.pid != 0
}
func (n *NgrokService) StartProcess(restart bool) NgrokProcessResult {
// if is running kill & restart to apply new config
if restart && n.isRunning() {
n.KillProcess()
}
// set ngrok authtoken first
err := n.SetAuthToken()
if err != nil {
return newProcessResultWithError(errors.Wrap(err, "ngrok StartProcess()"), "", "")
}
// ngrok --http http://localhost:8080 --region eu --bind-tls true --auth "ytd:ytd"
ngrokPath, _ := n.IsNgrokInstalled()
args := []string{
"http", fmt.Sprintf("http://localhost:8080"),
"--region", "eu",
"--bind-tls", "true",
"--host-header", "rewrite",
}
if appState.Config.PublicServer.Ngrok.Auth.Enabled {
args = append(args, "--auth")
args = append(args, fmt.Sprintf("%s:%s", appState.Config.PublicServer.Ngrok.Auth.Username, appState.Config.PublicServer.Ngrok.Auth.Password))
}
cmd := exec.CommandContext(
n.Context,
ngrokPath,
args...,
)
cmd.SysProcAttr = &syscall.SysProcAttr{
// set this to true otherwise if we restart ngrok our app will be killed too
Setpgid: true,
}
// Use the same pipe for standard error
stderr, err := cmd.StderrPipe()
if err != nil {
return newProcessResultWithError(errors.Wrap(err, "ngrok StartProcess() cmd.StderrPipe()"), "", "")
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return newProcessResultWithError(errors.Wrap(err, "ngrok StartProcess() cmd.StdoutPipe()"), "", "")
}
if err := cmd.Start(); err != nil {
return newProcessResultWithError(errors.Wrap(err, "ngrok StartProcess() cmd.Start()"), "", "")
}
// channel will send an error if ngrok quits unexpectedly.
errorChan := make(chan ngrokCmdError)
go errorReciever(cmd, stdout, stderr, errorChan)
// channel will recieve the string of the connection URL.
waitForConnectionChan := make(chan NgrokTunnelInfo, 1)
go n.GetPublicUrl(waitForConnectionChan)
// and finally, make a channel that will time out if all else fails.
timeoutChan := time.After(20 * time.Second)
// wait for something to happen...
for {
select {
case info := <-waitForConnectionChan:
fmt.Println("CONN INFO READ CHANNLEEEEE", info)
if info.Err != nil {
return newProcessResultWithError(errors.Wrap(err, "ngrok StartProcess() cmd.StderrPipe()"), "", "")
}
n.pid = cmd.Process.Pid
return newProcessResultWithUrl(info.PublicUrl)
case errResult := <-errorChan:
fmt.Println("ERRROR READ CHANNLEEEEE", err)
return newProcessResultWithError(errResult.err, "", errResult.errCode)
case <-timeoutChan:
fmt.Println("TIMEOUT READ CHANNLEEEEE")
return newProcessResultWithError(errors.New("Ngrok start was timeouted"), "", "ERR_NGROK_START_TIMEOUT")
default:
time.Sleep(500 * time.Millisecond)
fmt.Println("DEFAULT CASEEEE")
}
}
}
func (n *NgrokService) KillProcess() error {
if n.pid == 0 {
return nil
}
pgid, err := syscall.Getpgid(n.pid)
if err != nil {
return err
}
err = syscall.Kill(-pgid, 15) // note the minus sign
if err != nil {
return err
}
n.pid = 0
return nil
}
func (n *NgrokService) SetAuthToken() error {
ngrokPath, _ := n.IsNgrokInstalled()
args := []string{
"authtoken", appState.Config.PublicServer.Ngrok.Authtoken,
}
cmd := exec.Command(
ngrokPath,
args...,
)
err := cmd.Run()
if err != nil {
return errors.Wrap(err, "ngrok SetAuthToken()")
}
return nil
}
func (n *NgrokService) GetPublicUrl(waitForConnectionChan chan NgrokTunnelInfo) {
time.Sleep(3 * time.Second)
tunnels := &ngrokTunnels{}
res, err := http.Get("http://localhost:4040/api/tunnels")
if err != nil {
waitForConnectionChan <- NgrokTunnelInfo{Err: err}
return
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode > 299 {
waitForConnectionChan <- NgrokTunnelInfo{Err: errors.New("Ngrok api/tunnels bad status code")}
return
}
if err := json.NewDecoder(res.Body).Decode(&tunnels); err != nil {
waitForConnectionChan <- NgrokTunnelInfo{Err: err}
return
}
n.publicUrl = tunnels.Tunnels[0].PublicURL
waitForConnectionChan <- NgrokTunnelInfo{PublicUrl: tunnels.Tunnels[0].PublicURL}
return
}
func (n *NgrokService) checkNgrokProcessState() string {
p, err := os.FindProcess(n.pid)
if err != nil {
fmt.Println("checkNgrokProcessState os.FindProcess", err)
return ""
}
// send SIGCONT (on Osx/Linux) which is a safe signal to the process to test if it exists
signalErr := p.Signal(syscall.SIGCONT)
if signalErr != nil {
n.pid = 0
return "stopped"
}
return "running"
}
func (n *NgrokService) MonitorNgrokProcess() {
ticker := time.NewTicker(10 * time.Second)
for {
select {
case <-ticker.C:
status := n.checkNgrokProcessState()
if status == "" {
ticker.Stop()
return
}
if status == "stopped" {
// send event to fe
n.runtime.Events.Emit("ytd:ngrok", NgrokStateEventPayload{Status: NgrokStatusStopped})
SendNotification(n.runtime, NotificationEventPayload{Type: "error", Label: "Public server is crashed"}, appState.isInForeground)
// stop to monitor ngrok status
ticker.Stop()
}
}
}
}
func (n *NgrokService) IsNgrokInstalled() (string, error) {
ngrok, err := exec.LookPath("ngrok")
if n.runtime.System.Platform() == "darwin" && err != nil {
// on darwin check if ngrok is maybe installed by homebrew
// (searching for ngrok only give wrong results if installed with homebrew)
ngrok, err := exec.LookPath("/opt/homebrew/bin/ngrok")
return ngrok, err
}
return ngrok, err
}
func errorReciever(cmd *exec.Cmd, stdoutPipe io.ReadCloser, stderrPipe io.ReadCloser, errorChan chan ngrokCmdError) {
fmt.Println("STARTED ERROR RECEIVER")
stdout, err := ioutil.ReadAll(stdoutPipe)
if err != nil {
errorChan <- ngrokCmdError{err: errors.Wrap(err, "errorReciever ioutil.ReadAll(stdoutPipe)")}
return
}
err = cmd.Wait()
if err != nil {
errorChan <- ngrokCmdError{err: errors.Wrap(err, "errorReceiver() cmd.Wait()"), errCode: extractErrorCode(string(stdout))}
}
// close the channel before ending the goroutine.
close(errorChan)
return
}
func extractErrorCode(output string) string {
if strings.Contains(output, "ERR_NGROK") {
re := regexp.MustCompile("ERR_NGROK_([0-9].*)")
errCode := re.FindString(output)
return strings.TrimSpace(errCode)
}
return ""
}