/Program.cs

http://quick-fwknop-ssh-launcher.googlecode.com/ · C# · 124 lines · 106 code · 15 blank · 3 comment · 0 complexity · bec3ec092b03f4b2ffc2500effbf641b MD5 · raw file

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