Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
schettn committed Jun 24, 2024
1 parent 0f69925 commit ad710da
Showing 1 changed file with 64 additions and 26 deletions.
90 changes: 64 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { Context, PylonAPI, auth, defineService } from "@getcronit/pylon";
import { Handler } from "hono";
import { Agent } from "https";
import https from "https";
import http from "http";
import { createBunWebSocket } from "hono/bun";
import fetch from "node-fetch";

import dotenv from "dotenv";

const httpsAgent = new Agent({
rejectUnauthorized: false,
});

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

function basicProxy(proxy_url = ""): Handler {
Expand All @@ -29,8 +26,6 @@ function basicProxy(proxy_url = ""): Handler {
if (c.req.query()) url = url + "?" + new URLSearchParams(c.req.query());
// request

const blob = await c.req.blob();

console.log("Request", c.req.raw.method, url, c.req.raw.headers.values());

const headers = {};
Expand All @@ -39,27 +34,64 @@ function basicProxy(proxy_url = ""): Handler {
headers[key] = value;
});

const response = await axios({
method: c.req.raw.method,
url: url,
headers: headers,
data: blob.size > 0 ? blob : undefined,
responseType: "arraybuffer", // Adjust as per your response type (blob, json, etc.)
httpsAgent: httpsAgent, // Include any https agent configuration needed
const httpModule = url.startsWith("https") ? https : http;

const agent = new httpModule.Agent({
rejectUnauthorized: false,
});

console.log(
"Response",
response.status,
response.statusText,
response.headers
);
// Construct options for the https.request
const requestOptions = {
method: c.req.raw.method,
headers,
rejectUnauthorized: false,
agent: agent,
};

console.log("Request options", requestOptions);

// Perform the request using https.request
return new Promise(async (resolve, reject) => {
console.log("Requesting", url, requestOptions);
const req = httpModule.request(url, requestOptions, (res) => {
const chunks = [];
res.on("data", (chunk) => {
chunks.push(chunk);
});
res.on("end", () => {
const responseBody = Buffer.concat(chunks);

console.log(
"Response",
res.statusCode,
res.statusMessage,
res.headers
);

// Return the response
resolve(
new Response(responseBody, {
status: res.statusCode,
statusText: res.statusMessage,
headers: res.headers,
})
);
});
});

// Return the response
return new Response(response.data, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
// Handle request errors
req.on("error", (err) => {
console.error("Error making request:", err);
reject(err);
});

// Write request body if present
const blob = await c.req.blob();
if (blob.size > 0) {
req.write(blob);
}

req.end();
});
};
}
Expand Down Expand Up @@ -127,8 +159,14 @@ export const configureApp: PylonAPI["configureApp"] = async (app) => {

console.log("Connecting to", url);

const httpModule = url.startsWith("https") ? https : http;

const agent = new httpModule.Agent({
rejectUnauthorized: false,
});

const targetWs = new WebSocket(url, {
agent: httpsAgent,
agent: agent,
origin: new URL(c.req.url).origin,
});

Expand Down

0 comments on commit ad710da

Please sign in to comment.