public void SaveLocal() { if (Core.InvokeRequired) { Core.RunInCoreAsync(() => SaveLocal()); return; } try { // create new link file in temp dir string tempPath = Core.GetTempPath(); byte[] key = Utilities.GenerateKey(Core.StrongRndGen, 256); using (IVCryptoStream stream = IVCryptoStream.Save(tempPath, key)) { // project packets foreach (OpLink link in LocalTrust.Links.Values) if (link.Active) { ProjectData project = new ProjectData(); project.ID = link.Project; project.Name = GetProjectName(link.Project); if (link.Project == 0) project.UserName = Core.User.Settings.UserName; byte[] packet = SignedData.Encode(Network.Protocol, Core.User.Settings.KeyPair, project); stream.Write(packet, 0, packet.Length); // uplinks if (link.Uplink != null) { LinkData data = new LinkData(link.Project, link.Uplink.Trust.File.Key); packet = SignedData.Encode(Network.Protocol, Core.User.Settings.KeyPair, data); stream.Write(packet, 0, packet.Length); } // downlinks foreach (OpLink downlink in link.Downlinks) if (link.Confirmed.Contains(downlink.UserID)) { string title; link.Titles.TryGetValue(downlink.UserID, out title); LinkData data = new LinkData(link.Project, downlink.Trust.File.Key, title); packet = SignedData.Encode(Network.Protocol, Core.User.Settings.KeyPair, data); stream.Write(packet, 0, packet.Length); } } PacketStream streamEx = new PacketStream(stream, Network.Protocol, FileAccess.Write); // save top 5 web caches foreach (WebCache cache in Network.Cache.GetLastSeen(5)) streamEx.WritePacket(new WebCache(cache, TrustPacket.WebCache)); // save inheritable settings only if they can be inherited if (IsInheritNode(Core.UserID)) { if (Core.User.OpIcon != null) streamEx.WritePacket(new IconPacket(TrustPacket.Icon, Core.User.OpIcon)); if (Core.User.OpSplash != null) { MemoryStream splash = new MemoryStream(); Core.User.OpSplash.Save(splash, ImageFormat.Jpeg); LargeDataPacket.Write(streamEx, TrustPacket.Splash, splash.ToArray()); } } stream.WriteByte(0); // signal last packet stream.FlushFinalBlock(); } OpVersionedFile file = Cache.UpdateLocal(tempPath, key, null); Store.PublishDirect(GetLocsAbove(), Core.UserID, ServiceID, DataTypeFile, file.SignedHeader); SaveUplinkReqs(); } catch (Exception ex) { Network.UpdateLog("LinkControl", "Error updating local " + ex.Message); } }
private void Process_ProjectData(OpTrust trust, SignedData signed, ProjectData project) { if (!Utilities.CheckSignedData(trust.File.Key, signed.Data, signed.Signature)) return; if (project.ID != 0 && !ProjectNames.SafeContainsKey(project.ID)) ProjectNames.SafeAdd(project.ID, project.Name); trust.AddProject(project.ID, true); if (project.ID == 0) { trust.Name = project.UserName; Core.IndexName(trust.UserID, trust.Name); } OpLink link = trust.GetLink(project.ID); }
public static ProjectData Decode(G2Header root) { ProjectData project = new ProjectData(); G2Header child = new G2Header(root.Data); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) continue; switch (child.Name) { case Packet_ID: project.ID = BitConverter.ToUInt32(child.Data, child.PayloadPos); break; case Packet_Name: project.Name = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_UserName: project.UserName = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; } } return project; }