-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.go
More file actions
258 lines (225 loc) · 6.68 KB
/
Copy pathruntime.go
File metadata and controls
258 lines (225 loc) · 6.68 KB
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
package vmflow
import (
"context"
"crypto/tls"
"sync"
"github.com/cloudapp3/vmflow/engine"
)
// CertProvider is the certificate provider interface used by HTTPS rules.
// It is an alias of engine.CertProvider so embedders can depend on the
// top-level vmflow package without importing engine for certificate wiring.
type CertProvider = engine.CertProvider
// Runtime is a small embeddable facade over the forwarding engine.
//
// Use Runtime when vmflow is embedded into a larger control plane such as
// vmpulse. The larger application remains responsible for configuration,
// persistence, authentication, and business logic; Runtime owns only the
// in-process forwarding manager and real-time counters.
type Runtime struct {
manager *engine.Manager
collector *engine.Collector
mu sync.RWMutex
closed bool
}
// Options controls construction of an embedded Runtime.
type Options struct {
// Collector can be supplied when the host application wants to share or wrap
// the vmflow in-memory traffic counters. If nil, a new collector is created.
Collector *engine.Collector
// CertProvider is reserved for a future HTTPS rule implementation. Current
// builds accept only TCP, UDP, and tcp+udp rules.
CertProvider CertProvider
// UDPMaxSessions caps active UDP sessions across all rules in this runtime.
// Non-positive values use engine.DefaultUDPGlobalMaxSessions; larger values
// are clamped to engine.MaxUDPGlobalMaxSessions.
UDPMaxSessions int
}
// NewRuntime creates a new embeddable forwarding runtime.
func NewRuntime(opts Options) *Runtime {
collector := opts.Collector
if collector == nil {
collector = engine.NewCollector()
}
managerOpts := engine.ManagerOptions{UDPMaxSessions: opts.UDPMaxSessions}
var manager *engine.Manager
if opts.CertProvider != nil {
manager = engine.NewManagerWithCertOptions(collector, opts.CertProvider, managerOpts)
} else {
manager = engine.NewManagerWithOptions(collector, managerOpts)
}
return &Runtime{manager: manager, collector: collector}
}
// New creates a runtime with default options.
func New() *Runtime {
return NewRuntime(Options{})
}
// Manager returns the underlying engine manager for advanced use cases.
// Prefer the facade methods on Runtime when possible.
func (r *Runtime) Manager() *engine.Manager {
if r == nil {
return nil
}
r.mu.RLock()
defer r.mu.RUnlock()
return r.manager
}
// Collector returns the underlying in-memory collector.
func (r *Runtime) Collector() *engine.Collector {
if r == nil {
return nil
}
r.mu.RLock()
defer r.mu.RUnlock()
return r.collector
}
// Apply applies a full replacement snapshot. This is the usual mode for an
// embedded control plane that calculates desired state from its own database.
func (r *Runtime) Apply(rules []engine.Rule) engine.ApplyResult {
return r.ApplySnapshot(rules, engine.ApplySnapshotOptions{ReplaceAll: true})
}
// ApplySnapshot applies rules with explicit options.
func (r *Runtime) ApplySnapshot(rules []engine.Rule, opts engine.ApplySnapshotOptions) engine.ApplyResult {
manager, ok := r.activeManager()
if !ok {
return closedApplyResult(rules)
}
return manager.ApplySnapshot(rules, opts)
}
// StartRule starts a single rule without replacing other rules.
func (r *Runtime) StartRule(rule engine.Rule) error {
manager, ok := r.activeManager()
if !ok {
return ErrRuntimeClosed
}
return manager.StartRule(rule)
}
// RestartRule restarts or starts a single rule.
func (r *Runtime) RestartRule(rule engine.Rule) error {
manager, ok := r.activeManager()
if !ok {
return ErrRuntimeClosed
}
return manager.RestartRule(rule)
}
// StopRule stops a single rule and keeps its counters.
func (r *Runtime) StopRule(ruleID string) {
manager, ok := r.activeManager()
if !ok {
return
}
manager.StopRule(ruleID)
}
// RemoveRule stops a single rule and removes its counters.
func (r *Runtime) RemoveRule(ruleID string) {
manager, ok := r.activeManager()
if !ok {
return
}
manager.RemoveRule(ruleID)
}
// StopAll stops all running rules and keeps the runtime reusable.
func (r *Runtime) StopAll() {
manager, ok := r.activeManager()
if !ok {
return
}
manager.StopAll()
}
// Close stops all rules and marks the runtime as closed.
func (r *Runtime) Close() error {
if r == nil {
return nil
}
r.mu.Lock()
if r.closed {
r.mu.Unlock()
return nil
}
r.closed = true
manager := r.manager
r.mu.Unlock()
if manager != nil {
manager.StopAll()
}
return nil
}
// Shutdown stops the runtime. The current implementation stops synchronously;
// the context is accepted to keep the embedding API forward-compatible with
// future graceful drain support.
func (r *Runtime) Shutdown(ctx context.Context) error {
_ = ctx
return r.Close()
}
// RunningRules returns the currently running rules sorted by rule_id.
func (r *Runtime) RunningRules() []engine.Rule {
manager, ok := r.activeManager()
if !ok {
return nil
}
return manager.RunningRules()
}
// RunningCount returns the number of currently running rules.
func (r *Runtime) RunningCount() int {
manager, ok := r.activeManager()
if !ok {
return 0
}
return manager.RunningCount()
}
// Snapshot returns one rule's in-memory traffic snapshot.
func (r *Runtime) Snapshot(ruleID string) engine.TrafficSnapshot {
manager, ok := r.activeManager()
if !ok {
return engine.TrafficSnapshot{RuleID: ruleID}
}
return manager.Snapshot(ruleID)
}
// SnapshotAll returns all in-memory traffic snapshots sorted by rule_id.
func (r *Runtime) SnapshotAll() []engine.TrafficSnapshot {
manager, ok := r.activeManager()
if !ok {
return nil
}
return manager.SnapshotAll()
}
func (r *Runtime) activeManager() (*engine.Manager, bool) {
if r == nil {
return nil, false
}
r.mu.RLock()
defer r.mu.RUnlock()
if r.closed || r.manager == nil {
return nil, false
}
return r.manager, true
}
func closedApplyResult(rules []engine.Rule) engine.ApplyResult {
result := engine.ApplyResult{
FailedRules: len(rules),
TotalRules: len(rules),
}
if len(rules) == 0 {
return result
}
result.Items = make([]engine.ApplyItemResult, 0, len(rules))
for _, rule := range rules {
rule = rule.Standardize()
result.Items = append(result.Items, engine.ApplyItemResult{
RuleID: rule.RuleID,
Revision: rule.Revision,
Action: engine.ApplyActionFailed,
Status: "failed",
Error: ErrRuntimeClosed.Error(),
})
}
return result
}
// Ensure top-level CertProvider stays compatible with engine.CertProvider.
var _ CertProvider = certProviderFunc(nil)
type certProviderFunc func(*tls.ClientHelloInfo) (*tls.Certificate, error)
func (f certProviderFunc) GetCertificate(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
return f(hello)
}
func (f certProviderFunc) Obtain(ctx context.Context, domains []string) error {
return nil
}