コード例 #1
0
        /// <summary>
        /// Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            StringBuilder builder;

            builder = new StringBuilder();
            builder.Append("--ignore-missing-torrc ");
            builder.Append("--allow-missing-torrc ");
            builder.AppendFormat("--ControlPort {0}", controlPort);

            /** TEMPORARY: DELETE LATER **/
            builder.AppendFormat(" --SocksPort 9050");

            if (!string.IsNullOrWhiteSpace(configurationFile))
            {
                builder.AppendFormat(" -f \"{0}\"", configurationFile);
            }

            if (!string.IsNullOrWhiteSpace(defaultConfigurationFile))
            {
                builder.AppendFormat(" --defaults-torrc \"{0}\"", defaultConfigurationFile);
            }

            foreach (KeyValuePair <ConfigurationNames, object> over in overrides)
            {
                string value = "";

                ConfigurationAssocAttribute attribute = ReflectionHelper.GetAttribute <ConfigurationNames, ConfigurationAssocAttribute>(over.Key);

                if (attribute == null)
                {
                    continue;
                }

                if (over.Value != null)
                {
                    value = ReflectionHelper.Convert(over.Value, typeof(string)) as string;
                }

                if (value == null)
                {
                    value = "";
                }
                if (value.Contains(" "))
                {
                    value = "\"" + value + "\"";
                }

                builder.AppendFormat(" --{0} {1}", attribute.Name, value);
            }

            return(builder.ToString());
        }
コード例 #2
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;
            }
        }