-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathantenna.js
More file actions
163 lines (147 loc) · 4.44 KB
/
antenna.js
File metadata and controls
163 lines (147 loc) · 4.44 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Universal query dispatcher
// ** Has to be running on two seperate servers **
// ** Connect to dorm dispatcher **
var app = require('express')();
var bodyParser = require('body-parser')
var server = require('http').Server(app);
var io = require('socket.io')(server);
const request = require('request');
const uuidv4 = require('uuid/v4');
// Constants
const PORT = 4100;
// Google IP List
// IP_ADDR: IN_USE
let googleIPList = {};
// Data structure: { available: bool, getSearchResult: fn }
let socketDict = {};
let socketIdList = []
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }))
// parse application/json
app.use(bodyParser.json())
app.get('/', function(req, res){
res.send(`<h1>Mother Server</h1>`);
});
app.get('/api/', function(req, res){
res.send(`<h1>XINAHN API</h1>`);
});
let cacheSuggestions = {}
app.get('/api/suggestion', (req, res) => {
const q = req.query.q;
if (cacheSuggestions[encodeURI(q)]) {
console.log('[CACHE] using cache for request: ' + q)
return res.send([ q, cacheSuggestions[encodeURI(q)] ]);
}
try {
request(
`https://www.baidu.com/sugrec?ie=utf-8&json=1&prod=pc&from=pc_web&wd=${encodeURI(q)}`,
(err, response, body) => {
try {
if (err) return res.send([ q, []]);
if (body.startsWith('<')) {
console.log('[ERROR] Baidu rejecting keyword search.');
return res.send([ q, [] ]);
}
let result = JSON.parse(body);
let ourResponse = [];
if (result.g)
for(let s of result.g) {
ourResponse.push(s.q)
}
cacheSuggestions[encodeURI(q)] = ourResponse;
return res.send([ q, ourResponse]);
} catch {
console.log('[ERROR] Baidu rejecting keyword search.');
return res.send([ q, [] ]);
}
}
);
} catch {
console.log('[CACHE] suggestion cache went wrong: ' + q);
return res.send([ q, [] ]);
}
})
app.post('/api/search', (req, res) => {
const {
query,
page,
} = req.body;
console.log(`[SERVER] WEB_SEARCH query: ${query}, page: ${page}`);
const requestId = uuidv4();
let socketId = '';
for (let s of socketIdList) {
if (socketDict[s].available) {
socketDict[s].available = false;
socketId = s;
break;
}
}
console.log('[SERVER] Sending request to socket: ', socketId);
if (!socketId) return res.send({
errCode: 400,
errMsg: 'server is busy'
});
socketDict[socketId]
.getSearchResult({ query, page, requestId })
.then((result) => {
// Resolved, release the socket
socketDict[socketId].available = true;
return res.send(result);
})
})
io.on('connection', (socket) => {
socket.on('/auth', ({ authKey }) => {
if (authKey === 'YOUR_AUTH_KEY') {
console.log('[SERVER] Minion online! :D');
let googleip = '';
for (let gip in googleIPList) {
if (!googleIPList[gip]) {
googleIPList[gip] = true;
googleip = gip;
}
}
socket.emit('auth_success', { GOOGLE_IP: googleip });
socket.googleip = googleip;
if (socketIdList.indexOf(socket.id) === -1) {
socketIdList.push(socket.id);
socketDict[socket.id] = {
available: true,
getSearchResult: ({ query, page, requestId }) => {
return new Promise((resolve, reject) => {
socket.emit(
'search',
{
requestId,
query,
page,
},
);
socket.on(requestId, result => {
// console.log('[SERVER] getting result: ', result);
resolve(result);
});
});
}
};
}
}
})
socket.on('disconnect', () => {
try {
delete socketDict[socket.id];
googleIPList[socket.googleip] = false;
socketIdList.splice(socketIdList.indexOf(socket.id), 1);
console.log('[SERVER] Minion disconnected D:');
console.log(socketDict, socketIdList, socket.googleip);
} catch {
}
});
});
server.listen(PORT, function(){
console.log('[SERVER] Server up and running at %s port', PORT);
});