static void FiddlerApplication_BeforeRequest(Session rpSession) { if (Preference.Current.Network.UpstreamProxy.Enabled) rpSession["x-OverrideGateway"] = Preference.Current.Network.UpstreamProxy.Address; var rRequest = rpSession.oRequest; var rFullUrl = rpSession.fullUrl; var rPath = rpSession.PathAndQuery; NetworkSession rSession; if (rPath.StartsWith("/kcsapi/")) rSession = new ApiSession(rFullUrl); else if (rPath.StartsWith("/kcs/") || rPath.StartsWith("/gadget/")) rSession = new ResourceSession(rFullUrl, rPath); else rSession = new NetworkSession(rFullUrl); rSession.RequestBodyString = Uri.UnescapeDataString(rpSession.GetRequestBodyAsString()); rpSession.Tag = rSession; SessionSubject.OnNext(rSession); if (rFullUrl == GameConstants.GamePageUrl || rPath == "/gadget/js/kcs_flash.js") rpSession.bBufferResponse = true; var rResourceSession = rSession as ResourceSession; if (rResourceSession != null) CacheService.Instance.ProcessRequest(rResourceSession, rpSession); }
internal void ProcessRequest(ResourceSession rpResourceSession, Session rpSession) { if (CurrentMode == CacheMode.Disabled) return; string rFilename; var rNoVerification = CheckFileInCache(rpResourceSession.Path, out rFilename); rpResourceSession.CacheFilename = rFilename; if (rNoVerification == null) return; if (!rNoVerification.Value) { var rTimestamp = new DateTimeOffset(File.GetLastWriteTime(rFilename)); if (rpResourceSession.Path.OICContains("mainD2.swf") || rpResourceSession.Path.OICContains(".js") || !CheckFileVersionAndTimestamp(rpResourceSession, rTimestamp)) { rpSession.oRequest["If-Modified-Since"] = rTimestamp.ToString("R"); rpSession.bBufferResponse = true; return; } } rpSession.utilCreateResponseAndBypassServer(); LoadFile(rFilename, rpResourceSession, rpSession); }
void LoadFile(string rpFilename, ResourceSession rpResourceSession, Session rpSession) { if (!rpSession.bBufferResponse) rpSession.utilCreateResponseAndBypassServer(); rpSession.ResponseBody = File.ReadAllBytes(rpFilename); rpSession.oResponse["Server"] = "Apache"; rpSession.oResponse["Connection"] = "close"; rpSession.oResponse["Accept-Ranges"] = "bytes"; rpSession.oResponse["Cache-Control"] = "max-age=18000, public"; rpSession.oResponse["Date"] = DateTime.Now.ToString("R"); if (rpFilename.EndsWith(".swf", StringComparison.OrdinalIgnoreCase)) rpSession.oResponse["Content-Type"] = "application/x-shockwave-flash"; else if (rpFilename.EndsWith(".mp3", StringComparison.OrdinalIgnoreCase)) rpSession.oResponse["Content-Type"] = "audio/mpeg"; else if (rpFilename.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) rpSession.oResponse["Content-Type"] = "image/png"; else if (rpFilename.EndsWith(".css", StringComparison.OrdinalIgnoreCase)) rpSession.oResponse["Content-Type"] = "text/css"; else if (rpFilename.EndsWith(".js", StringComparison.OrdinalIgnoreCase)) rpSession.oResponse["Content-Type"] = "application/x-javascript"; rpResourceSession.State = NetworkSessionState.LoadedFromCache; }
static void FiddlerApplication_BeforeRequest(Session rpSession) { if (r_BlockingList.Any(rpSession.uriContains)) { rpSession.utilCreateResponseAndBypassServer(); return; } if (!r_UpstreamProxy.IsNullOrEmpty()) { rpSession["x-OverrideGateway"] = r_UpstreamProxy; } var rRequest = rpSession.oRequest; var rFullUrl = rpSession.fullUrl; var rPath = rpSession.PathAndQuery; NetworkSession rSession; if (rPath.StartsWith("/kcsapi/")) { rSession = new ApiSession(rFullUrl); } else if (rPath.StartsWith("/kcs/") || rPath.StartsWith("/gadget/")) { rSession = new ResourceSession(rFullUrl, rPath); } else { rSession = new NetworkSession(rFullUrl); } rSession.RequestBodyString = Uri.UnescapeDataString(rpSession.GetRequestBodyAsString()); rSession.Method = rpSession.RequestMethod; rpSession.Tag = rSession; if (rFullUrl.OICEquals(GameConstants.GamePageUrl) || rFullUrl.OICEquals("http://www.dmm.com/netgame_s/kancolle/") || rPath.OICEquals("/gadget/js/kcs_flash.js")) { rpSession.bBufferResponse = true; } var rResourceSession = rSession as ResourceSession; if (rResourceSession != null) { CacheService.Instance.ProcessRequest(rResourceSession, rpSession); } rSession.RequestHeaders = rpSession.RequestHeaders.Select(r => new SessionHeader(r.Name, r.Value)).ToArray(); SessionSubject.OnNext(rSession); if (!rpSession.bHasResponse && r_TrafficBarrier != null) { r_TrafficBarrier.Wait(); } }
internal void ProcessResponse(ResourceSession rpResourceSession, Session rpSession) { if (rpSession.responseCode != 304 || CurrentMode != CacheMode.VerifyVersion) return; LoadFile(rpResourceSession.CacheFilename, rpResourceSession, rpSession); rpResourceSession.State = NetworkSessionState.LoadedFromCache; }
static void FiddlerApplication_BeforeRequest(Session rpSession) { if (r_BlockingList.Any(rpSession.uriContains)) { rpSession.utilCreateResponseAndBypassServer(); return; } var rUpstreamProxyPreference = Preference.Instance.Network.UpstreamProxy; if (rUpstreamProxyPreference.Enabled && (!rUpstreamProxyPreference.HttpOnly || !rpSession.RequestMethod.OICEquals("CONNECT"))) { rpSession["x-OverrideGateway"] = $"{rUpstreamProxyPreference.Host.Value}:{rUpstreamProxyPreference.Port.Value}"; } var rRequest = rpSession.oRequest; var rFullUrl = rpSession.fullUrl; var rPath = rpSession.PathAndQuery; NetworkSession rSession; if (rPath.StartsWith("/kcsapi/")) { rSession = new ApiSession(rFullUrl); } else if (rPath.StartsWith("/kcs/") || rPath.StartsWith("/gadget/")) { rSession = new ResourceSession(rFullUrl, rPath); } else { rSession = new NetworkSession(rFullUrl); } rSession.RequestBodyString = Uri.UnescapeDataString(rpSession.GetRequestBodyAsString()); rSession.Method = rpSession.RequestMethod; rpSession.Tag = rSession; if (rFullUrl == GameConstants.GamePageUrl || rPath == "/gadget/js/kcs_flash.js") { rpSession.bBufferResponse = true; } var rResourceSession = rSession as ResourceSession; if (rResourceSession != null) { CacheService.Instance.ProcessRequest(rResourceSession, rpSession); } rSession.RequestHeaders = rpSession.RequestHeaders.Select(r => new SessionHeader(r.Name, r.Value)).ToArray(); SessionSubject.OnNext(rSession); }
internal void ProcessRequest(ResourceSession rpResourceSession, Session rpSession) { if (CurrentMode == CacheMode.Disabled) return; string rFilename; var rNoVerification = CheckFileinCache(rpSession, out rFilename); rpResourceSession.CacheFilename = rFilename; if (rNoVerification == null) return; if (rNoVerification.Value) LoadFile(rFilename, rpResourceSession, rpSession); else { rpSession.oRequest["If-Modified-Since"] = File.GetLastWriteTime(rFilename).ToString("R"); rpSession.bBufferResponse = true; } }
void RecordCachedFile(ResourceSession rpResourceSession, DateTime rpTimestamp, bool rpReplace) { using (var rCommand = r_Connection.CreateCommand()) { if (rpReplace) rCommand.CommandText = "REPLACE INTO cache.file(name, version, timestamp) VALUES(@name, @version, @timestamp);"; else rCommand.CommandText = "INSERT OR IGNORE INTO cache.file(name, version, timestamp) VALUES(@name, @version, @timestamp);"; rCommand.Parameters.AddWithValue("@name", rpResourceSession.Path); rCommand.Parameters.AddWithValue("@version", rpResourceSession.CacheVersion); rCommand.Parameters.AddWithValue("@timestamp", new DateTimeOffset(rpTimestamp).ToUnixTime()); rCommand.ExecuteNonQuery(); } }
internal void ProcessOnCompletion(ResourceSession rpResourceSession, Session rpSession) { if (rpSession.responseCode != 200 || rpResourceSession.State == NetworkSessionState.LoadedFromCache || rpResourceSession.CacheFilename == null || Convert.ToInt32(rpSession.ResponseHeaders["Content-Length"]) != rpSession.ResponseBody.Length) return; try { lock (r_ThreadSyncObject) { var rLastModified = rpSession.oResponse["Last-Modified"]; if (rLastModified.IsNullOrEmpty()) return; var rDirectoryName = Path.GetDirectoryName(rpResourceSession.CacheFilename); if (!Directory.Exists(rDirectoryName)) Directory.CreateDirectory(rDirectoryName); var rFile = new FileInfo(rpResourceSession.CacheFilename); if (rFile.Exists) rFile.Delete(); rpSession.SaveResponseBody(rFile.FullName); var rTimestamp = Convert.ToDateTime(rLastModified); rFile.LastWriteTime = rTimestamp; rpResourceSession.State = NetworkSessionState.Cached; RecordCachedFile(rpResourceSession, rTimestamp, true); } } catch (Exception e) { Logger.Write(LoggingLevel.Error, string.Format(StringResources.Instance.Main.Log_Exception_Cache_FailedToSaveFile, e.Message)); } }
internal void ProcessResponse(ResourceSession rpResourceSession, Session rpSession) { if (rpSession.responseCode != 304 || !rpResourceSession.Path.OICContains("mainD2.swf") && CurrentMode != CacheMode.VerifyVersion) return; LoadFile(rpResourceSession.CacheFilename, rpResourceSession, rpSession); RecordCachedFile(rpResourceSession, File.GetLastWriteTime(rpResourceSession.CacheFilename), false); rpResourceSession.State = NetworkSessionState.LoadedFromCache; }
bool CheckFileVersionAndTimestamp(ResourceSession rpResourceSession, DateTimeOffset rpTimestamp) { using (var rCommand = r_Connection.CreateCommand()) { rCommand.CommandText = "SELECT (CASE WHEN version IS NOT NULL THEN version ELSE '' END) = @version AND timestamp = @timestamp FROM cache.file WHERE name = @name;"; rCommand.Parameters.AddWithValue("@name", rpResourceSession.Path); rCommand.Parameters.AddWithValue("@version", rpResourceSession.CacheVersion ?? string.Empty); rCommand.Parameters.AddWithValue("@timestamp", rpTimestamp.ToUnixTime()); return Convert.ToBoolean(rCommand.ExecuteScalar()); } }
internal void ProcessOnCompletion(ResourceSession rpResourceSession, Session rpSession) { if (rpSession.responseCode != 200 || rpResourceSession.CacheFilename == null) return; try { var rDirectoryName = Path.GetDirectoryName(rpResourceSession.CacheFilename); if (!Directory.Exists(rDirectoryName)) Directory.CreateDirectory(rDirectoryName); var rFile = new FileInfo(rpResourceSession.CacheFilename); if (rFile.Exists) rFile.Delete(); rpSession.SaveResponseBody(rFile.FullName); rFile.LastWriteTime = Convert.ToDateTime(rpSession.oResponse["Last-Modified"]); rpResourceSession.State = NetworkSessionState.Cached; } catch (Exception e) { Logger.Write(LoggingLevel.Error, string.Format(StringResources.Instance.Main.Log_Exception_Cache_FailedToSaveFile, e.Message)); } }