Mercurial > 510Connectbot
comparison src/com/five_ten_sg/connectbot/transport/Telnet.java @ 0:0ce5cc452d02
initial version
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 22 May 2014 10:41:19 -0700 |
parents | |
children | f3b3bbd227b8 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0ce5cc452d02 |
---|---|
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 import android.content.Context; | |
37 import android.net.Uri; | |
38 import android.util.Log; | |
39 import de.mud.telnet.TelnetProtocolHandler; | |
40 | |
41 /** | |
42 * Telnet transport implementation.<br/> | |
43 * Original idea from the JTA telnet package (de.mud.telnet) | |
44 * | |
45 * @author Kenny Root | |
46 * | |
47 */ | |
48 public class Telnet extends AbsTransport { | |
49 private static final String TAG = "ConnectBot.Telnet"; | |
50 private static final String PROTOCOL = "telnet"; | |
51 | |
52 private static final int DEFAULT_PORT = 23; | |
53 | |
54 private TelnetProtocolHandler handler; | |
55 private Socket socket; | |
56 | |
57 private InputStream is; | |
58 private OutputStream os; | |
59 private int width; | |
60 private int height; | |
61 | |
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 * @param host | |
110 * @param bridge | |
111 * @param manager | |
112 */ | |
113 public Telnet(HostBean host, TerminalBridge bridge, TerminalManager manager) { | |
114 super(host, bridge, manager); | |
115 } | |
116 | |
117 public static String getProtocolName() { | |
118 return PROTOCOL; | |
119 } | |
120 | |
121 @Override | |
122 public void connect() { | |
123 try { | |
124 socket = new Socket(host.getHostname(), host.getPort()); | |
125 connected = true; | |
126 is = socket.getInputStream(); | |
127 os = socket.getOutputStream(); | |
128 bridge.onConnected(); | |
129 } | |
130 catch (UnknownHostException e) { | |
131 Log.d(TAG, "IO Exception connecting to host", e); | |
132 } | |
133 catch (IOException e) { | |
134 Log.d(TAG, "IO Exception connecting to host", e); | |
135 } | |
136 } | |
137 | |
138 @Override | |
139 public void close() { | |
140 connected = false; | |
141 | |
142 if (socket != null) | |
143 try { | |
144 socket.close(); | |
145 socket = null; | |
146 } | |
147 catch (IOException e) { | |
148 Log.d(TAG, "Error closing telnet socket.", e); | |
149 } | |
150 } | |
151 | |
152 @Override | |
153 public void flush() throws IOException { | |
154 os.flush(); | |
155 } | |
156 | |
157 @Override | |
158 public int getDefaultPort() { | |
159 return DEFAULT_PORT; | |
160 } | |
161 | |
162 @Override | |
163 public boolean isConnected() { | |
164 return connected; | |
165 } | |
166 | |
167 @Override | |
168 public boolean isSessionOpen() { | |
169 return connected; | |
170 } | |
171 | |
172 @Override | |
173 public boolean isAuthenticated() { | |
174 return isConnected(); | |
175 } | |
176 | |
177 @Override | |
178 public boolean willBlock() { | |
179 if (is == null) return true; | |
180 try { | |
181 return is.available() == 0; | |
182 } catch (Exception e) { | |
183 return true; | |
184 } | |
185 } | |
186 | |
187 @Override | |
188 public int read(byte[] buffer, int start, int len) throws IOException { | |
189 /* process all already read bytes */ | |
190 int n = 0; | |
191 | |
192 do { | |
193 n = handler.negotiate(buffer, start); | |
194 | |
195 if (n > 0) | |
196 return n; | |
197 } | |
198 while (n == 0); | |
199 | |
200 while (n <= 0) { | |
201 do { | |
202 n = handler.negotiate(buffer, start); | |
203 | |
204 if (n > 0) | |
205 return n; | |
206 } | |
207 while (n == 0); | |
208 | |
209 n = is.read(buffer, start, len); | |
210 | |
211 if (n < 0) { | |
212 bridge.dispatchDisconnect(false); | |
213 throw new IOException("Remote end closed connection."); | |
214 } | |
215 | |
216 handler.inputfeed(buffer, start, n); | |
217 n = handler.negotiate(buffer, start); | |
218 } | |
219 | |
220 return n; | |
221 } | |
222 | |
223 @Override | |
224 public void write(byte[] buffer) throws IOException { | |
225 try { | |
226 if (os != null) | |
227 os.write(buffer); | |
228 } | |
229 catch (SocketException e) { | |
230 bridge.dispatchDisconnect(false); | |
231 } | |
232 } | |
233 | |
234 @Override | |
235 public void write(int c) throws IOException { | |
236 try { | |
237 if (os != null) | |
238 os.write(c); | |
239 } | |
240 catch (SocketException e) { | |
241 bridge.dispatchDisconnect(false); | |
242 } | |
243 } | |
244 | |
245 @Override | |
246 public void setDimensions(int columns, int rows, int width, int height) { | |
247 try { | |
248 handler.setWindowSize(columns, rows); | |
249 } | |
250 catch (IOException e) { | |
251 Log.e(TAG, "Couldn't resize remote terminal", e); | |
252 } | |
253 } | |
254 | |
255 @Override | |
256 public String getDefaultNickname(String username, String hostname, int port) { | |
257 if (port == DEFAULT_PORT) { | |
258 return String.format("%s", hostname); | |
259 } | |
260 else { | |
261 return String.format("%s:%d", hostname, port); | |
262 } | |
263 } | |
264 | |
265 public static Uri getUri(String input) { | |
266 Matcher matcher = hostmask.matcher(input); | |
267 | |
268 if (!matcher.matches()) | |
269 return null; | |
270 | |
271 StringBuilder sb = new StringBuilder(); | |
272 sb.append(PROTOCOL) | |
273 .append("://") | |
274 .append(matcher.group(1)); | |
275 String portString = matcher.group(3); | |
276 int port = DEFAULT_PORT; | |
277 | |
278 if (portString != null) { | |
279 try { | |
280 port = Integer.parseInt(portString); | |
281 | |
282 if (port < 1 || port > 65535) { | |
283 port = DEFAULT_PORT; | |
284 } | |
285 } | |
286 catch (NumberFormatException nfe) { | |
287 // Keep the default port | |
288 } | |
289 } | |
290 | |
291 if (port != DEFAULT_PORT) { | |
292 sb.append(':'); | |
293 sb.append(port); | |
294 } | |
295 | |
296 sb.append("/#") | |
297 .append(Uri.encode(input)); | |
298 Uri uri = Uri.parse(sb.toString()); | |
299 return uri; | |
300 } | |
301 | |
302 @Override | |
303 public HostBean createHost(Uri uri) { | |
304 HostBean host = new HostBean(); | |
305 host.setProtocol(PROTOCOL); | |
306 host.setHostname(uri.getHost()); | |
307 int port = uri.getPort(); | |
308 | |
309 if (port < 0) | |
310 port = DEFAULT_PORT; | |
311 | |
312 host.setPort(port); | |
313 String nickname = uri.getFragment(); | |
314 | |
315 if (nickname == null || nickname.length() == 0) { | |
316 host.setNickname(getDefaultNickname(host.getUsername(), | |
317 host.getHostname(), host.getPort())); | |
318 } | |
319 else { | |
320 host.setNickname(uri.getFragment()); | |
321 } | |
322 | |
323 return host; | |
324 } | |
325 | |
326 @Override | |
327 public void getSelectionArgs(Uri uri, Map<String, String> selection) { | |
328 selection.put(HostDatabase.FIELD_HOST_PROTOCOL, PROTOCOL); | |
329 selection.put(HostDatabase.FIELD_HOST_NICKNAME, uri.getFragment()); | |
330 selection.put(HostDatabase.FIELD_HOST_HOSTNAME, uri.getHost()); | |
331 int port = uri.getPort(); | |
332 | |
333 if (port < 0) | |
334 port = DEFAULT_PORT; | |
335 | |
336 selection.put(HostDatabase.FIELD_HOST_PORT, Integer.toString(port)); | |
337 } | |
338 | |
339 public static String getFormatHint(Context context) { | |
340 return String.format("%s:%s", | |
341 context.getString(R.string.format_hostname), | |
342 context.getString(R.string.format_port)); | |
343 } | |
344 | |
345 /* (non-Javadoc) | |
346 * @see com.five_ten_sg.connectbot.transport.AbsTransport#usesNetwork() | |
347 */ | |
348 @Override | |
349 public boolean usesNetwork() { | |
350 return true; | |
351 } | |
352 } |