This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
142 lines (128 loc) · 4.46 KB
/
main.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
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
const http = require('http');
const url = require('url');
const net = require('net');
const fs = require('fs');
const process = require('process');
let settings = require('./settings.json');
const songData = require('./data.json');
let twitter;
if (settings.twitter.on) twitter = require('./twitter.js');
let server;
try {
if (settings.server.on) server = require('./server.js');
} catch (TypeError) {
console.log('v1.2.0~: New update applied. Edit settings.json properly according to settings-sample.json.');
process.exit();
}
const MAIN_HOST = 'prod-jp.lovelive.ge.klabgames.net';
if (settings.port) {
console.log(`Using this port number: ${settings.port}`);
} else {
console.log('Proxy port number is not specified in settings.json.');
console.log('Using default port number: 25252');
}
if (settings.log) {
let isAllFalse = true;
for (section of ['live']) {
if (Object.keys(settings.log).indexOf(section) === -1) {
console.log(`Whether ${section} data is logged is not specified.`);
console.log(`Using default ${section}-logging setting: false`);
settings.log[section] = false
} else {
isAllFalse = false;
}
}
if (isAllFalse) {
console.log('All log settings are set as false.');
process.exit();
}
} else {
console.log('Log settings are not specified in settings.json.');
process.exit();
}
const log = (txt) => {
fs.appendFile('log.txt', txt, (err) => {
if (err) throw err;
});
};
const getSongData = (body) => {
let data;
for (line of body.split('\n')) {
if (line[0] === '{') data = JSON.parse(line);
}
const id = data.live_difficulty_id.toString(); // type: string
if (Object.keys(songData).indexOf(id) !== -1) {
data.title = songData[id].join(' ');
}
return data;
}
const watcher = (path, body) => {
if (settings.log.live) {
if (path === '/main.php/live/partyList') {
const song = getSongData(body);
if (song.title === null) song.title = 'Not registered';
console.log(`Selected ${song.live_difficulty_id} (${song.title})`);
}
if (path === '/main.php/live/reward') {
const song = getSongData(body);
const result = [
['SONG', song.title ? song.title : song.live_difficulty_id],
['SCORE', song.score_smile + song.score_cute + song.score_cool],
['MAX COMBO', song.max_combo],
['PERFECT', song.perfect_cnt],
['GREAT', song.great_cnt],
['GOOD', song.good_cnt],
['BAD', song.bad_cnt],
['MISS', song.miss_cnt]
];
const resultText = '[LIVE RESULT]\n' + result.map((elm) => {
return elm.join(': ');
}).join('\n');
if (settings.twitter.on) twitter.post(resultText);
if (settings.server.on) server.post(body, song.title ? song.title : song.live_difficulty_id);
console.log(resultText);
log(`-----\n${Date().toString()}\n${resultText}\n`);
}
}
};
const proxy = http.createServer((req, res) => {
const reqUrl = url.parse(req.url);
const socket = req.socket || req.connection;
const srvReq = http.request({
host: reqUrl.hostname,
port: reqUrl.port || 80,
path: reqUrl.path,
method: req.method,
headers: req.headers,
agent: socket.$agent
}, function onsrvRes(srvRes) {
res.writeHead(srvRes.statusCode, srvRes.headers);
srvRes.pipe(res);
});
req.pipe(srvReq);
if (reqUrl.hostname === MAIN_HOST) {
let body = [];
req.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
watcher(reqUrl.path, body);
});
}
srvReq.on('error', (err) => {
console.log('failed');
});
}).listen(settings.port || 25252);
proxy.on('connect', (req, soc, head) => {
const reqUrl = url.parse('https://' + req.url);
const srvSoc = net.connect(reqUrl.port || 443, reqUrl.hostname, () => {
soc.write('HTTP/1.0 200 Connection established\r\n\r\n');
if (head && head.length) srvSoc.write(head);
soc.pipe(srvSoc);
srvSoc.pipe(soc);
});
});
proxy.on('connection', (socket) => {
socket.$agent = new http.Agent({keepAlive: true});
socket.$agent.on('error', err => console.log('agent:', err));
});