/// <summary>
 /// Create a new peers object.
 /// </summary>
 /// <param name="id">Initial value of the id property.</param>
 /// <param name="torrentid">Initial value of the torrentid property.</param>
 /// <param name="peer_id">Initial value of the peer_id property.</param>
 /// <param name="ip">Initial value of the ip property.</param>
 /// <param name="port">Initial value of the port property.</param>
 /// <param name="uploaded">Initial value of the uploaded property.</param>
 /// <param name="downloaded">Initial value of the downloaded property.</param>
 /// <param name="left">Initial value of the left property.</param>
 /// <param name="seeding">Initial value of the seeding property.</param>
 /// <param name="userid">Initial value of the userid property.</param>
 /// <param name="connectable">Initial value of the connectable property.</param>
 /// <param name="useragent">Initial value of the useragent property.</param>
 public static peers Createpeers(global::System.Int64 id, global::System.Int64 torrentid, global::System.String peer_id, global::System.String ip, global::System.Int32 port, global::System.Decimal uploaded, global::System.Decimal downloaded, global::System.Decimal left, global::System.Int64 seeding, global::System.Int64 userid, global::System.Int64 connectable, global::System.String useragent)
 {
     peers peers = new peers();
     peers.id = id;
     peers.torrentid = torrentid;
     peers.peer_id = peer_id;
     peers.ip = ip;
     peers.port = port;
     peers.uploaded = uploaded;
     peers.downloaded = downloaded;
     peers.left = left;
     peers.seeding = seeding;
     peers.userid = userid;
     peers.connectable = connectable;
     peers.useragent = useragent;
     return peers;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the peers EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddTopeers(peers peers)
 {
     base.AddObject("peers", peers);
 }
예제 #3
0
        //
        // GET: /Announce/Announce/
        // GET: /announce/123af0c917876f6d4711654b2293895f
        public ActionResult Announce(AnnounceModel announceModel)
        {
            if (!announceModel.IsValidRequest())
                return new BTErrorResult("Invalid request (see specification: http://bit.ly/bcYmSu)");

            if (!Regex.IsMatch(announceModel.Passkey, "[0-9a-fA-F]{32}"))
                return new BTErrorResult("Invalid passkey.");

            if (BLACKLIST_PORTS && IsPortBlackListed(Convert.ToInt32(announceModel.port)))
                return new BTErrorResult(string.Format("Port {0} is blacklisted", announceModel.port));

            try
            {
                using (var context = new OpenTrackerDbContext())
                {
                    var crntUser = (from u in context.users
                                    where u.passkey == announceModel.Passkey
                                    select u).Take(1).FirstOrDefault();
                    if (crntUser == null)
                        return new BTErrorResult(string.Format("Unknown passkey. Please re-download the torrent from {0}.",
                            TrackerSettings.BASE_URL));

                    if (crntUser.activated == 0)
                        return new BTErrorResult("Permission denied, you\'re not activated.");

                    var seeder = false;
                    if (announceModel.left == 0)
                        seeder = true;

                    // Entity Framework does not support BINARY keys
                    var EncodedPeerId = Convert.ToBase64String(Encoding.ASCII.GetBytes(announceModel.peer_id));
                    var EncodedInfoHash = BEncoder.FormatUrlInfoHash();

                    var torrentExist = (from t in context.torrents
                                        where t.info_hash == EncodedInfoHash
                                        select t).Take(1).FirstOrDefault();
                    if (torrentExist == null)
                        return new BTErrorResult("Torrent not registered with this tracker.");

                    var peerAlreadyExist = (from t in context.peers
                                            where t.torrentid == torrentExist.id
                                            && t.peer_id == EncodedPeerId
                                            && t.passkey == announceModel.Passkey
                                            select t).Take(1);
                    var existingPeerCount = peerAlreadyExist.Count();
                    peers p;
                    if (existingPeerCount == 1)
                    {
                        p = peerAlreadyExist.First();
                    }
                    else
                    {
                        var connectionLimit = (from t in context.peers
                                               where t.torrentid == torrentExist.id
                                               && t.passkey == announceModel.Passkey
                                               select t).Count();
                        if (connectionLimit >= 1 && !seeder)
                            return new BTErrorResult("Connection limit exceeded! " +
                                "You may only leech from one location at a time.");
                        if (connectionLimit >= 3 && seeder)
                            return new BTErrorResult("Connection limit exceeded.");

                        if (announceModel.left > 0 && crntUser.@class < (decimal)AccountValidation.Class.Administrator)
                        {
                            var epoch = Unix.ConvertToUnixTimestamp(DateTime.UtcNow);
                            var elapsed = Math.Floor((epoch - torrentExist.added) / 3600);

                            var uploadedGigs = crntUser.uploaded/(1024*1024*1024);
                            var ratio = ((crntUser.downloaded > 0) ? (crntUser.uploaded / crntUser.downloaded) : 1);

                            int wait;
                            if (ratio < (decimal)0.5 || uploadedGigs < 5)
                                wait = 48;
                            else if (ratio < (decimal)0.65 || uploadedGigs < (decimal)6.5)
                                wait = 24;
                            else if (ratio < (decimal)0.8 || uploadedGigs < 8)
                                wait = 12;
                            else if (ratio < (decimal)0.95 || uploadedGigs < (decimal)9.5)
                                wait = 6;
                            else
                                wait = 0;

                            if (elapsed < wait)
                                return new BTErrorResult(string.Format("Not authorized (wait {0}h) - READ THE FAQ!",
                                                                       (wait - elapsed)));
                        }

                        p = new peers
                        {
                            torrentid = torrentExist.id,
                            peer_id = EncodedPeerId,
                            userid = crntUser.id,
                            passkey = announceModel.Passkey,
                            useragent = Request.UserAgent
                        };
                    }

                    var remoteHost = Request.ServerVariables["REMOTE_HOST"];
                    var ip = !string.IsNullOrEmpty(announceModel.ip) ? announceModel.ip : remoteHost;

                    if (CHECK_CONNECTABLE)
                        p.connectable = IsConnectable(ip, Convert.ToInt32(announceModel.port)) ? 1 : 0;
                    if (announceModel.left != null)
                        p.left = (decimal) announceModel.left;
                    p.port = Convert.ToInt32(announceModel.port);
                    p.ip = ip;

                    p.seeding = seeder ? 1 : 0;

                    if (existingPeerCount == 0)
                        context.AddTopeers(p);
                    else
                    {
                        if (crntUser.@class < (decimal)AccountValidation.Class.Administrator)
                        {
                            var nonUpdatedPeer = peerAlreadyExist.First();
                            var thisUploaded = (announceModel.uploaded - nonUpdatedPeer.uploaded);
                            var thisDownloaded = (announceModel.downloaded - nonUpdatedPeer.downloaded);

                            p.uploaded += (decimal)thisUploaded;
                            p.downloaded += (decimal)thisDownloaded;

                            if (thisUploaded > 0)
                                crntUser.uploaded = (crntUser.uploaded + Convert.ToInt64(thisUploaded));
                            if (thisDownloaded > 0)
                                crntUser.downloaded = (crntUser.downloaded + Convert.ToInt64(thisDownloaded));
                        }

                        if (announceModel.Event == "completed")
                            torrentExist.snatches = torrentExist.snatches + 1; // torrentExist.snatches++;
                    }
                    context.SaveChanges();

                    if (announceModel.Event == "stopped")
                    {
                        var removePeer = (from pr in context.peers
                                          where pr.torrentid == torrentExist.id
                                          && pr.peer_id == EncodedPeerId
                                          select pr).Take(1).FirstOrDefault();
                        context.peers.DeleteObject(removePeer);
                        context.SaveChanges();

                        var announceResultStop = new AnnounceResult
                        {
                            Interval = ANNOUNCE_INTERVAL
                        };
                        return announceResultStop;
                    }

                    var announceResult = new AnnounceResult
                                             {
                                                 Interval = ANNOUNCE_INTERVAL
                                             };

                    var existingPeers = (from t in context.peers
                                            where t.torrentid == torrentExist.id
                                            select t).ToList();

                    foreach (var peer in existingPeers)
                        announceResult.AddPeer(peer.peer_id, peer.ip, peer.port);

                    return announceResult;
                }
            }
            catch (Exception)
            {
                return new BTErrorResult("Database unavailable");
            }
        }