PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Program.cs

http://quick-fwknop-ssh-launcher.googlecode.com/
C# | 124 lines | 106 code | 15 blank | 3 comment | 0 complexity | bec3ec092b03f4b2ffc2500effbf641b MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using System.Diagnostics;
  6
  7namespace Quick_Knock
  8{
  9    class Program
 10    {
 11        #region FWKNOP
 12            private static string fwPath;
 13            private static string fwBin;
 14            private static string fwSource;
 15            private static string fwDest;
 16            private static int fwPort;
 17            private static string fwProtocol;
 18            private static string fwUser;
 19        #endregion
 20
 21        #region PuTTY
 22            private static string pPath;
 23            private static string pBin;
 24            private static string pKeyPath;
 25            private static string pRForward;
 26            private static string pLForward;
 27            private static string pUser;
 28        #endregion
 29
 30        #region Launchers
 31            private static ProcessStartInfo fwStartInfo;
 32            private static ProcessStartInfo pStartInfo;
 33        #endregion
 34
 35        static void Main(string[] args)
 36        {
 37            string configFile = "config.ini";
 38            Console.WriteLine("Quick Knock v.01 Alpha");
 39
 40            LoadConfig(configFile);
 41
 42            PrepareFWKNOP();
 43            PrepareSSH();
 44
 45            Run();
 46        }
 47
 48        private static void Run()
 49        {
 50            try
 51	        {
 52	            using (Process exeProcess = Process.Start(fwStartInfo))
 53	            {
 54		            exeProcess.WaitForExit();
 55	            }
 56	        }
 57	        catch (Exception ex)
 58	        {
 59	            throw new Exception(ex.Message);
 60	        }
 61
 62            try
 63	        {
 64	            using (Process exeProcess = Process.Start(pStartInfo))
 65                {
 66                    exeProcess.CloseMainWindow();
 67	            }
 68	        }
 69	        catch (Exception ex)
 70	        {
 71	            throw new Exception(ex.Message);
 72	        }
 73        }
 74
 75        private static void PrepareFWKNOP()
 76        {
 77            fwStartInfo = new ProcessStartInfo();
 78            fwStartInfo.CreateNoWindow = false;
 79            fwStartInfo.UseShellExecute = false;
 80            fwStartInfo.LoadUserProfile = true;
 81            fwStartInfo.WorkingDirectory = fwPath;
 82            fwStartInfo.FileName = fwBin;
 83            fwStartInfo.WindowStyle = ProcessWindowStyle.Normal;
 84            fwStartInfo.Arguments = "-A " + fwProtocol + "/" + fwPort.ToString() + " -a " + fwSource + " -D " + fwDest + " -R -U " + fwUser;
 85        }
 86
 87        private static void PrepareSSH()
 88        {
 89            //Console.WriteLine("Please enter your SSH passphrase:");
 90            //pPassphrase = Console.ReadLine();
 91
 92            // Use ProcessStartInfo class
 93            pStartInfo = new ProcessStartInfo();
 94            pStartInfo.CreateNoWindow = false;
 95            pStartInfo.UseShellExecute = false;
 96            pStartInfo.WorkingDirectory = pPath;
 97            pStartInfo.FileName = pBin;
 98            pStartInfo.WindowStyle = ProcessWindowStyle.Normal;
 99            pStartInfo.Arguments = "-ssh -2 -4 -C -i " + pKeyPath + " -P " + fwPort + " -R " + pRForward + " -L " + pLForward + " -l " + pUser + " " + fwDest;
100        }
101
102        private static void LoadConfig(string configFile)
103        {
104            IniFile myConfig = new IniFile(configFile);
105            string fw = "fwknop";
106            string p = "putty";
107
108            fwPath = myConfig.GetValue(fw, "path");
109            fwBin = myConfig.GetValue(fw, "bin");
110            fwSource = myConfig.GetValue(fw, "source");
111            fwDest = myConfig.GetValue(fw, "destination");
112            int.TryParse(myConfig.GetValue(fw, "port"), out fwPort);
113            fwProtocol = myConfig.GetValue(fw, "protocol");
114            fwUser = myConfig.GetValue(fw, "user");
115
116            pPath = myConfig.GetValue(p, "path");
117            pBin = myConfig.GetValue(p, "bin");
118            pKeyPath = myConfig.GetValue(p, "keypath");
119            pRForward = myConfig.GetValue(p, "rforward");
120            pLForward = myConfig.GetValue(p, "lforward");
121            pUser = myConfig.GetValue(p, "user");
122        }
123    }
124}