private static void GetCoreCache() { log.Info("GetCoreCache()"); string core_url = Configuration.GetCoreUrl(); string certName = Configuration.GetPfxPath(); string password = Configuration.GetPfxPassword(); string resolver_id = Configuration.GetResolverId(); X509Certificate2Collection certificates = new X509Certificate2Collection(); certificates.Import(certName, password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet); ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => true; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(core_url); req.ServerCertificateValidationCallback += ValidateRemoteCertificate; req.AllowAutoRedirect = true; req.ClientCertificates = certificates; req.Method = "GET"; req.ContentType = "application/x-protobuf"; req.Headers["x-resolver-id"] = resolver_id; using (var response = req.GetResponseAsync().Result) { using (var stream = response.GetResponseStream()) { log.Debug($"Deserialize."); var cache = ProtoBuf.Serializer.Deserialize <Models.Cache>(stream); log.Debug($"Deserialized."); if (cache.CustomLists != null) { log.Debug($"Custom List count = {cache.CustomLists.ToArray().Count()}"); } if (cache.Domains != null) { log.Debug($"Domains count = {cache.Domains.ToArray().Count()}"); } if (cache.IPRanges != null) { log.Debug($"IPRanges count = {cache.IPRanges.ToArray().Count()}"); } if (cache.Policies != null) { log.Debug($"Policies count = {cache.Policies.ToArray().Count()}"); } CacheLiveStorage.CoreCache = cache; KresUpdater.UpdateNow(); } } }
public object UpdateNow(HttpListenerContext ctx, string postdata) { KresUpdater.UpdateNow(); return(0); }
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); }