using System.Collections.Generic; using Sog.IO; namespace Sog { public static class LimitIpDevice { private static FileStringIDList m_whiteList = null; private static FileStringIDList m_blackList = null; private static FileStringIDList m_whiteDeviceIdList = null; private static FileStringIDList m_blackDeviceIdList = null; private const string FileBasePath = "../cfg/sog/"; private static void Init() { m_whiteList = new FileStringIDList(FileBasePath + "white_ip.txt"); m_whiteList.ReadFromFile(); m_blackList = new FileStringIDList(FileBasePath + "black_ip.txt"); m_blackList.ReadFromFile(); m_whiteDeviceIdList = new FileStringIDList(FileBasePath + "white_deviceId.txt"); m_whiteDeviceIdList.ReadFromFile(); m_blackDeviceIdList = new FileStringIDList(FileBasePath + "black_deviceId.txt"); m_blackDeviceIdList.ReadFromFile(); TraceLog.Debug("LimitIPList.Init white ip count {0} black ip count {1} in list", m_whiteList.GetIdCount(), m_blackList.GetIdCount()); } public static void Clear() { m_whiteList = null; m_blackList = null; m_whiteDeviceIdList = null; } //是否再IP白名单之内 public static bool CheckInWhiteIp(string ip) { if (string.IsNullOrEmpty(ip)) { return false; } //初始化 if (m_whiteList == null) { Init(); } return m_whiteList != null && m_whiteList.IPInList(ip); } //是否再IP黑名单内 public static bool CheckInBlackIp(string ip) { if (string.IsNullOrEmpty(ip)) { return false; } if (m_blackList == null) { Init(); } return m_blackList != null && m_blackList.IPInList(ip); } // 是否再设备白名单中 public static bool CheckInWhiteDeviceId(string deviceId) { if (string.IsNullOrEmpty(deviceId)) { return false; } //初始化 if (m_whiteDeviceIdList == null) { Init(); } return m_whiteDeviceIdList != null && m_whiteDeviceIdList.IDInList(deviceId); } public static Dictionary.KeyCollection GetBlackList() { //初始化 if (m_whiteList == null) { Init(); } return m_whiteList?.GetAllId().Keys; } public static Dictionary.KeyCollection GetWhiteList() { //初始化 if (m_blackList == null) { Init(); } return m_blackList?.GetAllId().Keys; } public static Dictionary.KeyCollection GetWhiteDeviceList() { if (m_whiteDeviceIdList == null) { Init(); } return m_whiteDeviceIdList?.GetAllId().Keys; } public static Dictionary.KeyCollection GetBlackDeviceList() { if (m_blackDeviceIdList == null) { Init(); } return m_blackDeviceIdList?.GetAllId().Keys; } } }