-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathziti_init.c
388 lines (316 loc) · 12.6 KB
/
ziti_init.c
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "ziti-nodejs.h"
ziti_context ztx = NULL;
typedef struct {
napi_async_work work;
napi_threadsafe_function tsfn;
int zitiContextEventStatus;
bool on_init_cb_invoked;
} AddonData;
static const char *ALL_CONFIG_TYPES[] = {
"all",
NULL
};
#define ZROK_PROXY_CFG_V1 "zrok.proxy.v1"
#define ZROK_PROXY_CFG_V1_MODEL(XX, ...) \
XX(auth_scheme, model_string, none, auth_scheme, __VA_ARGS__) \
XX(basic_auth, model_string, none, basic_auth, __VA_ARGS__) \
XX(oauth, model_string, none, oauth, __VA_ARGS__)
DECLARE_MODEL(zrok_proxy_cfg_v1, ZROK_PROXY_CFG_V1_MODEL)
/**
* This function is responsible for calling the JavaScript callback function
* that was specified when the ziti_init(...) was called from JavaScript.
*/
static void CallJs(napi_env env, napi_value js_cb, void* context, void* data) {
napi_status status;
// This parameter is not used.
(void) context;
// env and js_cb may both be NULL if Node.js is in its cleanup phase, and
// items are left over from earlier thread-safe calls from the worker thread.
// When env is NULL, we simply skip over the call into Javascript
if (env != NULL) {
napi_value undefined, js_rc;
// Retrieve the JavaScript `undefined` value so we can use it as the `this`
// value of the JavaScript function call.
status = napi_get_undefined(env, &undefined);
// Retrieve the rc created by the worker thread.
int64_t rc = (int64_t)data;
status = napi_create_int64(env, (int64_t)rc, &js_rc);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Failed to napi_create_int64");
}
// Call the JavaScript function and pass it the rc
status = napi_call_function(
env,
undefined,
js_cb,
1,
&js_rc,
NULL
);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Failed to napi_call_function");
}
}
}
/**
*
*/
void on_ziti_init(ziti_context _ztx, int status, void* ctx) {
napi_status nstatus;
ZITI_NODEJS_LOG(DEBUG, "_ztx: %p", _ztx);
// Set the global ztx context variable
ztx = _ztx;
AddonData* addon_data = (AddonData*)ctx;
// Initiate the call into the JavaScript callback.
// The call into JavaScript will not have happened
// when this function returns, but it will be queued.
nstatus = napi_call_threadsafe_function(
addon_data->tsfn,
(void*) (long) status,
napi_tsfn_blocking);
if (nstatus != napi_ok) {
ZITI_NODEJS_LOG(ERROR, "Unable to napi_call_threadsafe_function");
}
}
/**
*
*/
static void on_ziti_event(ziti_context _ztx, const ziti_event_t *event) {
napi_status nstatus;
AddonData* addon_data = (AddonData*)ziti_app_ctx(_ztx);
switch (event->type) {
case ZitiContextEvent:
if (event->ctx.ctrl_status == ZITI_OK) {
const ziti_version *ctrl_ver = ziti_get_controller_version(_ztx);
const ziti_identity *proxy_id = ziti_get_identity(_ztx);
ZITI_NODEJS_LOG(INFO, "controller version = %s(%s)[%s]", ctrl_ver->version, ctrl_ver->revision, ctrl_ver->build_date);
ZITI_NODEJS_LOG(INFO, "identity = <%s>[%s]@%s", proxy_id->name, proxy_id->id, ziti_get_controller(_ztx));
}
else {
ZITI_NODEJS_LOG(ERROR, "Failed to connect to controller: %s", event->ctx.err);
}
addon_data->zitiContextEventStatus = event->ctx.ctrl_status;
break;
case ZitiServiceEvent:
if (event->service.removed != NULL) {
for (ziti_service **sp = event->service.removed; *sp != NULL; sp++) {
// service_check_cb(ztx, *sp, ZITI_SERVICE_UNAVAILABLE, app_ctx);
ZITI_NODEJS_LOG(INFO, "Service removed [%s]", (*sp)->name);
}
}
if (event->service.added != NULL) {
for (ziti_service **sp = event->service.added; *sp != NULL; sp++) {
// service_check_cb(ztx, *sp, ZITI_OK, app_ctx);
ZITI_NODEJS_LOG(INFO, "Service added [%s]", (*sp)->name);
}
}
if (event->service.changed != NULL) {
for (ziti_service **sp = event->service.changed; *sp != NULL; sp++) {
// service_check_cb(ztx, *sp, ZITI_OK, app_ctx);
ZITI_NODEJS_LOG(INFO, "Service changed [%s]", (*sp)->name);
}
}
//
for (int i = 0; event->service.added && event->service.added[i] != NULL; i++) {
ziti_service *s = event->service.added[i];
ziti_intercept_cfg_v1 *intercept = alloc_ziti_intercept_cfg_v1();
ziti_client_cfg_v1 clt_cfg = {
.hostname = {0},
.port = 0
};
zrok_proxy_cfg_v1 zrok_cfg = {
.auth_scheme = "",
.basic_auth = "",
.oauth = ""
};
if (ziti_service_get_config(s, ZITI_INTERCEPT_CFG_V1, intercept, (int (*)(void *, const char *, size_t))parse_ziti_intercept_cfg_v1) == ZITI_OK) {
const ziti_address *range_addr;
MODEL_LIST_FOREACH(range_addr, intercept->addresses) {
ziti_port_range *p;
MODEL_LIST_FOREACH(p, intercept->port_ranges) {
int lowport = p->low;
while (lowport <= p->high) {
track_service_to_hostname(s->name, (char *)range_addr->addr.hostname, lowport);
lowport++;
}
}
}
} else if (ziti_service_get_config(s, ZITI_CLIENT_CFG_V1, &clt_cfg, (int (*)(void *, const char *, unsigned long))parse_ziti_client_cfg_v1) == ZITI_OK) {
track_service_to_hostname(s->name, clt_cfg.hostname.addr.hostname, clt_cfg.port);
} else if (ziti_service_get_config(s, ZROK_PROXY_CFG_V1, &zrok_cfg, (int (*)(void *, const char *, unsigned long))parse_ziti_client_cfg_v1) == ZITI_OK) {
track_service_to_hostname(s->name, s->name, 80);
}
free_ziti_intercept_cfg_v1(intercept);
free(intercept);
}
for (int i = 0; event->service.changed && event->service.changed[i] != NULL; i++) {
ziti_service *s = event->service.changed[i];
ziti_intercept_cfg_v1 *intercept = alloc_ziti_intercept_cfg_v1();
ziti_client_cfg_v1 clt_cfg = {
.hostname = {0},
.port = 0
};
if (ziti_service_get_config(s, ZITI_INTERCEPT_CFG_V1, intercept, (int (*)(void *, const char *, size_t))parse_ziti_intercept_cfg_v1) == ZITI_OK) {
const ziti_address *range_addr;
MODEL_LIST_FOREACH(range_addr, intercept->addresses) {
ziti_port_range *p;
MODEL_LIST_FOREACH(p, intercept->port_ranges) {
int lowport = p->low;
while (lowport <= p->high) {
track_service_to_hostname(s->name, (char *)range_addr->addr.hostname, lowport);
lowport++;
}
}
}
} else if (ziti_service_get_config(s, ZITI_CLIENT_CFG_V1, &clt_cfg, (int (*)(void *, const char *, unsigned long))parse_ziti_client_cfg_v1) == ZITI_OK) {
track_service_to_hostname(s->name, clt_cfg.hostname.addr.hostname, clt_cfg.port);
}
free_ziti_intercept_cfg_v1(intercept);
free(intercept);
}
// Initiate the call into the JavaScript 'on_init' callback, now that we know about all the services
ZITI_NODEJS_LOG(DEBUG, "addon_data->on_init_cb_invoked %o", addon_data->on_init_cb_invoked);
if (!addon_data->on_init_cb_invoked) {
nstatus = napi_call_threadsafe_function(
addon_data->tsfn,
(void*) (long) addon_data->zitiContextEventStatus,
napi_tsfn_blocking);
addon_data->on_init_cb_invoked = true;
}
break;
case ZitiRouterEvent:
switch (event->router.status){
case EdgeRouterConnected:
ZITI_NODEJS_LOG(INFO, "ziti connected to edge router %s\nversion = %s", event->router.name, event->router.version);
break;
case EdgeRouterDisconnected:
ZITI_NODEJS_LOG(INFO, "ziti disconnected from edge router %s", event->router.name);
break;
case EdgeRouterRemoved:
ZITI_NODEJS_LOG(INFO, "ziti removed edge router %s", event->router.name);
break;
case EdgeRouterUnavailable:
ZITI_NODEJS_LOG(INFO, "edge router %s is not available", event->router.name);
break;
case EdgeRouterAdded:
ZITI_NODEJS_LOG(INFO, "edge router %s was added", event->router.name);
break;
}
default:
break;
}
}
/**
*
*/
napi_value _ziti_init(napi_env env, const napi_callback_info info) {
napi_status status;
napi_value jsRetval;
ZITI_NODEJS_LOG(DEBUG, "initializing");
// uv_mbed_set_debug(TRACE, stdout);
size_t argc = 2;
napi_value args[2];
status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Failed to parse arguments");
}
if (argc < 2) {
napi_throw_error(env, "EINVAL", "Too few arguments");
return NULL;
}
// Obtain length of location of identity file
size_t result;
size_t config_path_len;
status = napi_get_value_string_utf8(env, args[0], NULL, 0, &config_path_len);
if (status != napi_ok) {
napi_throw_error(env, "EINVAL", "location of identity file is not a string");
}
// Obtain length of location of identity file
char *config_file_name = calloc(1, config_path_len + 2);
status = napi_get_value_string_utf8(env, args[0], config_file_name, config_path_len + 1, &result);
if (status != napi_ok) {
napi_throw_error(env, "EINVAL", "Failed to obtain location of identity file");
}
ZITI_NODEJS_LOG(DEBUG, "config_file_name: %s", config_file_name);
// Obtain ptr to JS callback function
napi_value js_cb = args[1];
napi_value work_name;
AddonData *addon_data = calloc(1, sizeof(AddonData));
// Create a string to describe this asynchronous operation.
status = napi_create_string_utf8(
env,
"N-API on_ziti_init",
NAPI_AUTO_LENGTH,
&work_name);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Failed to napi_create_string_utf8");
}
// Convert the callback retrieved from JavaScript into a thread-safe function (tsfn)
// which we can call from a worker thread.
status = napi_create_threadsafe_function(
env,
js_cb,
NULL,
work_name,
0,
1,
NULL,
NULL,
NULL,
CallJs,
&(addon_data->tsfn));
if (status != napi_ok) {
napi_throw_error(env, NULL, "Failed to napi_create_threadsafe_function");
}
ziti_log_init(thread_loop, ZITI_LOG_DEFAULT_LEVEL, NULL);
ziti_config cfg = {0};
int rc = ziti_load_config(&cfg, config_file_name);
ZITI_NODEJS_LOG(DEBUG, "ziti_load_config => %d", rc);
if (rc != ZITI_OK) goto done;
rc = ziti_context_init(&ztx, &cfg);
ZITI_NODEJS_LOG(DEBUG, "ziti_context_init => %d", rc);
if (rc != ZITI_OK) goto done;
ziti_options opts = {
.events = ZitiContextEvent | ZitiServiceEvent | ZitiRouterEvent,
.event_cb = on_ziti_event,
.refresh_interval = 60,
.app_ctx = addon_data,
.config_types = ALL_CONFIG_TYPES,
.metrics_type = INSTANT,
};
rc = ziti_context_set_options(ztx, &opts);
ZITI_NODEJS_LOG(DEBUG, "ziti_context_set_options => %d", rc);
if (rc != ZITI_OK) goto done;
rc = ziti_context_run(ztx, thread_loop);
ZITI_NODEJS_LOG(DEBUG, "ziti_context_run => %d", rc);
done:
status = napi_create_int32(env, rc, &jsRetval);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Unable to create return value");
}
return jsRetval;
}
void expose_ziti_init(napi_env env, napi_value exports) {
napi_status status;
napi_value fn;
status = napi_create_function(env, NULL, 0, _ziti_init, NULL, &fn);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Unable to wrap native function '_ziti_init");
}
status = napi_set_named_property(env, exports, "ziti_init", fn);
if (status != napi_ok) {
napi_throw_error(env, NULL, "Unable to populate exports for 'ziti_init");
}
}