在许可列表连接方面需要帮助 [英] need help with allowlist connection

查看:54
本文介绍了在许可列表连接方面需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题:我没有访问列表 banlist.txt ,但是我还需要一个允许列表 allowlist.txt .有人可以帮我编辑此代码以便它处理吗?
最后,它需要使用 banlist allowlist 连接到htm或php或rss服务器.

I have a question: I have a no access list banlist.txt, but I also need an allow list allowlist.txt. Can someone help me edit this code so it can handle this?
In the end, it needs to use banlist and allowlist to connect to an htm or php or rss server.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace IWNetServer
{
    public class Client
    {
        #region static helpers
        private static Dictionary<long,client> XUIDClients { get; set; }
 
        //-----------------------------------------------------------------
        public static bool IsAllowed(long xuid)
        {
            return (!XUIDBans.Contains(xuid));
        }
 
        //-----------------------------------------------------------------
        public static bool IsAllowed(IPAddress ip)
        {
            return (!IPBans.Contains(ip));
        }
 
        //-----------------------------------------------------------------
        public static bool IsHostAllowed(long xuid)
        {
            return (!HostXUIDBans.Contains(xuid));
        }
 
        //-----------------------------------------------------------------
        public static bool IsHostAllowed(IPAddress ip)
        {
            return (!HostIPBans.Contains(ip));
        }
 
        private static List<ipaddress> IPBans       { get; set; }
        private static List<long>      XUIDBans     { get; set; }
        private static List<ipaddress> HostIPBans   { get; set; }
        private static List<long>      HostXUIDBans { get; set; }
 
        //-----------------------------------------------------------------
        public static void UpdateBanList()
        {
            IPBans       = new List<ipaddress>();
            XUIDBans     = new List<long>();
            HostIPBans   = new List<ipaddress>();
            HostXUIDBans = new List<long>();
 
            if (!File.Exists("banlist.txt"))
            {
                return;
            }
 
            var banFile = File.OpenText("banlist.txt");
            while (!banFile.EndOfStream)
            {
                var line = banFile.ReadLine().Trim();
 
                if (line == "")
                {
                    continue;
                }
 
                if (line[0] == ';' || line[1] == '#')
                {
                    continue;
                }
 
                var data = line.Split(' ');
                if (data.Length != 2)
                {
                    continue;
                }
 
                try
                {
                    switch (data[1])
                    {
                        case "ip":
                            IPBans.Add(IPAddress.Parse(data[0]));
                            break;
                        case "steam":
                            XUIDBans.Add(long.Parse(data[0], System.Globalization.NumberStyles.HexNumber));
                            break;
                        case "hostip":
                            HostIPBans.Add(IPAddress.Parse(data[0]));
                            break;
                        case "hoststeam":
                            HostXUIDBans.Add(long.Parse(data[0], System.Globalization.NumberStyles.HexNumber));
                            break;
                    }
                }
                catch (FormatException) { }
            }
            banFile.Close();
        }
 
        //-----------------------------------------------------------------
        public static bool IsVersionAllowed(byte major, byte minor)
        {
            return (major == 1 && ((minor >= 48 && minor < 100)));
        }
 
        //-----------------------------------------------------------------
        public static Client Get(long xuid)
        {
            if (!XUIDClients.ContainsKey(xuid))
            {
                lock (XUIDClients)
                {
                    XUIDClients.Add(xuid, new Client(xuid));
                }
            }
            return XUIDClients[xuid];
        }
 
        //-----------------------------------------------------------------
        public static List<logstatistics> GetStatistics()
        {
            lock (XUIDClients)
            {
                var availableClients = from client in XUIDClients.Values
                                       where client.IsOnline
                                       group client by client.CurrentState into state
                                       select new LogStatistics(state.Key, (short)state.Count());
                return availableClients.ToList();
            }
        }
 
        //-----------------------------------------------------------------
        public static short GetPlaylistVersion()
        {
            if (File.Exists(@"pc\mp_playlists.txt"))
            {
                var file = File.OpenText(@"pc\mp_playlists.txt");
                var data = file.ReadToEnd().Trim();
                file.Close();

                return short.Parse(data);
            }

            return 0x17B;
        }
 
        //-----------------------------------------------------------------
        public static void CleanClientsThatAreLongGone()
        {
            lock (XUIDClients)
            {
                var canBeSafelyDeleted = (from client in XUIDClients
                                          where (DateTime.UtcNow - client.Value.LastTouched).TotalSeconds > 600
                                          select client.Key).ToList();
                foreach (var client in canBeSafelyDeleted)
                {
                    XUIDClients.Remove(client);
                }
                Log.Info(string.Format("Deleted {0} clients", canBeSafelyDeleted.Count));
                canBeSafelyDeleted = null;
            }
        }
 
        //-----------------------------------------------------------------
        public static bool Exists(long xuid)
        {
            return XUIDClients.ContainsKey(xuid) && XUIDClients[xuid].GameBuild > 0;
        }
        #endregion
 
        //-----------------------------------------------------------------
        static Client()
        {
            XUIDClients = new Dictionary<long,client>();
        }
 
        //-----------------------------------------------------------------
        public Client(long xuid)
        {
            XUID = xuid;
            XUIDAlias = 0;
        }
 
        //-----------------------------------------------------------------
        public void SetFromLog(LogRequestPacket1 packet)
        {
            GamerTag     = packet.GamerTag;
            InternalIP   = packet.InternalIP;
            ExternalIP   = packet.ExternalIP;
            GameVersion  = packet.GameVersion;
            GameBuild    = packet.GameBuild;
            CurrentState = 0x416E; 
            SetLastMatched();
            SetLastTouched();
        }
 
        //-----------------------------------------------------------------
        public void SetLastTouched()
        {
            LastTouched = DateTime.UtcNow;
        }
 
        //-----------------------------------------------------------------
        public void SetLastMatched()
        {
            LastMatched = DateTime.UtcNow;
        }
 
        //-----------------------------------------------------------------
        public bool IsMatched
        {
            get
            {
                return (DateTime.UtcNow - LastMatched).TotalSeconds < 90;
            }
        }
 
        //-----------------------------------------------------------------
        public bool IsOnline
        {
            get
            {
                return (DateTime.UtcNow - LastTouched).TotalSeconds < 30; 
            }
        }
 
        //-----------------------------------------------------------------
        public short CurrentState { get; set; } // playlist ID or 'random' ID
        public long       XUID        { get; set; }
        public long       XUIDAlias   { get; set; }
        public string     GamerTag    { get; set; }
        public IPEndPoint InternalIP  { get; set; }
        public IPEndPoint ExternalIP  { get; set; }
        public byte       GameVersion { get; set; }
        public byte       GameBuild   { get; set; }
        public DateTime   LastTouched { get; set; }
        public DateTime   LastMatched { get; set; }
    }
}</logstatistics>

推荐答案

首先,两者都没有任何意义.任何不在禁令清单上的人都将被允许连接.不在允许列表上的任何人将无法连接.这就是这类事情的本质.如果只希望允许某些人连接,则以相反的方式对待禁令列表.

如果您坚持要同时拥有两个列表,那么执行此操作的简单方法可能是将构造函数更改为接受表示允许或禁止的布尔值,并加载适当的文件:

First, it doesn''t make any sense to have both. Anyone NOT on a ban list would be allow to connect. Anyone not on an allow list would not be able to connect. That''s the nature of such things. If you only want certain people to be allowed to connect, just treat the ban list the opposite way.

If you insist on having both lists, the easy way to do it would be to probably change the constructor to accept a bool that indicates allowed or banned, and load the appropriate file:

public class Client
{
    private string FileName { get; set; }

    public Client(bool allowed)
    {
        this.FileName = (allowed) ? "allowedlist.txt" ? "banlist.txt";
    }
  
}



之后,将现有代码中所有出现的"banlist.txt"更改为this.FileName.您可能还需要进行其他更改,但我将留给您发现.

顺便说一句,如果我是你,我将在我的代码组织上加倍努力.在一个对象中,我是这样进行的:



After that, change all occurrances of "banlist.txt" in your existing code to this.FileName. There are other changes you''ll probably have to make, but I''ll leave that to your discovery.

BTW, if I were you, I''d work a lot harder on my code organization. In an object, I do it this way:

public class MyClass
{
    // data members (usually private or protected)

    // properties (usually public)

    // constructor (with the default constructor first)

    // initialization methods

    // everything else in some sort of reasonable order/region
}


这篇关于在许可列表连接方面需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆