add support for socks5, socks4, and http proxies

This commit is contained in:
ading2210 2024-07-19 00:47:14 -04:00
parent 9f033fc848
commit 4d64b27a82
9 changed files with 58 additions and 5 deletions

View file

@ -88,6 +88,9 @@ class HTTPSession extends CurlSession {
if (this.cookie_filename && params.credentials !== "omit") {
c_func(_http_set_cookie_jar, [http_handle, this.cookie_filename]);
}
if (params.proxy) {
c_func_str(_request_set_proxy, [http_handle, params.proxy]);
}
this.start_request(http_handle);
});
@ -110,6 +113,12 @@ class HTTPSession extends CurlSession {
else {
url = "" + url;
}
if (this.options && this.options.proxy) {
params.proxy = this.options.proxy;
}
check_proxy(params.proxy);
let body = await this.constructor.create_options(params);
return await this.request_async(url, params, body);
}
@ -152,6 +161,7 @@ class HTTPSession extends CurlSession {
if (params.body instanceof ReadableStream) {
params.duplex = "half";
}
let request_obj = new Request("http://127.0.0.1/", params);
let array_buffer = await request_obj.arrayBuffer();
if (array_buffer.byteLength > 0) {

View file

@ -17,8 +17,15 @@ class TLSSocket extends CurlSession {
this.connected = false;
this.recv_loop = null;
this.set_connections(1, 0);
this.connect();
try {
check_proxy(this.options.proxy);
this.set_connections(1, 0);
this.connect();
}
catch (e) {
this.cleanup(true);
throw e;
}
}
connect() {
@ -40,6 +47,9 @@ class TLSSocket extends CurlSession {
this.http_handle = this.create_request(this.url, data_callback, finish_callback, headers_callback);
_tls_socket_set_options(this.http_handle, +this.options.verbose);
if (this.options.proxy) {
c_func_str(_request_set_proxy, [this.http_handle, this.options.proxy]);
}
this.start_request(this.http_handle);
}

View file

@ -104,4 +104,14 @@ function c_func_str(target, args=[]) {
let str = UTF8ToString(ptr);
_free(ptr);
return str;
}
//ensure that the proxy url has a valid protocol
function check_proxy(proxy) {
if (typeof proxy === "string" || proxy instanceof String) {
let protocol = new URL(proxy).protocol;
if (!["socks5h:", "socks4a:", "http:"].includes(protocol)) {
throw new TypeError("Only socks5h, socks4a, and http proxies are supported.");
}
}
}

View file

@ -19,8 +19,15 @@ class CurlWebSocket extends CurlSession {
this.http_handle = null;
this.recv_buffer = [];
this.set_connections(1, 0);
this.connect();
try {
check_proxy(this.options.proxy);
this.set_connections(1, 0);
this.connect();
}
catch (e) {
this.cleanup(true);
throw e;
}
}
connect() {
@ -52,6 +59,9 @@ class CurlWebSocket extends CurlSession {
this.http_handle = this.create_request(this.url, data_callback, finish_callback, headers_callback);
c_func(_http_set_options, [this.http_handle, JSON.stringify(request_options), null, 0]);
_websocket_set_options(this.http_handle);
if (this.options.proxy) {
c_func_str(_request_set_proxy, [this.http_handle, this.options.proxy]);
}
this.start_request(this.http_handle);
}