-
Notifications
You must be signed in to change notification settings - Fork 0
/
oisc.js
192 lines (173 loc) · 5 KB
/
oisc.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* OISC MITM
* Modify your local machine host files to point www.ogreisland.com to your server address
*/
console.log('OISC running...');
// Load the packet parser
var parser_outbound = require('./mod_parser_outbound');
var parser_inbound = require('./mod_parser_inbound');
autominer = require('./mod_autominer');
// Load the TCP Library
net = require('net');
http = require('http');
fs = require('fs');
// Set colors for console output
var red, blue, reset;
red = '\033[31m';
blue = '\033[34m';
reset = '\033[0m';
// Map oisc to global
oisc = {};
oisc.resources = {};
oisc.config = {
host: 'www.ogreisland.com',
port: 5301,
autocast_active: false,
speed: 0,
speed_server: 0,
};
oisc.params = {
autocast: false,
autocast_spell: '',
autocast_delay: 800,
automine: false,
loot_all: true,
loot_coins: true,
loot_value: 2
};
oisc.coords = {
x: 0,
y: 0,
z: 0,
obj: 0
};
/**
* Create server for game client to connect to
*/
net.createServer(function(socket) {
// Map the socket to global
oisc.server = socket;
oisc.server.setNoDelay(true);
// Alert of established connection
console.log('OISC server connection from game client established');
// Start a client to connect to OI server
if(oisc.client === undefined || oisc.client.writeable !== true) startClient();
/**
* Receive data from game client
*/
oisc.server.on('data', function(data) {
console.log(red + 'SERVER RECEIVED: ' + reset + blue + data + reset);
parser_outbound.parsePacket(data, function(send, receiver, packet) {
if(send !== false) {
if(receiver === 'client') {
console.log('sending to gameclient: ' + packet);
oisc.server.write(packet);
}
else if(receiver === 'server') {
console.log('sending to gameserver: ' + packet);
oisc.client.write(packet);
}
}
else {
console.log('not sending: ' + data);
}
});
});
/**
* Error on OISC server
*/
oisc.server.on('error', function (error) {
console.log(red + 'SERVER ERROR: ' + reset);
console.log(error.stack);
});
/**
* Game client disconnect
*/
oisc.server.on('end', function() {
console.log(red + 'SERVER CONNECTION ENDED' + reset);
oisc.client.destroy();
});
}).listen(oisc.config.port);
/**
* Create client to connect to OI server
*/
function startClient() {
/**
* Open connection to OI server
*/
oisc.client = net.createConnection({port: oisc.config.port, host: oisc.config.host}, function() {
console.log(red + 'CLIENT: ' + reset + blue + 'Connected to ' + oisc.config.host + ':' + oisc.config.port + reset);
});
/**
* Receive data from OI server
*/
oisc.client.on('data', function(data) {
console.log(red + 'CLIENT RECEIVED: ' + reset + blue + data + reset);
oisc.server.write(data);
parser_inbound.parsePacket(data, function(send, receiver, packet) {
});
});
/**
* Error on OISC client
*/
oisc.client.on('error', function(error) {
console.log('OISC Client Error: ' + error.stack);
});
/**
* OI Server disconnect
*/
oisc.client.on('end', function() {
console.log(red + 'CLIENT CONNECTION ENDED' + reset);
});
}
/**
* Proxy for port 80
*
* We need this because as of 12/2013 the game server hits
* www.ogreisland.com instead of stage.ogreisland.com, so our
* hostfile entry would kill the OI website without proxy.
* This method also injects css and js from appserv/
*/
http.createServer(function(request, response) {
var proxy = http.createClient(80, request.headers['host']);
var proxy_request = proxy.request(request.method, request.url, request.headers);
proxy_request.addListener('response', function(proxy_response) {
proxy_response.addListener('data', function(chunk) {
if(request.url == "/game2/client.aspx") {
// include styles and js for game
var newChunk = chunk.toString().replace(/(<\/head>)/, '<style>' + oisc.resources.css + '</style><script type="text/javascript">' + oisc.resources.js + '</script>$1');
chunk = new Buffer(newChunk.toString('binary'), 'binary');
}
else if(proxy_response.headers['content-type'].indexOf("text/html") > -1) {
// add OISC banner
var newChunk = chunk.toString().replace(/(<body>)/, '$1<div id="oisc_banner" style="position:fixed;background:#00c;z-index:99;display:block;width:100%;padding:2px;text-align:center">OISC Loaded</div>');
chunk = new Buffer(newChunk.toString('binary'), 'binary');
}
response.write(chunk, 'binary');
});
proxy_response.addListener('end', function() {
response.end();
});
delete proxy_response.headers['content-length'];
response.writeHead(proxy_response.statusCode, proxy_response.headers);
});
request.addListener('data', function(chunk) {
proxy_request.write(chunk, 'binary');
});
request.addListener('end', function() {
proxy_request.end();
});
}).listen(80);
/**
* Load resources for injection into DOM
*/
fs.readFile("appserv/oisc.css", function(err, data) {
if(err) { throw err; }
oisc.resources.css = data;
console.log('Loaded CSS');
});
fs.readFile("appserv/oisc.js", function(err, data) {
if(err) { throw err; }
oisc.resources.js = data;
console.log('Loaded JS');
});