public static ServiceSettings GetSettings()
        {
            Settings settings = new Settings();
            ServiceSettings serviceSettings = new ServiceSettings();

            serviceSettings.DynamicSocksHost = settings.DynamicSocksHost;
            serviceSettings.DynamicSocksPort = settings.DynamicSocksPort;
            serviceSettings.EnableCompression = settings.EnableCompression;
            serviceSettings.EnableDynamicSocks = settings.EnableDynamicSocks;
            serviceSettings.EnableTunnelValidation = settings.EnableTunnelValidation;
            serviceSettings.EnableVerbose = settings.EnableVerbose;
            serviceSettings.PlinkExecutable = settings.PlinkExecutable;
            serviceSettings.SshHostname = settings.SshHostname;
            serviceSettings.SshPassword = CryptoHelper.ToInsecureString(CryptoHelper.DecryptString(settings.SshPassword));
            serviceSettings.SshPort = settings.SshPort;
            serviceSettings.SshUsername = settings.SshUsername;
            serviceSettings.TunnelValidationLocalPort = settings.TunnelValidationLocalPort;
            serviceSettings.TunnelValidationPingInterval = settings.TunnelValidationPingInterval;
            serviceSettings.TunnelValidationPingTimeout = settings.TunnelValidationPingTimeout;
            serviceSettings.TunnelValidationRemotePort = settings.TunnelValidationRemotePort;
            serviceSettings.EnableIcmpValidation = settings.EnableIcmpValidation;
            serviceSettings.IcmpValidationHostnames = settings.IcmpValidationHostnames;
            serviceSettings.IcmpValidationPingSshServer = settings.IcmpValidationPingSshServer;
            serviceSettings.UiMinimizedToSysTray = settings.UiMinimizedToSysTray;
            serviceSettings.UiShowSysTrayNotifications = settings.UiShowSysTrayNotifications;
            serviceSettings.UiStartMinimized = settings.UiStartMinimized;
            serviceSettings.UiStartWithWindows = settings.UiStartWithWindows;
            serviceSettings.LocalTunnels = settings.LocalTunnels;
            serviceSettings.RemoteTunnels = settings.RemoteTunnels;

            return serviceSettings;
        }
        public static void SaveSettings(ServiceSettings serviceSettings)
        {
            try
            {
                Settings settings = new Settings();

                settings.DynamicSocksHost = serviceSettings.DynamicSocksHost;
                settings.DynamicSocksPort = serviceSettings.DynamicSocksPort;
                settings.EnableCompression = serviceSettings.EnableCompression;
                settings.EnableDynamicSocks = serviceSettings.EnableDynamicSocks;
                settings.EnableTunnelValidation = serviceSettings.EnableTunnelValidation;
                settings.EnableVerbose = serviceSettings.EnableVerbose;
                settings.PlinkExecutable = serviceSettings.PlinkExecutable;
                settings.SshHostname = serviceSettings.SshHostname;
                settings.SshPassword = CryptoHelper.EncryptString(CryptoHelper.ToSecureString(serviceSettings.SshPassword));
                settings.SshPort = serviceSettings.SshPort;
                settings.SshUsername = serviceSettings.SshUsername;
                settings.TunnelValidationLocalPort = serviceSettings.TunnelValidationLocalPort;
                settings.TunnelValidationPingInterval = serviceSettings.TunnelValidationPingInterval;
                settings.TunnelValidationPingTimeout = serviceSettings.TunnelValidationPingTimeout;
                settings.TunnelValidationRemotePort = serviceSettings.TunnelValidationRemotePort;
                settings.EnableIcmpValidation = serviceSettings.EnableIcmpValidation;
                settings.IcmpValidationHostnames = serviceSettings.IcmpValidationHostnames;
                settings.IcmpValidationPingSshServer = serviceSettings.IcmpValidationPingSshServer;
                settings.UiMinimizedToSysTray = serviceSettings.UiMinimizedToSysTray;
                settings.UiShowSysTrayNotifications = serviceSettings.UiShowSysTrayNotifications;
                settings.UiStartMinimized = serviceSettings.UiStartMinimized;
                settings.UiStartWithWindows = serviceSettings.UiStartWithWindows;
                settings.LocalTunnels = serviceSettings.LocalTunnels;
                settings.RemoteTunnels = serviceSettings.RemoteTunnels;

                settings.Save();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public static string GetPlinkArguments(ServiceSettings serviceSettings)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("-ssh -2 -N -v -P ");
            sb.Append(serviceSettings.SshPort);
            sb.Append(" ");

            if (serviceSettings.EnableCompression)
            {
                sb.Append("-C ");
            }

            if (serviceSettings.EnableDynamicSocks)
            {
                sb.Append("-D ");
                sb.Append("\"");
                if (!string.IsNullOrEmpty(serviceSettings.DynamicSocksHost))
                {
                    sb.Append(serviceSettings.DynamicSocksHost);
                    sb.Append(":");
                }
                sb.Append(serviceSettings.DynamicSocksPort);
                sb.Append("\" ");
            }

            if (serviceSettings.LocalTunnels != null && serviceSettings.LocalTunnels.Trim().Length > 0)
            {
                string[] localTunnels = StringHelper.ParseStringLines(serviceSettings.LocalTunnels);

                foreach (string line in localTunnels)
                {
                    sb.Append("-L \"");
                    sb.Append(line.Trim());
                    sb.Append("\" ");
                }
            }

            if (serviceSettings.RemoteTunnels != null && serviceSettings.RemoteTunnels.Trim().Length > 0)
            {
                string[] remoteTunnels = StringHelper.ParseStringLines(serviceSettings.RemoteTunnels);

                foreach (string line in remoteTunnels)
                {
                    sb.Append("-R \"");
                    sb.Append(line.Trim());
                    sb.Append("\" ");
                }
            }

            if (serviceSettings.EnableTunnelValidation && serviceSettings.TunnelValidationLocalPort > 0 && serviceSettings.TunnelValidationRemotePort > 0)
            {
                sb.Append("-L \"localhost:");
                sb.Append(serviceSettings.TunnelValidationLocalPort);
                sb.Append(":localhost:");
                sb.Append(serviceSettings.TunnelValidationLocalPort);
                sb.Append("\" -R \"localhost:");
                sb.Append(serviceSettings.TunnelValidationLocalPort);
                sb.Append(":localhost:");
                sb.Append(serviceSettings.TunnelValidationRemotePort);
                sb.Append("\" ");
            }

            sb.Append("\"");
            sb.Append(serviceSettings.SshUsername);
            sb.Append("@");
            sb.Append(serviceSettings.SshHostname);
            sb.Append("\"");

            return sb.ToString();
        }
        private static void ProcessOutputCharacters(StreamReader streamReader, ServiceSettings serviceSettings, bool testConnection, bool errorLine)
        {
            int outputCharInt;
            string line = string.Empty;

            while (-1 != (outputCharInt = streamReader.Read()))
            {
                char outputChar = (char)outputCharInt;
                if (outputChar == '\n' || outputChar == '\r')
                {
                    if (line != string.Empty)
                    {
                        if (errorLine)
                        {
                            line = "E: " + line;
                        }

                        ProcessLine(line, testConnection);
                    }

                    line = string.Empty;
                }
                else
                {
                    line += outputChar;

                    if (line.Contains("'s password:"))
                    {
                        _process.StandardInput.WriteLine(serviceSettings.SshPassword);
                        _process.StandardInput.Flush();

                        ProcessLine(line, testConnection);
                        line = string.Empty;
                    }
                }
            }
        }
        public static bool StartPlink(ServiceSettings serviceSettings = null, bool testConnection = false)
        {
            if (serviceSettings == null)
            {
                serviceSettings = SettingsHelper.GetSettings();
            }

            if (string.IsNullOrEmpty(serviceSettings.PlinkExecutable) || !File.Exists(serviceSettings.PlinkExecutable))
            {
                WcfServerHelper.BroadcastRemoteCallback(x => x.TestConnectionCallback(PlinkStatus.ExecutableNotFound));
                return false;
            }

            serviceSettings.EnableVerbose = true;

            _processInfo = new ProcessStartInfo();
            _processInfo.FileName = serviceSettings.PlinkExecutable;
            _processInfo.Arguments = GetPlinkArguments(serviceSettings);
            _processInfo.RedirectStandardOutput = true;
            _processInfo.RedirectStandardError = true;
            _processInfo.RedirectStandardInput = true;
            _processInfo.UseShellExecute = false;
            _processInfo.CreateNoWindow = true;

            _process = new Process();
            _process.StartInfo = _processInfo;
            _process.Start();
            _process.StandardInput.NewLine = Environment.NewLine;

            if (!testConnection)
            {
                _process.Exited += process_Exited;
                PlinkRunning = true;

                try
                {
                    PlinkJobObject plinkJobObject = new PlinkJobObject();
                    plinkJobObject.AddProcess(_process.Id);
                }
                catch (Exception ex)
                {
                    WcfServerHelper.BroadcastRemoteCallback(x => x.EventToLog(ex.Message + " ::: " + ex.StackTrace, DateTime.Now));
                    return false;
                }

                ClientHelper.PingClient();
            }

            Task.Factory.StartNew(() => ProcessOutputCharacters(_process.StandardError, serviceSettings, testConnection, true));

            Task.Factory.StartNew(() => ProcessOutputCharacters(_process.StandardOutput, serviceSettings, testConnection, false));

            return true;
        }
예제 #6
0
        private void ServiceRemotingCallback_SaveSettingsCallbackEvent(object sender, ServiceSettings serviceSettings)
        {
            btnSave.Invoke(new MethodInvoker(() =>
                {
                    btnSave.Enabled = true;

                    _currentSettings = serviceSettings;
                    _currentSettings.IsDirty = false;

                    bindingSource1.DataSource = _currentSettings;

                    AppendLog("Client", "Settings have been saved to service");
                }));
        }
예제 #7
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            this.ApplicationExiting = true;

            ServiceRemotingCallback.ClearAllEventHandlers();

            ServiceRemotingCallback.ServerStoppingEvent += ServiceRemotingCallback_ServerStoppingEvent;
            ServiceRemotingCallback.EventToLogEvent += ServiceRemotingCallback_EventToLogEvent;
            ServiceRemotingCallback.PingEvent += ServiceRemotingCallback_PingEvent;
            ServiceRemotingCallback.TestConnectionCallbackEvent += ServiceRemotingCallback_TestConnectionCallbackEvent;
            ServiceRemotingCallback.TunnelStatusChangedEvent += ServiceRemotingCallback_TunnelStatusChangedEvent;
            ServiceRemotingCallback.PlinkTextOutputEvent += ServiceRemotingCallback_PlinkTextOutputEvent;
            ServiceRemotingCallback.SaveSettingsCallbackEvent += ServiceRemotingCallback_SaveSettingsCallbackEvent;

            AppendLog("Client", "UI started - connected to Windows Service");
            _currentSettings = WcfClientHelper.RemotingObject.GetSettings();
            _currentSettings.IsDirty = false;
            bindingSource1.DataSource = _currentSettings;

            AppendLog("Client", "Windows Service settings retrieved and bound");

            WcfClientHelper.RemotingObject.RegisterCallbackClient();

            _pingTimer = new System.Timers.Timer(Constants.ClientPingInterval * 1000);
            _pingTimer.Elapsed += pingTimer_Elapsed;
            _pingTimer.AutoReset = true;
            _pingTimer.Start();
        }
 public void SaveSettingsCallback(ServiceSettings serviceSettings)
 {
     if (SaveSettingsCallbackEvent != null)
     {
         SaveSettingsCallbackEvent(this, serviceSettings);
     }
 }
 public void TestConnection(ServiceSettings serviceSettings)
 {
     Task.Factory.StartNew(() =>
     {
         ProcessHelper.StartPlink(serviceSettings, true);
     });
 }
        public void SaveSettings(ServiceSettings serviceSettings, bool restartPlink)
        {
            SettingsHelper.SaveSettings(serviceSettings);

            WcfServerHelper.BroadcastRemoteCallback(x => x.SaveSettingsCallback(SettingsHelper.GetSettings()), true);

            if (restartPlink && TunnelHelper.CurrentTunnelStatus == TunnelStatuses.Started)
            {
                TunnelHelper.ChangeTunnelStatus(TunnelStatuses.Paused, false);
                Thread.Sleep(400);
                TunnelHelper.ChangeTunnelStatus(TunnelStatuses.Started, false);
            }
        }