Create simple-proxy.js
Browse files- simple-proxy.js +138 -0
simple-proxy.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// simple-proxy.js - Enhanced version for Hugging Face Spaces
|
| 2 |
+
import http from 'node:http';
|
| 3 |
+
import net from 'node:net';
|
| 4 |
+
import { URL } from 'node:url';
|
| 5 |
+
|
| 6 |
+
// Configuration
|
| 7 |
+
const CONFIG = {
|
| 8 |
+
// Port - Hugging Face provides this via PORT environment variable
|
| 9 |
+
PORT: process.env.PORT || 8080,
|
| 10 |
+
|
| 11 |
+
// Allowed domains - customize this list for your needs
|
| 12 |
+
ALLOWED_DOMAINS: [
|
| 13 |
+
'huggingface.co',
|
| 14 |
+
'googleapis.com',
|
| 15 |
+
'example.com',
|
| 16 |
+
'*.example.com' // Allow subdomains
|
| 17 |
+
],
|
| 18 |
+
|
| 19 |
+
// Enable logging
|
| 20 |
+
LOG_REQUESTS: process.env.LOG_REQUESTS !== 'false',
|
| 21 |
+
|
| 22 |
+
// Timeout settings (ms)
|
| 23 |
+
CONNECT_TIMEOUT: 30000
|
| 24 |
+
};
|
| 25 |
+
|
| 26 |
+
// Logging helper
|
| 27 |
+
const logger = {
|
| 28 |
+
info: (msg) => console.log(`[INFO] ${new Date().toISOString()} - ${msg}`),
|
| 29 |
+
error: (msg) => console.error(`[ERROR] ${new Date().toISOString()} - ${msg}`),
|
| 30 |
+
warn: (msg) => console.warn(`[WARN] ${new Date().toISOString()} - ${msg}`)
|
| 31 |
+
};
|
| 32 |
+
|
| 33 |
+
// Check if domain is allowed
|
| 34 |
+
function isDomainAllowed(hostname) {
|
| 35 |
+
for (const pattern of CONFIG.ALLOWED_DOMAINS) {
|
| 36 |
+
if (pattern.startsWith('*.')) {
|
| 37 |
+
// Wildcard subdomain matching
|
| 38 |
+
const baseDomain = pattern.slice(2);
|
| 39 |
+
if (hostname === baseDomain || hostname.endsWith(`.${baseDomain}`)) {
|
| 40 |
+
return true;
|
| 41 |
+
}
|
| 42 |
+
} else if (hostname === pattern) {
|
| 43 |
+
return true;
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
return false;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
// Create HTTP server
|
| 50 |
+
const server = http.createServer((req, res) => {
|
| 51 |
+
// Only CONNECT method is supported for HTTPS proxying
|
| 52 |
+
res.writeHead(405, {
|
| 53 |
+
'Content-Type': 'application/json',
|
| 54 |
+
'Access-Control-Allow-Origin': '*'
|
| 55 |
+
});
|
| 56 |
+
res.end(JSON.stringify({
|
| 57 |
+
error: 'Method Not Allowed',
|
| 58 |
+
message: 'This proxy only supports CONNECT method for HTTPS tunneling'
|
| 59 |
+
}));
|
| 60 |
+
});
|
| 61 |
+
|
| 62 |
+
// Handle CONNECT requests (HTTPS tunneling)
|
| 63 |
+
server.on('connect', (req, clientSocket, head) => {
|
| 64 |
+
const { hostname, port } = new URL(`http://${req.url}`);
|
| 65 |
+
const targetPort = port || '443';
|
| 66 |
+
|
| 67 |
+
if (CONFIG.LOG_REQUESTS) {
|
| 68 |
+
logger.info(`CONNECT ${hostname}:${targetPort} from ${req.socket.remoteAddress}`);
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
// Security checks
|
| 72 |
+
if (targetPort !== '443') {
|
| 73 |
+
logger.warn(`Blocked non-HTTPS port: ${targetPort}`);
|
| 74 |
+
clientSocket.end('HTTP/1.1 403 Forbidden\r\n\r\nNon-HTTPS ports not allowed');
|
| 75 |
+
return;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
if (!isDomainAllowed(hostname)) {
|
| 79 |
+
logger.warn(`Blocked domain: ${hostname}`);
|
| 80 |
+
clientSocket.end('HTTP/1.1 403 Forbidden\r\n\r\nDomain not allowed');
|
| 81 |
+
return;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
// Connect to target server
|
| 85 |
+
const serverSocket = net.connect({
|
| 86 |
+
host: hostname,
|
| 87 |
+
port: targetPort,
|
| 88 |
+
timeout: CONFIG.CONNECT_TIMEOUT
|
| 89 |
+
});
|
| 90 |
+
|
| 91 |
+
// Handle successful connection
|
| 92 |
+
serverSocket.on('connect', () => {
|
| 93 |
+
clientSocket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
|
| 94 |
+
serverSocket.write(head);
|
| 95 |
+
|
| 96 |
+
// Pipe data between client and server
|
| 97 |
+
serverSocket.pipe(clientSocket);
|
| 98 |
+
clientSocket.pipe(serverSocket);
|
| 99 |
+
|
| 100 |
+
if (CONFIG.LOG_REQUESTS) {
|
| 101 |
+
logger.info(`Connected to ${hostname}:${targetPort}`);
|
| 102 |
+
}
|
| 103 |
+
});
|
| 104 |
+
|
| 105 |
+
// Handle errors
|
| 106 |
+
serverSocket.on('error', (err) => {
|
| 107 |
+
logger.error(`Connection error to ${hostname}:${targetPort} - ${err.message}`);
|
| 108 |
+
clientSocket.end('HTTP/1.1 502 Bad Gateway\r\n\r\n');
|
| 109 |
+
});
|
| 110 |
+
|
| 111 |
+
clientSocket.on('error', (err) => {
|
| 112 |
+
logger.error(`Client socket error: ${err.message}`);
|
| 113 |
+
serverSocket.destroy();
|
| 114 |
+
});
|
| 115 |
+
|
| 116 |
+
// Handle timeouts
|
| 117 |
+
serverSocket.setTimeout(CONFIG.CONNECT_TIMEOUT, () => {
|
| 118 |
+
logger.warn(`Timeout connecting to ${hostname}:${targetPort}`);
|
| 119 |
+
clientSocket.end('HTTP/1.1 504 Gateway Timeout\r\n\r\n');
|
| 120 |
+
serverSocket.destroy();
|
| 121 |
+
});
|
| 122 |
+
});
|
| 123 |
+
|
| 124 |
+
// Start server
|
| 125 |
+
server.listen(CONFIG.PORT, () => {
|
| 126 |
+
logger.info(`Secure HTTPS Proxy Server started on port ${CONFIG.PORT}`);
|
| 127 |
+
logger.info(`Allowed domains: ${CONFIG.ALLOWED_DOMAINS.join(', ')}`);
|
| 128 |
+
logger.info(`Ready to accept CONNECT requests`);
|
| 129 |
+
});
|
| 130 |
+
|
| 131 |
+
// Handle graceful shutdown
|
| 132 |
+
process.on('SIGTERM', () => {
|
| 133 |
+
logger.info('Received SIGTERM, shutting down gracefully');
|
| 134 |
+
server.close(() => {
|
| 135 |
+
logger.info('Server closed');
|
| 136 |
+
process.exit(0);
|
| 137 |
+
});
|
| 138 |
+
});
|