internal ChannelForwardedTCPIP() : base() { setLocalWindowSizeMax(LOCAL_WINDOW_SIZE_MAX); setLocalWindowSize(LOCAL_WINDOW_SIZE_MAX); setLocalPacketSize(LOCAL_MAXIMUM_PACKET_SIZE); io = new IO(); connected = true; }
public override void run() { try { socket = Util.createSocket(host, port, TIMEOUT); socket.NoDelay=true; io = new IO(); io.setInputStream(socket.GetStream()); io.setOutputStream(socket.GetStream()); sendOpenConfirmation(); } catch //(Exception e) { sendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED); close = true; disconnect(); return; } thread = Thread.CurrentThread; Buffer buf = new Buffer(rmpsize); Packet packet = new Packet(buf); int i = 0; try { while (thread != null && io != null && io.In != null) { i = io.In.Read(buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20 // padding and mac ); if (i <= 0) { eof(); break; } if (close) break; packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA); buf.putInt(recipient); buf.putInt(i); buf.skip(i); getSession().write(packet, this, i); } } catch //(Exception e) { //Console.Error.WriteLine(e); } disconnect(); }
internal ChannelSession() : base() { type = _session; io = new IO(); }
public void disconnect() { if (!isConnected) return; //Console.Error.WriteLine(this+": disconnect"); //Thread.dumpStack(); if (JSch.getLogger().isEnabled(Logger.INFO)) { JSch.getLogger().log(Logger.INFO, "Disconnecting from " + host + " port " + port); } /* for(int i=0; i<Channel.pool.Count; i++){ try{ Channel c=Channel.pool[i]; if(c.session==this) c.eof(); } catch(Exception e){ } } */ Channel.disconnect(this); isConnected = false; PortWatcher.delPort(this); ChannelForwardedTCPIP.delPort(this); lock (_lock) { if (connectThread != null) { Thread.Sleep(1); // .yield(); connectThread.Interrupt(); connectThread = null; } } thread = null; try { if (io != null) { if (io.In != null) io.In.Close(); if (io.Out != null) io.Out.Close(); if (io.out_ext != null) io.out_ext.Close(); } if (proxy == null) { if (socket != null) socket.Close(); } else { lock (proxy) { proxy.Close(); } proxy = null; } } catch //(Exception e) { // e.printStackTrace(); } io = null; socket = null; // lock(jsch.pool){ // jsch.pool.Remove(this); // } jsch.removeSession(this); //System.gc(); }
public void connect(int connectTimeout) { if (isConnected) { throw new JSchException("session is already connected"); } io = new IO(); if (random == null) { try { Type c = Type.GetType(getConfig("random")); random = (Random)(c.newInstance()); } catch (Exception e) { throw new JSchException(e.ToString(), e); } } Packet.setRandom(random); if (JSch.getLogger().isEnabled(Logger.INFO)) { JSch.getLogger().log(Logger.INFO, "Connecting to " + host + " port " + port); } try { int i, j; if (proxy == null) { Stream In; Stream Out; if (socket_factory == null) { socket = Util.createSocket(host, port, connectTimeout); In = socket.GetStream(); Out = socket.GetStream(); } else { socket = socket_factory.createSocket(host, port); In = socket_factory.GetStream(socket); Out = socket_factory.GetStream(socket); } //if(timeout>0){ socket.setSoTimeout(timeout); } socket.NoDelay = true; io.setInputStream(In); io.setOutputStream(Out); } else { lock (proxy) { proxy.connect(socket_factory, host, port, connectTimeout); io.setInputStream(proxy.getInputStream()); io.setOutputStream(proxy.getOutputStream()); socket = proxy.getSocket(); } } if (connectTimeout > 0 && socket != null) { socket.ReceiveTimeout = connectTimeout; socket.SendTimeout = connectTimeout; //socket.setSoTimeout(connectTimeout); } isConnected = true; if (JSch.getLogger().isEnabled(Logger.INFO)) { JSch.getLogger().log(Logger.INFO, "Connection established"); } jsch.addSession(this); { // Some Cisco devices will miss to read '\n' if it is sent separately. byte[] foo = new byte[V_C.Length + 1]; Array.Copy(V_C, 0, foo, 0, V_C.Length); foo[foo.Length - 1] = (byte)'\n'; io.put(foo, 0, foo.Length); } while (true) { i = 0; j = 0; while (i < buf.buffer.Length) { j = io.getByte(); if (j < 0) break; buf.buffer[i] = (byte)j; i++; if (j == 10) break; } if (j < 0) { throw new JSchException("connection is closed by foreign host"); } if (buf.buffer[i - 1] == 10) { // 0x0a i--; if (i > 0 && buf.buffer[i - 1] == 13) { // 0x0d i--; } } if (i <= 3 || ((i != buf.buffer.Length) && (buf.buffer[0] != 'S' || buf.buffer[1] != 'S' || buf.buffer[2] != 'H' || buf.buffer[3] != '-'))) { // It must not start with 'SSH-' //Console.Error.WriteLine(Encoding.UTF8.GetString(buf.buffer, 0, i); continue; } if (i == buf.buffer.Length || i < 7 || // SSH-1.99 or SSH-2.0 (buf.buffer[4] == '1' && buf.buffer[6] != '9') // SSH-1.5 ) { throw new JSchException("invalid server's version string"); } break; } V_S = new byte[i]; Array.Copy(buf.buffer, 0, V_S, 0, i); //Console.Error.WriteLine("V_S: ("+i+") ["+Encoding.UTF8.GetString(V_S)+"]"); if (JSch.getLogger().isEnabled(Logger.INFO)) { JSch.getLogger().log(Logger.INFO, "Remote version string: " + Encoding.UTF8.GetString(V_S)); JSch.getLogger().log(Logger.INFO, "Local version string: " + Encoding.UTF8.GetString(V_C)); } send_kexinit(); buf = Read(buf); if (buf.getCommand() != SSH_MSG_KEXINIT) { throw new JSchException("invalid protocol: " + buf.getCommand()); } if (JSch.getLogger().isEnabled(Logger.INFO)) { JSch.getLogger().log(Logger.INFO, "SSH_MSG_KEXINIT received"); } KeyExchange kex = receive_kexinit(buf); while (true) { buf = Read(buf); if (kex.getState() == buf.getCommand()) { bool result = kex.next(buf); if (!result) { //Console.Error.WriteLine("verify: "+result); in_kex = false; throw new JSchException("verify: " + result); } } else { in_kex = false; throw new JSchException("invalid protocol(kex): " + buf.getCommand()); } if (kex.getState() == KeyExchange.STATE_END) { break; } } try { checkHost(host, port, kex); } catch (JSchException ee) { in_kex = false; throw ee; } send_newkeys(); // receive SSH_MSG_NEWKEYS(21) buf = Read(buf); //Console.Error.WriteLine("read: 21 ? "+buf.getCommand()); if (buf.getCommand() == SSH_MSG_NEWKEYS) { if (JSch.getLogger().isEnabled(Logger.INFO)) { JSch.getLogger().log(Logger.INFO, "SSH_MSG_NEWKEYS received"); } receive_newkeys(buf, kex); } else { in_kex = false; throw new JSchException("invalid protocol(newkyes): " + buf.getCommand()); } bool auth = false; bool auth_cancel = false; UserAuth ua = null; try { Type c = Type.GetType(getConfig("userauth.none")); ua = (UserAuth)(c.newInstance()); } catch (Exception e) { throw new JSchException(e.ToString(), e); } auth = ua.start(this); string cmethods = getConfig("PreferredAuthentications"); string[] cmethoda = Util.split(cmethods, ","); string smethods = null; if (!auth) { smethods = ((UserAuthNone)ua).getMethods(); if (smethods != null) { smethods = smethods.ToLower(); } else { // methods: publickey,password,keyboard-interactive //smethods="publickey,password,keyboard-interactive"; smethods = cmethods; } } string[] smethoda = Util.split(smethods, ","); int methodi = 0; while (true) { loop: while (!auth && cmethoda != null && methodi < cmethoda.Length) { string method = cmethoda[methodi++]; bool acceptable = false; for (int k = 0; k < smethoda.Length; k++) { if (smethoda[k].Equals(method)) { acceptable = true; break; } } if (!acceptable) { continue; } if (JSch.getLogger().isEnabled(Logger.INFO)) { string str = "Authentications that can continue: "; for (int k = methodi - 1; k < cmethoda.Length; k++) { str += cmethoda[k]; if (k + 1 < cmethoda.Length) str += ","; } JSch.getLogger().log(Logger.INFO, str); JSch.getLogger().log(Logger.INFO, "Next authentication method: " + method); } ua = null; try { Type c = null; if (getConfig("userauth." + method) != null) { c = Type.GetType(getConfig("userauth." + method)); ua = (UserAuth)(c.newInstance()); } } catch { if (JSch.getLogger().isEnabled(Logger.WARN)) { JSch.getLogger().log(Logger.WARN, "failed to load " + method + " method"); } } if (ua != null) { auth_cancel = false; try { auth = ua.start(this); if (auth && JSch.getLogger().isEnabled(Logger.INFO)) { JSch.getLogger().log(Logger.INFO, "Authentication succeeded (" + method + ")."); } } catch (JSchAuthCancelException ) { auth_cancel = true; } catch (JSchPartialAuthException ee) { smethods = ee.getMethods(); smethoda = Util.split(smethods, ","); methodi = 0; //Console.Error.WriteLine("PartialAuth: "+methods); auth_cancel = false; goto loop; } /* catch (RuntimeException ee) { throw ee; }*/ catch //(Exception ee) { //Console.Error.WriteLine("ee: "+ee); // SSH_MSG_DISCONNECT: 2 Too many authentication failures goto outloop; } } } break; } outloop: if (!auth) { if (auth_cancel) throw new JSchException("Auth cancel"); throw new JSchException("Auth fail"); } if (connectTimeout > 0 || timeout > 0) { socket.SendTimeout = timeout; socket.ReceiveTimeout = timeout; //socket.setSoTimeout(timeout); } isAuthed = true; lock (_lock) { if (isConnected) { connectThread = new Thread(run); connectThread.Name="Connect thread " + host + " session"; if (daemon_thread) { connectThread.IsBackground=daemon_thread; } connectThread.Start(); } else { // The session has been already down and // we don't have to start new thread. } } } catch (Exception e) { in_kex = false; if (isConnected) { try { packet.reset(); buf.putByte((byte)SSH_MSG_DISCONNECT); buf.putInt(3); buf.putString(e.ToString().getBytes()); buf.putString("en".getBytes()); write(packet); disconnect(); } catch //(Exception ee) { } } isConnected = false; //e.printStackTrace(); /*if (e is RuntimeException) throw (RuntimeException)e;*/ if (e is JSchException) throw (JSchException)e; throw new JSchException("Session.connect: " + e); } finally { Util.bzero(this.password); this.password = null; } }