Exemplo n.º 1
0
        public IActionResult Manage(string name, string task)
        {
            if (Defaults.ValidServices.Any(s => name == s) &&
                Defaults.ValidActions.Any(s => task == s))
            {
                if (task.Equals("config"))
                {
                    var type   = Utility.GetServiceType(name);
                    var config = _serviceConfigManager.Generate(type);
                    _response.Result = config;
                    return(Ok(_response));
                }

                var message = _serviceManager.Process(name, task, out var error);

                if (error != null)
                {
                    _response.Errors.Add(message, error);
                    return(StatusCode(500, _response));
                }

                _response.Message = message;
                return(Ok(_response));
            }

            _response.Errors.Add(Errors.INVALID_SERVICE_OR_ACTION_ATTEMPT, "");
            return(BadRequest(_response));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> GetUserServiceSetupResources(int id, string name = "")
        {
            var user = await Db.SingleByIdAsync <User>(id);

            if (user == null)
            {
                _response.Errors.Add(Errors.USER_NOT_FOUND, "");
            }

            if (HasErrors())
            {
                return(BadRequest(_response));
            }

            if (Defaults.ValidServices.Any(s => s == name) || name.IsEmpty())
            {
                if (!name.IsEmpty())
                {
                    var type = Utility.GetServiceType(name);

                    EnsureServiceAccess(user, type);

                    var configs = _serviceConfigManager.Generate(type);
                    _response.Result = await GenerateUserServiceResource(user, type, configs);
                }
                else
                {
                    var resultDictionary = new Dictionary <string, UserServiceResource>();
                    foreach (var serviceName in Defaults.ValidServices)
                    {
                        var type = Utility.GetServiceType(serviceName);

                        // TODO: Fix this constraint once the other services are implemented.
                        // The config manager's dictionary cannot lookup a null value, this fixes that (since GetServiceType returns null for ShadowSOCKS/SSHTunnel)
                        if (type != null && EnsureServiceAccess(user, type, false))
                        {
                            var configs = _serviceConfigManager.Generate(type);
                            resultDictionary.Add(serviceName,
                                                 await GenerateUserServiceResource(user, type, configs));
                        }
                    }

                    _response.Result = resultDictionary;
                }
            }

            if (_response.Result != null)
            {
                return(Ok(_response));
            }

            _response.Errors.Add(Errors.INVALID_SERVICE_OR_ACTION_ATTEMPT, "");
            return(BadRequest(_response));
        }
Exemplo n.º 3
0
        private void InitiateServices()
        {
            if (initiated)
            {
                return;
            }

            var type         = typeof(IService);
            var implementers = AppDomain.CurrentDomain.GetAssemblies()
                               .SelectMany(s => s.GetTypes())
                               .Where(p => type.IsAssignableFrom(p))
                               .Where(p => p != type) // Skip `IService` itself, it cannot be activated
                               .ToArray();


            _logger.LogDebug("IS: Found " + implementers.Length + " services to activate.");

            foreach (var serviceType in implementers)
            {
                _logger.LogDebug("IS: Processing activation request for " + serviceType);
                var service = (IService)Activator.CreateInstance(serviceType, _appConfig, _logger, _db, _authenticator,
                                                                 _localNetworks, _localAddresses, _statistician, _cache, _processRunner);
                var config = _serviceConfigManager.Generate(serviceType);
                service.SetConfig(config);
                _logger.LogDebug("IS: Activation succeeded for " + serviceType);
                _services.TryAdd(serviceType, service);
            }

            _logger.LogDebug("IS: Successfully initialized " + _services.Count + " service(s).");
            initiated = true;
        }