-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlocal.go
97 lines (84 loc) · 2.23 KB
/
local.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
package wormhole
import (
"flag"
"net"
"strings"
"time"
"github.com/jpillora/backoff"
"github.com/sirupsen/logrus"
"github.com/superfly/wormhole/config"
"github.com/superfly/wormhole/local"
)
const (
localServerRetry = 200 * time.Millisecond // how often to retry local server until ready
minWormholeBackoff = 100 * time.Millisecond // min backoff between retries to wormhole server
maxWormholeBackoff = 5 * time.Second // max backoff between retries to wormhole server
)
// StartLocal ...
func StartLocal(cfg *config.ClientConfig) {
log := cfg.Logger.WithFields(logrus.Fields{"prefix": "wormhole"})
release, err := computeRelease(cfg.ReleaseID, cfg.ReleaseDesc, cfg.ReleaseBranch)
if err != nil {
log.Warn(err)
}
log.Debugln("Computed release:", release)
var handler local.ConnectionHandler
switch cfg.Protocol {
case config.SSH:
handler, err = local.NewSSHHandler(cfg, release)
if err != nil {
log.Fatal(err)
}
case config.TCP:
handler, err = local.NewTCPHandler(cfg, release)
if err != nil {
log.Fatal(err)
}
case config.HTTP2:
handler, err = local.NewHTTP2Handler(cfg, release)
if err != nil {
log.Fatal(err)
}
default:
log.Fatal("Unknown wormhole transport layer protocol selected.")
}
args := flag.Args()
if len(args) > 0 {
cmd := strings.Join(args, " ")
process := NewProcess(cfg.Logger, cmd, handler)
err := process.Run()
if err != nil {
log.Fatalf("Error running program: %s", err.Error())
return
}
}
log.Infoln("Attempting to connect to local server on:", cfg.LocalEndpoint)
for {
conn, err := net.Dial("tcp", cfg.LocalEndpoint)
if conn != nil {
conn.Close()
}
if err == nil {
log.Infoln("Local server is ready on:", cfg.LocalEndpoint)
break
}
time.Sleep(localServerRetry)
}
b := &backoff.Backoff{
Min: minWormholeBackoff,
Max: maxWormholeBackoff,
Jitter: true,
}
log.Infoln("Attempting to connect to wormhole server on:", cfg.RemoteEndpoint)
for {
err := handler.ListenAndServe()
if err != nil {
d := b.Duration()
log.Errorf("Failed to connect to wormhole server: %s. Will try again in %s", err.Error(), d.String())
time.Sleep(d)
continue
}
log.Debug("Handler exited with no errors. Starting again")
b.Reset()
}
}