Beispiel #1
0
        public BoardService(OpCore core )
        {
            Core       = core;
            Network = Core.Network;
            Protocol = Network.Protocol;
            Store    = Network.Store;
            Trust = Core.Trust;

            Core.SecondTimerEvent += Core_SecondTimer;
            Core.MinuteTimerEvent += Core_MinuteTimer;

            Network.CoreStatusChange += new StatusChange(Network_StatusChange);

            Store.StoreEvent[ServiceID, 0] += new StoreHandler(Store_Local);
            Store.ReplicateEvent[ServiceID, 0] += new ReplicateHandler(Store_Replicate);
            Store.PatchEvent[ServiceID, 0] += new PatchHandler(Store_Patch);

            Network.Searches.SearchEvent[ServiceID, 0] += new SearchRequestHandler(Search_Local);

            Core.Transfers.FileSearch[ServiceID, 0] += new FileSearchHandler(Transfers_FileSearch);
            Core.Transfers.FileRequest[ServiceID, 0] += new FileRequestHandler(Transfers_FileRequest);

            if (Core.Sim != null)
                PruneSize = 16;

            LocalFileKey = Core.User.Settings.FileKey;

            BoardPath = Core.User.RootPath + Path.DirectorySeparatorChar + "Data" + Path.DirectorySeparatorChar + ServiceID.ToString();

            if (!Directory.Exists(BoardPath))
                Directory.CreateDirectory(BoardPath);

            // get available board header targets
            string[] directories = Directory.GetDirectories(BoardPath);

            SortedDictionary<ulong, ulong> targets = new SortedDictionary<ulong, ulong>(); // key distance to self, value target

            foreach (string path in directories)
            {
                string dir = Path.GetFileName(path); // gets dir name

                ulong id = BitConverter.ToUInt64(Utilities.FromBase64String(dir), 0);

                targets[Core.UserID ^ id] = id;
            }

            // load closest targets
            int loaded = 0;
            foreach (ulong id in targets.Values)
            {
                LoadHeader(id);

                loaded++;
                if (loaded == PruneSize)
                    break;
            }

            Loading = false;
        }
Beispiel #2
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                protocol.WritePacket(null, IMPacket.Alive, null);

                return protocol.WriteFinish();
            }
        }
Beispiel #3
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame close = protocol.WritePacket(null, CommPacket.Close, null);

                protocol.WritePacket(close, Packet_Message, UTF8Encoding.UTF8.GetBytes(Reason));

                return protocol.WriteFinish();
            }
        }
Beispiel #4
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame packet = protocol.WritePacket(null, VoicePacket.Audio, Audio);

                protocol.WritePacket(packet, Packet_FrameSize, CompactNum.GetBytes(FrameSize));

                return protocol.WriteFinish();
            }
        }
Beispiel #5
0
        public PacketStream(Stream stream, G2Protocol protocol, FileAccess access)
        {
            ParentStream = stream;
            Protocol     = protocol;
            Access       = access;

            if (access == FileAccess.Read)
            {
                ReadBuffer = new byte[4096]; // break/resume relies on 4kb buffer
            }
        }
Beispiel #6
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame header = protocol.WritePacket(null, ProfilePacket.Attachment, null);

                protocol.WritePacket(header, Packet_Name, UTF8Encoding.UTF8.GetBytes(Name));
                protocol.WritePacket(header, Packet_Size, CompactNum.GetBytes(Size));

                return protocol.WriteFinish();
            }
        }
Beispiel #7
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame file = protocol.WritePacket(null, BoardPacket.PostFile, null);

                protocol.WritePacket(file, Packet_Name, UTF8Encoding.UTF8.GetBytes(Name));
                protocol.WritePacket(file, Packet_Size, CompactNum.GetBytes(Size));

                return protocol.WriteFinish();
            }
        }
Beispiel #8
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame ack = protocol.WritePacket(null, MailPacket.Ack, null);

                protocol.WritePacket(ack, Packet_MailID, MailID);
                protocol.WritePacket(ack, Packet_Source, Source);
                protocol.WritePacket(ack, Packet_Target, Target);
                protocol.WritePacket(ack, Packet_TargetVersion, BitConverter.GetBytes(TargetVersion));
                protocol.WritePacket(ack, Packet_SourceVersion, BitConverter.GetBytes(SourceVersion));

                return protocol.WriteFinish();
            }
        }
Beispiel #9
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame invite = protocol.WritePacket(null, ChatPacket.Invite , null);

                protocol.WritePacket(invite, Packet_RoomID, BitConverter.GetBytes(RoomID));

                if (Host != null)
                    protocol.WritePacket(invite, Packet_Host, Host);

                if(SignedInvite != null)
                    protocol.WritePacket(invite, Packet_SignedInvite, SignedInvite);

                if (Title.Length > 0)
                    protocol.WritePacket(invite, Packet_Title, UTF8Encoding.UTF8.GetBytes(Title));

                return protocol.WriteFinish();
            }
        }
Beispiel #10
0
        private bool ReadNext(G2Header root)
        {
            if (ReadSize > 0)
            {
                int prevStart = Start;

                ReadStatus = G2Protocol.ReadNextPacket(root, ref Start, ref ReadSize);

                ParentPos += (Start - prevStart);

                if (ReadStatus == G2ReadResult.PACKET_GOOD)
                {
                    return(true);
                }
            }

            // hit the exact end of the buffer read in, signal to read the next buffer in
            else
            {
                ReadStatus = G2ReadResult.PACKET_INCOMPLETE;
            }

            return(false);
        }
Beispiel #11
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame sr = protocol.WritePacket(null, CommPacket.SessionRequest, null);

                protocol.WritePacket(sr, Packet_Key, EncryptedKey);

                return protocol.WriteFinish();
            }
        }
Beispiel #12
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame sa = protocol.WritePacket(null, CommPacket.SessionAck, null);

                protocol.WritePacket(sa, Packet_Name, UTF8Encoding.UTF8.GetBytes(Name));

                return protocol.WriteFinish();
            }
        }
Beispiel #13
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame bdy = protocol.WritePacket(null, RootPacket.Comm, Payload);

                protocol.WritePacket(bdy, Packet_SenderDht, BitConverter.GetBytes(SenderID));
                protocol.WritePacket(bdy, Packet_SenderClient, BitConverter.GetBytes(SenderClient));
                protocol.WritePacket(bdy, Packet_TargetDht, BitConverter.GetBytes(TargetID));
                protocol.WritePacket(bdy, Packet_TargetClient, BitConverter.GetBytes(TargetClient));
                protocol.WritePacket(bdy, Packet_Type,   BitConverter.GetBytes(PacketType));
                protocol.WritePacket(bdy, Packet_ID,     BitConverter.GetBytes(PeerID));
                protocol.WritePacket(bdy, Packet_Seq,    BitConverter.GetBytes(Sequence));

                if(Ident != 0) protocol.WritePacket(bdy, Packet_Ident, BitConverter.GetBytes(Ident));

                if (ToAddress != null) ToAddress.WritePacket(protocol, bdy, Packet_To);
                if (RemoteProxy != null) RemoteProxy.WritePacket(protocol, bdy, Packet_Proxy);

                return protocol.WriteFinish();
            }
        }
Beispiel #14
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame notify = protocol.WritePacket(null, LocationPacket.Notify, SignedLocation);

                protocol.WritePacket(notify, Packet_Timeout, CompactNum.GetBytes(Timeout));

                if(GoingOffline)
                    protocol.WritePacket(notify, Packet_GoingOffline, null);

                return protocol.WriteFinish();
            }
        }
Beispiel #15
0
 public TaggedStream(string path, G2Protocol protocol, ProcessTagsHandler processTags)
     : base(path, FileMode.Open, FileAccess.Read, FileShare.Read)
 {
     Init(path, protocol, processTags);
 }
Beispiel #16
0
 public virtual byte[] Encode(G2Protocol protocol)
 {
     return(null);
 }
Beispiel #17
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame header = protocol.WritePacket(null, ProfilePacket.Field, null);

                protocol.WritePacket(header, Packet_Type, BitConverter.GetBytes((byte)FieldType));
                protocol.WritePacket(header, Packet_Name, UTF8Encoding.UTF8.GetBytes(Name));
                protocol.WritePacket(header, Packet_Value, Value);

                return protocol.WriteFinish();
            }
        }
Beispiel #18
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                // signals start of encryption
                if (Start)
                    protocol.WritePacket(null, CommPacket.CryptStart, null);

                // padding for encrypted block
                else
                    protocol.WritePacket(null, CommPacket.CryptPadding, Padding);

                return protocol.WriteFinish();
            }
        }
Beispiel #19
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame ping = protocol.WritePacket(null, LocationPacket.Ping, null);

                protocol.WritePacket(ping, Packet_RemoteVersion, CompactNum.GetBytes(RemoteVersion));

                return protocol.WriteFinish();
            }
        }
Beispiel #20
0
        public static InvitePackage OpenInvite(byte[] decrypted, G2Protocol protocol)
        {
            // if we get down here, opening invite was success

            MemoryStream mem = new MemoryStream(decrypted);
            PacketStream stream = new PacketStream(mem, protocol, FileAccess.Read);

            InvitePackage package = new InvitePackage();

            G2Header root = null;
            while (stream.ReadPacket(ref root))
            {
                if (root.Name == InvitePacket.Info)
                    package.Info = OneWayInvite.Decode(root);

                if (root.Name == InvitePacket.Contact)
                    package.Contacts.Add(DhtContact.ReadPacket(root));

                if (root.Name == InvitePacket.WebCache)
                    package.Caches.Add(WebCache.Decode(root));
            }

            return package;
        }
Beispiel #21
0
        // initializing lookup network (from the settings of a loaded operation)
        public OpCore(DeOpsContext context)
        {
            Context = context;
            Sim = context.Sim;

            StartTime = TimeNow;
            GuiProtocol = new G2Protocol();

            Network = new DhtNetwork(this, true);

            // for each core, re-load the lookup cache items
            Context.Cores.LockReading(() =>
            {
                foreach (OpCore core in Context.Cores)
                    core.User.Load(LoadModeType.LookupCache);
            });

            ServiceBandwidth[DhtServiceID] = new BandwidthLog(RecordBandwidthSeconds);

            // get cache from all loaded cores
            AddService(new LookupService(this));

            if (Sim != null)
                Sim.Internet.RegisterAddress(this);

            CoreThread = new Thread(RunCore);
            CoreThread.Name = "Lookup Thread";

            if (Sim == null || Sim.Internet.TestCoreThread)
                CoreThread.Start();
        }
Beispiel #22
0
        // initializing operation network
        public OpCore(DeOpsContext context, string userPath, string pass)
        {
            Context = context;
            Sim = context.Sim;

            StartTime = TimeNow;
            GuiProtocol = new G2Protocol();

            User = new OpUser(userPath, pass, this);
            User.Load(LoadModeType.Settings);

            Network = new DhtNetwork(this, false);

            TunnelID = (ushort)RndGen.Next(1, ushort.MaxValue);

            Test test = new Test(); // should be empty unless running a test

            User.Load(LoadModeType.AllCaches);

            // delete data dirs if frsh start indicated
            if (Sim != null && Sim.Internet.FreshStart)
                for (int service = 1; service < 20; service++ ) // 0 is temp folder, cleared on startup
                {
                    string dirpath = User.RootPath + Path.DirectorySeparatorChar + "Data" + Path.DirectorySeparatorChar + service.ToString();
                    if (Directory.Exists(dirpath))
                        Directory.Delete(dirpath, true);
                }

            if (Sim != null) KeyMax = 32;

            Context.KnownServices[DhtServiceID] = "Dht";
            ServiceBandwidth[DhtServiceID] = new BandwidthLog(RecordBandwidthSeconds);

            // permanent - order is important here
            AddService(new TransferService(this));
            AddService(new LocationService(this));
            AddService(new LocalSync(this));
            AddService(new BuddyService(this));
            AddService(new UpdateService(this));

            if (!User.Settings.GlobalIM)
                AddService(new TrustService(this));

            // optional
            AddService(new IMService(this));
            AddService(new ChatService(this));
            AddService(new ShareService(this));

            if (Type.GetType("Mono.Runtime") == null)
                AddService(new VoiceService(this));

            if (!User.Settings.GlobalIM)
            {
                AddService(new ProfileService(this));
                AddService(new MailService(this));
                AddService(new BoardService(this));
                AddService(new PlanService(this));
                AddService(new StorageService(this));
            }

            if (Sim != null)
                Sim.Internet.RegisterAddress(this);

            CoreThread = new Thread(RunCore);
            CoreThread.Name = User.Settings.Operation + " Thread";

            if (Sim == null || Sim.Internet.TestCoreThread)
                CoreThread.Start();

            #if DEBUG
            DebugWindowsActive = true;
            #endif
        }
Beispiel #23
0
        void Init(string path, G2Protocol protocol, ProcessTagsHandler processTags)
        {
            Seek(-8, SeekOrigin.End);

            byte[] sizeBytes = new byte[8];
            Read(sizeBytes, 0, sizeBytes.Length);
            long fileSize = BitConverter.ToInt64(sizeBytes, 0);

            if (processTags != null)
            {
                // read public packets
                Seek(fileSize, SeekOrigin.Begin);

                PacketStream stream = new PacketStream(this, protocol, FileAccess.Read);

                // dont need to close packetStream
                processTags.Invoke(stream);
            }

            // set public size down here so reading packet stream works without mixing up true file lenght
            InternalSize = fileSize;

            // set back to the beginning of the file
            Seek(0, SeekOrigin.Begin);
        }
Beispiel #24
0
        public static void EncryptTagFile(string source, string destination, RijndaelManaged crypt, G2Protocol protocol, ref byte[] hash, ref long size)
        {
            const int bufferSize = 1024 * 16;
            byte[] buffer = new byte[bufferSize];

            using (IVCryptoStream stream = IVCryptoStream.Save(destination, crypt.Key, crypt.IV))
            {
                using (FileStream localfile = File.OpenRead(source))
                {
                    int read = bufferSize;
                    while (read == bufferSize)
                    {
                        read = localfile.Read(buffer, 0, bufferSize);
                        stream.Write(buffer, 0, read);
                    }
                }

                stream.FlushFinalBlock();
            }

            HashTagFile(destination, protocol, ref hash, ref size);
        }
Beispiel #25
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame ka = protocol.WritePacket(null, CommPacket.KeyAck, null);

                protocol.WritePacket(ka, Packet_Key, PublicKey);

                return protocol.WriteFinish();
            }
        }
Beispiel #26
0
        public static void HashTagFile(string path, G2Protocol protocol, ref byte[] hash, ref long size)
        {
            using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
            {
                // record size of file
                long originalSize = file.Length;

                // sha hash 128k chunks of file
                SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();

                int read = 0;
                int chunkSize = 128; // 128kb chunks
                int chunkBytes = chunkSize * 1024;
                int buffSize = file.Length > chunkBytes ? chunkBytes : (int)file.Length;
                byte[] chunk = new byte[buffSize];
                List<byte[]> hashes = new List<byte[]>();

                read = 1;
                while (read > 0)
                {
                    read = file.Read(chunk, 0, buffSize);

                    if (read > 0)
                        hashes.Add(sha.ComputeHash(chunk, 0, read));
                }

                // write packets - 200 sub-hashes per packet
                int writePos = 0;
                int hashesLeft = hashes.Count;

                while (hashesLeft > 0)
                {
                    int writeCount = (hashesLeft > 100) ? 100 : hashesLeft;

                    hashesLeft -= writeCount;

                    SubHashPacket packet = new SubHashPacket();
                    packet.ChunkSize = chunkSize;
                    packet.TotalCount = hashes.Count;
                    packet.SubHashes = new byte[20 * writeCount];

                    for (int i = 0; i < writeCount; i++)
                        hashes[writePos++].CopyTo(packet.SubHashes, 20 * i);

                    byte[] encoded = packet.Encode(protocol);

                    file.Write(encoded, 0, encoded.Length);
                }

                // write null - end packets
                file.WriteByte(0);

                // attach original size to end of file
                byte[] sizeBytes = BitConverter.GetBytes(originalSize);
                file.Write(sizeBytes, 0, sizeBytes.Length);

                // sha1 hash tagged file
                file.Seek(0, SeekOrigin.Begin);
                hash = sha.ComputeHash(file);
                size = file.Length;
            }
        }
Beispiel #27
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame kr = protocol.WritePacket(null, CommPacket.KeyRequest, null);

                if (Encryption != null)
                {
                    protocol.WritePacket(kr, Packet_Encryption, UTF8Encoding.UTF8.GetBytes(Encryption));
                    protocol.WritePacket(kr, Packet_Key, Key);
                    protocol.WritePacket(kr, Packet_IV, IV);
                }

                return protocol.WriteFinish();
            }
        }
Beispiel #28
0
        public byte[] EncodeLight(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame loc = protocol.WritePacket(null, LocationPacket.Data, null);

                Source.WritePacket(protocol, loc, Packet_Source);
                protocol.WritePacket(loc, Packet_IP, IP.GetAddressBytes());

                foreach (DhtAddress proxy in Proxies)
                    proxy.WritePacket(protocol, loc, Packet_Proxies);

                if (TunnelClient != null)
                    protocol.WritePacket(loc, Packet_TunnelClient, TunnelClient.ToBytes());

                foreach (DhtAddress server in TunnelServers)
                    server.WritePacket(protocol, loc, Packet_TunnelServers);

                return protocol.WriteFinish();
            }
        }
Beispiel #29
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame update = protocol.WritePacket(null, CommPacket.ProxyUpdate, null);

                Proxy.WritePacket(protocol, update, Packet_Proxy);

                return protocol.WriteFinish();
            }
        }
Beispiel #30
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame header = protocol.WritePacket(null, DataPacket.VersionedFile, null);

                protocol.WritePacket(header, Packet_Key, Key);
                protocol.WritePacket(header, Packet_Version, CompactNum.GetBytes(Version));

                if (FileHash != null)
                {
                    protocol.WritePacket(header, Packet_FileHash, FileHash);
                    protocol.WritePacket(header, Packet_FileSize, CompactNum.GetBytes(FileSize));
                    protocol.WritePacket(header, Packet_FileKey, FileKey);
                }

                protocol.WritePacket(header, Packet_Extra, Extra);

                return protocol.WriteFinish();
            }
        }
Beispiel #31
0
 public TaggedStream(string path, G2Protocol protocol)
     : base(path, FileMode.Open, FileAccess.Read, FileShare.Read)
 {
     Init(path, protocol, null);
 }
Beispiel #32
0
        public PacketsForm(DhtNetwork network)
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            Network = network;

            Protocol = new G2Protocol();

            Text = "Packets (" + Network.GetLabel() + ")";

            RefreshView();

            Network.UpdatePacketLog += AsyncUpdateLog;
        }
Beispiel #33
0
        public override byte[] Encode(G2Protocol protocol)
        {
            lock (protocol.WriteSection)
            {
                G2Frame packet = protocol.WritePacket(null, CommPacket.Data, null);

                protocol.WritePacket(packet, Packet_Service, CompactNum.GetBytes(Service));
                protocol.WritePacket(packet, Packet_DataType, CompactNum.GetBytes(DataType));
                protocol.WritePacket(packet, Packet_Data, Data);

                return protocol.WriteFinish();
            }
        }