/
functions.go
165 lines (142 loc) · 4.13 KB
/
functions.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
package main
import (
"time"
"github.com/albertrdixon/gearbox/logger"
"github.com/albertrdixon/gearbox/process"
"github.com/albertrdixon/transmon/config"
"github.com/albertrdixon/transmon/pia"
"github.com/albertrdixon/transmon/transmission"
"github.com/albertrdixon/transmon/vpn"
"github.com/cenkalti/backoff"
"golang.org/x/net/context"
)
func portCheck(t *process.Process, c *config.Config, ctx context.Context) error {
if transmission.
NewRawClient(c.Transmission.URL.String(), c.Transmission.User, c.Transmission.Pass).
CheckPort() {
return nil
}
logger.Infof("Transmission port not open, stopping transmission")
t.Stop()
ip, er := getIP(c.OpenVPN.Tun, c.Timeout.Duration, ctx)
if er != nil {
return er
}
logger.Infof("New bind ip: (%s) %s", c.OpenVPN.Tun, ip)
port, er := getPort(ip, c.PIA.User, c.PIA.Pass, c.PIA.ClientID, c.Timeout.Duration, ctx)
if er != nil {
return er
}
logger.Infof("New peer port: %d", port)
if er := transmission.UpdateSettings(c.Transmission.Config, ip, port); er != nil {
return er
}
logger.Infof("Starting transmission")
go t.ExecuteAndRestart(ctx)
return nil
}
func portUpdate(c *config.Config, ctx context.Context) error {
ip, er := getIP(c.OpenVPN.Tun, c.Timeout.Duration, ctx)
if er != nil || ctx.Err() != nil {
return er
}
logger.Infof("New bind ip: (%s) %s", c.OpenVPN.Tun, ip)
port, er := getPort(ip, c.PIA.User, c.PIA.Pass, c.PIA.ClientID, c.Timeout.Duration, ctx)
if er != nil || ctx.Err() != nil {
return er
}
logger.Infof("New peer port: %d", port)
notify := func(e error, w time.Duration) {
logger.Debugf("Failed to update transmission port: %v", er)
}
operation := func() error {
select {
default:
return transmission.
NewRawClient(c.Transmission.URL.String(), c.Transmission.User, c.Transmission.Pass).
UpdatePort(port)
case <-ctx.Done():
return nil
}
}
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = c.Timeout.Duration
return backoff.RetryNotify(operation, b, notify)
}
func restartProcesses(t, v *process.Process, c *config.Config, ctx context.Context) error {
var (
notify = func(e error, t time.Duration) {
logger.Errorf("Failed to restart processes (retry in %v): %v", t, e)
}
operation = func() error {
t.Stop()
v.Stop()
return startProcesses(t, v, c, ctx)
}
b = backoff.NewExponentialBackOff()
)
b.MaxElapsedTime = 30 * time.Minute
b.MaxInterval = 10 * time.Second
return backoff.RetryNotify(operation, b, notify)
}
func startProcesses(t, v *process.Process, c *config.Config, ctx context.Context) error {
t.SetUser(uint32(c.Transmission.UID), uint32(c.Transmission.GID))
logger.Infof("Starting openvpn")
go v.ExecuteAndRestart(ctx)
ip, er := getIP(c.OpenVPN.Tun, c.Timeout.Duration, ctx)
if er != nil {
return er
}
logger.Infof("New bind ip: (%s) %s", c.OpenVPN.Tun, ip)
port, er := getPort(ip, c.PIA.User, c.PIA.Pass, c.PIA.ClientID, c.Timeout.Duration, ctx)
if er != nil {
return er
}
logger.Infof("New peer port: %d", port)
if er := transmission.UpdateSettings(c.Transmission.Config, ip, port); er != nil {
return er
}
logger.Infof("Starting transmission")
go t.ExecuteAndRestart(ctx)
return nil
}
func getPort(ip, user, pass, id string, timeout time.Duration, c context.Context) (int, error) {
var port int
notify := func(e error, w time.Duration) {
logger.Errorf("Failed to get port from PIA (retry in %v): %v", w, e)
}
fn := func() error {
select {
default:
p, er := pia.RequestPort(ip, user, pass, id)
if er != nil {
return er
}
port = p
return nil
case <-c.Done():
return nil
}
}
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = timeout
return port, backoff.RetryNotify(fn, b, notify)
}
func getIP(dev string, timeout time.Duration, c context.Context) (string, error) {
var address string
notify := func(e error, w time.Duration) {
logger.Errorf("Failed to get IP for %q (retry in %v): %v", dev, w, e)
}
fn := func() (er error) {
select {
default:
address, er = vpn.FindIP(dev)
return
case <-c.Done():
return
}
}
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = timeout
return address, backoff.RetryNotify(fn, b, notify)
}