Пример #1
0
        public override void Decode(byte[] buffer, int offset, int length)
        {
            BEncodedValue val;

            using (RawReader reader = new RawReader(new MemoryStream(buffer, offset, length, false), false))
            {
                BEncodedDictionary d = BEncodedDictionary.Decode <BEncodedDictionary>(reader);
                int totalSize        = 0;

                if (d.TryGetValue(MessageTypeKey, out val))
                {
                    messageType = (eMessageType)((BEncodedNumber)val).Number;
                }
                if (d.TryGetValue(PieceKey, out val))
                {
                    piece = (int)((BEncodedNumber)val).Number;
                }
                if (d.TryGetValue(TotalSizeKey, out val))
                {
                    totalSize = (int)((BEncodedNumber)val).Number;
                    metadata  = new byte[Math.Min(totalSize - piece * BlockSize, BlockSize)];
                    reader.Read(metadata, 0, metadata.Length);
                }
            }
        }
Пример #2
0
        public override void Decode(byte[] buffer, int offset, int length)
        {
            BEncodedValue      val;
            BEncodedDictionary d = BEncodedDictionary.Decode <BEncodedDictionary>(buffer, offset, length, false);

            if (d.TryGetValue(MaxRequestKey, out val))
            {
                maxRequests = (int)((BEncodedNumber)val).Number;
            }
            if (d.TryGetValue(VersionKey, out val))
            {
                version = ((BEncodedString)val).Text;
            }
            if (d.TryGetValue(PortKey, out val))
            {
                LocalPort = (int)((BEncodedNumber)val).Number;
            }

            LoadSupports((BEncodedDictionary)d[SupportsKey]);

            if (d.TryGetValue(MetadataSizeKey, out val))
            {
                metadataSize = (int)((BEncodedNumber)val).Number;
            }
        }
Пример #3
0
 protected string GetString(BEncodedDictionary dictionary, BEncodedString key)
 {
     if (dictionary.TryGetValue(key, out BEncodedValue value))
     {
         return(((BEncodedString)value).Text);
     }
     return(null);
 }
        public static bool TryDecodeMessage(BEncodedDictionary dictionary, out DhtMessage message, out string error)
        {
            message = null;
            error   = null;

            if (!dictionary.TryGetValue(MessageTypeKey, out BEncodedValue messageType))
            {
                message = null;
                error   = "The BEncodedDictionary did not contain the 'q' key, so the message type could not be identified";
                return(false);
            }

            if (messageType.Equals(QueryMessage.QueryType))
            {
                message = queryDecoders[(BEncodedString)dictionary[QueryNameKey]](dictionary);
            }
            else if (messageType.Equals(ErrorMessage.ErrorType))
            {
                message = new ErrorMessage(dictionary);
                messages.Remove(message.TransactionId);
            }
            else
            {
                QueryMessage   query;
                BEncodedString key = (BEncodedString)dictionary[TransactionIdKey];
                if (messages.TryGetValue(key, out query))
                {
                    messages.Remove(key);
                    try
                    {
                        message = query.CreateResponse(dictionary);
                    }
                    catch
                    {
                        error = "Response dictionary was invalid";
                    }
                }
                else
                {
                    error = "Response had bad transaction ID";
                }
            }

            // If the transaction ID is null, or invalid, we should bail out
            if (message != null && message.TransactionId == null)
            {
                error = "Response had a null transation ID";
            }

            // If the node ID is null, or invalid, we should bail out
            if (message != null && message.Id == null)
            {
                error = "Response had a null node ID";
            }

            return(error == null && message != null);
        }
Пример #5
0
        protected long GetLong(BEncodedDictionary dictionary, BEncodedString key)
        {
            BEncodedValue value;

            if (dictionary.TryGetValue(key, out value))
            {
                return(((BEncodedNumber)value).Number);
            }
            throw new ArgumentException(string.Format("The value for key {0} was not a BEncodedNumber", key));
        }
Пример #6
0
        protected string GetString(BEncodedDictionary dictionary, BEncodedString key)
        {
            BEncodedValue value;

            if (dictionary.TryGetValue(key, out value))
            {
                return(value.ToString());
            }
            return("");
        }
Пример #7
0
        public void Decode(BEncodedValue value)
        {
            BEncodedDictionary val = value as BEncodedDictionary;

            if (val != null)
            {
                //if do not find key do not throw exception just continue with default value ;)
                BEncodedValue result;
                //For number maybe best is to do ((int)((BEncodedNumber)result).Number) but keep using convert and ToString()
                if (val.TryGetValue(new BEncodedString("SavePath"), out result))
                {
                    SavePath = result.ToString();
                }

                if (val.TryGetValue(new BEncodedString("GlobalMaxConnections"), out result))
                {
                    GlobalMaxConnections = Convert.ToInt32(result.ToString());
                }

                if (val.TryGetValue(new BEncodedString("GlobalMaxHalfOpenConnections"), out result))
                {
                    GlobalMaxHalfOpenConnections = Convert.ToInt32(result.ToString());
                }

                if (val.TryGetValue(new BEncodedString("GlobalMaxDownloadSpeed"), out result))
                {
                    GlobalMaxDownloadSpeed = Convert.ToInt32(result.ToString());
                }

                if (val.TryGetValue(new BEncodedString("GlobalMaxUploadSpeed"), out result))
                {
                    GlobalMaxUploadSpeed = Convert.ToInt32(result.ToString());
                }

                if (val.TryGetValue(new BEncodedString("ListenPort"), out result))
                {
                    ListenPort = Convert.ToInt32(result.ToString());
                }

                if (val.TryGetValue(new BEncodedString("UsePnP"), out result))
                {
                    UsePnP = Convert.ToBoolean(result.ToString());
                }

                if (val.TryGetValue(new BEncodedString("TorrentsPath"), out result))
                {
                    TorrentsPath = result.ToString();
                }
            }
        }
Пример #8
0
        protected BEncodedDictionary GetDictionary(BEncodedDictionary dictionary, BEncodedString key)
        {
//            // Required? Probably.
//            if (dictionary == InfoDict)
//                CheckCanEditSecure ();

            BEncodedValue value;

            if (dictionary.TryGetValue(key, out value))
            {
                return((BEncodedDictionary)value);
            }
            return(null);
        }
Пример #9
0
        public void Decode(BEncodedValue value)
        {
            BEncodedDictionary val = value as BEncodedDictionary;

            if (val != null)
            {
                //if do not find key do not throw exception just continue with default value ;)
                BEncodedValue result;
                //For number maybe best is to do ((int)((BEncodedNumber)result).Number) but keep using convert and ToString()

                if (val.TryGetValue(new BEncodedString("MaxDownloadSpeed"), out result))
                {
                    MaxDownloadSpeed = Convert.ToInt32(result.ToString());
                }

                if (val.TryGetValue(new BEncodedString("MaxUploadSpeed"), out result))
                {
                    MaxUploadSpeed = Convert.ToInt32(result.ToString());
                }

                if (val.TryGetValue(new BEncodedString("MaxConnections"), out result))
                {
                    MaxConnections = Convert.ToInt32(result.ToString());
                }

                if (val.TryGetValue(new BEncodedString("UploadSlots"), out result))
                {
                    UploadSlots = Convert.ToInt32(result.ToString());
                }

                if (val.TryGetValue(new BEncodedString("SavePath"), out result))
                {
                    savePath = result.ToString();
                }
            }
        }
Пример #10
0
        void Initialise(BEncodedDictionary metadata)
        {
            Metadata = metadata;

            BEncodedValue value;

            if (!Metadata.TryGetValue(AnnounceListKey, out value))
            {
                value = new BEncodedList();
                Metadata.Add(AnnounceListKey, value);
            }
            Announces = new RawTrackerTiers((BEncodedList)value);

            if (string.IsNullOrEmpty(Encoding))
            {
                Encoding = "UTF-8";
            }

            if (InfoDict == null)
            {
                InfoDict = new BEncodedDictionary();
            }
        }
Пример #11
0
        public void Decode(BEncodedValue value)
        {
            BEncodedDictionary val = value as BEncodedDictionary;

            if (val != null)
            {
                //if do not find key do not throw exception just continue with default value ;)
                BEncodedValue result;

                //For number maybe best is to do ((int)((BEncodedNumber)result).Number) but keep using convert and ToString()

                if (val.TryGetValue("Width", out result))
                {
                    width = Convert.ToInt32(result.ToString());
                }

                if (val.TryGetValue("height", out result))
                {
                    height = Convert.ToInt32(result.ToString());
                }

                if (val.TryGetValue("splitterDistance", out result))
                {
                    splitterDistance = Convert.ToInt32(result.ToString());
                }

                if (val.TryGetValue("VScrollValue", out result))
                {
                    VScrollValue = Convert.ToInt32(result.ToString());
                }

                if (val.TryGetValue("HScrollValue", out result))
                {
                    HScrollValue = Convert.ToInt32(result.ToString());
                }

                if (val.TryGetValue("ShowToolbar", out result))
                {
                    ShowToolbar = Convert.ToBoolean(result.ToString());
                }

                if (val.TryGetValue("ShowDetail", out result))
                {
                    ShowDetail = Convert.ToBoolean(result.ToString());
                }

                if (val.TryGetValue("ShowStatusbar", out result))
                {
                    ShowStatusbar = Convert.ToBoolean(result.ToString());
                }

                int i = 0;
                while (val.TryGetValue("TorrentViewColumnWidth" + i.ToString(), out result))
                {
                    torrentViewColumnWidth.Add(Convert.ToInt32(result.ToString()));
                    i++;
                }

                i = 0;
                while (val.TryGetValue("PeerViewColumnWidth" + i.ToString(), out result))
                {
                    peerViewColumnWidth.Add(Convert.ToInt32(result.ToString()));
                    i++;
                }

                i = 0;
                while (val.TryGetValue("PieceViewColumnWidth" + i.ToString(), out result))
                {
                    pieceViewColumnWidth.Add(Convert.ToInt32(result.ToString()));
                    i++;
                }

                if (val.TryGetValue("CustomButtonPath", out result))
                {
                    CustomButtonPath = result.ToString();
                }
            }
        }
Пример #12
0
        private void ShowTorrentInformation()
        {
            try
            {
                BEncodedValue  value;
                BEncodedString str;
                bool           b;

                txtInfo.Clear();

                txtInfo.AppendText("种子文件: " + currentTorrentFilename + Environment.NewLine);

                b = torrent.TryGetValue("created by", out value);
                if (b)
                {
                    str = value as BEncodedString;
                    if (str != null)
                    {
                        txtInfo.AppendText("创建程序: " + str.ToString() + Environment.NewLine);
                    }
                }

                b = torrent.TryGetValue("creation date", out value);
                if (b)
                {
                    str = value as BEncodedString;
                    if (str != null)
                    {
                        string creationDateString = str.ToString();
                        try
                        {
                            long longDate;
                            longDate = Convert.ToInt64(creationDateString);
                            DateTime creationDate = DateTime.FromBinary(longDate);
                            txtInfo.AppendText("创建日期: " + creationDate.ToString() + Environment.NewLine);
                        }
                        catch (Exception)
                        {
                            txtInfo.AppendText("创建日期: (无效)" + Environment.NewLine);
                        }
                    }
                }

                b = torrent.TryGetValue("encoding", out value);
                if (b)
                {
                    str = value as BEncodedString;
                    if (str != null)
                    {
                        txtInfo.AppendText("编码: " + str.ToString() + Environment.NewLine);
                    }
                }

                // 如果不存在,那肯定不是种子文件
                value = torrent["info"];
                BEncodedDictionary dict        = (BEncodedDictionary)value;
                BEncodedString     torrentName = (BEncodedString)dict["name"];
                txtInfo.AppendText("种子名称: " + torrentName.ToString() + Environment.NewLine);
                BEncodedNumber pieceLength = (BEncodedNumber)dict["piece length"];
                txtInfo.AppendText("分块大小: ");
                string postfix;
                double val = 0;
                var    num = pieceLength.Number;
                if (num < 1024)
                {
                    postfix = " B";
                    val     = num;
                }
                else if (num < 1024 * 1024)
                {
                    postfix = " KB";
                    val     = num / (double)1024;
                }
                else if (num < 1024 * 1024 * 1024)
                {
                    postfix = " MB";
                    val     = num / (double)(1024 * 1024);
                }
                else
                {
                    postfix = " GB";
                    val     = num / (double)(1024 * 1024 * 1024);
                }
                val = Math.Round(val, 2);
                txtInfo.AppendText(val.ToString() + postfix + Environment.NewLine);
                BEncodedString pieces = (BEncodedString)dict["pieces"];
                txtInfo.AppendText("分块数量: " + (pieces.TextBytes.Length / 20).ToString() + Environment.NewLine);
            }
            catch (Exception)
            {
                isHandlingTorrent = false;
                txtInfo.AppendText("解析种子时出现错误。");
            }
        }
        static T Deserialize <T> (BEncodedDictionary dict)
            where T : new()
        {
            T   builder = new T();
            var props   = builder.GetType().GetProperties();

            foreach (var property in props)
            {
                if (!dict.TryGetValue(property.Name, out BEncodedValue? value))
                {
                    continue;
                }

                if (property.PropertyType == typeof(bool))
                {
                    property.SetValue(builder, bool.Parse(value.ToString() !));
                }
                else if (property.PropertyType == typeof(string))
                {
                    property.SetValue(builder, ((BEncodedString)value).Text);
                }
                else if (property.PropertyType == typeof(FastResumeMode))
                {
                    property.SetValue(builder, Enum.Parse(typeof(FastResumeMode), ((BEncodedString)value).Text));
                }
                else if (property.PropertyType == typeof(TimeSpan))
                {
                    property.SetValue(builder, TimeSpan.FromTicks(((BEncodedNumber)value).Number));
                }
                else if (property.PropertyType == typeof(int))
                {
                    property.SetValue(builder, (int)((BEncodedNumber)value).Number);
                }
                else if (property.PropertyType == typeof(IPAddress))
                {
                    property.SetValue(builder, IPAddress.Parse(((BEncodedString)value).Text));
                }
                else if (property.PropertyType == typeof(IPEndPoint))
                {
                    var        list     = (BEncodedList)value;
                    IPEndPoint?endPoint = null;
                    if (list.Count == 2)
                    {
                        var ipAddress = (BEncodedString)list.Single(t => t is BEncodedString);
                        var port      = (BEncodedNumber)list.Single(t => t is BEncodedNumber);
                        endPoint = new IPEndPoint(IPAddress.Parse(ipAddress.Text), (int)port.Number);
                    }
                    property.SetValue(builder, endPoint);
                }
                else if (property.PropertyType == typeof(IList <EncryptionType>))
                {
                    var list = (IList <EncryptionType>)property.GetValue(builder) !;
                    list.Clear();
                    foreach (BEncodedString encryptionType in (BEncodedList)value)
                    {
                        list.Add((EncryptionType)Enum.Parse(typeof(EncryptionType), encryptionType.Text));
                    }
                }
                else
                {
                    throw new NotSupportedException($"{property.Name} => type: ${property.PropertyType}");
                }
            }
            return(builder);
        }
        protected BEncodedDictionary GetDictionary(BEncodedDictionary dictionary, BEncodedString key)
        {
//            // Required? Probably.
//            if (dictionary == InfoDict)
//                CheckCanEditSecure ();

            BEncodedValue value;
            if (dictionary.TryGetValue(key, out value))
                return (BEncodedDictionary) value;
            return null;
        }
 protected long GetLong(BEncodedDictionary dictionary, BEncodedString key)
 {
     BEncodedValue value;
     if (dictionary.TryGetValue(key, out value))
         return ((BEncodedNumber) value).Number;
     throw new ArgumentException(string.Format("The value for key {0} was not a BEncodedNumber", key));
 }
 protected string GetString(BEncodedDictionary dictionary, BEncodedString key)
 {
     BEncodedValue value;
     if (dictionary.TryGetValue(key, out value))
         return value.ToString();
     return "";
 }
        private void Initialise(BEncodedDictionary metadata)
        {
            Metadata = metadata;

            BEncodedValue value;
            if (!Metadata.TryGetValue(AnnounceListKey, out value))
            {
                value = new BEncodedList();
                Metadata.Add(AnnounceListKey, value);
            }
            Announces = new RawTrackerTiers((BEncodedList) value);

            if (string.IsNullOrEmpty(Encoding))
                Encoding = "UTF-8";

            if (InfoDict == null)
                InfoDict = new BEncodedDictionary();
        }