Debian Codegraph @

src/backend/plugins/pacrunner-duktape/pacrunner-duktape.c

raw
1 /* pacrunner-duktape.c
2 *
3 * Copyright 2022-2023 The Libproxy Team
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include <gio/gio.h>
23
24 #include <unistd.h>
25 #ifdef __WIN32__
26 #include <ws2tcpip.h>
27 #else
28 #include <netdb.h>
29 #include <netinet/in.h>
30 #endif
31
32 #include "pacrunner-duktape.h"
33 #include "pacutils.h"
34 #include "px-plugin-pacrunner.h"
35
36 #include "duktape.h"
37
38 struct _PxPacRunnerDuktape {
39 GObject parent_instance;
40 duk_context *ctx;
41 };
42
43 static void px_pacrunner_iface_init (PxPacRunnerInterface *iface);
44
45 G_DEFINE_FINAL_TYPE_WITH_CODE (PxPacRunnerDuktape,
46 px_pacrunner_duktape,
47 G_TYPE_OBJECT,
48 G_IMPLEMENT_INTERFACE (PX_TYPE_PACRUNNER, px_pacrunner_iface_init))
49
50
51 static duk_ret_t
52 dns_resolve (duk_context *ctx)
53 {
54 const char *hostname = NULL;
55 struct addrinfo *info;
56 char tmp[INET6_ADDRSTRLEN + 1];
57
58 if (duk_get_top (ctx) != 1) {
59 /* Invalid number of arguments */
60 return 0;
61 }
62
63 /* We do not need to free the string - It's managed by Duktape. */
64 hostname = duk_get_string (ctx, 0);
65 if (!hostname)
66 return 0;
67
68 /* Look it up */
69 if (getaddrinfo (hostname, NULL, NULL, &info))
70 return 0;
71
72 /* Try for IPv4 */
73 if (getnameinfo (info->ai_addr,
74 info->ai_addrlen,
75 tmp,
76 INET6_ADDRSTRLEN + 1,
77 NULL,
78 0,
79 NI_NUMERICHOST)) {
80 freeaddrinfo (info);
81 duk_push_null (ctx);
82 return 1;
83 }
84 freeaddrinfo (info);
85
86 /* Create the return value */
87 duk_push_string (ctx, tmp);
88
89 return 1;
90 }
91
92 static duk_ret_t
93 my_ip_address (duk_context *ctx)
94 {
95 char hostname[1024];
96
97 hostname[sizeof (hostname) - 1] = '\0';
98
99 if (!gethostname (hostname, sizeof (hostname) - 1)) {
100 duk_push_string (ctx, hostname);
101 return dns_resolve (ctx);
102 }
103
104 return duk_error (ctx, DUK_ERR_ERROR, "Unable to find hostname!");
105 }
106
107 static duk_ret_t
108 alert (duk_context *ctx)
109 {
110 const char *str = NULL;
111
112 /* do nothing if PX_DEBUG_PACALERT environment is not set */
113 if (!getenv ("PX_DEBUG_PACALERT"))
114 return 0;
115
116 /* only get first argument of alert() as string */
117 str = duk_get_string (ctx, 0);
118 if (!str)
119 return 0;
120
121 fprintf (stderr, "PAC-alert: %s\n", str);
122 return 0;
123 }
124
125 static void
126 px_pacrunner_duktape_init (PxPacRunnerDuktape *self)
127 {
128 self->ctx = duk_create_heap_default ();
129 if (!self->ctx)
130 return;
131
132 duk_push_c_function (self->ctx, dns_resolve, 1);
133 duk_put_global_string (self->ctx, "dnsResolve");
134
135 duk_push_c_function (self->ctx, my_ip_address, 1);
136 duk_put_global_string (self->ctx, "myIpAddress");
137
138 duk_push_c_function (self->ctx, alert, 1);
139 duk_put_global_string (self->ctx, "alert");
140
141 duk_push_string (self->ctx, JAVASCRIPT_ROUTINES);
142 if (duk_peval_noresult (self->ctx))
143 goto error;
144
145 return;
146
147 error:
148 duk_destroy_heap (self->ctx);
149 }
150
151 static void
152 px_pacrunner_duktape_dispose (GObject *object)
153 {
154 PxPacRunnerDuktape *self = PX_PACRUNNER_DUKTAPE (object);
155
156 g_clear_pointer (&self->ctx, duk_destroy_heap);
157
158 G_OBJECT_CLASS (px_pacrunner_duktape_parent_class)->dispose (object);
159 }
160
161 static void
162 px_pacrunner_duktape_class_init (PxPacRunnerDuktapeClass *klass)
163 {
164 GObjectClass *object_class = G_OBJECT_CLASS (klass);
165
166 object_class->dispose = px_pacrunner_duktape_dispose;
167 }
168
169 static gboolean
170 px_pacrunner_duktape_set_pac (PxPacRunner *pacrunner,
171 GBytes *pac_data)
172 {
173 PxPacRunnerDuktape *self = PX_PACRUNNER_DUKTAPE (pacrunner);
174 gsize len;
175 gconstpointer content = g_bytes_get_data (pac_data, &len);
176
177 duk_push_lstring (self->ctx, content, len);
178
179 if (duk_peval_noresult (self->ctx)) {
180 return FALSE;
181 }
182
183 return TRUE;
184 }
185
186 static char *
187 px_pacrunner_duktape_run (PxPacRunner *pacrunner,
188 GUri *uri)
189 {
190 PxPacRunnerDuktape *self = PX_PACRUNNER_DUKTAPE (pacrunner);
191 duk_int_t result;
192
193 duk_get_global_string (self->ctx, "FindProxyForURL");
194 duk_push_string (self->ctx, g_uri_to_string (uri));
195 duk_push_string (self->ctx, g_uri_get_host (uri));
196 result = duk_pcall (self->ctx, 2);
197
198 if (result == 0) {
199 const char *proxy = duk_get_string (self->ctx, 0);
200 char *proxy_string;
201
202 if (!proxy) {
203 duk_pop (self->ctx);
204 return g_strdup ("");
205 }
206
207 proxy_string = g_strdup (proxy);
208
209 duk_pop (self->ctx);
210
211 return proxy_string;
212 }
213
214 duk_pop (self->ctx);
215 return g_strdup ("");
216 }
217
218 static void
219 px_pacrunner_iface_init (PxPacRunnerInterface *iface)
220 {
221 iface->set_pac = px_pacrunner_duktape_set_pac;
222 iface->run = px_pacrunner_duktape_run;
223 }