1 2 package ch.ethz.ssh2.channel; 3 4 import java.io.IOException ; 5 import java.net.InetSocketAddress ; 6 import java.net.ServerSocket ; 7 import java.net.Socket ; 8 9 15 public class LocalAcceptThread extends Thread implements IChannelWorkerThread 16 { 17 ChannelManager cm; 18 String host_to_connect; 19 int port_to_connect; 20 21 final ServerSocket ss; 22 23 public LocalAcceptThread(ChannelManager cm, int local_port, String host_to_connect, int port_to_connect) 24 throws IOException 25 { 26 this.cm = cm; 27 this.host_to_connect = host_to_connect; 28 this.port_to_connect = port_to_connect; 29 30 ss = new ServerSocket (local_port); 31 } 32 33 public LocalAcceptThread(ChannelManager cm, InetSocketAddress localAddress, String host_to_connect, 34 int port_to_connect) throws IOException 35 { 36 this.cm = cm; 37 this.host_to_connect = host_to_connect; 38 this.port_to_connect = port_to_connect; 39 40 ss = new ServerSocket (); 41 ss.bind(localAddress); 42 } 43 44 public void run() 45 { 46 try 47 { 48 cm.registerThread(this); 49 } 50 catch (IOException e) 51 { 52 stopWorking(); 53 return; 54 } 55 56 while (true) 57 { 58 Socket s = null; 59 60 try 61 { 62 s = ss.accept(); 63 } 64 catch (IOException e) 65 { 66 stopWorking(); 67 return; 68 } 69 70 Channel cn = null; 71 StreamForwarder r2l = null; 72 StreamForwarder l2r = null; 73 74 try 75 { 76 77 78 cn = cm.openDirectTCPIPChannel(host_to_connect, port_to_connect, s.getInetAddress().getHostAddress(), s 79 .getPort()); 80 81 } 82 catch (IOException e) 83 { 84 85 86 try 87 { 88 s.close(); 89 } 90 catch (IOException ignore) 91 { 92 } 93 94 continue; 95 } 96 97 try 98 { 99 r2l = new StreamForwarder(cn, null, null, cn.stdoutStream, s.getOutputStream(), "RemoteToLocal"); 100 l2r = new StreamForwarder(cn, r2l, s, s.getInputStream(), cn.stdinStream, "LocalToRemote"); 101 } 102 catch (IOException e) 103 { 104 try 105 { 106 107 cn.cm.closeChannel(cn, "Weird error during creation of StreamForwarder (" + e.getMessage() + ")", 108 true); 109 } 110 catch (IOException ignore) 111 { 112 } 113 114 continue; 115 } 116 117 r2l.setDaemon(true); 118 l2r.setDaemon(true); 119 r2l.start(); 120 l2r.start(); 121 } 122 } 123 124 public void stopWorking() 125 { 126 try 127 { 128 129 ss.close(); 130 } 131 catch (IOException e) 132 { 133 } 134 } 135 } 136 | Popular Tags |