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