This repository has been archived by the owner on Mar 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TelnetClient.hx
164 lines (121 loc) · 4.16 KB
/
TelnetClient.hx
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
package rn.net.tcp.telnet;
import sys.net.Host;
import haxe.io.Bytes;
import haxe.io.BytesBuffer;
import rn.net.tcp.TcpClient;
import rn.net.INetworkClient;
import rn.net.NetworkStream;
using StringTools;
class TelnetClient extends TcpClient implements INetworkClient {
public var telnetControlChar:String;
public var telnetSendWait:Float = .05;
public var telnetReadWait:Float = .05;
public var telnetResponseWaiting:Float = .5;
public var telnetEndLineChar:String = String.fromCharCode(13);
//------------------------------------------------------------------------------------------
public var telnetLogin:String;
public var telnetPassword:String;
//------------------------------------------------------------------------------------------
public dynamic function telnetLoginProc () {
var result = true;
if (telnetLogin != null ? telnetLogin.length > 0 : false) {
_readData(false);
result = sendTextLine(telnetLogin);
}
if (result && (telnetPassword != null ? telnetPassword.length > 0 : false)) {
_readData(false);
result = sendTextLine(telnetPassword);
}
return result;
}
public dynamic function telnetCheckingProc () {
if (connected) {
Sys.sleep(.5);
return telnetControlChar != null ? readText().indexOf(telnetControlChar) >= 0 : true;
}
return false;
}
//------------------------------------------------------------------------------------------
function telnetConnectProc () {
var result = telnetLoginProc != null ? telnetLoginProc() : true;
if (result)
result = telnetCheckingProc != null ? telnetCheckingProc() : true;
if (!result)
disconnect();
return result;
}
public override function connect (host:Host, port:Int) {
if (super.connect(host, port))
return telnetConnectProc();
return false;
}
public override function reconnect () {
if (super.reconnect())
return telnetConnectProc();
return false;
}
//------------------------------------------------------------------------------------------
override public function sendData (data:Bytes) {
if (connected)
try {
for (i in 0...data.length) {
sendByte(data.get(i));
Sys.sleep(telnetSendWait);
}
return true;
}
catch (e:Dynamic) {
disconnect();
}
return false;
}
override public function sendText (text:String) {
var char = String.fromCharCode(0xFF);
return text != null ? sendData(Bytes.ofString(text.replace(char, char+char))) : false;
}
override public function sendTextLine (text:String)
return text != null ? sendText(text + telnetEndLineChar) : false;
//------------------------------------------------------------------------------------------
override function _readData (readLine:Bool) return parseTelnet(tcpSocket.dataStream, new BytesBuffer(), readLine).getBytes();
function parseTelnet (stream:NetworkStream, sb:BytesBuffer, readline:Bool) {
do {
while (stream != null ? stream.dataAvailable : false) {
var input = stream.readByte();
switch (input) {
case Verbs.IAC:
var inputverb = stream.readByte();
switch (inputverb) {
case Verbs.IAC:
sb.addByte(inputverb); // literal IAC = 255 escaped, so append char 255 to string
case Verbs.DO, Verbs.DONT, Verbs.WILL, Verbs.WONT:
var inputoption = stream.readByte();
if (inputoption == -1)
break;
stream.writeByte(Verbs.IAC);
if (inputoption == Options.SGA)
stream.writeByte(inputverb == Verbs.DO ? Verbs.WILL : Verbs.DO);
else
stream.writeByte(inputverb == Verbs.DO ? Verbs.WONT : Verbs.DONT);
stream.writeByte(inputoption);
default:
}
default:
if (readline && (input == 0 || input == 10 || input == 13))
return sb;
if (input != -1)
sb.addByte(input);
}
}
Sys.sleep(telnetReadWait);
}
while (stream != null ? stream.dataAvailable : false);
return sb;
}
//------------------------------------------------------------------------------------------
public function telnetResponse (?request:String) {
if (request != null)
sendTextLine(request);
Sys.sleep(telnetResponseWaiting);
return readText(false);
}
}