Exemplo n.º 1
0
        /// <summary>
        /// Creates instance of identity client from snapshot.
        /// </summary>
        /// <param name="Snapshot">Identity client snapshot.</param>
        /// <param name="Images">Hexadecimal image data mapping to SHA256 hash.</param>
        /// <param name="ProfileServer">Profile server that hosts identity's profile.</param>
        /// <returns>New identity client instance.</returns>
        public static IdentityClient CreateFromSnapshot(IdentitySnapshot Snapshot, Dictionary <string, string> Images, ProfileServer ProfileServer)
        {
            IdentityClient res = new IdentityClient();

            res.challenge       = Crypto.FromHex(Snapshot.Challenge);
            res.clientChallenge = Crypto.FromHex(Snapshot.ClientChallenge);

            res.keys = new KeysEd25519();
            res.keys.ExpandedPrivateKeyHex = Snapshot.ExpandedPrivateKeyHex;
            res.keys.PublicKeyHex          = Snapshot.PublicKeyHex;
            res.keys.PrivateKeyHex         = Snapshot.PrivateKeyHex;
            res.keys.ExpandedPrivateKey    = Crypto.FromHex(res.keys.ExpandedPrivateKeyHex);
            res.keys.PublicKey             = Crypto.FromHex(res.keys.PublicKeyHex);
            res.keys.PrivateKey            = Crypto.FromHex(res.keys.PrivateKeyHex);

            res.extraData     = Snapshot.ExtraData;
            res.hostingActive = Snapshot.HostingActive;
            res.identityId    = Crypto.FromHex(Snapshot.IdentityId);
            res.imageFileName = Snapshot.ImageFileName != null?Path.Combine(CommandProcessor.ImagesDirectory, Snapshot.ImageFileName) : null;

            res.location           = new GpsLocation(Snapshot.LocationLatitude, Snapshot.LocationLongitude);
            res.name               = Snapshot.Name;
            res.profileInitialized = Snapshot.ProfileInitialized;

            res.profileImage = Snapshot.ProfileImageHash != null?Crypto.FromHex(Images[Snapshot.ProfileImageHash]) : null;

            res.thumbnailImage = Snapshot.ThumbnailImageHash != null?Crypto.FromHex(Images[Snapshot.ThumbnailImageHash]) : null;

            res.profileServerKey = Crypto.FromHex(Snapshot.ProfileServerKey);
            res.type             = Snapshot.Type;
            res.version          = Snapshot.Version;

            res.profileServer  = ProfileServer;
            res.log            = new PrefixLogger("ProfileServerSimulator.IdentityClient", "[" + res.Name + "] ");
            res.messageBuilder = new MessageBuilder(0, new List <SemVer>()
            {
                SemVer.V100
            }, res.keys);
            res.InitializeTcpClient();

            return(res);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates identity client snapshot.
        /// </summary>
        /// <returns>Identity client snapshot.</returns>
        public IdentitySnapshot CreateSnapshot()
        {
            IdentitySnapshot res = new IdentitySnapshot()
            {
                Challenge             = Crypto.ToHex(this.challenge),
                ClientChallenge       = Crypto.ToHex(this.clientChallenge),
                ExpandedPrivateKeyHex = this.keys.ExpandedPrivateKeyHex,
                ExtraData             = this.extraData,
                HostingActive         = this.hostingActive,
                IdentityId            = Crypto.ToHex(this.identityId),
                ImageFileName         = Path.GetFileName(imageFileName),
                LocationLatitude      = this.location.Latitude,
                LocationLongitude     = this.location.Longitude,
                Name               = this.name,
                PrivateKeyHex      = this.keys.PrivateKeyHex,
                ProfileInitialized = this.profileInitialized,
                ProfileImageHash   = null,
                ProfileServerKey   = Crypto.ToHex(this.profileServerKey),
                ProfileServerName  = this.profileServer.Name,
                PublicKeyHex       = this.keys.PublicKeyHex,
                ThumbnailImageHash = null,
                Type               = this.type,
                Version            = this.version
            };


            if (this.profileImage != null)
            {
                byte[] profileImageHash    = Crypto.Sha256(profileImage);
                string profileImageHashHex = Crypto.ToHex(profileImageHash);
                res.ProfileImageHash = profileImageHashHex;
            }

            if (this.thumbnailImage != null)
            {
                byte[] thumbnailImageHash    = Crypto.Sha256(thumbnailImage);
                string thumbnailImageHashHex = Crypto.ToHex(thumbnailImageHash);
                res.ThumbnailImageHash = thumbnailImageHashHex;
            }

            return(res);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Stops all running profile servers and takes snapshot of the simulation.
        /// <para>All profile servers are expected to be stopped when this method is called.</para>
        /// </summary>
        /// <param name="RunningServerNames">List of servers that were running before the snapshot was taken.</param>
        /// <param name="ProfileServers">List of simulation profile servers.</param>
        /// <param name="IdentityClients">List of simulation identities.</param>
        /// <returns>true if the function succeeds, false otherwise.</returns>
        public bool Take(HashSet <string> RunningServerNames, Dictionary <string, ProfileServer> ProfileServers, Dictionary <string, IdentityClient> IdentityClients)
        {
            log.Trace("()");

            foreach (ProfileServer server in ProfileServers.Values)
            {
                ProfileServerSnapshot serverSnapshot = server.CreateSnapshot();
                serverSnapshot.IsRunning = RunningServerNames.Contains(server.Name);
                this.ProfileServers.Add(serverSnapshot);
            }

            foreach (IdentityClient identity in IdentityClients.Values)
            {
                IdentitySnapshot identitySnapshot = identity.CreateSnapshot();

                if (identitySnapshot.ProfileImageHash != null)
                {
                    if (!this.Images.ContainsKey(identitySnapshot.ProfileImageHash))
                    {
                        string imageDataHex = Crypto.ToHex(identity.ProfileImage);
                        this.Images.Add(identitySnapshot.ProfileImageHash, imageDataHex);
                    }
                }

                if (identitySnapshot.ThumbnailImageHash != null)
                {
                    if (!this.Images.ContainsKey(identitySnapshot.ThumbnailImageHash))
                    {
                        string imageDataHex = Crypto.ToHex(identity.ThumbnailImage);
                        this.Images.Add(identitySnapshot.ThumbnailImageHash, imageDataHex);
                    }
                }

                this.Identities.Add(identitySnapshot);
            }


            bool error = false;

            try
            {
                if (Directory.Exists(snapshotDirectory))
                {
                    Directory.Delete(snapshotDirectory, true);
                }

                Directory.CreateDirectory(snapshotDirectory);
            }
            catch (Exception e)
            {
                log.Error("Exception occurred while trying to delete and recreate '{0}': {1}", snapshotDirectory, e.ToString());
                error = true;
            }

            if (!error)
            {
                try
                {
                    string serializedProfileServers = JsonConvert.SerializeObject(this.ProfileServers, Formatting.Indented);
                    string serializedIdentities     = JsonConvert.SerializeObject(this.Identities, Formatting.Indented);
                    string serializedImages         = JsonConvert.SerializeObject(this.Images, Formatting.Indented);

                    File.WriteAllText(profileServersFile, serializedProfileServers);
                    File.WriteAllText(identitiesFile, serializedIdentities);
                    File.WriteAllText(imagesFile, serializedImages);
                }
                catch (Exception e)
                {
                    log.Error("Exception occurred while trying to save serialized simulation information: {0}", e.ToString());
                    error = true;
                }
            }

            if (!error)
            {
                foreach (ProfileServer server in ProfileServers.Values)
                {
                    string serverInstanceDirectory   = server.GetInstanceDirectoryName();
                    string snapshotInstanceDirectory = Path.Combine(new string[] { snapshotDirectory, "bin", server.Name });
                    if (!Helpers.DirectoryCopy(serverInstanceDirectory, snapshotInstanceDirectory, true, new string[] { "logs", "tmp" }))
                    {
                        log.Error("Unable to copy files from directory '{0}' to '{1}'.", serverInstanceDirectory, snapshotInstanceDirectory);
                        error = true;
                        break;
                    }

                    string logsDirectory = Path.Combine(snapshotInstanceDirectory, "Logs");
                    try
                    {
                        if (Directory.Exists(logsDirectory))
                        {
                            Directory.Delete(logsDirectory, true);
                        }
                    }
                    catch (Exception e)
                    {
                        log.Error("Exception occurred while trying to delete directory '{0}': {1}", logsDirectory, e.ToString());
                        error = true;
                        break;
                    }
                }
            }

            bool res = !error;

            log.Trace("(-):{0}", res);
            return(res);
        }