forked from zhuowei/ModPEScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replserver.js
54 lines (49 loc) · 1.13 KB
/
replserver.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
(function() {
var serverSocket, mainThread;
function startServer() {
mainThread = new java.lang.Thread(new java.lang.Runnable({
run: mainLoop
}));
mainThread.start();
}
function mainLoop() {
try {
setupSocket();
while (!serverSocket.isClosed()) {
var clientSocket = serverSocket.accept();
handleClientSocket(clientSocket);
}
} catch (e) {
print(e + ":" + e.lineNumber);
}
}
function setupSocket() {
serverSocket = new java.net.ServerSocket(0);
print("Socket set up at " + serverSocket.getLocalPort());
}
function handleClientSocket(clientSocket) {
var thread = new java.lang.Thread(new java.lang.Runnable({
run: function() {
try {
clientLoop(clientSocket);
} catch (e) {
print(e);
}
}
}));
thread.start();
}
function clientLoop(clientSocket) {
var is = clientSocket.getInputStream();
var os = clientSocket.getOutputStream();
var dis = new java.io.DataInputStream(is);
var dos = new java.io.DataOutputStream(os);
while (!clientSocket.isClosed()) {
var cmd = String(dis.readUTF());
var response = String(eval(cmd));
print(cmd + ":" + response);
dos.writeUTF(response);
}
}
startServer();
})();