-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelnet_proxy_service.js
104 lines (92 loc) · 3.73 KB
/
telnet_proxy_service.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
// $Id: telnet_proxy_service.js,v 1.1 2012/09/11 17:30:21 ree Exp $
// telnet_proxy_service.js
// Synchronet Service for redirecting incoming Telnet connections to another Telnet server.
// I wrote this to allow fTelnet to connect to servers that cannot run their own Flash Socket Policy Server
// If you plan on using it for the same purpose, you'll also need to enable flashpolicyserver.js
//
// NOTE: Currently the script is hardcoded to only allow proxying to port 23, since allowing other ports is just asking for abuse. Feel free to remove that restriction if you want though.
//
// Example configuration (in ctrl/services.ini)
//
// ; Telnet Proxy Service (used with fTelnet for hosts that don't accept Flash Socket Policy connections)
// ; Unless you know what you're doing, you probably don't want to enable this service!
// [TelnetProxy]
// Port=2323
// Options=NO_HOST_LOOKUP
// Command=telnet_proxy_service.js
//include definitions for synchronet
load("nodedefs.js");
load("sockdefs.js");
load("sbbsdefs.js");
const DEBUG = false;
var DoQuit = false;
var ServerSocket = null;
var StartTime = time();
// Main line
try {
// Check where the user wants to go
var HostAndPort = client.socket.recvline(512, 5);
if (HostAndPort) {
var Host = "";
var Port = 23;
// Parse the input
if (HostAndPort.indexOf(":") != -1) {
Host = HostAndPort.split(":")[0];
Port = parseInt(HostAndPort.split(":")[1], 10);
} else {
Host = HostAndPort;
}
// Check which port was requested
if (Port == 23) {
log(LOG_INFO, "Re-directing to " + Host + ":" + Port);
client.socket.send("Re-directing to " + Host + ":" + Port + "... ");
// Connect to the local synchronet server
ServerSocket = new Socket();
if (ServerSocket.connect(Host, Port)) {
// Variables we'll use in the loop
var DoYield = true;
var ClientData = [];
var ServerData = [];
// Loop while we're still connected on both ends
while ((client.socket.is_connected) && (ServerSocket.is_connected)) {
// Should we yield or not (default true, but disable if we had line activity)
DoYield = true;
// Check if the client sent anything
if (client.socket.data_waiting) {
while (client.socket.data_waiting) {
ServerSocket.sendBin(client.socket.recvBin(1), 1);
}
DoYield = false;
}
// Check if the server sent anything
if (ServerSocket.data_waiting) {
while (ServerSocket.data_waiting) {
client.socket.sendBin(ServerSocket.recvBin(1), 1);
}
DoYield = false;
}
// Yield if we didn't transfer any data
if (DoYield) {
mswait(10);
yield();
}
}
} else {
// ServerSocket.connect() failed
log(LOG_ERR, "Unable to connect to telnet server: " + Host + ":" + Port);
client.socket.send("Unable to connect");
mswait(2500);
}
} else {
log(LOG_ERR, "Port " + Port + " was requested and denied");
}
} else {
log(LOG_ERR, "No Host:Port was specified");
}
} catch (err) {
log(LOG_ERR, err.toString());
} finally {
if (ServerSocket != null) {
ServerSocket.close();
}
}