mirror of
https://github.com/bdnugget/sse-vue-test.git
synced 2026-11-07 07:07:27 +00:00
Ghetto fix internal ips so I can use it on local network (Vue IP hardcoded derp)
;
This commit is contained in:
1 parent
fa784058ab
commit
20857d4c8f
3 files changed
+17
-13
No files matched your search
@@ -3,15 +3,17 @@ const express = require('express');
|
||||
const http = require('http');
|
||||
const path = require('path');
|
||||
const { createProxyMiddleware } = require('http-proxy-middleware');
|
||||
// Prefer local network address instead of internal IP
|
||||
const localIP = [].concat(...Object.values(require('os').networkInterfaces())).find(x => !x.internal && x.family === 'IPv4')?.address || "127.0.0.1";
|
||||
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
|
||||
app.use(express.static(path.join(__dirname, 'public')));
|
||||
|
||||
app.use('/stream', createProxyMiddleware({ target: 'http://localhost:3001', ws: false }));
|
||||
app.use('/stream', createProxyMiddleware({ target: 'http://' + localIP + ':3001', ws: false, changeOrigin: true }));
|
||||
|
||||
server.listen(3000, () => {
|
||||
console.log('Server running at http://localhost:3000/');
|
||||
server.listen(3000, localIP, () => {
|
||||
console.log('Server running at http://' + localIP + ':3000/');
|
||||
});
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ export default {
|
||||
},
|
||||
methods: {
|
||||
setupSSE() {
|
||||
const eventSource = new EventSource('http://localhost:3000/stream');
|
||||
const eventSource = new EventSource(`http://192.168.178.115:3000/stream`);
|
||||
eventSource.onmessage = event => {
|
||||
this.messages.push(event.data);
|
||||
};
|
||||
|
||||
+11
-9
@@ -1,17 +1,19 @@
|
||||
const http = require('http');
|
||||
const { isIPInRange } = require('./ipTools');
|
||||
// const vuePort = 8080; // @TODO bd This is not guaranteed, find a better way!
|
||||
|
||||
const server = http.createServer((req, res) => {
|
||||
const allowedOrigins = [
|
||||
'http://localhost:8080',
|
||||
'http://192.168.0.0/16:8080', // Common LAN range 192.168.x.x
|
||||
'http://10.0.0.0/8:8080', // Common LAN range 10.x.x.x
|
||||
'http://172.16.0.0/12:8080', // Common LAN range 172.16.x.x - 172.31.x.x
|
||||
// 'http://192.168.178.115:8080',
|
||||
const allowedSubnets = [
|
||||
'192.168.0.0/16', // Common LAN range 192.168.x.x
|
||||
'10.0.0.0/8', // Common LAN range 10.x.x.x
|
||||
'172.16.0.0/12', // Common LAN range 172.16.x.x - 172.31.x.x
|
||||
];
|
||||
|
||||
const origin = req.headers.origin;
|
||||
console.log('orig: ' + origin);
|
||||
if (allowedOrigins.includes(origin)) {
|
||||
console.log(origin);
|
||||
const ip = origin.split('//')[1].split(':')[0]; // Extract IP part from the origin
|
||||
|
||||
if (allowedSubnets.some(subnet => isIPInRange(ip, subnet))) {
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
@@ -31,7 +33,7 @@ const server = http.createServer((req, res) => {
|
||||
const nextTick = new Date(d.getTime() + randomInterval);
|
||||
const message = `data: ${d.toLocaleTimeString()} - Next interval: ${randomInterval/1000}s at ${nextTick.toLocaleTimeString()}\n\n`;
|
||||
res.write(message);
|
||||
|
||||
|
||||
setTimeout(sendRandomMessage, randomInterval); // Set timeout for the next message
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user