예제 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConfigurationAssocAttribute"/> class.
 /// </summary>
 /// <param name="name">The name of the configuration within the tor <c>torrc</c> configuration file.</param>
 public ConfigurationAssocAttribute(string name)
 {
     this.defaultValue = null;
     this.name         = name;
     this.type         = null;
     this.validation   = ConfigurationValidation.None;
 }
예제 #2
0
        /// <summary>
        /// Sets the value of a configuration.
        /// </summary>
        /// <param name="name">The configuration value to set.</param>
        /// <param name="value">The value to assign to the configuration.</param>
        private void SetValue(ConfigurationNames name, object value)
        {
            ConfigurationAssocAttribute attribute = ReflectionHelper.GetAttribute <ConfigurationNames, ConfigurationAssocAttribute>(name);

            if (attribute == null)
            {
                return;
            }

            ConfigurationValidation validation = attribute.Validation;

            if (validation != ConfigurationValidation.None)
            {
                if (validation.HasFlag(ConfigurationValidation.NonNull) && value == null)
                {
                    throw new ArgumentNullException("value");
                }

                if (validation.HasFlag(ConfigurationValidation.NonZero) ||
                    validation.HasFlag(ConfigurationValidation.NonNegative) ||
                    validation.HasFlag(ConfigurationValidation.PortRange))
                {
                    int converted = Convert.ToInt32(value);

                    if (validation.HasFlag(ConfigurationValidation.NonZero) && converted == 0)
                    {
                        throw new ArgumentOutOfRangeException("value", "The value of this configuration cannot be zero");
                    }
                    if (validation.HasFlag(ConfigurationValidation.NonNegative) && converted < 0)
                    {
                        throw new ArgumentOutOfRangeException("value", "The value of this configuration cannot be below zero");
                    }
                    if (validation.HasFlag(ConfigurationValidation.PortRange) && (converted <= 0 || short.MaxValue < converted))
                    {
                        throw new ArgumentOutOfRangeException("value", "The value of this configuration must be within a valid port range");
                    }
                }

                if (validation.HasFlag(ConfigurationValidation.SizeDivision))
                {
                    double converted = Convert.ToDouble(value);

                    if (converted % 1024 != 0)
                    {
                        throw new ArgumentException("The value of this configuration must be divisible by 1024");
                    }
                }
            }

            values[name] = value;

            if (PropertyChanged != null)
            {
                PropertyChanged(client, new PropertyChangedEventArgs(attribute.Name));
            }

            SetConfCommand command  = new SetConfCommand(attribute.Name, ReflectionHelper.Convert(value, typeof(string)) as string);
            Response       response = command.Dispatch(client);
        }
예제 #3
0
        /// <summary>
        /// Sets the value of a configuration supplied with the process arguments. Setting a configuration value within the create
        /// parameters overrides any values stored in the configuration file. The control port can also be set using this method.
        /// </summary>
        /// <param name="configuration">The configuration which should be overridden.</param>
        /// <param name="value">The value of the configuration. This should align with the type specified in the <see cref="Tor.Config.Configuration"/> class.</param>
        public void SetConfig(ConfigurationNames configuration, object value)
        {
            ConfigurationAssocAttribute attribute = ReflectionHelper.GetAttribute <ConfigurationNames, ConfigurationAssocAttribute>(configuration);

            if (attribute == null)
            {
                return;
            }

            if (value != null && !value.GetType().Equals(attribute.Type))
            {
                throw new ArgumentException("The value of this configuration should be of type " + attribute.Type, "value");
            }

            ConfigurationValidation validation = attribute.Validation;

            if (validation != ConfigurationValidation.None)
            {
                if (validation.HasFlag(ConfigurationValidation.NonNull) && value == null)
                {
                    throw new ArgumentNullException("value");
                }

                if (validation.HasFlag(ConfigurationValidation.NonZero) ||
                    validation.HasFlag(ConfigurationValidation.NonNegative) ||
                    validation.HasFlag(ConfigurationValidation.PortRange))
                {
                    int converted = Convert.ToInt32(value);

                    if (validation.HasFlag(ConfigurationValidation.NonZero) && converted == 0)
                    {
                        throw new ArgumentOutOfRangeException("value", "The value of this configuration cannot be zero");
                    }
                    if (validation.HasFlag(ConfigurationValidation.NonNegative) && converted < 0)
                    {
                        throw new ArgumentOutOfRangeException("value", "The value of this configuration cannot be below zero");
                    }
                    if (validation.HasFlag(ConfigurationValidation.PortRange) && (converted <= 0 || short.MaxValue < converted))
                    {
                        throw new ArgumentOutOfRangeException("value", "The value of this configuration must be within a valid port range");
                    }
                }

                if (validation.HasFlag(ConfigurationValidation.SizeDivision))
                {
                    double converted = Convert.ToDouble(value);

                    if (converted % 1024 != 0)
                    {
                        throw new ArgumentException("The value of this configuration must be divisible by 1024");
                    }
                }
            }

            if (configuration == ConfigurationNames.ControlPort)
            {
                controlPort = Convert.ToInt32(value);
            }
            else
            {
                overrides[configuration] = value;
            }
        }