示例#1
0
        /*
         * public void finalize() throws Throwable{
         * disconnect();
         * super.finalize();
         * session=null;
         * }
         */

        public virtual void disconnect()
        {
            //System.Out.println(this+":disconnect "+io+" "+io.in);
            if (!connected)
            {
                return;
            }
            connected = false;

            close();

            _eof_remote = eof_local = true;

            thread = null;

            try {
                if (io != null)
                {
                    io.close();
                }
            }
            catch (Exception) {
                //e.printStackTrace();
            }
            io = null;
            AChannel.del(this);
        }
示例#2
0
        internal static void disconnect(Session session)
        {
            AChannel[] channels = null;
            int        count    = 0;

            lock (pool) {
                channels = new AChannel[pool.Count];
                for (int i = 0; i < pool.Count; i++)
                {
                    try {
                        AChannel c = ((AChannel)(pool[i]));
                        if (c.session == session)
                        {
                            channels[count++] = c;
                        }
                    }
                    catch (Exception) {
                    }
                }
            }
            for (int i = 0; i < count; i++)
            {
                channels[i].disconnect();
            }
        }
示例#3
0
        public void request(Session session, AChannel channel)
        {
            Buffer buf    = new Buffer();
            Packet packet = new Packet(buf);

            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.getRecipient());
            buf.putString(StringAux.getBytes("signal"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putString(StringAux.getBytes(signal));
            session.write(packet);
        }
示例#4
0
 internal static AChannel getChannel(int id, Session session)
 {
     lock (pool) {
         for (int i = 0; i < pool.Count; i++)
         {
             AChannel c = (AChannel)(pool[i]);
             if (c.id == id && c.session == session)
             {
                 return(c);
             }
         }
     }
     return(null);
 }
示例#5
0
        public void request(Session session, AChannel channel)
        {
            Packet packet = session.packet;
            Buffer buf    = session.buf;

            // send
            // byte     SSH_MSG_CHANNEL_REQUEST(98)
            // uint32 recipient channel
            // string request type       // "exec"
            // boolean want reply        // 0
            // string command
            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.getRecipient());
            buf.putString(StringAux.getBytesUTF8("exec"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putString(StringAux.getBytesUTF8(command));
            session.write(packet);
        }
示例#6
0
 public override void disconnect()
 {
     close();
     thread = null;
     try {
         if (io != null)
         {
             try {
                 if (io.ins != null)
                 {
                     io.ins.Close();
                 }
             }
             catch {
             }
             try {
                 if (io.outs != null)
                 {
                     io.outs.Close();
                 }
             }
             catch {
             }
         }
         try {
             if (socket != null)
             {
                 socket.Close();
             }
         }
         catch {
         }
     }
     catch (Exception e) {
         Console.WriteLine(e.StackTrace);
     }
     io = null;
     AChannel.del(this);
 }
示例#7
0
        public void request(Session session, AChannel channel)
        {
            Buffer buf    = new Buffer();
            Packet packet = new Packet(buf);

            // byte      SSH_MSG_CHANNEL_REQUEST(98)
            // uint32 recipient channel
            // string request type        // "x11-req"
            // boolean want reply         // 0
            // boolean   single connection
            // string    x11 authentication protocol // "MIT-MAGIC-COOKIE-1".
            // string    x11 authentication cookie
            // uint32    x11 screen number
            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.getRecipient());
            buf.putString(StringAux.getBytes("x11-req"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putByte((byte)0);
            buf.putString(StringAux.getBytes("MIT-MAGIC-COOKIE-1"));
            buf.putString(ChannelX11.getFakedCookie(session));
            buf.putInt(0);
            session.write(packet);
        }
示例#8
0
        public void request(Session session, AChannel channel)
        {
            Buffer buf    = new Buffer();
            Packet packet = new Packet(buf);

            bool reply = waitForReply();

            if (reply)
            {
                channel.reply = -1;
            }

            packet.reset();
            buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST);
            buf.putInt(channel.getRecipient());
            buf.putString(StringAux.getBytesUTF8("subsystem"));
            buf.putByte((byte)(waitForReply() ? 1 : 0));
            buf.putString(StringAux.getBytesUTF8(subsystem));
            session.write(packet);

            if (reply)
            {
                while (channel.reply == -1)
                {
                    try {
                        ThreadAux.Sleep(10);
                    }
                    catch (System.Exception) {
                    }
                }
                if (channel.reply == 0)
                {
                    throw new SshClientException("failed to send subsystem request");
                }
            }
        }
示例#9
0
 internal static void del(AChannel c)
 {
     lock (pool) {
         pool.Remove(c);
     }
 }
示例#10
0
 public void request(Session session, AChannel channel, String subsystem, bool want_reply)
 {
     this.subsystem  = subsystem;
     this.want_reply = want_reply;
     this.request(session, channel);
 }
        public override void connect()
        {
            try {
                if (!session.isConnected())
                {
                    throw new SshClientException("session is down");
                }
                Buffer buf    = new Buffer(150);
                Packet packet = new Packet(buf);
                // send
                // byte   SSH_MSG_CHANNEL_OPEN(90)
                // string channel type         //
                // uint32 sender channel       // 0
                // uint32 initial window size  // 0x100000(65536)
                // uint32 maxmum packet size   // 0x4000(16384)

                packet.reset();
                buf.putByte((byte)90);
                buf.putString(StringAux.getBytes("direct-tcpip"));
                buf.putInt(id);
                buf.putInt(lwsize);
                buf.putInt(lmpsize);
                buf.putString(StringAux.getBytes(host));
                buf.putInt(port);
                buf.putString(StringAux.getBytes(originator_IP_address));
                buf.putInt(originator_port);
                session.write(packet);

                int retry = 1000;
                try {
                    while (this.getRecipient() == -1 &&
                           session.isConnected() &&
                           retry > 0 &&
                           !_eof_remote)
                    {
                        //Thread.sleep(500);
                        ThreadAux.Sleep(50);
                        retry--;
                    }
                }
                catch {
                }

                if (!session.isConnected())
                {
                    throw new SshClientException("session is down");
                }
                if (retry == 0 || this._eof_remote)
                {
                    throw new SshClientException("channel is not opened.");
                }

                /*
                 * if(this.eof_remote){      // failed to open
                 * disconnect();
                 * return;
                 * }
                 */

                connected = true;

                thread = new ThreadAux(this);
                thread.start();
            }
            catch (Exception e) {
                io.close();
                io = null;
                AChannel.del(this);
                if (e is SshClientException)
                {
                    throw (SshClientException)e;
                }
            }
        }