xbintsc 0.6.20 → 0.6.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +5 -0
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/cli/main.js +1 -0
- package/dist/src/cli/main.js.map +1 -1
- package/dist/src/driver/build-key.d.ts +3 -0
- package/dist/src/driver/build-key.js +5 -2
- package/dist/src/driver/build-key.js.map +1 -1
- package/dist/src/driver/compiler.js +11 -0
- package/dist/src/driver/compiler.js.map +1 -1
- package/dist/src/driver/config.d.ts +12 -0
- package/dist/src/driver/config.js +22 -0
- package/dist/src/driver/config.js.map +1 -1
- package/dist/src/driver/fingerprint.d.ts +11 -0
- package/dist/src/driver/fingerprint.js +25 -0
- package/dist/src/driver/fingerprint.js.map +1 -1
- package/dist/src/driver/index.d.ts +2 -1
- package/dist/src/driver/index.js +1 -0
- package/dist/src/driver/index.js.map +1 -1
- package/dist/src/driver/link-features.js +36 -11
- package/dist/src/driver/link-features.js.map +1 -1
- package/dist/src/driver/options.d.ts +8 -1
- package/dist/src/driver/paths.d.ts +10 -0
- package/dist/src/driver/paths.js +18 -0
- package/dist/src/driver/paths.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +22 -6
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/storage.d.ts +34 -0
- package/dist/src/driver/storage.js +67 -0
- package/dist/src/driver/storage.js.map +1 -0
- package/dist/src/driver/toolchain.d.ts +1 -1
- package/dist/src/driver/toolchain.js +10 -1
- package/dist/src/driver/toolchain.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/gui/features.d.ts +1 -1
- package/dist/src/extensions/gui/features.js +2 -2
- package/dist/src/extensions/gui/features.js.map +1 -1
- package/dist/src/extensions/gui/index.js +3 -1
- package/dist/src/extensions/gui/index.js.map +1 -1
- package/dist/src/extensions/node/http2/feature.d.ts +14 -0
- package/dist/src/extensions/node/http2/feature.js +28 -0
- package/dist/src/extensions/node/http2/feature.js.map +1 -0
- package/dist/src/extensions/node/http2/index.d.ts +5 -1
- package/dist/src/extensions/node/http2/index.js +6 -1
- package/dist/src/extensions/node/http2/index.js.map +1 -1
- package/dist/src/extensions/node/index.js +3 -1
- package/dist/src/extensions/node/index.js.map +1 -1
- package/dist/src/extensions/node/tls/feature.d.ts +12 -0
- package/dist/src/extensions/node/tls/feature.js +24 -0
- package/dist/src/extensions/node/tls/feature.js.map +1 -0
- package/dist/src/extensions/node/tls/index.d.ts +5 -1
- package/dist/src/extensions/node/tls/index.js +5 -1
- package/dist/src/extensions/node/tls/index.js.map +1 -1
- package/dist/src/extensions/node/zlib/compression.js +11 -6
- package/dist/src/extensions/node/zlib/compression.js.map +1 -1
- package/dist/src/extensions/registry.d.ts +4 -1
- package/dist/src/extensions/registry.js +9 -2
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/extensions/storage.d.ts +22 -0
- package/dist/src/extensions/storage.js +51 -0
- package/dist/src/extensions/storage.js.map +1 -0
- package/dist/src/extensions/tls.d.ts +26 -0
- package/dist/src/extensions/tls.js +46 -0
- package/dist/src/extensions/tls.js.map +1 -0
- package/dist/tests/driver/config.test.js +31 -0
- package/dist/tests/driver/config.test.js.map +1 -1
- package/dist/tests/driver/link-features.test.js +13 -2
- package/dist/tests/driver/link-features.test.js.map +1 -1
- package/dist/tests/driver/storage.test.d.ts +1 -0
- package/dist/tests/driver/storage.test.js +29 -0
- package/dist/tests/driver/storage.test.js.map +1 -0
- package/dist/tests/e2e/fetch.test.js +3 -1
- package/dist/tests/e2e/fetch.test.js.map +1 -1
- package/dist/tests/e2e/http2.test.d.ts +7 -0
- package/dist/tests/e2e/http2.test.js +201 -0
- package/dist/tests/e2e/http2.test.js.map +1 -0
- package/dist/tests/e2e/net-helpers.d.ts +28 -0
- package/dist/tests/e2e/net-helpers.js +59 -0
- package/dist/tests/e2e/net-helpers.js.map +1 -0
- package/dist/tests/e2e/node-modules.test.js +3 -5
- package/dist/tests/e2e/node-modules.test.js.map +1 -1
- package/dist/tests/e2e/storage.test.d.ts +7 -0
- package/dist/tests/e2e/storage.test.js +86 -0
- package/dist/tests/e2e/storage.test.js.map +1 -0
- package/dist/tests/e2e/tls.test.d.ts +10 -0
- package/dist/tests/e2e/tls.test.js +197 -0
- package/dist/tests/e2e/tls.test.js.map +1 -0
- package/dist/tests/extensions/gui-features.test.js +1 -1
- package/dist/tests/extensions/gui-features.test.js.map +1 -1
- package/dist/tests/extensions/storage.test.d.ts +1 -0
- package/dist/tests/extensions/storage.test.js +51 -0
- package/dist/tests/extensions/storage.test.js.map +1 -0
- package/doc/ai/cli.md +2 -0
- package/doc/ai/contributing.md +14 -0
- package/doc/ai/extensions.md +31 -3
- package/doc/ai/language-support.md +1 -1
- package/doc/ai/zh-CN/cli.md +2 -0
- package/doc/ai/zh-CN/contributing.md +12 -0
- package/doc/ai/zh-CN/extensions.md +27 -2
- package/doc/ai/zh-CN/language-support.md +1 -1
- package/doc/gui/api.md +1 -1
- package/doc/gui/build.md +7 -5
- package/doc/gui/media.md +4 -4
- package/doc/gui/rendering.md +1 -1
- package/doc/gui.md +3 -3
- package/doc/implemented/async.md +1 -1
- package/doc/implemented/extensions.md +2 -1
- package/doc/implemented/fetch.md +19 -4
- package/doc/implemented/node/http2.md +51 -0
- package/doc/implemented/node/tls.md +56 -0
- package/doc/implemented/node.md +8 -0
- package/doc/implemented/storage.md +65 -0
- package/doc/implemented.md +4 -2
- package/doc/unimplemented/node/modules.md +6 -6
- package/doc/unimplemented/toolchain.md +1 -1
- package/doc/xbintsc.config.schema.json +16 -1
- package/doc/zh-CN/gui/api.md +1 -1
- package/doc/zh-CN/gui/build.md +6 -4
- package/doc/zh-CN/gui/media.md +3 -3
- package/doc/zh-CN/gui/rendering.md +1 -1
- package/doc/zh-CN/gui.md +3 -3
- package/doc/zh-CN/implemented/async.md +1 -1
- package/doc/zh-CN/implemented/extensions.md +2 -1
- package/doc/zh-CN/implemented/fetch.md +15 -3
- package/doc/zh-CN/implemented/node/http2.md +44 -0
- package/doc/zh-CN/implemented/node/tls.md +47 -0
- package/doc/zh-CN/implemented/node.md +7 -0
- package/doc/zh-CN/implemented/storage.md +52 -0
- package/doc/zh-CN/implemented.md +4 -2
- package/doc/zh-CN/unimplemented/node/modules.md +3 -3
- package/doc/zh-CN/unimplemented/toolchain.md +1 -1
- package/package.json +16 -8
- package/runtime/ext_gui/dom_api_form.cpp +10 -0
- package/runtime/ext_gui/gui_media.cpp +1 -1
- package/runtime/ext_gui/media.cpp +1 -1
- package/runtime/ext_gui/media_api.h +2 -2
- package/runtime/ext_node/http2/http2.c +25 -11
- package/runtime/ext_node/http2/http2_backend.c +48 -0
- package/runtime/ext_node/http2/http2_bridge.c +25 -0
- package/runtime/ext_node/http2/parts/api.inc +40 -0
- package/runtime/ext_node/http2/parts/client.inc +215 -0
- package/runtime/ext_node/http2/parts/common.inc +330 -0
- package/runtime/ext_node/http2/parts/events.inc +139 -0
- package/runtime/ext_node/http2/parts/server.inc +301 -0
- package/runtime/ext_node/http2/parts/streams.inc +254 -0
- package/runtime/ext_node/tls/parts/api.inc +38 -0
- package/runtime/ext_node/tls/parts/connect.inc +107 -0
- package/runtime/ext_node/tls/parts/helpers.inc +121 -0
- package/runtime/ext_node/tls/parts/server.inc +101 -0
- package/runtime/ext_node/tls/parts/socket.inc +171 -0
- package/runtime/ext_node/tls/tls.c +21 -13
- package/runtime/ext_node/tls/tls_backend.c +50 -0
- package/runtime/ext_node/zlib/{miniz/compression_bridge.c → compression_bridge.c} +1 -1
- package/runtime/xt_fetch/async.inc +19 -12
- package/runtime/xt_fetch/socket.inc +55 -0
- package/runtime/xt_fetch.c +3 -0
- package/runtime/xt_http2/nghttp2_compat.h +19 -0
- package/runtime/xt_http2/nghttp2_static.h +18 -0
- package/runtime/xt_http2_api.h +38 -0
- package/runtime/xt_node_tls_api.h +37 -0
- package/runtime/xt_protocol_registry.c +22 -0
- package/runtime/xt_storage/indexed_db.c +408 -0
- package/runtime/xt_storage/indexed_db_internal.h +26 -0
- package/runtime/xt_storage/indexed_db_store.c +305 -0
- package/runtime/xt_storage/storage.c +209 -0
- package/runtime/xt_storage/storage_internal.h +66 -0
- package/runtime/xt_tls/parts/api.inc +21 -0
- package/runtime/xt_tls/parts/client.inc +112 -0
- package/runtime/xt_tls/parts/io.inc +157 -0
- package/runtime/xt_tls/parts/server.inc +102 -0
- package/runtime/xt_tls/parts/setup.inc +146 -0
- package/runtime/xt_tls/tls_bridge.c +27 -0
- package/runtime/xt_tls/tls_core.c +39 -0
- package/runtime/xt_tls/tls_internal.h +73 -0
- package/runtime/xt_tls_api.h +73 -0
- package/scripts/build-gui.ts +9 -8
- package/scripts/build-media.ts +2 -2
- package/scripts/build-tls.ts +248 -0
- package/scripts/check-file-length.ts +4 -21
- package/scripts/fetch-vendored.ts +244 -0
- package/scripts/package-release.ts +13 -3
- package/src/cli/main.ts +1 -0
- package/src/driver/build-key.ts +13 -2
- package/src/driver/compiler.ts +13 -0
- package/src/driver/config.ts +35 -0
- package/src/driver/fingerprint.ts +24 -0
- package/src/driver/index.ts +8 -0
- package/src/driver/link-features.ts +33 -11
- package/src/driver/options.ts +8 -1
- package/src/driver/paths.ts +20 -0
- package/src/driver/runtime-objects.ts +25 -6
- package/src/driver/storage.ts +84 -0
- package/src/driver/toolchain.ts +11 -2
- package/src/driver/version.ts +1 -1
- package/src/extensions/gui/features.ts +2 -2
- package/src/extensions/gui/index.ts +3 -1
- package/src/extensions/node/http2/feature.ts +33 -0
- package/src/extensions/node/http2/index.ts +6 -1
- package/src/extensions/node/index.ts +3 -1
- package/src/extensions/node/tls/feature.ts +29 -0
- package/src/extensions/node/tls/index.ts +5 -1
- package/src/extensions/node/zlib/compression.ts +11 -6
- package/src/extensions/registry.ts +9 -2
- package/src/extensions/storage.ts +58 -0
- package/src/extensions/tls.ts +52 -0
- package/vendor/sqlite3/sqlite3.c +271131 -0
- package/vendor/sqlite3/sqlite3.h +14447 -0
- package/{runtime/ext_gui → vendor/stb}/stb_image.h +17 -1
- /package/runtime/{ext_media → xt_media}/decode_audio.cpp +0 -0
- /package/runtime/{ext_media → xt_media}/media.cpp +0 -0
- /package/runtime/{ext_media → xt_media}/media_bridge.c +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.c +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_common.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_export.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.c +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.c +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_zip.h +0 -0
- /package/{runtime/ext_gui → vendor/nanosvg}/nanosvg.h +0 -0
- /package/{runtime/ext_gui → vendor/nanosvg}/nanosvgrast.h +0 -0
- /package/{runtime/ext_gui → vendor/stb}/stb_truetype.h +0 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/* -- tls.connect ---------------------------------------------------------- */
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Accepted forms (Node):
|
|
5
|
+
* connect(options[, callback])
|
|
6
|
+
* connect(port[, host][, options][, callback])
|
|
7
|
+
* The handshake is synchronous in this runtime: the returned socket is already
|
|
8
|
+
* secure, and the callback runs once before `connect` returns.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
static void xt_tls_parse_connect(int32_t argc, xt_value *argv, xt_value *options, int *port,
|
|
12
|
+
char **host, xt_value *callback) {
|
|
13
|
+
*options = xt_undefined();
|
|
14
|
+
*port = 0;
|
|
15
|
+
*host = NULL;
|
|
16
|
+
*callback = xt_undefined();
|
|
17
|
+
int index = 0;
|
|
18
|
+
if (argc > 0 && XT_IS_OBJECT(argv[0])) {
|
|
19
|
+
*options = argv[0];
|
|
20
|
+
index = 1;
|
|
21
|
+
} else if (argc > 0 && XT_IS_NUMBER(argv[0])) {
|
|
22
|
+
*port = (int)xt_to_number(argv[0]);
|
|
23
|
+
index = 1;
|
|
24
|
+
if (argc > 1 && XT_IS_STRING(argv[1])) {
|
|
25
|
+
*host = xt_tls_text(argv[1]);
|
|
26
|
+
index = 2;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
for (int32_t i = index; i < argc; i++) {
|
|
30
|
+
if (XT_IS_OBJECT(argv[i]) && *options == xt_undefined()) {
|
|
31
|
+
*options = argv[i];
|
|
32
|
+
} else if (XT_IS_FUNCTION(argv[i])) {
|
|
33
|
+
*callback = argv[i];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/*
|
|
39
|
+
* `tls.connect` returns before `secureConnect` fires in Node, so the callback
|
|
40
|
+
* can close over the `const socket = connect(...)` binding. Doing it inline
|
|
41
|
+
* would run the callback while that binding is still uninitialised; a zero
|
|
42
|
+
* delay timer restores the asynchronous ordering without blocking.
|
|
43
|
+
*/
|
|
44
|
+
static xt_value xt_tls_deferred_connect(xt_value thisValue, xt_value env, int32_t argc,
|
|
45
|
+
xt_value *argv) {
|
|
46
|
+
(void)thisValue;
|
|
47
|
+
(void)argc;
|
|
48
|
+
(void)argv;
|
|
49
|
+
xt_value socket = xt_closure_env(env, 0);
|
|
50
|
+
xt_value callback = xt_closure_env(env, 1);
|
|
51
|
+
xt_node_emit0(socket, "secureConnect");
|
|
52
|
+
xt_node_emit0(socket, "connect");
|
|
53
|
+
if (XT_IS_FUNCTION(callback)) xt_call_with_this(callback, socket, 0, NULL);
|
|
54
|
+
return XT_UNDEFINED;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
xt_value xt_tls_backend_connect(int32_t argc, xt_value *argv) {
|
|
58
|
+
if (xt_tls_core_api == NULL) {
|
|
59
|
+
xt_throw(xt_error_from_message("Error: the tls link feature is not linked"));
|
|
60
|
+
return XT_UNDEFINED;
|
|
61
|
+
}
|
|
62
|
+
xt_value options;
|
|
63
|
+
int port = 0;
|
|
64
|
+
char *host = NULL;
|
|
65
|
+
xt_value callback;
|
|
66
|
+
xt_tls_parse_connect(argc, argv, &options, &port, &host, &callback);
|
|
67
|
+
|
|
68
|
+
if (host == NULL) host = xt_tls_option_text(options, "host");
|
|
69
|
+
if (host == NULL) host = xt_tls_text(xt_string_from_cstr("127.0.0.1"));
|
|
70
|
+
char *servername = xt_tls_option_text(options, "servername");
|
|
71
|
+
char *ca = xt_tls_option_text(options, "ca");
|
|
72
|
+
char *cert = xt_tls_option_text(options, "cert");
|
|
73
|
+
char *key = xt_tls_option_text(options, "key");
|
|
74
|
+
char *alpn = xt_tls_alpn_option(options);
|
|
75
|
+
if (port == 0) {
|
|
76
|
+
xt_value portValue = XT_IS_OBJECT(options) ? xt_object_get_cstr(options, "port") : xt_undefined();
|
|
77
|
+
if (!xt_truthy(xt_is_nullish(portValue))) port = (int)xt_to_number(portValue);
|
|
78
|
+
}
|
|
79
|
+
if (port == 0) port = 443;
|
|
80
|
+
int verify = xt_tls_option_flag(options, "rejectUnauthorized", 1);
|
|
81
|
+
if (servername == NULL) servername = xt_tls_text(xt_string_from_cstr(host));
|
|
82
|
+
|
|
83
|
+
char error[256];
|
|
84
|
+
error[0] = '\0';
|
|
85
|
+
xt_tls_conn *conn = xt_tls_core_api->client_connect(host, port, servername, verify, ca, cert, key,
|
|
86
|
+
alpn, error, sizeof(error));
|
|
87
|
+
free(host);
|
|
88
|
+
free(ca);
|
|
89
|
+
free(cert);
|
|
90
|
+
free(key);
|
|
91
|
+
free(alpn);
|
|
92
|
+
if (conn == NULL) {
|
|
93
|
+
free(servername);
|
|
94
|
+
xt_throw(xt_tls_error_value(error));
|
|
95
|
+
return XT_UNDEFINED;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
xt_value socket = xt_tls_socket_wrap(conn);
|
|
99
|
+
xt_node_set(socket, "servername", xt_string_from_cstr(servername != NULL ? servername : ""));
|
|
100
|
+
free(servername);
|
|
101
|
+
xt_node_set(socket, "__secure", xt_bool(1));
|
|
102
|
+
xt_value captures[2] = {socket, callback};
|
|
103
|
+
xt_value deferred = xt_closure_new((void *)xt_tls_deferred_connect, 2, captures);
|
|
104
|
+
xt_value timer[2] = {deferred, xt_number(0)};
|
|
105
|
+
xt_set_timeout(2, timer);
|
|
106
|
+
return socket;
|
|
107
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/* -- value helpers -------------------------------------------------------- */
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Options follow Node's shape: `tls.connect({ port, host, servername, ca,
|
|
5
|
+
* cert, key, rejectUnauthorized, ALPNProtocols }, callback)`. PEM values may be
|
|
6
|
+
* strings or Buffers, so every text option goes through {@link xt_tls_text}.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
static char *xt_tls_text(xt_value value) {
|
|
10
|
+
if (xt_node_is_buffer(value)) {
|
|
11
|
+
size_t length = 0;
|
|
12
|
+
unsigned char *bytes = xt_node_buffer_bytes(value, &length);
|
|
13
|
+
char *text = (char *)malloc(length + 1);
|
|
14
|
+
if (text == NULL) {
|
|
15
|
+
free(bytes);
|
|
16
|
+
return NULL;
|
|
17
|
+
}
|
|
18
|
+
if (length > 0) memcpy(text, bytes, length);
|
|
19
|
+
text[length] = '\0';
|
|
20
|
+
free(bytes);
|
|
21
|
+
return text;
|
|
22
|
+
}
|
|
23
|
+
const char *text = xt_string_data(xt_to_string(value));
|
|
24
|
+
if (text == NULL) return NULL;
|
|
25
|
+
size_t length = strlen(text);
|
|
26
|
+
char *copy = (char *)malloc(length + 1);
|
|
27
|
+
if (copy == NULL) return NULL;
|
|
28
|
+
memcpy(copy, text, length + 1);
|
|
29
|
+
return copy;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static char *xt_tls_option_text(xt_value options, const char *key) {
|
|
33
|
+
if (!XT_IS_OBJECT(options)) return NULL;
|
|
34
|
+
xt_value value = xt_object_get_cstr(options, key);
|
|
35
|
+
if (xt_truthy(xt_is_nullish(value))) return NULL;
|
|
36
|
+
return xt_tls_text(value);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
static int xt_tls_option_flag(xt_value options, const char *key, int fallback) {
|
|
40
|
+
if (!XT_IS_OBJECT(options)) return fallback;
|
|
41
|
+
xt_value value = xt_object_get_cstr(options, key);
|
|
42
|
+
if (xt_truthy(xt_is_nullish(value))) return fallback;
|
|
43
|
+
return xt_truthy(value) ? 1 : 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Join an `ALPNProtocols` array (or a comma-separated string) into a list. */
|
|
47
|
+
static char *xt_tls_alpn_option(xt_value options) {
|
|
48
|
+
if (!XT_IS_OBJECT(options)) return NULL;
|
|
49
|
+
xt_value value = xt_object_get_cstr(options, "ALPNProtocols");
|
|
50
|
+
if (xt_truthy(xt_is_nullish(value))) value = xt_object_get_cstr(options, "alpn");
|
|
51
|
+
if (xt_truthy(xt_is_nullish(value))) return NULL;
|
|
52
|
+
if (XT_IS_ARRAY(value)) {
|
|
53
|
+
size_t capacity = 64;
|
|
54
|
+
char *joined = (char *)malloc(capacity);
|
|
55
|
+
if (joined == NULL) return NULL;
|
|
56
|
+
size_t length = 0;
|
|
57
|
+
joined[0] = '\0';
|
|
58
|
+
int32_t count = (int32_t)xt_to_number(xt_array_length(value));
|
|
59
|
+
for (int32_t i = 0; i < count; i++) {
|
|
60
|
+
char *item = xt_tls_text(xt_array_get(value, xt_number((double)i)));
|
|
61
|
+
if (item == NULL) continue;
|
|
62
|
+
size_t itemLength = strlen(item);
|
|
63
|
+
if (length + itemLength + 2 > capacity) {
|
|
64
|
+
capacity = (length + itemLength + 2) * 2;
|
|
65
|
+
char *grown = (char *)realloc(joined, capacity);
|
|
66
|
+
if (grown == NULL) {
|
|
67
|
+
free(item);
|
|
68
|
+
free(joined);
|
|
69
|
+
return NULL;
|
|
70
|
+
}
|
|
71
|
+
joined = grown;
|
|
72
|
+
}
|
|
73
|
+
if (length > 0) joined[length++] = ',';
|
|
74
|
+
memcpy(joined + length, item, itemLength);
|
|
75
|
+
length += itemLength;
|
|
76
|
+
joined[length] = '\0';
|
|
77
|
+
free(item);
|
|
78
|
+
}
|
|
79
|
+
return joined;
|
|
80
|
+
}
|
|
81
|
+
return xt_tls_text(value);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static unsigned char *xt_tls_bytes(xt_value value, size_t *outLength) {
|
|
85
|
+
if (xt_node_is_buffer(value)) return xt_node_buffer_bytes(value, outLength);
|
|
86
|
+
xt_value text = xt_to_string(value);
|
|
87
|
+
const char *data = xt_string_data(text);
|
|
88
|
+
size_t length = data ? (size_t)xt_string_length(xt_as_string(text)) : 0;
|
|
89
|
+
unsigned char *bytes = (unsigned char *)malloc(length + 1);
|
|
90
|
+
if (bytes == NULL) {
|
|
91
|
+
*outLength = 0;
|
|
92
|
+
return NULL;
|
|
93
|
+
}
|
|
94
|
+
if (length > 0) memcpy(bytes, data, length);
|
|
95
|
+
bytes[length] = '\0';
|
|
96
|
+
*outLength = length;
|
|
97
|
+
return bytes;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
static xt_value *xt_tls_box(xt_value value) {
|
|
101
|
+
xt_value *box = (xt_value *)malloc(sizeof(xt_value));
|
|
102
|
+
if (box != NULL) *box = value;
|
|
103
|
+
return box;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static xt_value xt_tls_error_value(const char *message) {
|
|
107
|
+
return xt_error_from_message(message[0] != '\0' ? message : "TLS operation failed");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
static xt_tls_conn *xt_tls_conn_of(xt_value socket) {
|
|
111
|
+
double raw = xt_to_number(xt_object_get_cstr(socket, "__conn"));
|
|
112
|
+
return (xt_tls_conn *)(uintptr_t)raw;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
static void xt_tls_set_conn(xt_value socket, xt_tls_conn *conn) {
|
|
116
|
+
xt_node_set(socket, "__conn", xt_number((double)(uintptr_t)conn));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
static int xt_tls_socket_fd(xt_value socket) {
|
|
120
|
+
return (int)xt_to_number(xt_object_get_cstr(socket, "__fd"));
|
|
121
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/* -- tls.createServer ----------------------------------------------------- */
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Listening is delegated to `net.Server`: `createServer` builds a plain net
|
|
5
|
+
* server, then replaces the handling of each accepted socket with a TLS
|
|
6
|
+
* handshake. Reusing the net server keeps `listen`/`address`/`listening` and
|
|
7
|
+
* the error surface identical to `node:net`.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
static xt_value xt_tls_server_connection(xt_value server, xt_value env, int32_t argc, xt_value *argv) {
|
|
11
|
+
(void)env;
|
|
12
|
+
if (argc < 1) return XT_UNDEFINED;
|
|
13
|
+
xt_value socket = argv[0];
|
|
14
|
+
int fd = xt_tls_socket_fd(socket);
|
|
15
|
+
if (fd < 0) return XT_UNDEFINED;
|
|
16
|
+
|
|
17
|
+
/* Take the descriptor away from the net socket: its readiness watcher would
|
|
18
|
+
* otherwise read encrypted bytes as plaintext. */
|
|
19
|
+
xt_loop_remove(fd);
|
|
20
|
+
xt_node_set(socket, "__fd", xt_number(-1));
|
|
21
|
+
xt_node_set(socket, "__closed", xt_bool(1));
|
|
22
|
+
|
|
23
|
+
xt_tls_server_config *config =
|
|
24
|
+
(xt_tls_server_config *)(uintptr_t)xt_to_number(xt_object_get_cstr(server, "__tlsConfig"));
|
|
25
|
+
if (config == NULL || xt_tls_core_api == NULL) {
|
|
26
|
+
xt_tls_backend_closesocket(fd);
|
|
27
|
+
return XT_UNDEFINED;
|
|
28
|
+
}
|
|
29
|
+
char error[256];
|
|
30
|
+
error[0] = '\0';
|
|
31
|
+
xt_tls_conn *conn = xt_tls_core_api->server_accept(config, fd, error, sizeof(error));
|
|
32
|
+
if (conn == NULL) {
|
|
33
|
+
xt_tls_backend_closesocket(fd);
|
|
34
|
+
xt_node_emit1(server, "tlsClientError", xt_tls_error_value(error));
|
|
35
|
+
return XT_UNDEFINED;
|
|
36
|
+
}
|
|
37
|
+
xt_value secureSocket = xt_tls_socket_wrap(conn);
|
|
38
|
+
xt_node_set(secureSocket, "__secure", xt_bool(1));
|
|
39
|
+
xt_node_emit1(server, "secureConnection", secureSocket);
|
|
40
|
+
return XT_UNDEFINED;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static xt_value xt_tls_server_close(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
44
|
+
(void)env;
|
|
45
|
+
xt_tls_server_config *config =
|
|
46
|
+
(xt_tls_server_config *)(uintptr_t)xt_to_number(xt_object_get_cstr(thisValue, "__tlsConfig"));
|
|
47
|
+
if (config != NULL && xt_tls_core_api != NULL) {
|
|
48
|
+
xt_tls_core_api->config_free(config);
|
|
49
|
+
xt_node_set(thisValue, "__tlsConfig", xt_number(0));
|
|
50
|
+
}
|
|
51
|
+
xt_value netClose = xt_object_get_cstr(thisValue, "__netClose");
|
|
52
|
+
if (XT_IS_FUNCTION(netClose)) xt_call_with_this(netClose, thisValue, argc, argv);
|
|
53
|
+
return thisValue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
xt_value xt_tls_backend_create_server(int32_t argc, xt_value *argv) {
|
|
57
|
+
if (xt_tls_core_api == NULL) {
|
|
58
|
+
xt_throw(xt_error_from_message("Error: the tls link feature is not linked"));
|
|
59
|
+
return XT_UNDEFINED;
|
|
60
|
+
}
|
|
61
|
+
xt_value options = xt_undefined();
|
|
62
|
+
xt_value listener = xt_undefined();
|
|
63
|
+
for (int32_t i = 0; i < argc; i++) {
|
|
64
|
+
if (XT_IS_OBJECT(argv[i]) && options == xt_undefined()) options = argv[i];
|
|
65
|
+
else if (XT_IS_FUNCTION(argv[i]) && listener == xt_undefined()) listener = argv[i];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
char *cert = xt_tls_option_text(options, "cert");
|
|
69
|
+
char *key = xt_tls_option_text(options, "key");
|
|
70
|
+
char *ca = xt_tls_option_text(options, "ca");
|
|
71
|
+
char *alpn = xt_tls_alpn_option(options);
|
|
72
|
+
int requestCert = xt_tls_option_flag(options, "requestCert", 0);
|
|
73
|
+
int rejectUnauthorized = xt_tls_option_flag(options, "rejectUnauthorized", 1);
|
|
74
|
+
char error[256];
|
|
75
|
+
error[0] = '\0';
|
|
76
|
+
xt_tls_server_config *config = xt_tls_core_api->server_config(
|
|
77
|
+
cert, key, ca, requestCert, requestCert && rejectUnauthorized ? 1 : 0, alpn, error, sizeof(error));
|
|
78
|
+
free(cert);
|
|
79
|
+
free(key);
|
|
80
|
+
free(ca);
|
|
81
|
+
free(alpn);
|
|
82
|
+
if (config == NULL) {
|
|
83
|
+
xt_throw(xt_tls_error_value(error));
|
|
84
|
+
return XT_UNDEFINED;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
xt_value server = xt_net_static(xt_string_from_cstr("createServer"), 0, NULL);
|
|
88
|
+
xt_node_set(server, "__tlsConfig", xt_number((double)(uintptr_t)config));
|
|
89
|
+
xt_value netClose = xt_object_get_cstr(server, "close");
|
|
90
|
+
xt_node_set(server, "__netClose", netClose);
|
|
91
|
+
xt_node_define_method(server, "close", (void *)xt_tls_server_close);
|
|
92
|
+
|
|
93
|
+
/* Attach the handshake handler before any listener: the event emitter flushes
|
|
94
|
+
* buffered events on `on`, so an early connection must not beat it. */
|
|
95
|
+
xt_array_push(xt_node_listeners(server, "connection", 1),
|
|
96
|
+
xt_closure_new((void *)xt_tls_server_connection, 0, NULL));
|
|
97
|
+
if (XT_IS_FUNCTION(listener)) {
|
|
98
|
+
xt_array_push(xt_node_listeners(server, "secureConnection", 1), listener);
|
|
99
|
+
}
|
|
100
|
+
return server;
|
|
101
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/* -- TLSSocket ------------------------------------------------------------ */
|
|
2
|
+
|
|
3
|
+
static void xt_tls_socket_finish(xt_value socket, int emitEnd) {
|
|
4
|
+
int fd = xt_tls_socket_fd(socket);
|
|
5
|
+
if (fd >= 0) xt_loop_remove(fd);
|
|
6
|
+
xt_tls_conn *conn = xt_tls_conn_of(socket);
|
|
7
|
+
if (conn != NULL) {
|
|
8
|
+
xt_tls_core_api->close(conn);
|
|
9
|
+
xt_tls_set_conn(socket, NULL);
|
|
10
|
+
}
|
|
11
|
+
xt_node_set(socket, "__fd", xt_number(-1));
|
|
12
|
+
if (!xt_truthy(xt_object_get_cstr(socket, "__closed"))) {
|
|
13
|
+
xt_node_set(socket, "__closed", xt_bool(1));
|
|
14
|
+
if (emitEnd) xt_node_emit0(socket, "end");
|
|
15
|
+
xt_node_emit0(socket, "close");
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static void xt_tls_socket_read(void *userdata, int events) {
|
|
20
|
+
(void)events;
|
|
21
|
+
if (xt_tls_core_api == NULL) return;
|
|
22
|
+
xt_value socket = *(xt_value *)userdata;
|
|
23
|
+
xt_tls_conn *conn = xt_tls_conn_of(socket);
|
|
24
|
+
if (conn == NULL) return;
|
|
25
|
+
unsigned char buffer[65536];
|
|
26
|
+
int count = xt_tls_core_api->read(conn, buffer, sizeof(buffer));
|
|
27
|
+
if (count > 0) {
|
|
28
|
+
const char *encoding = xt_node_encoding(xt_object_get_cstr(socket, "__encoding"));
|
|
29
|
+
xt_node_emit1(socket, "data", xt_node_encode(buffer, (size_t)count, encoding));
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (count == 0) {
|
|
33
|
+
xt_tls_socket_finish(socket, 1);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const char *message = xt_tls_core_api->error(conn);
|
|
37
|
+
char detail[256];
|
|
38
|
+
snprintf(detail, sizeof(detail), "%s", message != NULL ? message : "TLS read failed");
|
|
39
|
+
xt_tls_socket_finish(socket, 0);
|
|
40
|
+
xt_node_emit1(socket, "error", xt_tls_error_value(detail));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
static xt_value xt_tls_socket_wrap(xt_tls_conn *conn) {
|
|
44
|
+
xt_value socket = xt_object_new_with_proto(xt_tls_socket_proto());
|
|
45
|
+
int fd = xt_tls_core_api->fd(conn);
|
|
46
|
+
xt_tls_set_conn(socket, conn);
|
|
47
|
+
xt_node_set(socket, "__fd", xt_number((double)fd));
|
|
48
|
+
xt_node_set(socket, "encrypted", xt_bool(1));
|
|
49
|
+
xt_node_set(socket, "authorized", xt_bool(1));
|
|
50
|
+
const char *alpn = xt_tls_core_api->alpn(conn);
|
|
51
|
+
if (alpn != NULL) xt_node_set(socket, "alpnProtocol", xt_string_from_cstr(alpn));
|
|
52
|
+
xt_loop_add(fd, XT_IO_READ, xt_tls_socket_read, xt_tls_box(socket));
|
|
53
|
+
return socket;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static xt_value xt_tls_socket_write(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
57
|
+
(void)env;
|
|
58
|
+
xt_tls_conn *conn = xt_tls_conn_of(thisValue);
|
|
59
|
+
if (conn == NULL) {
|
|
60
|
+
xt_value callback = XT_IS_FUNCTION(xt_arg(argc, argv, 1)) ? xt_arg(argc, argv, 1) : xt_arg(argc, argv, 2);
|
|
61
|
+
if (XT_IS_FUNCTION(callback)) {
|
|
62
|
+
xt_value error = xt_tls_error_value("Cannot write after the TLS socket was closed");
|
|
63
|
+
xt_call_with_this(callback, xt_undefined(), 1, &error);
|
|
64
|
+
}
|
|
65
|
+
return xt_bool(0);
|
|
66
|
+
}
|
|
67
|
+
size_t length = 0;
|
|
68
|
+
unsigned char *bytes = xt_tls_bytes(xt_arg(argc, argv, 0), &length);
|
|
69
|
+
int result = xt_tls_core_api->write(conn, bytes, length);
|
|
70
|
+
free(bytes);
|
|
71
|
+
xt_value callback = XT_IS_FUNCTION(xt_arg(argc, argv, 1)) ? xt_arg(argc, argv, 1) : xt_arg(argc, argv, 2);
|
|
72
|
+
if (XT_IS_FUNCTION(callback)) xt_call_with_this(callback, xt_undefined(), 0, NULL);
|
|
73
|
+
return xt_bool(result == 0);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static xt_value xt_tls_socket_end(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
77
|
+
(void)env;
|
|
78
|
+
if (argc > 0 && !xt_truthy(xt_is_nullish(argv[0]))) {
|
|
79
|
+
xt_tls_socket_write(thisValue, env, 1, &argv[0]);
|
|
80
|
+
}
|
|
81
|
+
int fd = xt_tls_socket_fd(thisValue);
|
|
82
|
+
if (fd >= 0) {
|
|
83
|
+
/* Half-close: the peer still gets our FIN but we keep reading its reply. */
|
|
84
|
+
#if defined(_WIN32)
|
|
85
|
+
shutdown(fd, SD_SEND);
|
|
86
|
+
#else
|
|
87
|
+
shutdown(fd, SHUT_WR);
|
|
88
|
+
#endif
|
|
89
|
+
xt_node_set(thisValue, "__ending", xt_bool(1));
|
|
90
|
+
}
|
|
91
|
+
return thisValue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static xt_value xt_tls_socket_destroy(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
95
|
+
(void)env;
|
|
96
|
+
(void)argc;
|
|
97
|
+
(void)argv;
|
|
98
|
+
xt_tls_socket_finish(thisValue, 1);
|
|
99
|
+
return thisValue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
static xt_value xt_tls_socket_set_encoding(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
103
|
+
(void)env;
|
|
104
|
+
xt_node_set(thisValue, "__encoding", xt_arg(argc, argv, 0));
|
|
105
|
+
return thisValue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
static xt_value xt_tls_socket_noop(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
109
|
+
(void)env;
|
|
110
|
+
(void)argc;
|
|
111
|
+
(void)argv;
|
|
112
|
+
return thisValue;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
static xt_value xt_tls_socket_address(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
116
|
+
(void)env;
|
|
117
|
+
(void)argc;
|
|
118
|
+
(void)argv;
|
|
119
|
+
int fd = xt_tls_socket_fd(thisValue);
|
|
120
|
+
if (fd < 0) {
|
|
121
|
+
xt_value local = xt_object_new();
|
|
122
|
+
xt_node_set(local, "address", xt_string_from_cstr("127.0.0.1"));
|
|
123
|
+
xt_node_set(local, "port", xt_number(0));
|
|
124
|
+
xt_node_set(local, "family", xt_string_from_cstr("IPv4"));
|
|
125
|
+
return local;
|
|
126
|
+
}
|
|
127
|
+
struct sockaddr_storage storage;
|
|
128
|
+
xt_tls_backend_socklen_t length = sizeof(storage);
|
|
129
|
+
if (getsockname(fd, (struct sockaddr *)&storage, &length) != 0) return xt_null();
|
|
130
|
+
char host[256];
|
|
131
|
+
char service[32];
|
|
132
|
+
if (getnameinfo((struct sockaddr *)&storage, length, host, sizeof(host), service, sizeof(service),
|
|
133
|
+
NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
|
|
134
|
+
return xt_null();
|
|
135
|
+
}
|
|
136
|
+
xt_value local = xt_object_new();
|
|
137
|
+
xt_node_set(local, "address", xt_string_from_cstr(host));
|
|
138
|
+
xt_node_set(local, "port", xt_number((double)atoi(service)));
|
|
139
|
+
xt_node_set(local, "family", xt_string_from_cstr(strchr(host, ':') != NULL ? "IPv6" : "IPv4"));
|
|
140
|
+
return local;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
static xt_value xt_tls_socket_certificate(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
144
|
+
(void)env;
|
|
145
|
+
(void)argc;
|
|
146
|
+
(void)argv;
|
|
147
|
+
xt_tls_conn *conn = xt_tls_conn_of(thisValue);
|
|
148
|
+
if (conn == NULL) return xt_null();
|
|
149
|
+
xt_value certificate = xt_object_new();
|
|
150
|
+
xt_node_set(certificate, "subject", xt_string_from_cstr(""));
|
|
151
|
+
xt_node_set(certificate, "issuer", xt_string_from_cstr(""));
|
|
152
|
+
return certificate;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static xt_value xt_tls_socket_proto(void) {
|
|
156
|
+
static xt_value proto = 0;
|
|
157
|
+
xt_gc_add_root(&proto);
|
|
158
|
+
if (proto) return proto;
|
|
159
|
+
proto = xt_object_new();
|
|
160
|
+
xt_node_install_emitter(proto);
|
|
161
|
+
xt_node_define_method(proto, "write", (void *)xt_tls_socket_write);
|
|
162
|
+
xt_node_define_method(proto, "end", (void *)xt_tls_socket_end);
|
|
163
|
+
xt_node_define_method(proto, "destroy", (void *)xt_tls_socket_destroy);
|
|
164
|
+
xt_node_define_method(proto, "setEncoding", (void *)xt_tls_socket_set_encoding);
|
|
165
|
+
xt_node_define_method(proto, "pause", (void *)xt_tls_socket_noop);
|
|
166
|
+
xt_node_define_method(proto, "resume", (void *)xt_tls_socket_noop);
|
|
167
|
+
xt_node_define_method(proto, "setTimeout", (void *)xt_tls_socket_noop);
|
|
168
|
+
xt_node_define_method(proto, "address", (void *)xt_tls_socket_address);
|
|
169
|
+
xt_node_define_method(proto, "getPeerCertificate", (void *)xt_tls_socket_certificate);
|
|
170
|
+
return proto;
|
|
171
|
+
}
|
|
@@ -1,26 +1,34 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Node.js `tls` module for xbintsc —
|
|
2
|
+
* Node.js `tls` module for xbintsc — importable bindings.
|
|
3
|
+
*
|
|
4
|
+
* The implementation is the `node-tls` link feature (`tls_backend.c`), which
|
|
5
|
+
* needs mbedTLS and is therefore only linked when a program imports this
|
|
6
|
+
* module. This translation unit is always compiled with the node extension so
|
|
7
|
+
* the import resolves either way: without the feature the calls throw a clear
|
|
8
|
+
* runtime error instead of failing at link time.
|
|
3
9
|
*/
|
|
4
10
|
|
|
5
11
|
#include "../node_common.h"
|
|
12
|
+
#include "../../xt_node_tls_api.h"
|
|
6
13
|
|
|
7
|
-
xt_value
|
|
8
|
-
(
|
|
9
|
-
|
|
10
|
-
|
|
14
|
+
static xt_value xt_tls_unsupported(void) {
|
|
15
|
+
xt_throw(xt_error_from_message(
|
|
16
|
+
"Error: node:tls is not available (the 'node-tls'/'tls' link features are not linked; "
|
|
17
|
+
"run `npm run tls` to build them)"));
|
|
11
18
|
return XT_UNDEFINED;
|
|
12
19
|
}
|
|
13
20
|
|
|
21
|
+
xt_value xt_tls_connect(int32_t argc, xt_value *argv) {
|
|
22
|
+
if (xt_tls_node_api == NULL) return xt_tls_unsupported();
|
|
23
|
+
return xt_tls_node_api->connect(argc, argv);
|
|
24
|
+
}
|
|
25
|
+
|
|
14
26
|
xt_value xt_tls_create_server(int32_t argc, xt_value *argv) {
|
|
15
|
-
(
|
|
16
|
-
(
|
|
17
|
-
xt_throw(xt_error_from_message("Error: node:tls is not supported in compiled binaries"));
|
|
18
|
-
return XT_UNDEFINED;
|
|
27
|
+
if (xt_tls_node_api == NULL) return xt_tls_unsupported();
|
|
28
|
+
return xt_tls_node_api->create_server(argc, argv);
|
|
19
29
|
}
|
|
20
30
|
|
|
21
31
|
xt_value xt_tls_socket_ctor(int32_t argc, xt_value *argv) {
|
|
22
|
-
(
|
|
23
|
-
(
|
|
24
|
-
xt_throw(xt_error_from_message("Error: node:tls is not supported in compiled binaries"));
|
|
25
|
-
return XT_UNDEFINED;
|
|
32
|
+
if (xt_tls_node_api == NULL) return xt_tls_unsupported();
|
|
33
|
+
return xt_tls_node_api->socket_ctor(argc, argv);
|
|
26
34
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* xbintsc `node-tls` link feature — the JavaScript `node:tls` surface.
|
|
3
|
+
*
|
|
4
|
+
* Compiled only when a program imports `node:tls` (its `requires` also pulls
|
|
5
|
+
* in the mbedTLS `tls` transport). The implementation is split by area under
|
|
6
|
+
* `tls/parts/` and `#include`d here as one translation unit so the helpers stay
|
|
7
|
+
* `static`, following the `net`/`http` modules.
|
|
8
|
+
*
|
|
9
|
+
* `TLSSocket` is an EventEmitter over an `xt_tls_conn`; client connects and
|
|
10
|
+
* server accepts run the blocking mbedTLS handshake (the runtime has a
|
|
11
|
+
* cooperative, single-threaded reactor), then a readiness watcher decrypts
|
|
12
|
+
* records and emits `data`. `createServer` reuses `net.Server` for listening
|
|
13
|
+
* and replaces its raw `connection` handling with a handshake that emits
|
|
14
|
+
* `secureConnection`.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
#include "../node_common.h"
|
|
18
|
+
#include "../../xt_node_tls_api.h"
|
|
19
|
+
#include "../../xt_tls_api.h"
|
|
20
|
+
|
|
21
|
+
#include <stdint.h>
|
|
22
|
+
#include <string.h>
|
|
23
|
+
|
|
24
|
+
#if defined(_WIN32)
|
|
25
|
+
#include <winsock2.h>
|
|
26
|
+
#include <ws2tcpip.h>
|
|
27
|
+
typedef int xt_tls_backend_socklen_t;
|
|
28
|
+
#define xt_tls_backend_closesocket closesocket
|
|
29
|
+
#else
|
|
30
|
+
#include <netdb.h>
|
|
31
|
+
#include <sys/socket.h>
|
|
32
|
+
#include <unistd.h>
|
|
33
|
+
typedef socklen_t xt_tls_backend_socklen_t;
|
|
34
|
+
#define xt_tls_backend_closesocket close
|
|
35
|
+
#endif
|
|
36
|
+
|
|
37
|
+
/* Provided by net.c (linked with the node extension). */
|
|
38
|
+
extern xt_value xt_net_static(xt_value name, int32_t argc, xt_value *argv);
|
|
39
|
+
|
|
40
|
+
/* Byte helpers exported by buffer.c. */
|
|
41
|
+
int xt_node_is_buffer(xt_value value);
|
|
42
|
+
unsigned char *xt_node_buffer_bytes(xt_value value, size_t *outLength);
|
|
43
|
+
|
|
44
|
+
static xt_value xt_tls_socket_proto(void);
|
|
45
|
+
|
|
46
|
+
#include "parts/helpers.inc"
|
|
47
|
+
#include "parts/socket.inc"
|
|
48
|
+
#include "parts/connect.inc"
|
|
49
|
+
#include "parts/server.inc"
|
|
50
|
+
#include "parts/api.inc"
|
|
@@ -22,6 +22,7 @@ typedef struct {
|
|
|
22
22
|
size_t request_length;
|
|
23
23
|
xt_fetch_buffer raw;
|
|
24
24
|
int failed;
|
|
25
|
+
char error[256];
|
|
25
26
|
} xt_fetch_hop;
|
|
26
27
|
|
|
27
28
|
struct xt_fetch_state {
|
|
@@ -67,13 +68,14 @@ static void xt_fetch_hop_run(xt_async_job *job) {
|
|
|
67
68
|
hop->failed = 1;
|
|
68
69
|
return;
|
|
69
70
|
}
|
|
70
|
-
|
|
71
|
-
if (
|
|
71
|
+
xt_fetch_stream stream;
|
|
72
|
+
if (xt_fetch_stream_open(&parsed, &stream, hop->error, sizeof(hop->error)) != 0) {
|
|
72
73
|
hop->failed = 1;
|
|
73
74
|
return;
|
|
74
75
|
}
|
|
75
|
-
if (
|
|
76
|
-
|
|
76
|
+
if (xt_fetch_stream_send(&stream, hop->request, hop->request_length) != 0) {
|
|
77
|
+
snprintf(hop->error, sizeof(hop->error), "cannot write the request");
|
|
78
|
+
xt_fetch_stream_close(&stream);
|
|
77
79
|
hop->failed = 1;
|
|
78
80
|
return;
|
|
79
81
|
}
|
|
@@ -107,14 +109,18 @@ static void xt_fetch_hop_run(xt_async_job *job) {
|
|
|
107
109
|
if (stop) break;
|
|
108
110
|
}
|
|
109
111
|
}
|
|
110
|
-
int count =
|
|
112
|
+
int count = xt_fetch_stream_recv(&stream, chunk, sizeof(chunk));
|
|
111
113
|
if (count > 0) {
|
|
112
114
|
xt_fetch_buffer_append(&hop->raw, chunk, (size_t)count);
|
|
113
115
|
continue;
|
|
114
116
|
}
|
|
117
|
+
if (count < 0 && hop->raw.length == 0) {
|
|
118
|
+
snprintf(hop->error, sizeof(hop->error), "connection failed");
|
|
119
|
+
hop->failed = 1;
|
|
120
|
+
}
|
|
115
121
|
break;
|
|
116
122
|
}
|
|
117
|
-
|
|
123
|
+
xt_fetch_stream_close(&stream);
|
|
118
124
|
}
|
|
119
125
|
|
|
120
126
|
#define XT_FETCH_HOP(c) ((xt_fetch_hop *)(c))
|
|
@@ -125,7 +131,13 @@ static void xt_fetch_hop_complete(xt_async_job *job) {
|
|
|
125
131
|
xt_fetch_hop *hop = XT_FETCH_HOP(job);
|
|
126
132
|
xt_fetch_state *state = hop->state;
|
|
127
133
|
if (hop->failed) {
|
|
128
|
-
|
|
134
|
+
if (hop->error[0] != '\0') {
|
|
135
|
+
char message[320];
|
|
136
|
+
snprintf(message, sizeof(message), "fetch failed: %s", hop->error);
|
|
137
|
+
xt_fetch_state_reject(state, message);
|
|
138
|
+
} else {
|
|
139
|
+
xt_fetch_state_reject(state, "fetch failed");
|
|
140
|
+
}
|
|
129
141
|
return;
|
|
130
142
|
}
|
|
131
143
|
|
|
@@ -266,11 +278,6 @@ static void xt_fetch_start(xt_fetch_state *state) {
|
|
|
266
278
|
xt_fetch_state_reject(state, "Failed to parse URL");
|
|
267
279
|
return;
|
|
268
280
|
}
|
|
269
|
-
if (parsed.secure) {
|
|
270
|
-
xt_fetch_state_reject(state, "fetch failed: https is not supported");
|
|
271
|
-
return;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
281
|
xt_value requestHeaders = xt_headers_from_source(state->headers_source);
|
|
275
282
|
xt_fetch_buffer request;
|
|
276
283
|
xt_fetch_buffer_init(&request);
|