-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathduel-proxy.js
133 lines (113 loc) · 3.78 KB
/
duel-proxy.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
'use strict';
const fs = require('fs');
const Proxy = require('http-mitm-proxy');
const proxy = Proxy();
const ext = {
cardMap: require('./cardmap'),
config: require('./config'),
};
[['config'], ['cardmap', 'cardMap']].forEach((val) => {
const fileName = val[0];
const varName = val[1] || val[0];
fs.watchFile(`./${fileName}.js`, (curr, prev) => {
if(curr.mtime == prev.mtime) return; // Do nothing if file was not modified
delete require.cache[require.resolve(`./${fileName}`)]; // Clear cached require entry
try {
ext[varName] = require(`./${fileName}`); // Reload config
console.log(`${fileName}.js modified, reloaded file.`);
} catch(err) {
console.error(`Failed to reload ${fileName}.js: `, err);
}
});
});
proxy.onRequest(function(ctx, callback) {
if (ctx.clientToProxyRequest.headers.host == 'att-jpb.mo.konami.net') {
const chunks = [];
ctx.use(Proxy.gunzip);
ctx.onResponseData((ctx, chunk, callback) => {
chunks.push(chunk);
return callback(null, null);
});
ctx.onResponseEnd((ctx, callback) => {
const body = parseResponse(Buffer.concat(chunks));
ctx.proxyToClientResponse.write(body);
if(ext.config.logging.res.enabled) {
fs.appendFile(ext.config.logging.res.filename, body.toString() + ext.config.logging.res.divider, () => {
return callback();
});
} else {
return callback();
}
});
}
return callback();
});
proxy.listen({port: 8000}, () => {
console.log('Listening on port 8000.');
});
function parseResponse(chunk) {
let req = chunk.toString();
try {
if(req.charAt(0) == '@') req = req.slice(1); // Remove '@'
// Parse JSON
let data = JSON.parse(req);
data = editResponse(data);
req = '@' + JSON.stringify(data);
} catch(e) {
if(e.name == 'TypeError')
console.error('Invalid JSON.');
else
console.log('Unknown error:', e);
return chunk;
}
// if(ext.config.logging.req.enabled) {
// fs.appendFile(ext.config.logging.req.filename, req.toString() + ext.config.logging.req.divider, () => {});
// }
return req;
}
function editResponse(data) {
if(!data.res || !data.res[0] || !data.res[0][1] || !data.res[0][1].Duel)
return data;
const myDeck = data.res[0][1].Duel.Deck[0];
const theirDeck = data.res[0][1].Duel.Deck[1];
console.log('Found battle.');
console.log('Random Seed (original):', data.res[0][1].Duel.RandSeed);
console.log('Their deck (original):', prettyDeck(theirDeck.Main.CardIds));
console.log('My deck:', prettyDeck(myDeck.Main.CardIds));
if(ext.config.replace.theirDeck) {
theirDeck.Main.CardIds = ext.config.new.theirDeck;
theirDeck.Main.Rare = Array(theirDeck.Main.CardIds.length).fill(1);
}
if(ext.config.replace.myDeck) {
myDeck.Main.CardIds = ext.config.new.myDeck;
myDeck.Main.Rare = Array(myDeck.Main.CardIds.length).fill(1);
}
if(ext.config.enableAuto) {
data.res[0][1].Duel.auto = 1;
}
if(ext.config.makeMineRare) {
// Make all my cards rare
myDeck.Main.Rare = myDeck.Main.Rare.map(c => 3);
}
if(ext.config.makeTheirsRare) {
// Make all my cards rare
myDeck.Main.Rare = theirDeck.Main.Rare.map(c => 3);
}
if(ext.config.replace.randSeed) {
data.res[0][1].Duel.RandSeed = ext.config.new.randSeed;
}
console.log('New Random Seed:', data.res[0][1].Duel.RandSeed);
console.log('My new deck:', prettyDeck(myDeck.Main.CardIds));
console.log('Their new deck:', prettyDeck(theirDeck.Main.CardIds));
console.log('========================================');
data.res[0][1].Duel.Deck[0] = myDeck;
data.res[0][1].Duel.Deck[1] = theirDeck;
return data;
}
function prettyDeck(deckIds) {
return deckIds.map(id => {
if(id in ext.cardMap)
return ext.cardMap[id] + ` (${id})`;
return id;
});
}