-
Notifications
You must be signed in to change notification settings - Fork 121
/
index.js
143 lines (125 loc) · 4.1 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
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
/**
* Initialization
*/
var _ = require('lodash');
var fs = require('fs');
var bp = require('body-parser');
var cors = require('cors');
var express = require('express');
var app = express();
var forge = require('node-forge');
var player = null;
var inventory = null;
var releasing = null;
if (!fs.existsSync(__dirname + '/data/save/')) fs.mkdirSync(__dirname + '/data/save/');
/**
* Mitm
*/
var PokemonGoMITM = require('pokemon-go-mitm');
var Player = require('./lib/player');
var Inventory = require('./lib/inventory');
new PokemonGoMITM({port: 8081})
.setResponseHandler('GetPlayer', function (data) {
if (data.success && player === null) {
player = new Player(data.player_data.username);
player.load(data.player_data);
player.save();
}
})
.setResponseHandler('GetInventory', function (data) {
if (data.success && player !== null) {
var delta = data.inventory_delta;
var pokemon = _.reduce(delta.inventory_items, function (result, item) {
return (item.inventory_item_data !== undefined &&
item.inventory_item_data.pokemon_data !== undefined &&
!item.inventory_item_data.pokemon_data.is_egg) ? _.concat(result, item.inventory_item_data.pokemon_data) : result;
}, []);
var candy = _.reduce(delta.inventory_items, function (result, item) {
return (item.inventory_item_data !== undefined &&
item.inventory_item_data.candy !== undefined) ? _.concat(result, item.inventory_item_data.candy) : result;
}, []);
if (inventory === null) {
inventory = new Inventory(player.username);
inventory.load(candy, pokemon);
inventory.save();
} else {
inventory.update(candy, pokemon);
inventory.save();
}
} else if (player === null) {
console.error("WARNING: Cannot set inventory. No player logged in.");
} else if (!data.inventory_delta) {
console.log(data);
}
})
.setRequestHandler("ReleasePokemon", function (data) {
releasing = data.pokemon_id;
})
.setResponseHandler("ReleasePokemon", function(data) {
if (data.result === 'SUCCESS' && releasing !== null) {
inventory.release(releasing);
inventory.save();
releasing = null;
}
})
.setRequestHandler("EvolvePokemon", function (data) {
releasing = data.pokemon_id;
})
.setResponseHandler("EvolvePokemon", function(data) {
if (data.result === 'SUCCESS' && releasing !== null) {
inventory.release(releasing);
inventory.save();
releasing = null;
}
});
/**
* Express
*/
app.use(bp.json());
app.use(cors());
app.use(express.static('public'));
app.use("/ca.pem", express.static('.http-mitm-proxy/certs/ca.pem'));
app.use("/ca.crt", express.static('.http-mitm-proxy/certs/ca.crt'));
app.get('/api/player', function (req, res, next) {
if (player) return res.send(player.toJSON());
else return res.json(null);
});
app.post('/api/player/settings', function (req, res, next) {
if (player) {
player.settings = req.body;
player.save();
}
return res.send();
});
app.get('/api/inventory', function (req, res, next) {
if (inventory) {
res.send(inventory.toJSON());
inventory.updates = false;
}
else return res.json(null);
});
app.post('/api/logout', function (req, res, next) {
player = null;
inventory = null;
releasing = null;
res.send();
});
app.listen(3000, function () {
console.log('Pokemon GO Optimizer front-end listening on port 3000');
var ip = require('quick-local-ip').getLocalIP4();
console.log('Accept the cert at http://' + ip + ':3000/ca.pem if you haven\'t already done so');
});
/**
* Certificate
*/
function createCrt() {
if (fs.existsSync(__dirname + '/.http-mitm-proxy/certs/ca.pem')) {
if (!fs.existsSync(__dirname + '/.http-mitm-proxy/certs/ca.crt')) {
var cert = forge.pki.certificateFromPem(fs.readFileSync('.http-mitm-proxy/certs/ca.pem'));
fs.writeFile('.http-mitm-proxy/certs/ca.crt', forge.asn1.toDer(forge.pki.certificateToAsn1(cert)).getBytes(), {encoding: 'binary'});
}
} else {
setTimeout(createCrt, 15000);
}
}
createCrt();