public LocationData GetLocalLocation() { LocationData location = new LocationData(); location.Key = Core.User.Settings.KeyPublic; location.IP = Core.LocalIP; location.Source = Network.GetLocalSource(); if (Network.UseLookupProxies) { location.TunnelClient = new TunnelAddress(Core.Context.Lookup.Network.Local, Core.TunnelID); foreach (TcpConnect socket in Core.Context.Lookup.Network.TcpControl.ProxyServers) { location.TunnelServers.Add(new DhtAddress(socket.RemoteIP, socket)); } } foreach (TcpConnect socket in Network.TcpControl.ProxyServers) { location.Proxies.Add(new DhtAddress(socket.RemoteIP, socket)); } location.Name = Core.User.Settings.UserName; location.Place = Core.User.Settings.Location; location.GmtOffset = (int)TimeZone.CurrentTimeZone.GetUtcOffset(Core.TimeNow).TotalMinutes; location.Away = LocalAway; location.AwayMessage = LocalAway ? Core.User.Settings.AwayMessage : ""; location.Version = LocationVersion++; foreach (uint service in GetTag.HandlerMap.Keys) { foreach (uint datatype in GetTag.HandlerMap[service].Keys) { PatchTag tag = new PatchTag(); tag.Service = service; tag.DataType = datatype; tag.Tag = GetTag[service, datatype].Invoke(); if (tag.Tag != null) { Debug.Assert(tag.Tag.Length < 8); if (tag.Tag.Length < 8) { location.Tags.Add(tag); } } } } return(location); }
public static PatchTag FromBytes(byte[] data, int pos, int size) { PatchTag tag = new PatchTag(); byte control = data[pos]; int sLength = (control & 0x38) >> 3; int dLength = (control & 0x07); tag.Service = CompactNum.ToUInt32(data, pos + 1, sLength); tag.DataType = CompactNum.ToUInt32(data, pos + 1 + sLength, dLength); int dataPos = 1 + sLength + dLength; tag.Tag = Utilities.ExtractBytes(data, pos + dataPos, size - dataPos); return(tag); }
public static LocationData Decode(byte[] data) { G2Header root = new G2Header(data); G2Protocol.ReadPacket(root); if (root.Name != LocationPacket.Data) { return(null); } LocationData loc = new LocationData(); G2Header child = new G2Header(root.Data); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) { continue; } switch (child.Name) { case Packet_Key: loc.Key = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); loc.UserID = Utilities.KeytoID(loc.Key); break; case Packet_Source: loc.Source = DhtSource.ReadPacket(child); loc.UserID = loc.Source.UserID; // encode light doesnt send full key break; case Packet_IP: loc.IP = new IPAddress(Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize)); break; case Packet_Proxies: loc.Proxies.Add(DhtAddress.ReadPacket(child)); break; case Packet_Name: loc.Name = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Place: loc.Place = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Version: loc.Version = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_GMTOffset: loc.GmtOffset = BitConverter.ToInt32(child.Data, child.PayloadPos); break; case Packet_Away: loc.Away = BitConverter.ToBoolean(child.Data, child.PayloadPos); break; case Packet_AwayMsg: loc.AwayMessage = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Tag: loc.Tags.Add(PatchTag.FromBytes(child.Data, child.PayloadPos, child.PayloadSize)); break; case Packet_TunnelClient: loc.TunnelClient = TunnelAddress.FromBytes(child.Data, child.PayloadPos); break; case Packet_TunnelServers: loc.TunnelServers.Add(DhtAddress.ReadPacket(child)); break; } } return(loc); }