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