-
Notifications
You must be signed in to change notification settings - Fork 6
/
service.go
204 lines (173 loc) · 5.3 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
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
package datachannel
//
// OpenVPN data channel
//
import (
"encoding/hex"
"fmt"
"sync"
"github.com/ooni/minivpn/internal/model"
"github.com/ooni/minivpn/internal/session"
"github.com/ooni/minivpn/internal/workers"
"github.com/ooni/minivpn/pkg/config"
)
var (
serviceName = "datachannel"
)
// Service is the datachannel service. Make sure you initialize
// the channels before invoking [Service.StartWorkers].
type Service struct {
// MuxerToData moves packets up to us
MuxerToData chan *model.Packet
// DataOrControlToMuxer is a shared channel to write packets to the muxer layer below
DataOrControlToMuxer *chan *model.Packet
// TUNToData moves bytes down from the TUN layer above
TUNToData chan []byte
// DataToTUN moves bytes up from us to the TUN layer above us
DataToTUN chan []byte
// KeyReady is where the TLSState layer passes us any new keys
KeyReady chan *session.DataChannelKey
}
// StartWorkers starts the data-channel workers.
//
// We start three workers:
//
// 1. moveUpWorker BLOCKS on dataPacketUp to read a packet coming from the muxer and
// eventually BLOCKS on tunUp to deliver it;
//
// 2. moveDownWorker BLOCKS on tunDown to read a packet and
// eventually BLOCKS on dataOrControlToMuxer to deliver it;
//
// 3. keyWorker BLOCKS on keyUp to read a dataChannelKey and
// initializes the internal state with the resulting key;
func (s *Service) StartWorkers(
config *config.Config,
workersManager *workers.Manager,
sessionManager *session.Manager,
) {
dc, err := NewDataChannelFromOptions(config.Logger(), config.OpenVPNOptions(), sessionManager)
if err != nil {
config.Logger().Warnf("cannot initialize channel %v", err)
return
}
ws := &workersState{
dataChannel: dc,
dataOrControlToMuxer: *s.DataOrControlToMuxer,
dataToTUN: s.DataToTUN,
keyReady: s.KeyReady,
logger: config.Logger(),
muxerToData: s.MuxerToData,
sessionManager: sessionManager,
tunToData: s.TUNToData,
workersManager: workersManager,
}
firstKeyReady := make(chan any)
workersManager.StartWorker(ws.moveUpWorker)
workersManager.StartWorker(func() { ws.moveDownWorker(firstKeyReady) })
workersManager.StartWorker(func() { ws.keyWorker(firstKeyReady) })
}
// workersState contains the data channel state.
type workersState struct {
dataChannel *DataChannel
dataOrControlToMuxer chan<- *model.Packet
dataToTUN chan<- []byte
keyReady <-chan *session.DataChannelKey
logger model.Logger
muxerToData <-chan *model.Packet
sessionManager *session.Manager
tunToData <-chan []byte
workersManager *workers.Manager
}
// moveDownWorker moves packets down the stack. It will BLOCK on PacketDown
func (ws *workersState) moveDownWorker(firstKeyReady <-chan any) {
workerName := serviceName + ":moveDownWorker"
defer func() {
ws.workersManager.OnWorkerDone(workerName)
ws.workersManager.StartShutdown()
}()
ws.logger.Debugf("%s: started", workerName)
select {
// wait for the first key to be ready
case <-firstKeyReady:
for {
select {
case data := <-ws.tunToData:
// TODO: writePacket should get the ACTIVE KEY (verify this)
packet, err := ws.dataChannel.writePacket(data)
if err != nil {
ws.logger.Warnf("error encrypting: %v", err)
continue
}
select {
case ws.dataOrControlToMuxer <- packet:
case <-ws.workersManager.ShouldShutdown():
return
}
case <-ws.workersManager.ShouldShutdown():
return
}
}
case <-ws.workersManager.ShouldShutdown():
return
}
}
// moveUpWorker moves packets up the stack
func (ws *workersState) moveUpWorker() {
workerName := fmt.Sprintf("%s: moveUpWorker", serviceName)
defer func() {
ws.workersManager.OnWorkerDone(workerName)
ws.workersManager.StartShutdown()
}()
ws.logger.Debugf("%s: started", workerName)
for {
select {
// TODO: opportunistically try to kill lame duck
case pkt := <-ws.muxerToData:
// TODO(ainghazal): factor out as handler function
decrypted, err := ws.dataChannel.readPacket(pkt)
if err != nil {
ws.logger.Warnf("error decrypting: %v", err)
continue
}
if len(decrypted) == 16 {
// TODO: should reply to this keepalive ping
// "2a 18 7b f3 64 1e b4 cb 07 ed 2d 0a 98 1f c7 48"
fmt.Println(hex.Dump(decrypted))
continue
}
// POSSIBLY BLOCK writing up towards TUN
ws.dataToTUN <- decrypted
case <-ws.workersManager.ShouldShutdown():
return
}
}
}
// keyWorker receives notifications from key ready
func (ws *workersState) keyWorker(firstKeyReady chan<- any) {
workerName := fmt.Sprintf("%s: keyWorker", serviceName)
defer func() {
ws.workersManager.OnWorkerDone(workerName)
ws.workersManager.StartShutdown()
}()
ws.logger.Debugf("%s: started", workerName)
once := &sync.Once{}
for {
select {
case key := <-ws.keyReady:
// TODO(ainghazal): thread safety here - need to lock.
// When we actually get to key rotation, we need to add locks.
// Use RW lock, reader locks.
err := ws.dataChannel.setupKeys(key)
if err != nil {
ws.logger.Warnf("error on key derivation: %v", err)
continue
}
ws.sessionManager.SetNegotiationState(model.S_GENERATED_KEYS)
once.Do(func() {
close(firstKeyReady)
})
case <-ws.workersManager.ShouldShutdown():
return
}
}
}