Exemplo n.º 1
0
 public void Close()
 {
     lock (tcprelay.Handlers)
     {
         tcprelay.Handlers.Remove(this);
     }
     lock (this) {
         if (_closed)
         {
             return;
         }
         _closed = true;
     }
     try
     {
         connection?.Shutdown(SocketShutdown.Both);
         connection?.Close();
     }
     catch (Exception e)
     {
         Logging.LogUsefulException(e);
     }
     try
     {
         remote?.Shutdown(SocketShutdown.Both);
         remote?.Close();
     }
     catch (Exception e)
     {
         Logging.LogUsefulException(e);
     }
     lock (_encryptionLock)
     {
         lock (_decryptionLock)
         {
             encryptor?.Dispose();
         }
     }
 }
Exemplo n.º 2
0
        public void Close()
        {
            lock (_closeConnLock)
            {
                if (_closed)
                {
                    return;
                }

                _closed = true;
            }

            OnClosed?.Invoke(this, new SSRelayEventArgs(_server));

            try
            {
                _connection.Shutdown(SocketShutdown.Both);
                _connection.Close();
            }
            catch (Exception e)
            {
                Logger.LogUsefulException(e);
            }

            if (_currentRemoteSession != null)
            {
                try
                {
                    IProxy remote = _currentRemoteSession.Remote;
                    remote.Shutdown(SocketShutdown.Both);
                    remote.Close();
                }
                catch (Exception e)
                {
                    Logger.LogUsefulException(e);
                }
            }

            lock (_encryptionLock)
            {
                lock (_decryptionLock)
                {
                    _encryptor?.Dispose();
                }
            }
        }
Exemplo n.º 3
0
        private void ProxyConnectTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            ProxyTimer timer = (ProxyTimer)sender;

            timer.Elapsed -= ProxyConnectTimer_Elapsed;
            timer.Enabled  = false;
            timer.Dispose();


            if (_proxyConnected || _destConnected || _closed)
            {
                return;
            }
            IProxy proxy = timer.Session.Remote;

            Logger.Info($"Proxy {proxy.ProxyEndPoint} timed out");
            proxy.Close();
            Close();
        }
Exemplo n.º 4
0
        public void connect(int connectTimeout)
        {
            if (_isConnected)
            {
                throw new JSchException("session is already connected");
            }
            io = new IO();
            if (random == null)
            {
                try
                {
                    var c = Type.GetType(getConfig("random"));
                    random = (Random)Activator.CreateInstance(c);

                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("connect: random " + e);
                }
            }
            Packet.setRandom(random);

            try
            {
                int i, j;
                // int pad = 0;

                if (proxy == null)
                {
                    proxy = jsch.getProxy(host);
                    if (proxy != null)
                    {
                        lock (proxy)
                        {
                            proxy.Close();
                        }
                    }
                }

                if (proxy == null)
                {
                    Stream In;
                    Stream 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.setTcpNoDelay(true);
                    io.setInputStream(In);
                    io.setOutputStream(Out);
                }
                else
                {
                    lock (proxy)
                    {
                        proxy.Connect(socket_factory, host, port, connectTimeout);
                        io.setInputStream(proxy.InputStream);
                        io.setOutputStream(proxy.OutputStream);
                        socket = proxy.Socket;
                    }
                }

                if (connectTimeout > 0 && socket != null)
                {
                    socket.setSoTimeout(connectTimeout);
                }

                _isConnected = true;

                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 (buf.buffer[i - 1] == 13)
                        {
                            // 0x0d
                            i--;
                        }
                    }

                    if (i > 4 && (i != buf.buffer.Length) &&
                        (buf.buffer[0] != 'S' || buf.buffer[1] != 'S' ||
                         buf.buffer[2] != 'H' || buf.buffer[3] != '-'))
                    {
                        //System.err.println(new String(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);
                {
                    // 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);
                }

                buf = read(buf);
                //System.Console.WriteLine("read: 20 ? "+buf.buffer[5]);
                if (buf.buffer[5] != SSH_MSG_KEXINIT)
                {
                    throw new JSchException("invalid protocol: " + buf.buffer[5]);
                }
                KeyExchange kex = receive_kexinit(buf);

                while (true)
                {
                    buf = read(buf);
                    if (kex.getState() == buf.buffer[5])
                    {
                        bool result = kex.next(buf);
                        if (!result)
                        {
                            //System.Console.WriteLine("verify: "+result);
                            in_kex = false;
                            throw new JSchException("verify: " + result);
                        }
                    }
                    else
                    {
                        in_kex = false;
                        throw new JSchException("invalid protocol(kex): " + buf.buffer[5]);
                    }
                    if (kex.getState() == KeyExchange.STATE_END)
                    {
                        break;
                    }
                }

                try
                {
                    checkHost(host, kex);
                }
                catch (JSchException ee)
                {
                    in_kex = false;
                    throw ee;
                }

                send_newkeys();

                // receive SSH_MSG_NEWKEYS(21)
                buf = read(buf);
                //System.Console.WriteLine("read: 21 ? "+buf.buffer[5]);
                if (buf.buffer[5] == SSH_MSG_NEWKEYS)
                {
                    receive_newkeys(buf, kex);
                }
                else
                {
                    in_kex = false;
                    throw new JSchException("invalid protocol(newkyes): " + buf.buffer[5]);
                }

                bool auth = false;
                bool auth_cancel = false;

                UserAuthNone usn = new UserAuthNone(userinfo);
                auth = usn.start(this);

                String methods = null;
                if (!auth)
                {
                    methods = usn.getMethods();
                    if (methods != null)
                    {
                        methods = methods.ToLower();
                    }
                    else
                    {
                        // methods: publickey,password,keyboard-interactive
                        methods = "publickey,password,keyboard-interactive";
                    }
                }

                while (true)
                {
                    while (!auth && methods != null && methods.Length > 0)
                    {
                        UserAuth us = null;
                        if (methods.StartsWith("publickey"))
                        {
                            lock (jsch.identities)
                            {
                                if (jsch.identities.Count > 0)
                                {
                                    us = new UserAuthPublicKey(userinfo);
                                }
                            }
                        }
                        else if (methods.StartsWith("keyboard-interactive"))
                        {
                            if (userinfo is UIKeyboardInteractive)
                            {
                                us = new UserAuthKeyboardInteractive(userinfo);
                            }
                        }
                        else if (methods.StartsWith("password"))
                        {
                            us = new UserAuthPassword(userinfo);
                        }
                        if (us != null)
                        {
                            try
                            {
                                auth = us.start(this);
                                auth_cancel = false;
                            }
                            catch (JSchAuthCancelException)
                            {
                                auth_cancel = true;
                            }
                            catch (JSchPartialAuthException ee)
                            {
                                methods = ee.getMethods();
                                auth_cancel = false;
                                continue;
                            }
                            catch (Exception ee)
                            {
                                Console.WriteLine("ee: " + ee);
                            }
                        }
                        if (!auth)
                        {
                            int comma = methods.IndexOf(",");
                            if (comma == -1) break;
                            methods = methods.Substring(comma + 1);
                        }
                    }
                    break;
                }

                if (connectTimeout > 0 || timeout > 0)
                {
                    socket.setSoTimeout(timeout);
                }

                if (auth)
                {
                    isAuthed = true;
                    connectThread = new JavaThread(this);
                    connectThread.Name("Connect thread " + host + " session");
                    connectThread.Start();
                    return;
                }
                if (auth_cancel)
                    throw new JSchException("Auth cancel");
                throw new JSchException("Auth fail");
            }
            catch (Exception e)
            {
                in_kex = false;
                if (_isConnected)
                {
                    try
                    {
                        packet.reset();
                        buf.putByte((byte)SSH_MSG_DISCONNECT);
                        buf.putInt(3);
                        buf.putString(new JavaString(e.ToString()).GetBytes());
                        buf.putString(new JavaString("en").GetBytes());
                        write(packet);
                        disconnect();
                    }
                    catch (Exception) { }
                }
                _isConnected = false;
                if (e is JSchException) throw (JSchException)e;
                throw new JSchException("Session.connect: " + e);
            }
        }