0
|
1 /*
|
|
2 * ConnectBot: simple, powerful, open-source SSH client for Android
|
|
3 * Copyright 2007 Kenny Root, Jeffrey Sharkey
|
|
4 *
|
|
5 * Licensed under the Apache License, Version 2.0 (the "License");
|
|
6 * you may not use this file except in compliance with the License.
|
|
7 * You may obtain a copy of the License at
|
|
8 *
|
|
9 * http://www.apache.org/licenses/LICENSE-2.0
|
|
10 *
|
|
11 * Unless required by applicable law or agreed to in writing, software
|
|
12 * distributed under the License is distributed on an "AS IS" BASIS,
|
|
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14 * See the License for the specific language governing permissions and
|
|
15 * limitations under the License.
|
|
16 */
|
|
17
|
|
18 package com.five_ten_sg.connectbot.transport;
|
|
19
|
|
20 import java.io.IOException;
|
|
21 import java.io.InputStream;
|
|
22 import java.io.OutputStream;
|
|
23 import java.net.Socket;
|
|
24 import java.net.SocketException;
|
|
25 import java.net.UnknownHostException;
|
|
26 import java.nio.charset.Charset;
|
|
27 import java.util.Map;
|
|
28 import java.util.regex.Matcher;
|
|
29 import java.util.regex.Pattern;
|
|
30
|
|
31 import com.five_ten_sg.connectbot.R;
|
|
32 import com.five_ten_sg.connectbot.bean.HostBean;
|
|
33 import com.five_ten_sg.connectbot.service.TerminalBridge;
|
|
34 import com.five_ten_sg.connectbot.service.TerminalManager;
|
|
35 import com.five_ten_sg.connectbot.util.HostDatabase;
|
11
|
36
|
0
|
37 import android.content.Context;
|
|
38 import android.net.Uri;
|
|
39 import android.util.Log;
|
13
|
40
|
0
|
41 import de.mud.telnet.TelnetProtocolHandler;
|
|
42
|
13
|
43
|
0
|
44 /**
|
|
45 * Telnet transport implementation.<br/>
|
|
46 * Original idea from the JTA telnet package (de.mud.telnet)
|
|
47 *
|
|
48 * @author Kenny Root
|
|
49 *
|
|
50 */
|
|
51 public class Telnet extends AbsTransport {
|
|
52 private static final String TAG = "ConnectBot.Telnet";
|
|
53 private static final String PROTOCOL = "telnet";
|
|
54
|
|
55 private static final int DEFAULT_PORT = 23;
|
|
56
|
|
57 private TelnetProtocolHandler handler;
|
|
58 private Socket socket;
|
|
59
|
|
60 private InputStream is;
|
|
61 private OutputStream os;
|
|
62 private int width;
|
|
63 private int height;
|
|
64
|
|
65 private boolean connected = false;
|
|
66
|
|
67 static final Pattern hostmask;
|
|
68 static {
|
|
69 hostmask = Pattern.compile("^([0-9a-z.-]+)(:(\\d+))?$", Pattern.CASE_INSENSITIVE);
|
|
70 }
|
|
71
|
|
72 public Telnet() {
|
|
73 handler = new TelnetProtocolHandler() {
|
|
74 /** get the current terminal type */
|
|
75 @Override
|
|
76 public String getTerminalType() {
|
|
77 return getEmulation();
|
|
78 }
|
|
79 /** get the current window size */
|
|
80 @Override
|
|
81 public int[] getWindowSize() {
|
|
82 return new int[] { width, height };
|
|
83 }
|
|
84 /** notify about local echo */
|
|
85 @Override
|
|
86 public void setLocalEcho(boolean echo) {
|
|
87 /* EMPTY */
|
|
88 }
|
|
89 /** write data to our back end */
|
|
90 @Override
|
|
91 public void write(byte[] b) throws IOException {
|
|
92 if (os != null)
|
|
93 os.write(b);
|
|
94 }
|
|
95 /** sent on IAC EOR (prompt terminator for remote access systems). */
|
|
96 @Override
|
|
97 public void notifyEndOfRecord() {
|
|
98 }
|
|
99 @Override
|
|
100 protected String getCharsetName() {
|
|
101 Charset charset = bridge.getCharset();
|
|
102
|
|
103 if (charset != null)
|
|
104 return charset.name();
|
|
105 else
|
|
106 return "";
|
|
107 }
|
|
108 };
|
|
109 }
|
|
110
|
|
111 /**
|
|
112 * @param host
|
|
113 * @param bridge
|
|
114 * @param manager
|
|
115 */
|
|
116 public Telnet(HostBean host, TerminalBridge bridge, TerminalManager manager) {
|
|
117 super(host, bridge, manager);
|
|
118 }
|
|
119
|
|
120 public static String getProtocolName() {
|
|
121 return PROTOCOL;
|
|
122 }
|
|
123
|
|
124 @Override
|
|
125 public void connect() {
|
|
126 try {
|
|
127 socket = new Socket(host.getHostname(), host.getPort());
|
|
128 connected = true;
|
|
129 is = socket.getInputStream();
|
|
130 os = socket.getOutputStream();
|
|
131 bridge.onConnected();
|
|
132 }
|
|
133 catch (UnknownHostException e) {
|
|
134 Log.d(TAG, "IO Exception connecting to host", e);
|
|
135 }
|
|
136 catch (IOException e) {
|
|
137 Log.d(TAG, "IO Exception connecting to host", e);
|
|
138 }
|
|
139 }
|
|
140
|
|
141 @Override
|
|
142 public void close() {
|
|
143 connected = false;
|
|
144
|
|
145 if (socket != null)
|
|
146 try {
|
|
147 socket.close();
|
|
148 socket = null;
|
|
149 }
|
|
150 catch (IOException e) {
|
|
151 Log.d(TAG, "Error closing telnet socket.", e);
|
|
152 }
|
|
153 }
|
|
154
|
|
155 @Override
|
|
156 public void flush() throws IOException {
|
|
157 os.flush();
|
|
158 }
|
|
159
|
|
160 @Override
|
|
161 public int getDefaultPort() {
|
|
162 return DEFAULT_PORT;
|
|
163 }
|
|
164
|
|
165 @Override
|
|
166 public boolean isConnected() {
|
|
167 return connected;
|
|
168 }
|
|
169
|
|
170 @Override
|
|
171 public boolean isSessionOpen() {
|
|
172 return connected;
|
|
173 }
|
|
174
|
|
175 @Override
|
|
176 public boolean isAuthenticated() {
|
|
177 return isConnected();
|
|
178 }
|
|
179
|
|
180 @Override
|
|
181 public boolean willBlock() {
|
|
182 if (is == null) return true;
|
|
183 try {
|
|
184 return is.available() == 0;
|
|
185 } catch (Exception e) {
|
|
186 return true;
|
|
187 }
|
|
188 }
|
|
189
|
|
190 @Override
|
|
191 public int read(byte[] buffer, int start, int len) throws IOException {
|
|
192 /* process all already read bytes */
|
|
193 int n = 0;
|
|
194
|
|
195 do {
|
|
196 n = handler.negotiate(buffer, start);
|
|
197
|
|
198 if (n > 0)
|
|
199 return n;
|
|
200 }
|
|
201 while (n == 0);
|
|
202
|
|
203 while (n <= 0) {
|
|
204 do {
|
|
205 n = handler.negotiate(buffer, start);
|
|
206
|
|
207 if (n > 0)
|
|
208 return n;
|
|
209 }
|
|
210 while (n == 0);
|
|
211
|
|
212 n = is.read(buffer, start, len);
|
|
213
|
|
214 if (n < 0) {
|
|
215 bridge.dispatchDisconnect(false);
|
|
216 throw new IOException("Remote end closed connection.");
|
|
217 }
|
|
218
|
|
219 handler.inputfeed(buffer, start, n);
|
|
220 n = handler.negotiate(buffer, start);
|
|
221 }
|
|
222
|
|
223 return n;
|
|
224 }
|
|
225
|
|
226 @Override
|
|
227 public void write(byte[] buffer) throws IOException {
|
|
228 try {
|
|
229 if (os != null)
|
|
230 os.write(buffer);
|
|
231 }
|
|
232 catch (SocketException e) {
|
|
233 bridge.dispatchDisconnect(false);
|
|
234 }
|
|
235 }
|
|
236
|
|
237 @Override
|
|
238 public void write(int c) throws IOException {
|
|
239 try {
|
|
240 if (os != null)
|
|
241 os.write(c);
|
|
242 }
|
|
243 catch (SocketException e) {
|
|
244 bridge.dispatchDisconnect(false);
|
|
245 }
|
|
246 }
|
|
247
|
|
248 @Override
|
|
249 public void setDimensions(int columns, int rows, int width, int height) {
|
|
250 try {
|
|
251 handler.setWindowSize(columns, rows);
|
|
252 }
|
|
253 catch (IOException e) {
|
|
254 Log.e(TAG, "Couldn't resize remote terminal", e);
|
|
255 }
|
|
256 }
|
|
257
|
|
258 @Override
|
|
259 public String getDefaultNickname(String username, String hostname, int port) {
|
|
260 if (port == DEFAULT_PORT) {
|
|
261 return String.format("%s", hostname);
|
|
262 }
|
|
263 else {
|
|
264 return String.format("%s:%d", hostname, port);
|
|
265 }
|
|
266 }
|
|
267
|
12
|
268 public Uri getUri(String input) {
|
0
|
269 Matcher matcher = hostmask.matcher(input);
|
|
270
|
|
271 if (!matcher.matches())
|
|
272 return null;
|
|
273
|
|
274 StringBuilder sb = new StringBuilder();
|
|
275 sb.append(PROTOCOL)
|
|
276 .append("://")
|
|
277 .append(matcher.group(1));
|
|
278 String portString = matcher.group(3);
|
|
279 int port = DEFAULT_PORT;
|
|
280
|
|
281 if (portString != null) {
|
|
282 try {
|
|
283 port = Integer.parseInt(portString);
|
|
284
|
|
285 if (port < 1 || port > 65535) {
|
|
286 port = DEFAULT_PORT;
|
|
287 }
|
|
288 }
|
|
289 catch (NumberFormatException nfe) {
|
|
290 // Keep the default port
|
|
291 }
|
|
292 }
|
|
293
|
|
294 if (port != DEFAULT_PORT) {
|
|
295 sb.append(':');
|
|
296 sb.append(port);
|
|
297 }
|
|
298
|
|
299 sb.append("/#")
|
|
300 .append(Uri.encode(input));
|
|
301 Uri uri = Uri.parse(sb.toString());
|
|
302 return uri;
|
|
303 }
|
|
304
|
|
305 @Override
|
|
306 public HostBean createHost(Uri uri) {
|
|
307 HostBean host = new HostBean();
|
|
308 host.setProtocol(PROTOCOL);
|
|
309 host.setHostname(uri.getHost());
|
|
310 int port = uri.getPort();
|
|
311
|
|
312 if (port < 0)
|
|
313 port = DEFAULT_PORT;
|
|
314
|
|
315 host.setPort(port);
|
|
316 String nickname = uri.getFragment();
|
|
317
|
|
318 if (nickname == null || nickname.length() == 0) {
|
|
319 host.setNickname(getDefaultNickname(host.getUsername(),
|
|
320 host.getHostname(), host.getPort()));
|
|
321 }
|
|
322 else {
|
|
323 host.setNickname(uri.getFragment());
|
|
324 }
|
|
325
|
|
326 return host;
|
|
327 }
|
|
328
|
|
329 @Override
|
|
330 public void getSelectionArgs(Uri uri, Map<String, String> selection) {
|
|
331 selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL);
|
|
332 selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment());
|
|
333 selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost());
|
|
334 int port = uri.getPort();
|
|
335
|
|
336 if (port < 0)
|
|
337 port = DEFAULT_PORT;
|
|
338
|
|
339 selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port));
|
|
340 }
|
|
341
|
12
|
342 public String getFormatHint(Context context) {
|
0
|
343 return String.format("%s:%s",
|
|
344 context.getString(R.string.format_hostname),
|
|
345 context.getString(R.string.format_port));
|
|
346 }
|
|
347
|
|
348 @Override
|
|
349 public boolean usesNetwork() {
|
|
350 return true;
|
|
351 }
|
|
352 }
|