private void Delete() { try { thread = null; if (ss != null) { ss.Stop(); } ss = null; } catch (Exception) { } }
public void run() { Buffer buf = new Buffer(300); // ?? Packet packet = new Packet(buf); thread = this; try { while (thread != null) { Socket socket = new Socket(ss.AcceptSocket()); socket.setTcpNoDelay(true); Stream In = socket.getInputStream(); Stream Out = socket.getOutputStream(); ChannelDirectTCPIP channel = new ChannelDirectTCPIP(); channel.init(); channel.setInputStream(In); channel.setOutputStream(Out); session.addChannel(channel); ((ChannelDirectTCPIP)channel).setHost(host); ((ChannelDirectTCPIP)channel).setPort(rport); ((ChannelDirectTCPIP)channel).setOrgIPAddress(socket.getInetAddress().getHostAddress()); ((ChannelDirectTCPIP)channel).setOrgPort(socket.getPort()); channel.connect(); if (channel.exitstatus != -1) { } } } catch (Exception) { } Delete(); }
public JavaThread(JavaRunnable r) : this(new Thread(new ThreadStart(r.run))) { }
private void Delete() { try { thread = null; if (ss != null) ss.Stop(); ss = null; } catch (Exception) { } }
public void run() { Buffer buf = new Buffer(300); // ?? Packet packet = new Packet(buf); thread = this; try { while (thread != null) { Socket socket = new Socket(ss.AcceptSocket()); socket.setTcpNoDelay(true); Stream In = socket.getInputStream(); Stream Out = socket.getOutputStream(); ChannelDirectTCPIP channel = new ChannelDirectTCPIP(); channel.init(); channel.setInputStream(In); channel.setOutputStream(Out); session.addChannel(channel); ((ChannelDirectTCPIP) channel).setHost(host); ((ChannelDirectTCPIP) channel).setPort(rport); ((ChannelDirectTCPIP) channel).setOrgIPAddress(socket.getInetAddress().getHostAddress()); ((ChannelDirectTCPIP) channel).setOrgPort(socket.getPort()); channel.connect(); if (channel.exitstatus != -1) { } } } catch (Exception) { } Delete(); }
public void run() { thread = this; byte[] foo; Buffer buf = new Buffer(); Packet packet = new Packet(buf); int i = 0; Channel channel; int[] start = new int[1]; int[] length = new int[1]; KeyExchange kex = null; try { while (_isConnected && thread != null) { buf = read(buf); int msgType = buf.buffer[5] & 0xff; if (kex != null && kex.getState() == msgType) { bool result = kex.next(buf); if (!result) { throw new JSchException("verify: " + result); } continue; } switch (msgType) { case SSH_MSG_KEXINIT: //System.Console.WriteLine("KEXINIT"); kex = receive_kexinit(buf); break; case SSH_MSG_NEWKEYS: //System.Console.WriteLine("NEWKEYS"); send_newkeys(); receive_newkeys(buf, kex); kex = null; break; case SSH_MSG_CHANNEL_DATA: buf.getInt(); buf.getByte(); buf.getByte(); i = buf.getInt(); channel = Channel.getChannel(i, this); foo = buf.getString(start, length); if (channel == null) { break; } try { channel.write(foo, start[0], length[0]); } catch (Exception) { //System.Console.WriteLine(e); try { channel.disconnect(); } catch (Exception) { } break; } int len = length[0]; channel.setLocalWindowSize(channel.lwsize - len); if (channel.lwsize < channel.lwsize_max / 2) { packet.reset(); buf.putByte((byte)SSH_MSG_CHANNEL_WINDOW_ADJUST); buf.putInt(channel.getRecipient()); buf.putInt(channel.lwsize_max - channel.lwsize); write(packet); channel.setLocalWindowSize(channel.lwsize_max); } break; case SSH_MSG_CHANNEL_EXTENDED_DATA: buf.getInt(); buf.getShort(); i = buf.getInt(); channel = Channel.getChannel(i, this); buf.getInt(); // data_type_code == 1 foo = buf.getString(start, length); //System.Console.WriteLine("stderr: "+new String(foo,start[0],length[0])); if (channel == null) { break; } //channel.write(foo, start[0], length[0]); channel.write_ext(foo, start[0], length[0]); len = length[0]; channel.setLocalWindowSize(channel.lwsize - len); if (channel.lwsize < channel.lwsize_max / 2) { packet.reset(); buf.putByte((byte)SSH_MSG_CHANNEL_WINDOW_ADJUST); buf.putInt(channel.getRecipient()); buf.putInt(channel.lwsize_max - channel.lwsize); write(packet); channel.setLocalWindowSize(channel.lwsize_max); } break; case SSH_MSG_CHANNEL_WINDOW_ADJUST: buf.getInt(); buf.getShort(); i = buf.getInt(); channel = Channel.getChannel(i, this); if (channel == null) { break; } channel.addRemoteWindowSize(buf.getInt()); break; case SSH_MSG_CHANNEL_EOF: buf.getInt(); buf.getShort(); i = buf.getInt(); channel = Channel.getChannel(i, this); if (channel != null) { //channel._eof_remote=true; //channel.eof(); channel.eof_remote(); } /* packet.reset(); buf.putByte((byte)SSH_MSG_CHANNEL_EOF); buf.putInt(channel.getRecipient()); write(packet); */ break; case SSH_MSG_CHANNEL_CLOSE: buf.getInt(); buf.getShort(); i = buf.getInt(); channel = Channel.getChannel(i, this); if (channel != null) { // channel.close(); channel.disconnect(); } /* if(Channel.pool.Count==0){ thread=null; } */ break; case SSH_MSG_CHANNEL_OPEN_CONFIRMATION: buf.getInt(); buf.getShort(); i = buf.getInt(); channel = Channel.getChannel(i, this); if (channel == null) { //break; } channel.setRecipient(buf.getInt()); channel.setRemoteWindowSize(buf.getInt()); channel.setRemotePacketSize(buf.getInt()); break; case SSH_MSG_CHANNEL_OPEN_FAILURE: buf.getInt(); buf.getShort(); i = buf.getInt(); channel = Channel.getChannel(i, this); if (channel == null) { //break; } int reason_code = buf.getInt(); //foo=buf.getString(); // additional textual information //foo=buf.getString(); // language tag channel.exitstatus = reason_code; channel._close = true; channel._eof_remote = true; channel.setRecipient(0); break; case SSH_MSG_CHANNEL_REQUEST: buf.getInt(); buf.getShort(); i = buf.getInt(); foo = buf.getString(); bool reply = (buf.getByte() != 0); channel = Channel.getChannel(i, this); if (channel != null) { byte reply_type = (byte)SSH_MSG_CHANNEL_FAILURE; if ((new JavaString(foo)).Equals("exit-status")) { i = buf.getInt(); // exit-status channel.setExitStatus(i); // System.Console.WriteLine("exit-stauts: "+i); // channel.close(); reply_type = (byte)SSH_MSG_CHANNEL_SUCCESS; } if (reply) { packet.reset(); buf.putByte(reply_type); buf.putInt(channel.getRecipient()); write(packet); } } else { } break; case SSH_MSG_CHANNEL_OPEN: buf.getInt(); buf.getShort(); foo = buf.getString(); JavaString ctyp = new JavaString(foo); //System.Console.WriteLine("type="+ctyp); if (!new JavaString("forwarded-tcpip").Equals(ctyp) && !(new JavaString("x11").Equals(ctyp) && x11_forwarding)) { Console.WriteLine("Session.run: CHANNEL OPEN " + ctyp); throw new IOException("Session.run: CHANNEL OPEN " + ctyp); } else { channel = Channel.getChannel(ctyp); addChannel(channel); channel.getData(buf); channel.init(); packet.reset(); buf.putByte((byte)SSH_MSG_CHANNEL_OPEN_CONFIRMATION); buf.putInt(channel.getRecipient()); buf.putInt(channel.id); buf.putInt(channel.lwsize); buf.putInt(channel.lmpsize); write(packet); JavaThread tmp = new JavaThread(channel); tmp.Name("Channel " + ctyp + " " + host); tmp.Start(); break; } case SSH_MSG_CHANNEL_SUCCESS: buf.getInt(); buf.getShort(); i = buf.getInt(); channel = Channel.getChannel(i, this); if (channel == null) { break; } channel.reply = 1; break; case SSH_MSG_CHANNEL_FAILURE: buf.getInt(); buf.getShort(); i = buf.getInt(); channel = Channel.getChannel(i, this); if (channel == null) { break; } channel.reply = 0; break; case SSH_MSG_GLOBAL_REQUEST: buf.getInt(); buf.getShort(); foo = buf.getString(); // request name reply = (buf.getByte() != 0); if (reply) { packet.reset(); buf.putByte((byte)SSH_MSG_REQUEST_FAILURE); write(packet); } break; case SSH_MSG_REQUEST_FAILURE: case SSH_MSG_REQUEST_SUCCESS: JavaThread t = grr.getThread(); if (t != null) { grr.setReply(msgType == SSH_MSG_REQUEST_SUCCESS ? 1 : 0); t.Interrupt(); } break; default: Console.WriteLine("Session.run: unsupported type " + msgType); throw new IOException("Unknown SSH message type " + msgType); } } } catch (Exception) { //System.Console.WriteLine("# Session.run"); //e.printStackTrace(); } try { disconnect(); } catch (NullReferenceException) { //System.Console.WriteLine("@1"); //e.printStackTrace(); } catch (Exception) { //System.Console.WriteLine("@2"); //e.printStackTrace(); } _isConnected = false; }
/* public void finalize() throws Throwable{ disconnect(); jsch=null; } */ public void disconnect() { if (!_isConnected) return; //System.Console.WriteLine(this+": disconnect"); //Thread.dumpStack(); /* for(int i=0; i<Channel.pool.Count; i++){ try{ Channel c=((Channel)(Channel.pool[i])); if(c.session==this) c.eof(); } catch(System.Exception e){ } } */ Channel.disconnect(this); _isConnected = false; PortWatcher.delPort(this); ChannelForwardedTCPIP.delPort(this); lock (connectThread) { connectThread.Interrupt(); connectThread = null; } thread = null; try { if (io != null) { if (io.ins != null) io.ins.Close(); if (io.outs != null) io.outs.Close(); if (io.outs_ext != null) io.outs_ext.Close(); } if (proxy == null) { if (socket != null) socket.Close(); } else { lock (proxy) { proxy.Close(); } proxy = null; } } catch (Exception) { // e.printStackTrace(); } io = null; socket = null; // lock(jsch.pool){ // jsch.pool.removeElement(this); // } jsch.removeSession(this); //System.gc(); }