Example #1
0
        public static ServiceConfig Load(string _ConfigPath, bool CreateFile, string CertPassword)
        {
            var c = new ServiceConfig();

            c.ConfigPath = _ConfigPath;

            if (!System.IO.File.Exists(c.ConfigPath))
            {
                if (CreateFile)
                {
                    c.ConfigIsNew = true;
                    c.Save();
                    return(c);
                }
                else
                {
                    throw new FileNotFoundException("Could not load config file: " + c.ConfigPath);
                }
            }
            else
            {
                c.ConfigIsNew = false;

                StreamReader   file       = File.OpenText(_ConfigPath);
                JsonSerializer serializer = new JsonSerializer();
                serializer.NullValueHandling = NullValueHandling.Ignore;
                serializer.TypeNameHandling  = TypeNameHandling.Auto;
                serializer.Formatting        = Formatting.Indented;

                c = (ServiceConfig)serializer.Deserialize(file, typeof(ServiceConfig));

                file.Close();
                file.Dispose();

                if (c == null)
                {
                    throw new FileLoadException("Config file was not loaded: " + _ConfigPath);
                }

                return(c);
            }
        }
        public static ServiceConfig Defaults(bool GenerateCerts, int DefaultCertLength, Hash.HashProvider Provider,
                                             string SwarmName, Uri SwarmPublicUri, Uri ServerUri, Uri LocalServerUri, X509Certificate2 SigningCert,
                                             Version ServiceApiCompartibilityVersion, Version ServiceApiVersion, IObjectCache ObjectCache)
        {
            var config = new ServiceConfig();

            config.ServerPeerInfo = new Peer()
            {
                Version = new AppVersionInfo()
                {
                    ApiCompartibilityVersion = ServiceApiCompartibilityVersion,
                    ApiVersion = ServiceApiVersion,
                    Name       = Assembly.GetExecutingAssembly().GetName().FullName,
                    Version    = Assembly.GetExecutingAssembly().GetName().Version
                }
            };

            config.Swarm = new Swarm()
            {
                Accessibility = Swarm.JoinAccessibility.NoRestrictions,

                RootCertMinLength   = DefaultCertLength,
                ServerCertMinLength = DefaultCertLength,
                UserCertMinLength   = DefaultCertLength,

                BlobMinLength  = 1024 * 128,
                BlobMaxLength  = 1024 * 1024,
                BlobMinStorage = new TimeSpan(24, 0, 0),

                ItemMinLength  = 1024,
                ItemMaxLength  = 1024 * 128,
                ItemMinStorage = new TimeSpan(24, 0, 0),

                MessageMinLength  = 1024,
                MessageMaxLength  = 1024 * 128,
                MessageMinStorage = new TimeSpan(4, 0, 0),

                PaddingEnforced = true,
                SwarmName       = SwarmName,
                PublicAddress   = SwarmPublicUri
            };

            config.Server = new Server()
            {
                ServiceAddress = LocalServerUri.OriginalString,
                StoreCache     = ObjectCache
            };

            //start the cache
            config.Server.StoreCache.Initialize();

            if (GenerateCerts)
            {
                var swarmKey = new CertBuilder {
                    Issuer      = SigningCert,
                    SubjectName = "CN=" + config.Swarm.SwarmName,
                    KeyStrength = config.Swarm.RootCertMinLength,
                    NotBefore   = DateTime.Now,
                    NotAfter    = DateTime.Now.AddYears(10)
                }.BuildX509();

                config.Swarm.Cert = new Cert(swarmKey);
                //config.Swarm.PublicKey = Utility.GetPublicKey(swarmKey);

                var serverKey = new CertBuilder {
                    Issuer      = SigningCert,
                    SubjectName = "CN=" + config.Swarm.SwarmName,
                    KeyStrength = config.Swarm.RootCertMinLength,
                    NotBefore   = DateTime.Now,
                    NotAfter    = DateTime.Now.AddYears(10)
                }.BuildX509();

                config.Server.Cert         = new Cert(serverKey);
                config.ServerPeerInfo.Cert = config.Server.Cert.RemovePrivateKey();
                //config.ServerPeerInfo.ComputeHash(Provider, new Cert(SigningCert));
            }

            return(config);
        }