-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
102 lines (86 loc) · 2.76 KB
/
Copy pathproxy.js
File metadata and controls
102 lines (86 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require('dotenv').config();
const express = require('express');
const https = require('https');
const { Pool } = require('pg');
const app = express();
const PORT = process.env.PROXY_PORT || 3001;
console.log('🛠 Starting proxy.js...');
console.log('🧪 Creating DB Pool...');
const pool = new Pool({
user: process.env.PG_USER,
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
password: process.env.PG_PASSWORD,
port: parseInt(process.env.PG_PORT, 10),
});
console.log('🧪 DB Pool created');
// 🧠 Fetch server + token from DB
async function getAuthInfo() {
try {
const result = await pool.query('SELECT server, ph_auth_token FROM configurations LIMIT 1');
if (result.rows.length === 0) throw new Error('No config found in DB');
const { server, ph_auth_token } = result.rows[0];
console.log(`🔗 SOAR target from DB: ${server}`);
return { server, token: ph_auth_token };
} catch (err) {
console.error('❌ DB error:', err.message);
process.exit(1);
}
}
// CORS
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept, Authorization, ph-auth-token'
);
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
if (req.method === 'OPTIONS') return res.sendStatus(200);
next();
});
// Logging
app.use(express.json());
app.use((req, res, next) => {
console.log(`📩 Incoming Request: ${req.method} ${req.originalUrl}`);
console.log('🧾 Incoming Headers:', req.headers);
next();
});
// Manual proxy handler
(async () => {
const { server, token } = await getAuthInfo();
const targetHost = new URL(server).hostname;
const targetPort = 443;
app.all('/proxy/*', (req, res) => {
const path = req.originalUrl.replace(/^\/proxy/, '');
const options = {
hostname: targetHost,
port: targetPort,
path,
method: req.method,
rejectUnauthorized: false,
headers: {
'Content-Type': 'application/json',
'ph-auth-token': token,
},
};
console.log(`➡️ ${req.method} ${path}`);
const proxyReq = https.request(options, (proxyRes) => {
let data = '';
proxyRes.on('data', chunk => data += chunk);
proxyRes.on('end', () => {
res.status(proxyRes.statusCode).send(data);
});
});
proxyReq.on('error', err => {
console.error('❌ Proxy Error:', err.message);
res.status(500).send('Proxy Error: ' + err.message);
});
if (req.body && Object.keys(req.body).length > 0) {
proxyReq.write(JSON.stringify(req.body));
}
proxyReq.end();
});
app.listen(PORT, () => {
console.log(`✅ Proxy running at http://localhost:${PORT} → ${server}`);
});
})();