/// <exception cref="NSch.JSchException"></exception> public virtual void Connect(int connectTimeout) { if (isConnected) { throw new JSchException("session is already connected"); } io = new IO(); if (random == null) { try { Type c = Sharpen.Runtime.GetType(GetConfig("random")); random = (Random)(System.Activator.CreateInstance(c)); } 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; int j; if (proxy == null) { InputStream @in; OutputStream @out; if (socket_factory == null) { socket = Util.CreateSocket(host, port, connectTimeout); @in = socket.GetInputStream(); @out = socket.GetOutputStream(); } else { socket = socket_factory.CreateSocket(host, port); @in = socket_factory.GetInputStream(socket); @out = socket_factory.GetOutputStream(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; } 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]; System.Array.Copy(V_C, 0, foo, 0, V_C.Length); foo[foo.Length - 1] = unchecked((byte)(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] = unchecked((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-' //System.err.println(new String(buf.buffer, 0, i); continue; } if (i == buf.buffer.Length || i < 7 || (buf.buffer[4] == '1' && buf.buffer[6] != '9')) { // SSH-1.99 or SSH-2.0 // SSH-1.5 throw new JSchException("invalid server's version string"); } break; } V_S = new byte[i]; System.Array.Copy(buf.buffer, 0, V_S, 0, i); //System.err.println("V_S: ("+i+") ["+new String(V_S)+"]"); if (JSch.GetLogger().IsEnabled(Logger.INFO)) { JSch.GetLogger().Log(Logger.INFO, "Remote version string: " + Util.Byte2str(V_S)); JSch.GetLogger().Log(Logger.INFO, "Local version string: " + Util.Byte2str(V_C)); } Send_kexinit(); buf = Read(buf); if (buf.GetCommand() != SSH_MSG_KEXINIT) { in_kex = false; 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()) { kex_start_time = Runtime.CurrentTimeMillis(); bool result = kex.Next(buf); if (!result) { //System.err.println("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; } Send_newkeys(); // receive SSH_MSG_NEWKEYS(21) buf = Read(buf); //System.err.println("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 = Sharpen.Runtime.GetType(GetConfig("userauth.none")); ua = (UserAuth)(System.Activator.CreateInstance(c)); } 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) { //System.err.println("methods: "+methods); 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; } //System.err.println(" method: "+method); if (JSch.GetLogger().IsEnabled(Logger.INFO)) { string str = "Authentications that can continue: "; for (int k_1 = methodi - 1; k_1 < cmethoda.Length; k_1++) { str += cmethoda[k_1]; if (k_1 + 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 = Sharpen.Runtime.GetType(GetConfig("userauth." + method)); ua = (UserAuth)(System.Activator.CreateInstance(c)); } } catch (Exception) { 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) { string tmp = smethods; smethods = ee.GetMethods(); smethoda = Util.Split(smethods, ","); if (!tmp.Equals(smethods)) { methodi = 0; } //System.err.println("PartialAuth: "+methods); auth_cancel = false; goto loop_continue; } catch (RuntimeException ee) { throw; } catch (Exception) { //System.err.println("ee: "+ee); // SSH_MSG_DISCONNECT: 2 Too many authentication failures goto loop_break; } } } break; loop_continue: ; } loop_break: ; if (!auth) { if (auth_cancel) { throw new JSchException("Auth cancel"); } throw new JSchException("Auth fail"); } if (connectTimeout > 0 || timeout > 0) { socket.ReceiveTimeout = timeout; } isAuthed = true; lock (Lock) { if (isConnected) { connectThread = new Sharpen.Thread(this); connectThread.SetName("Connect thread " + host + " session"); if (daemon_thread) { connectThread.SetDaemon(daemon_thread); } connectThread.Start(); } } } catch (Exception e) { // The session has been already down and // we don't have to start new thread. in_kex = false; if (isConnected) { try { packet.Reset(); buf.PutByte(unchecked((byte)SSH_MSG_DISCONNECT)); buf.PutInt(3); buf.PutString(Util.Str2byte(e.ToString())); buf.PutString(Util.Str2byte("en")); Write(packet); Disconnect(); } catch (Exception) { } } 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; } }
/// <exception cref="NSch.JSchException"></exception> public virtual void Connect(SocketFactory socket_factory, string host, int port, int timeout) { try { if (socket_factory == null) { socket = Util.CreateSocket(proxy_host, proxy_port, timeout); @in = socket.GetInputStream(); @out = socket.GetOutputStream(); } else { socket = socket_factory.CreateSocket(proxy_host, proxy_port); @in = socket_factory.GetInputStream(socket); @out = socket_factory.GetOutputStream(socket); } if (timeout > 0) { socket.ReceiveTimeout = timeout; } socket.NoDelay = true; @out.Write(Util.Str2byte("CONNECT " + host + ":" + port + " HTTP/1.0\r\n")); if (user != null && passwd != null) { byte[] code = Util.Str2byte(user + ":" + passwd); code = Util.ToBase64(code, 0, code.Length); @out.Write(Util.Str2byte("Proxy-Authorization: Basic ")); @out.Write(code); @out.Write(Util.Str2byte("\r\n")); } @out.Write(Util.Str2byte("\r\n")); @out.Flush(); int foo = 0; StringBuilder sb = new StringBuilder(); while (foo >= 0) { foo = @in.Read(); if (foo != 13) { sb.Append((char)foo); continue; } foo = @in.Read(); if (foo != 10) { continue; } break; } if (foo < 0) { throw new IOException(); } string response = sb.ToString(); string reason = "Unknow reason"; int code_1 = -1; try { foo = response.IndexOf(' '); int bar = response.IndexOf(' ', foo + 1); code_1 = System.Convert.ToInt32(Sharpen.Runtime.Substring(response, foo + 1, bar) ); reason = Sharpen.Runtime.Substring(response, bar + 1); } catch (Exception) { } if (code_1 != 200) { throw new IOException("proxy error: " + reason); } int count = 0; while (true) { count = 0; while (foo >= 0) { foo = @in.Read(); if (foo != 13) { count++; continue; } foo = @in.Read(); if (foo != 10) { continue; } break; } if (foo < 0) { throw new IOException(); } if (count == 0) { break; } } } catch (RuntimeException e) { throw; } catch (Exception e) { try { if (socket != null) { socket.Close(); } } catch (Exception) { } string message = "ProxyHTTP: " + e.ToString(); if (e is Exception) { throw new JSchException(message, (Exception)e); } throw new JSchException(message); } }
public override void Run() { try { socket = Util.CreateSocket(host, port, TIMEOUT); socket.NoDelay = true; io = new IO(); io.SetInputStream(socket.GetInputStream()); io.SetOutputStream(socket.GetOutputStream()); SendOpenConfirmation(); } catch (Exception) { SendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED); close = true; Disconnect(); return; } thread = Sharpen.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 = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20); // padding and mac if (i <= 0) { Eof(); break; } if (close) { break; } packet.Reset(); buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA)); buf.PutInt(recipient); buf.PutInt(i); buf.Skip(i); GetSession().Write(packet, this, i); } } catch (Exception) { } //System.err.println(e); Disconnect(); }
public override void Run() { try { if (lport == -1) { Type c = Sharpen.Runtime.GetType(target); daemon = (ForwardedTCPIPDaemon)System.Activator.CreateInstance(c); PipedOutputStream @out = new PipedOutputStream(); io.SetInputStream(new Channel.PassiveInputStream(this, @out, 32 * 1024), false); daemon.SetChannel(this, GetInputStream(), @out); object[] foo = GetPort(GetSession(), rport); daemon.SetArg((object[])foo[3]); new Sharpen.Thread(daemon).Start(); } else { socket = (factory == null) ? Util.CreateSocket(target, lport, TIMEOUT) : factory. CreateSocket(target, lport); socket.NoDelay = true; io.SetInputStream(socket.GetInputStream()); io.SetOutputStream(socket.GetOutputStream()); } SendOpenConfirmation(); } catch (Exception) { SendOpenFailure(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED); close = true; Disconnect(); return; } thread = Sharpen.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 = [email protected](buf.buffer, 14, buf.buffer.Length - 14 - 32 - 20); // padding and mac if (i <= 0) { Eof(); break; } packet.Reset(); if (close) { break; } buf.PutByte(unchecked((byte)Session.SSH_MSG_CHANNEL_DATA)); buf.PutInt(recipient); buf.PutInt(i); buf.Skip(i); GetSession().Write(packet, this, i); } } catch (Exception) { } //System.err.println(e); //thread=null; //eof(); Disconnect(); }
/// <exception cref="NSch.JSchException"></exception> public virtual void Connect(SocketFactory socket_factory, string host, int port, int timeout) { try { if (socket_factory == null) { socket = Util.CreateSocket(proxy_host, proxy_port, timeout); //socket=new Socket(proxy_host, proxy_port); @in = socket.GetInputStream(); @out = socket.GetOutputStream(); } else { socket = socket_factory.CreateSocket(proxy_host, proxy_port); @in = socket_factory.GetInputStream(socket); @out = socket_factory.GetOutputStream(socket); } if (timeout > 0) { socket.ReceiveTimeout = timeout; } socket.NoDelay = true; byte[] buf = new byte[1024]; int index = 0; buf[index++] = 5; buf[index++] = 2; buf[index++] = 0; // NO AUTHENTICATION REQUIRED buf[index++] = 2; // USERNAME/PASSWORD @out.Write(buf, 0, index); //in.read(buf, 0, 2); Fill(@in, buf, 2); bool check = false; switch ((buf[1]) & unchecked((int)(0xff))) { case 0: { // NO AUTHENTICATION REQUIRED check = true; break; } case 2: { // USERNAME/PASSWORD if (user == null || passwd == null) { break; } index = 0; buf[index++] = 1; buf[index++] = unchecked((byte)(user.Length)); System.Array.Copy(Util.Str2byte(user), 0, buf, index, user.Length); index += user.Length; buf[index++] = unchecked((byte)(passwd.Length)); System.Array.Copy(Util.Str2byte(passwd), 0, buf, index, passwd.Length); index += passwd.Length; @out.Write(buf, 0, index); //in.read(buf, 0, 2); Fill(@in, buf, 2); if (buf[1] == 0) { check = true; } break; } default: { break; } } if (!check) { try { socket.Close(); } catch (Exception) { } throw new JSchException("fail in SOCKS5 proxy"); } index = 0; buf[index++] = 5; buf[index++] = 1; // CONNECT buf[index++] = 0; byte[] hostb = Util.Str2byte(host); int len = hostb.Length; buf[index++] = 3; // DOMAINNAME buf[index++] = unchecked((byte)(len)); System.Array.Copy(hostb, 0, buf, index, len); index += len; buf[index++] = unchecked((byte)((int)(((uint)port) >> 8))); buf[index++] = unchecked((byte)(port & unchecked((int)(0xff)))); @out.Write(buf, 0, index); //in.read(buf, 0, 4); Fill(@in, buf, 4); if (buf[1] != 0) { try { socket.Close(); } catch (Exception) { } throw new JSchException("ProxySOCKS5: server returns " + buf[1]); } switch (buf[3] & unchecked((int)(0xff))) { case 1: { //in.read(buf, 0, 6); Fill(@in, buf, 6); break; } case 3: { //in.read(buf, 0, 1); Fill(@in, buf, 1); //in.read(buf, 0, buf[0]+2); Fill(@in, buf, (buf[0] & unchecked((int)(0xff))) + 2); break; } case 4: { //in.read(buf, 0, 18); Fill(@in, buf, 18); break; } default: { break; } } } catch (RuntimeException e) { throw; } catch (Exception e) { try { if (socket != null) { socket.Close(); } } catch (Exception) { } string message = "ProxySOCKS5: " + e.ToString(); if (e is Exception) { throw new JSchException(message, (Exception)e); } throw new JSchException(message); } }