experimental wtv-search and wtv-proxy (disabled by default, requires external services)

This commit is contained in:
zefie
2025-07-22 14:53:37 -04:00
parent 60f6709171
commit dca7ce7389
6 changed files with 386 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
minisrv_service_file = true;
request_is_async = true;
const proxyUrl = minisrv_config.services['wtv-proxy'].wrp_url;
if (!proxyUrl.endsWith('/')) {
proxyUrl += '/';
}
// Remove 'wtv-proxy:/' from the start of request_url
let forwardPath = request_headers.request_url.replace(/^wtv-proxy:\//, '');
// Build the full URL to forward to
const targetUrl = proxyUrl + forwardPath;
// Forward the request using http(s) module
const urlObj = new URL(targetUrl);
const lib = urlObj.protocol === 'https:' ? https : http;
lib.get(targetUrl, (res) => {
let headers = `200 OK\n`;
// Copy content-type if present
if (res.headers['content-type']) {
headers += `Content-Type: ${res.headers['content-type']}\n`;
}
// Optionally copy other headers as needed
let data = [];
res.on('data', chunk => data.push(chunk));
res.on('end', () => {
sendToClient(socket, headers, Buffer.concat(data));
});
}).on('error', err => {
sendToClient(socket, '200 OK\nContent-Type: text/plain', `Error fetching image: ${err.message}`);
});

View File

@@ -0,0 +1,68 @@
minisrv_service_file = true;
request_is_async = true;
const proxyUrl = minisrv_config.services['wtv-proxy'].wrp_url;
if (!proxyUrl.endsWith('/')) {
proxyUrl += '/';
}
// Remove 'wtv-proxy:/' from the start of request_url
let forwardPath = request_headers.request_url.replace(/^wtv-proxy:\//, '');
// Build the full URL to forward to
var targetUrl = proxyUrl + forwardPath;
// Forward the request using http(s) module
const urlObj = new URL(targetUrl);
const lib = urlObj.protocol === 'https:' ? https : http;
coords = request_headers.request_url.split("?")[1];
if (!coords) {
coords = '0,0'
}
console.log(`Forwarding request to ${targetUrl} with coordinates ${coords}`);
targetUrl += `?${coords}`; // Append coordinates to the target URL
lib.get(targetUrl, (res) => {
let headers = `200 OK\n`;
// Copy content-type if present
if (res.headers['content-type']) {
headers += `Content-Type: ${res.headers['content-type']}\n`;
}
// Optionally copy other headers as needed
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
if (data.startsWith('<HTML')) {
// success page
// Find all <a href="..."><img src="..."></a> and extract the URLs
const aHrefMatch = data.match(/<a\s+href="([^"]+)"/i);
const imgSrcMatch = data.match(/<img\s+src="([^"]+)"/i);
const links = [];
if (aHrefMatch && imgSrcMatch) {
links.push({
href: aHrefMatch[1],
img: imgSrcMatch[1]
});
}
var proxy_id = links[0].href.replace(/\/map\//, '');
proxy_id = proxy_id.replace(/\.map/, '');
var imgExt = links[0].img.split('.').pop().split('?')[0].toLowerCase();
const urlInputMatch = data.match(/<input[^>]+type=["']text["'][^>]+name=["']url["'][^>]+value=["']([^"']+)["']/i);
let pageUrl = '';
if (urlInputMatch) {
pageUrl = urlInputMatch[1];
}
var redirectUrl = `wtv-proxy:/proxy?id=${proxy_id}&t=${imgExt}&url=${encodeURIComponent(pageUrl)}`;
sendToClient(socket, {'Status': 302, 'Location': redirectUrl}, '');
} else {
var idx = data.indexOf('<BR>');
data = data.substring(0, idx);
var redirectUrl = `wtv-proxy:/proxy?err=${escape(data)}`;
sendToClient(socket, {'Status': 302, 'Location': redirectUrl}, '');
}
});
}).on('error', err => {
sendToClient(socket, '200 OK\nContent-Type: text/plain', `Error fetching image: ${err.message}`);
});

View File

@@ -0,0 +1,118 @@
minisrv_service_file = true;
request_is_async = true;
proxyUrl = minisrv_config.services[service_name].wrp_url;
if (!proxyUrl.endsWith('/')) {
proxyUrl += '/';
}
if (!request_headers.query.url) {
headers = `200 OK
Content-Type: text/html`;
data = `
<html>
<head>
<title>Web Rendering Proxy</title>
</head>
<body bgcolor="#191919" text="#44cc55" link="36d5ff" vlink="36d5ff">
<h1>Web Rendering Proxy</h1>
<p>Welcome to the Web Rendering Proxy.<br>Please provide a valid URL to render.</p>
<form method="POST" action="wtv-proxy:/proxy">
<label for="url"> URL:</label>
<input type="text" id="url" name="url" value="https://" size=38>
<input type="hidden" name="z" value="1.0">
<input type="hidden" name="t" value="jpg">
<input type="hidden" name="c" value="256">
<input type="hidden" name="m" value="ismap">
<input type="submit" value="Go">
</form>
</body>
</html>`
sendToClient(socket, headers, data);
} else {
if (request_headers.query.err) {
finishPage(`<h1>Error</h1><p>${request_headers.query.err}</p>`).join('<br>');
} else {
const params = new URLSearchParams({
url: request_headers.query.url,
z: request_headers.query.z || '1.0',
t: request_headers.query.t || 'jpg',
c: request_headers.query.c || '256',
m: request_headers.query.m || 'ismap'
});
const fullUrl = proxyUrl + '?' + params.toString();
const urlObj = new URL(fullUrl);
const lib = urlObj.protocol === 'https:' ? https : http;
if (request_headers.query.id) {
finishPage(`<a href="/map/${request_headers.query.id}.map"><img src="/img/${request_headers.query.id}.${params.get('t')}" ISMAP></a>`);
} else {
function fetch(url) {
return new Promise((resolve, reject) => {
lib.get(url, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => resolve({ text: () => Promise.resolve(data) }));
}).on('error', reject);
});
}
fetch(fullUrl)
.then(response => response.text())
.then(text => { process(text); })
.catch(err => { finishPage(`Error fetching page: ${err.message}`); });
}
}
}
function process(content) {
if (content.startsWith('<HTML')) {
// success page
// Find all <a href="..."><img src="..."></a> and extract the URLs
const aHrefMatch = content.match(/<a\s+href="([^"]+)"/i);
const imgSrcMatch = content.match(/<img\s+src="([^"]+)"/i);
const links = [];
if (aHrefMatch && imgSrcMatch) {
links.push({
href: aHrefMatch[1],
img: imgSrcMatch[1]
});
}
finishPage(links.map(link => `<a href="${link.href}"><img src="${link.img}" ISMAP></a>`).join('<br>'));
// You can now use the `links` array as needed
} else {
var idx = content.indexOf('<BR>');
content = content.substring(0, idx);
finishPage(content);
}
}
function finishPage(content) {
headers = `200 OK
Content-Type: text/html`;
data = `
<html>
<head>
<title>Web Rendering Proxy</title>
</head>
<body bgcolor="#191919" text="#44cc55" link="36d5ff" vlink="36d5ff">
<form method="POST" action="wtv-proxy:/proxy">
<label for="url">URL:</label>
<input type="text" id="url" name="url" value="${request_headers.query.url}" size=30>
<input type="hidden" name="z" value="${request_headers.query.z || '1.0'}">
<input type="hidden" name="t" value="${request_headers.query.t || 'jpg'}">
<input type="hidden" name="c" value="${request_headers.query.c || '216'}">
<input type="submit" value="Go">
<input type="submit" name="Fn" value="Bk">
<input type="submit" name="Fn" value="Re">
<hr>
${content}
</form>
<hr>
<center>
<a href="/proxy">Start Over</a>
</center>
</body>
</html>`;
sendToClient(socket, headers, data);
}