Exemplo n.º 1
0
        private void formLogin_Load(object sender, EventArgs e)
        {
            try
            {
                string path = Environment.SystemDirectory + "\\drivers\\etc\\hosts";
                if (System.IO.File.Exists(path))
                {
                    HostAction hostAction = new HostAction(path);
                    hostAction.RestoreHosts();
                }
                else
                {
                    formSelectTicket.hostEnable = false;
                    MessageBox.Show("没有找到hosts文件,请新建后重新启动本程序以恢复自动切换功能,否则切换功能将不可用.", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex) {
                formSelectTicket.hostEnable = false;
                MessageBox.Show(string.Format("由于加载Hosts文件失败,失败原因:{0}\r\n可能导致软件的IP切换功能无效,如果想启用,请允许程序访问Hosts文件或者去除Hosts文件的保护,然后重新启动本程序", ex.Message), "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            ConfigInfo.readfromfile();
            txtName.Text = EncodeAndDecode.DecodeBase64(EncodeAndDecode.DecodeBase64(ConfigInfo.conf.username));
            txtPwd.Text  = EncodeAndDecode.DecodeBase64(EncodeAndDecode.DecodeBase64(ConfigInfo.conf.password));
            IntPtr o = this.Handle;

            GetRandCodeImg();
            formStyle.ShowForm(this.Handle, 500);
        }
Exemplo n.º 2
0
 private void InitHostPath()
 {
     try
     {
         hostAction = new HostAction(Environment.SystemDirectory + "\\drivers\\etc\\hosts");
     }
     catch
     {
         hostEnable = false;
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Manages the host helper.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="allRegistrations">All registrations.</param>
        /// <returns></returns>
        private IEnumerable <ExecutionResult> ManageHostHelper(HostAction action, List <KeyValuePair <string, List <Tuple <IEntity, IEntity, IEntity> > > > allRegistrations)
        {
            Tuple <IEntity, IEntity, IEntity> selected;
            var retval      = new List <ExecutionResult>();
            var config      = (CustomConfigReader)_configuration.Configuration;
            var baseAddress = $"http://{Environment.MachineName}:{config.dotNetOptions.hostPort}";

            allRegistrations.ForEach(r => {
                Parallel.ForEach(r.Value, z => {
                    try {
                        selected = InitializeServiceHost(z, baseAddress, out var newHost, out var metadata, out var selectedLibrary);

                        // Let's see if DynamicHosts already has a ServiceHost for the selected library
                        var found = DynamicHosts.FirstOrDefault(p => string.Equals(p.Value?.Description?.ConfigurationName?
                                                                                   .Replace("_", "."), metadata.ClassName));

                        // Should we start listening or stop our ServiceHost?
                        if (action == HostAction.StartListening)
                        {
                            if (found.Key != null && DynamicHosts.ContainsKey(found.Key))
                            {
                                DynamicHosts[found.Key].Close();
                                DynamicHosts[found.Key] = newHost;
                            }
                            else
                            {
                                DynamicHosts.Add(selectedLibrary, newHost);
                            }
                            DynamicHosts[found.Key ?? selectedLibrary].Open();
                        }
                        else
                        {
                            var key = found.Key ?? selectedLibrary;
                            if (DynamicHosts.ContainsKey(key))
                            {
                                DynamicHosts[key].Close();
                                DynamicHosts.Remove(key);
                            }
                        }
                    } catch (NullReferenceException e) {
                        // If Service couldn't be activated we'll remove it from Database. It's most likely due to a method that
                        // wasn't compiled properly when proxy assembly was produced.
                        Task.Run(async() => await _dataService.RemoveFaultyService(z));
                    } catch (InvalidOperationException e) {
                        // Similar to above (ContractDescription has zero operations)
                        Task.Run(async() => await _dataService.RemoveFaultyService(z));
                    } catch (Exception e) {
                        retval.Add(new ExecutionResult {
                            LastExceptionIfAny = e
                        });
                    }
                });
Exemplo n.º 4
0
        /// <summary>
        /// Manages the hosts (starts & stops). If no entity is passed in then it'll load everything
        /// as per configuration in database, otherwise it'll load the one that's been specified.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="host">The host.</param>
        /// <returns>Collection of errors encountered.</returns>
        public IEnumerable <ExecutionResult> ManageHost(HostAction action, IEntity host = null)
        {
            var retval = new List <ExecutionResult>();
            List <KeyValuePair <string, List <Tuple <IEntity, IEntity, IEntity> > > > registration;

            try {
                // Do we have to process one library or all of them?
                var library = host as DynamicLibrary;
                var result  = _dataService.GetDynamicLibraries(library?.AssemblyName);

                if (result.IsSuccess && (registration = (result.Tag as
                                                         Dictionary <string, List <Tuple <IEntity, IEntity, IEntity> > >)?.ToList()) != null)
                {
                    retval.AddRange(ManageHostHelper(action, registration));
                }
            } catch (Exception e) {
                retval.Add(new ExecutionResult {
                    LastExceptionIfAny = e
                });
            }

            return(retval);
        }
Exemplo n.º 5
0
        private void formLogin_Load(object sender, EventArgs e)
        {
            try
            {
                string path = Environment.SystemDirectory + "\\drivers\\etc\\hosts";
                if (System.IO.File.Exists(path))
                {
                    var hostAction = new HostAction(path);
                    hostAction.RestoreHosts();
                }
                else
                {
                    this.btnSwitchServer.Enabled = false;
                    formSelectTicket.hostEnable  = false;
                    MessageBox.Show("没有找到hosts文件,请新建后重新启动本程序以恢复自动切换功能,否则切换功能将不可用.", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex) {
                formSelectTicket.hostEnable = false;
                MessageBox.Show(string.Format("由于加载Hosts文件失败,失败原因:{0}\r\n可能导致软件的IP切换功能无效,如果想启用,请允许程序访问Hosts文件或者去除Hosts文件的保护,然后重新启动本程序", ex.Message), "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            GetAutoCompleteSource();
            if (_nameSource != null)
            {
                cboName.Items.AddRange(_nameSource);
            }


            var dc = new DesCryption();

            cboName.Text = ConfigInfo.conf.username;
            if (cboName.Text != string.Empty)
            {
                txtPwd.Text = dc.DecryptString(ConfigInfo.conf.password, ConfigInfo.conf.username);
            }
        }
Exemplo n.º 6
0
        public HostAction GetHostAction()
        {
            HostAction action = HostAction.ShowUsage;

            if (_args == null || _args.Length == 0)
            {
                action = HostAction.RunWinService;
            }
            else if (_args.Length > 1)
            {
                action = HostAction.ShowUsage;
            }
            else
            {
                string argument = _args[0];

                if (argument == "-i")    // install
                {
                    action = HostAction.InstallWinService;
                }
                else if (argument == "-u")   // uninstall
                {
                    action = HostAction.UninstallWinService;
                }
                else if (argument == "-console")
                {
                    action = HostAction.RunConsole;
                }
                else if (argument == "-daemon")
                {
                    action = HostAction.RunLinuxDaemon;
                }
            }

            return(action);
        }