Пример #1
0
        public static bool IsIPChanged(this DomainConfigItem configItem)
        {
            var currentAddress = IPHelper.GetAddress(configItem.Interface, configItem.AddressFamily);
            var lastAddress    = configItem.LastSyncSuccessCurrentIP;

            return(currentAddress != lastAddress);
        }
Пример #2
0
        public static void AddItem(DomainConfigItem item)
        {
            var configs = GetConfigs();

            configs.Add(item);

            File.WriteAllBytes(ConfigFilePath, JsonSerializer.SerializeToUtf8Bytes(configs, DefaultOptions));
        }
Пример #3
0
        public static void UpdateItem(DomainConfigItem item)
        {
            var configs = GetConfigs() ?? new List <DomainConfigItem>();

            var i = configs.FindIndex(c => c.Equals(item));

            if (i == -1)
            {
                throw new KeyNotFoundException($"No record with domain name '{item.Domain}' and type '{item.Type}' was found.");
            }

            configs[i] = item;
            File.WriteAllBytes(ConfigFilePath, JsonSerializer.SerializeToUtf8Bytes(configs, DefaultOptions));
        }
Пример #4
0
        public static int DeleteItem(DomainConfigItem item)
        {
            var configs       = GetConfigs() ?? new List <DomainConfigItem>();
            var originalCount = configs.Count;

            // TODO 这里不可能查到多个相等并返回 List 的
            configs = configs.Where(c => !c.Equals(item)).ToList();
            var currentCount = configs.Count;

            var deletedCount = originalCount - currentCount;

            if (deletedCount > 0)
            {
                File.WriteAllBytes(ConfigFilePath, JsonSerializer.SerializeToUtf8Bytes(configs, DefaultOptions));
            }

            return(deletedCount);
        }
Пример #5
0
 public static bool Exists(DomainConfigItem item, bool ignoreProvider = true)
 {
     return(GetConfigs().Any(c => c.Equals(item, ignoreProvider)));
 }
Пример #6
0
 /// <summary>
 /// 根据传入的条件精确查询域名配置。
 /// </summary>
 /// <param name="query">可以不填写 <see cref="DomainConfigItem.Provider"/>,仅通过 <see cref="DomainConfigItem.Domain"/> 和 <see cref="DomainConfigItem.Type"/> 就能确定唯一。一旦填写了 <see cref="DomainConfigItem.Provider"/>,那它也将进行判断。</param>
 /// <returns>匹配的记录或 null。</returns>
 public static DomainConfigItem GetSingle(DomainConfigItem query)
 {
     return(GetConfigs().SingleOrDefault(c => c.Equals(query, ignoreProvider: String.IsNullOrEmpty(query.Provider))));
 }
Пример #7
0
        public static T GetProviderInfo <T>(DomainConfigItem configItem) where T : class, IProviderConfig
        {
            var json = JsonSerializer.Serialize(configItem.ProviderInfo);

            return(JsonSerializer.Deserialize <T>(json));
        }