-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenvpn.go
291 lines (249 loc) · 6.37 KB
/
openvpn.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
// Package openvpn provides an API for connecting to, sending commands to, and receiving messages from an OpenVPN management socket.
package openvpn
import (
"bufio"
"encoding/base64"
"fmt"
"io"
"net"
"strings"
"github.com/cedws/openvpn-mgmt-go/message"
)
const (
messageClientConnect = "CLIENT:CONNECT"
messageClientReauth = "CLIENT:REAUTH"
messageClientEnv = "CLIENT:ENV"
messageClientEstablished = "CLIENT:ESTABLISHED"
messageClientDisconnect = "CLIENT:DISCONNECT"
messageEcho = "ECHO"
messageFatal = "FATAL"
messageHold = "HOLD"
messageInfo = "INFO"
messageLog = "LOG"
)
var (
ErrSCRV1Prefix = fmt.Errorf("invalid SCRV1 string; no prefix")
ErrSCRV1Fields = fmt.Errorf("invalid SCRV1 string; expected 3 fields")
ErrSCRV1InvalidField = fmt.Errorf("invalid SCRV1 string; field not base64")
)
type (
OnClientConnectFunc func(message.ClientConnect)
OnClientReauthFunc func(message.ClientReauth)
OnClientEstablishedFunc func(message.ClientEstablished)
OnClientDisconnectFunc func(message.ClientDisconnect)
OnEchoFunc func(message.Echo)
OnFatalFunc func(message.Fatal)
OnHoldFunc func(message.Hold)
OnInfoFunc func(message.Info)
OnLogFunc func(message.Log)
)
type Command interface {
String() string
}
type Event interface {
Parse(string) error
}
type SCRV1 struct {
Password string
ChallengeResponse string
}
func (s *SCRV1) DecodeString(line string) error {
if !strings.HasPrefix(line, "SCRV1:") {
return ErrSCRV1Prefix
}
split := strings.Split(line, ":")
if len(split) != 3 {
return ErrSCRV1Fields
}
password, err := base64.StdEncoding.DecodeString(split[1])
if err != nil {
return ErrSCRV1InvalidField
}
response, err := base64.StdEncoding.DecodeString(split[2])
if err != nil {
return ErrSCRV1InvalidField
}
s.Password = string(password)
s.ChallengeResponse = string(response)
return nil
}
type Socket struct {
onClientConnect OnClientConnectFunc
onClientReauth OnClientReauthFunc
onClientEstablished OnClientEstablishedFunc
onClientDisconnect OnClientDisconnectFunc
onEcho OnEchoFunc
onFatal OnFatalFunc
onHold OnHoldFunc
onInfo OnInfoFunc
onLog OnLogFunc
conn io.ReadWriteCloser
messageCh chan any
}
func DialUnix(addr string) (*Socket, error) {
sock, err := net.Dial("unix", addr)
if err != nil {
return nil, err
}
return &Socket{
conn: sock,
messageCh: make(chan any),
}, nil
}
func DialTCP(addr string) (*Socket, error) {
sock, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
return &Socket{
conn: sock,
messageCh: make(chan any),
}, nil
}
func parse[T any, U interface {
*T
Event
}](line string) U {
u := U(new(T))
_ = u.Parse(line)
return u
}
func readEnv(messageCh <-chan any) map[string]string {
envMap := make(map[string]string)
for m := range messageCh {
if envVar, ok := m.(*message.ClientEnvVar); ok {
if envVar.End {
break
}
envMap[envVar.Key] = envVar.Value
} else {
panic("expected to read environment")
}
}
return envMap
}
func (s *Socket) OnClientConnect(handleFunc OnClientConnectFunc) {
s.onClientConnect = handleFunc
}
func (s *Socket) OnClientReauth(handleFunc OnClientReauthFunc) {
s.onClientReauth = handleFunc
}
func (s *Socket) OnClientEstablished(handleFunc OnClientEstablishedFunc) {
s.onClientEstablished = handleFunc
}
func (s *Socket) OnClientDisconnect(handleFunc OnClientDisconnectFunc) {
s.onClientDisconnect = handleFunc
}
func (s *Socket) OnEcho(handleFunc OnEchoFunc) {
s.onEcho = handleFunc
}
func (s *Socket) OnFatal(handleFunc OnFatalFunc) {
s.onFatal = handleFunc
}
func (s *Socket) OnHold(handleFunc OnHoldFunc) {
s.onHold = handleFunc
}
func (s *Socket) OnInfo(handleFunc OnInfoFunc) {
s.onInfo = handleFunc
}
func (s *Socket) OnLog(handleFunc OnLogFunc) {
s.onLog = handleFunc
}
func (s *Socket) Start() {
go s.read()
for m := range s.messageCh {
if m == nil {
continue
}
switch m := m.(type) {
case *message.ClientConnect:
m.Env = readEnv(s.messageCh)
if s.onClientConnect != nil {
go s.onClientConnect(*m)
}
case *message.ClientDisconnect:
m.Env = readEnv(s.messageCh)
if s.onClientDisconnect != nil {
go s.onClientDisconnect(*m)
}
case *message.ClientEstablished:
m.Env = readEnv(s.messageCh)
if s.onClientEstablished != nil {
go s.onClientEstablished(*m)
}
case *message.ClientReauth:
m.Env = readEnv(s.messageCh)
if s.onClientReauth != nil {
go s.onClientReauth(*m)
}
case *message.Echo:
if s.onEcho != nil {
go s.onEcho(*m)
}
case *message.Fatal:
if s.onFatal != nil {
go s.onFatal(*m)
}
case *message.Hold:
if s.onHold != nil {
go s.onHold(*m)
}
case *message.Info:
if s.onInfo != nil {
go s.onInfo(*m)
}
case *message.Log:
if s.onLog != nil {
go s.onLog(*m)
}
case message.ClientEnvVar:
panic("got unexpected client env var message")
}
}
}
func (s *Socket) read() {
scanner := bufio.NewScanner(s.conn)
for scanner.Scan() {
line := scanner.Text()
if len(line) < 2 || line[0] != '>' {
continue
}
n := line[1:]
args := strings.SplitN(n, ",", 2)
if len(args) < 1 {
continue
}
switch source := args[0]; source {
case messageClientConnect:
s.messageCh <- parse[message.ClientConnect](line)
case messageClientDisconnect:
s.messageCh <- parse[message.ClientDisconnect](line)
case messageClientEstablished:
s.messageCh <- parse[message.ClientEstablished](line)
case messageClientReauth:
s.messageCh <- parse[message.ClientReauth](line)
case messageEcho:
s.messageCh <- parse[message.Echo](line)
case messageFatal:
s.messageCh <- parse[message.Fatal](line)
case messageHold:
s.messageCh <- parse[message.Hold](line)
case messageInfo:
s.messageCh <- parse[message.Info](line)
case messageLog:
s.messageCh <- parse[message.Log](line)
case messageClientEnv:
s.messageCh <- parse[message.ClientEnvVar](line)
}
}
}
func (s *Socket) Close() error {
close(s.messageCh)
return s.conn.Close()
}
func (s *Socket) Dispatch(c Command) error {
if _, err := s.conn.Write([]byte(c.String() + "\n")); err != nil {
return fmt.Errorf("openvpn: error dispatching command: %w", err)
}
return nil
}