This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
cbor_plugins.go
283 lines (242 loc) · 8.64 KB
/
cbor_plugins.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
// cbor_plugins.go - cbor plugin system for kaetzchen services
// Copyright (C) 2018 David Stainton.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package kaetzchen implements support for provider side auto-responder
// agents.
package kaetzchen
import (
"errors"
"fmt"
"sync"
"time"
"github.com/katzenpost/core/monotime"
sConstants "github.com/katzenpost/core/sphinx/constants"
"github.com/katzenpost/core/worker"
"github.com/katzenpost/server/cborplugin"
"github.com/katzenpost/server/internal/glue"
"github.com/katzenpost/server/internal/packet"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/text/secure/precis"
"gopkg.in/eapache/channels.v1"
"gopkg.in/op/go-logging.v1"
)
// PluginChans maps from Recipient ID to channel.
type PluginChans = map[[sConstants.RecipientIDLength]byte]*channels.InfiniteChannel
// PluginName is the name of a plugin.
type PluginName = string
// PluginParameters maps from parameter key to value.
type PluginParameters = map[PluginName]interface{}
// ServiceMap maps from plugin name to plugin parameters
// and is used by Mix Descriptors which describe Providers
// with plugins. Each plugin can optionally set one or more
// parameters.
type ServiceMap = map[PluginName]PluginParameters
// CBORPluginWorker is similar to Kaetzchen worker but uses
// CBOR over HTTP over UNIX domain socket to talk to plugins.
type CBORPluginWorker struct {
sync.Mutex
worker.Worker
glue glue.Glue
log *logging.Logger
haltOnce sync.Once
pluginChans PluginChans
clients []*cborplugin.Client
}
// OnKaetzchen enqueues the pkt for processing by our thread pool of plugins.
func (k *CBORPluginWorker) OnKaetzchen(pkt *packet.Packet) {
handlerCh, ok := k.pluginChans[pkt.Recipient.ID]
if !ok {
k.log.Debugf("Failed to find handler. Dropping Kaetzchen request: %v", pkt.ID)
return
}
handlerCh.In() <- pkt
}
func (k *CBORPluginWorker) worker(recipient [sConstants.RecipientIDLength]byte, pluginClient cborplugin.ServicePlugin) {
// Kaetzchen delay is our max dwell time.
maxDwell := time.Duration(k.glue.Config().Debug.KaetzchenDelay) * time.Millisecond
defer k.haltOnce.Do(k.haltAllClients)
handlerCh, ok := k.pluginChans[recipient]
if !ok {
k.log.Debugf("Failed to find handler. Dropping Kaetzchen request: %v", recipient)
kaetzchenRequestsDropped.Inc()
return
}
ch := handlerCh.Out()
for {
var pkt *packet.Packet
select {
case <-k.HaltCh():
k.log.Debugf("Terminating gracefully.")
return
case e := <-ch:
pkt = e.(*packet.Packet)
if dwellTime := monotime.Now() - pkt.DispatchAt; dwellTime > maxDwell {
k.log.Debugf("Dropping packet: %v (Spend %v in queue)", pkt.ID, dwellTime)
packetsDropped.Inc()
pkt.Dispose()
continue
}
}
k.processKaetzchen(pkt, pluginClient)
kaetzchenRequests.Inc()
}
}
func (k *CBORPluginWorker) haltAllClients() {
k.log.Debug("Halting plugin clients.")
for _, client := range k.clients {
go client.Halt()
}
}
func (k *CBORPluginWorker) processKaetzchen(pkt *packet.Packet, pluginClient cborplugin.ServicePlugin) {
kaetzchenRequestsTimer = prometheus.NewTimer(kaetzchenRequestsDuration)
defer kaetzchenRequestsTimer.ObserveDuration()
defer pkt.Dispose()
ct, surb, err := packet.ParseForwardPacket(pkt)
if err != nil {
k.log.Debugf("Dropping Kaetzchen request: %v (%v)", pkt.ID, err)
kaetzchenRequestsDropped.Inc()
return
}
resp, err := pluginClient.OnRequest(&cborplugin.Request{
ID: pkt.ID,
Payload: ct,
HasSURB: surb != nil,
})
switch err {
case nil:
case ErrNoResponse:
k.log.Debugf("Processed Kaetzchen request: %v (No response)", pkt.ID)
kaetzchenRequests.Inc()
return
default:
k.log.Debugf("Failed to handle Kaetzchen request: %v (%v), response: %s", pkt.ID, err, resp)
return
}
if len(resp) == 0 {
k.log.Debugf("No reply from Kaetzchen: %v", pkt.ID)
return
}
// Iff there is a SURB, generate a SURB-Reply and schedule.
if surb != nil {
// Prepend the response header.
resp = append([]byte{0x01, 0x00}, resp...)
respPkt, err := packet.NewPacketFromSURB(pkt, surb, resp)
if err != nil {
k.log.Debugf("Failed to generate SURB-Reply: %v (%v)", pkt.ID, err)
return
}
k.log.Debugf("Handing off newly generated SURB-Reply: %v (Src:%v)", respPkt.ID, pkt.ID)
k.glue.Scheduler().OnPacket(respPkt)
return
}
k.log.Debugf("No SURB provided: %v", pkt.ID)
}
// KaetzchenForPKI returns the plugins Parameters map for publication in the PKI doc.
func (k *CBORPluginWorker) KaetzchenForPKI() ServiceMap {
s := make(ServiceMap)
for _, k := range k.clients {
capa := k.Capability()
if _, ok := s[capa]; ok {
// skip adding twice
continue
}
params := make(PluginParameters)
p := k.GetParameters()
if p != nil {
for key, value := range *p {
params[key] = value
}
}
s[capa] = params
}
return s
}
// IsKaetzchen returns true if the given recipient is one of our workers.
func (k *CBORPluginWorker) IsKaetzchen(recipient [sConstants.RecipientIDLength]byte) bool {
_, ok := k.pluginChans[recipient]
return ok
}
func (k *CBORPluginWorker) launch(command, capability, endpoint string, args []string) (*cborplugin.Client, error) {
k.log.Debugf("Launching plugin: %s", command)
plugin := cborplugin.New(command, capability, endpoint, k.glue.LogBackend())
err := plugin.Start(command, args)
return plugin, err
}
// NewCBORPluginWorker returns a new CBORPluginWorker
func NewCBORPluginWorker(glue glue.Glue) (*CBORPluginWorker, error) {
kaetzchenWorker := CBORPluginWorker{
glue: glue,
log: glue.LogBackend().GetLogger("CBOR plugin worker"),
pluginChans: make(PluginChans),
clients: make([]*cborplugin.Client, 0),
}
capaMap := make(map[string]bool)
for _, pluginConf := range glue.Config().Provider.CBORPluginKaetzchen {
kaetzchenWorker.log.Noticef("Configuring plugin handler for %s", pluginConf.Capability)
// Ensure no duplicates.
capa := pluginConf.Capability
if capa == "" {
return nil, errors.New("kaetzchen plugin capability cannot be empty string")
}
if pluginConf.Disable {
kaetzchenWorker.log.Noticef("Skipping disabled Kaetzchen: '%v'.", capa)
continue
}
if capaMap[capa] {
return nil, fmt.Errorf("provider: Kaetzchen '%v' registered more than once", capa)
}
// Sanitize the endpoint.
if pluginConf.Endpoint == "" {
return nil, fmt.Errorf("provider: Kaetzchen: '%v' provided no endpoint", capa)
} else if epNorm, err := precis.UsernameCaseMapped.String(pluginConf.Endpoint); err != nil {
return nil, fmt.Errorf("provider: Kaetzchen: '%v' invalid endpoint: %v", capa, err)
} else if epNorm != pluginConf.Endpoint {
return nil, fmt.Errorf("provider: Kaetzchen: '%v' invalid endpoint, not normalized", capa)
}
rawEp := []byte(pluginConf.Endpoint)
if len(rawEp) == 0 || len(rawEp) > sConstants.RecipientIDLength {
return nil, fmt.Errorf("provider: Kaetzchen: '%v' invalid endpoint, length out of bounds", capa)
}
// Add an infinite channel for this plugin.
var endpoint [sConstants.RecipientIDLength]byte
copy(endpoint[:], rawEp)
kaetzchenWorker.pluginChans[endpoint] = channels.NewInfiniteChannel()
// Start the plugin clients.
for i := 0; i < pluginConf.MaxConcurrency; i++ {
kaetzchenWorker.log.Noticef("Starting Kaetzchen plugin client: %s %d", capa, i)
var args []string
if len(pluginConf.Config) > 0 {
args = []string{}
for key, val := range pluginConf.Config {
args = append(args, fmt.Sprintf("-%s", key), val.(string))
}
}
pluginClient, err := kaetzchenWorker.launch(pluginConf.Command, pluginConf.Capability, pluginConf.Endpoint, args)
if err != nil {
kaetzchenWorker.log.Error("Failed to start a plugin client: %s", err)
return nil, err
}
// Accumulate a list of all clients to facilitate clean shutdown.
kaetzchenWorker.clients = append(kaetzchenWorker.clients, pluginClient)
// Start the workers _after_ we have added all of the entries to pluginChans
// otherwise the worker() goroutines race this thread.
defer kaetzchenWorker.Go(func() {
kaetzchenWorker.worker(endpoint, pluginClient)
})
}
capaMap[capa] = true
}
return &kaetzchenWorker, nil
}