private static List <Models.CacheCustomList> LoadCustomLists() { var result = new List <Models.CacheCustomList>(); var csv = new CsvHelper.CsvReader(File.OpenText("custom.csv")); while (csv.Read()) { var item = new Models.CacheCustomList { Identity = csv.GetField(0), WhiteList = csv.GetField(1).Split(';'), BlackList = csv.GetField(2).Split(';'), PolicyId = Convert.ToInt32(csv.GetField(3)) }; result.Add(item); } return(result); }
public string Bypass(HttpContext ctx, string clientIpAddress, string domainToWhitelist, string authToken, string base64encodedUrlToRedirectTo) { log.Info($"Bypass request, ip={clientIpAddress}, {domainToWhitelist}."); if (string.Compare(authToken, "BFLMPSVZ", StringComparison.OrdinalIgnoreCase) != 0) { return(""); } var idbytes = Encoding.ASCII.GetBytes(clientIpAddress); var idcrc = Crc64.Compute(0, idbytes); string identity = idcrc.ToString("X"); IPAddress ip; if (!IPAddress.TryParse(clientIpAddress, out ip)) { return(new Exception($"unable to parse ip address {clientIpAddress}.").Message); } //TODO: check ipv6 reverse var bytes = ip.GetAddressBytes().Reverse().ToArray(); BigMath.Int128 intip; if (bytes.Length == 4) { intip = new BigMath.Int128(0, BitConverter.ToUInt32(bytes, 0)); } else if (bytes.Length == 16) { intip = new BigMath.Int128(BitConverter.ToUInt64(bytes, 0), BitConverter.ToUInt64(bytes, 8)); } else { return(new Exception($"unable to parse ip address {clientIpAddress}.").Message); } List <Models.CacheIPRange> ipranges; List <Models.CacheCustomList> customlists; if (CacheLiveStorage.CoreCache.IPRanges != null) { ipranges = CacheLiveStorage.CoreCache.IPRanges.ToList(); } else { ipranges = new List <Models.CacheIPRange>(); } if (CacheLiveStorage.CoreCache.IPRanges != null) { customlists = CacheLiveStorage.CoreCache.CustomLists.ToList(); } else { customlists = new List <Models.CacheCustomList>(); } ipranges.Add(new Models.CacheIPRange() { Identity = identity, Proto_IpFrom = Encoding.ASCII.GetBytes(intip.ToString()), Proto_IpTo = Encoding.ASCII.GetBytes(intip.ToString()), PolicyId = 0 }); var item = customlists.FirstOrDefault(t => string.Compare(t.Identity, identity, StringComparison.OrdinalIgnoreCase) == 0); if (item == null) { item = new Models.CacheCustomList() { Identity = identity, WhiteList = new List <string>() { domainToWhitelist }, BlackList = new List <string>(), PolicyId = 0 }; log.Info($"Identity {identity} now has {domainToWhitelist} whitelisted."); } else { if (!item.WhiteList.Contains(domainToWhitelist)) { var list = item.WhiteList.ToList(); list.Add(domainToWhitelist); item.WhiteList = list; foreach (var entry in item.WhiteList) { log.Info($"Identity {identity} now has {entry} whitelisted."); } } else { log.Info($"Identity {identity} has {domainToWhitelist} already whitelisted."); } } customlists.RemoveAll(t => string.Compare(t.Identity, identity, StringComparison.OrdinalIgnoreCase) == 0); customlists.Add(item); CacheLiveStorage.CoreCache.IPRanges = ipranges; CacheLiveStorage.CoreCache.CustomLists = customlists; log.Info($"Updating kres modules."); KresUpdater.UpdateSmallCaches(); KresUpdater.UpdateNow(); log.Info($"Kres modules have been updated."); //var redirectUrl = Base64Decode(base64encodedUrlToRedirectTo); //ctx.Response.RedirectLocation = redirectUrl; return(null); }