This repository has been archived by the owner on Nov 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
100 lines (79 loc) · 2.11 KB
/
index.js
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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var request = require('request');
var bodyParser = require('body-parser');
app.use(bodyParser());
app.get('/realtime/', function(req, res){
res.sendFile(__dirname + '/files/index.html');
});
app.post('/events/', function(req, res){
if(req.body){
console.log(req.body);
if(req.body.is_admin){
io.to("admin").emit("event", req.body);
}else{
io.emit("event", req.body);
}
res.status(201);
res.send("{}");
return;
}
res.status(400);
res.send("400 Bad Request");
});
io.on("connection", function(socket){
// Authentication Check
var headers = socket.handshake.headers;
request({
url:"http://localhost:8000/api/auth/",
json:true,
headers:{
"Cookie":headers.cookie,
"Accept":"application/json"
}
},function (error, response, body){
if (error || response.statusCode != 200){
console.log("Connected from Unknown User");
// socket.disconnect();
}else if(body.is_staff) {
console.log("Connected from " + body.username + " (admin)");
socket.join("admin");
}else{
console.log("Connected from " + body.username);
}
});
});
http.listen(8001, function(){
console.log('listening on *:8001');
});
// Proxy Server for Development
var http = require('http');
var httpProxy = require('http-proxy');
var url = require('url');
var proxy = httpProxy.createProxyServer({});
var server = http.createServer(function (req, res) {
var hostname = req.headers.host.split(":")[0];
var pathname = url.parse(req.url).pathname;
if(pathname.indexOf("/socket.io/") === 0 || pathname.indexOf("/realtime/") === 0 ){
proxy.web(req, res, {
target: "http://localhost:8001"
});
return;
}
proxy.web(req, res, {
target: "http://localhost:8000"
});
});
server.on('upgrade', function (req, socket, head) {
console.log("upgrade");
proxy.ws(req, socket, head, {
target: {
host: 'localhost',
port: 8001
}
});
});
server.listen(8080, function(){
console.log('listening on *:8080');
});