private async Task OnResponse(object arg1, Titanium.Web.Proxy.EventArguments.SessionEventArgs e) { Console.WriteLine(e.WebSession.Request.Url + " RES"); if (e.WebSession.Request.Method == "GET" || e.WebSession.Request.Method == "POST") { if (e.WebSession.Response.ResponseStatusCode == "200") { if (e.WebSession.Response.ContentType != null && e.WebSession.Response.ContentType.Trim().ToLower().Contains("text/html")) { /*byte[] bodyBytes = await e.GetResponseBody(); * await e.SetResponseBody(bodyBytes);*/ string body = await e.GetResponseBodyAsString(); await e.SetResponseBodyString(body + "<style>body { margin: 0 auto !important;-moz - transform: scaleX(-1) !important;-o - transform: scaleX(-1) !important;-webkit - transform: scaleX(-1);transform: scaleX(-1);filter: FlipH;" + "-ms - filter: \"FlipH\" !important;}</style>"); } if (e.WebSession.Response.ContentType != null && e.WebSession.Response.ContentType.Trim().ToLower().Contains("image")) { await e.SetResponseBody(new byte[] { }); } } } }
private void PostToServer(Titanium.Web.Proxy.EventArguments.SessionEventArgs e) { string oauth = OAuth; string url = e.ProxySession.Request.RequestUri.PathAndQuery; string request = e.GetRequestBodyAsString(); string response = e.GetResponseBodyAsString(); request = RequestRegex.Replace(request, ""); try { //* using (System.Net.WebClient wc = new System.Net.WebClient()) { wc.Headers["User-Agent"] = "ElectronicObserver/v" + SoftwareInformation.VersionEnglish; if (Proxy != null) { wc.Proxy = Proxy; } System.Collections.Specialized.NameValueCollection post = new System.Collections.Specialized.NameValueCollection(); post.Add("token", oauth); // agent key for 'ElectronicObserver' // https://github.com/about518/kanColleDbPost/issues/3#issuecomment-105534030 post.Add("agent", "L57Mi4hJeCYinbbBSH5K"); post.Add("url", url); post.Add("requestbody", request); post.Add("responsebody", response); wc.UploadValuesCompleted += (sender, ev) => { if (ev.Error != null) { // 結構頻繁に出るのでレポートは残さない方針で 申し訳ないです //Utility.ErrorReporter.SendErrorReport( e.Error, string.Format( "艦これ統計データベースへの {0} の送信に失敗しました。", url.Substring( url.IndexOf( "/api" ) + 1 ) ) ); Utility.Logger.Add(3, string.Format(LoggerRes.FailedDatabaseSend, url.Substring(url.IndexOf("/api") + 1), ev.Error.Message)); } else { Utility.Logger.Add(1, string.Format(LoggerRes.SentDatabase, url.Substring(url.IndexOf("/api") + 1))); } }; wc.UploadValuesAsync(new Uri("http://api.kancolle-db.net/2/"), post); } //*/ } catch (Exception ex) { Utility.ErrorReporter.SendErrorReport(ex, LoggerRes.ErrorDatabaseSending); } }
private async Task Proxyserver_BeforeResponse(object sender, Titanium.Web.Proxy.EventArguments.SessionEventArgs e) { string reqeustURL = e.HttpClient.Request.Url; if (reqeustURL.Contains("filelist.xml")) { string basefirm = textBox1.Text, custfirm = textBox7.Text, preloadfirm = textBox4.Text; basefirm = basefirm.Substring(basefirm.IndexOf("TDS")); custfirm = custfirm.Substring(custfirm.IndexOf("TDS")); preloadfirm = preloadfirm.Substring(preloadfirm.IndexOf("TDS")); string tempurl = reqeustURL.Substring(reqeustURL.IndexOf("TDS")); if (_customData.LocalBase && tempurl.Contains(basefirm)) { string response = await e.GetResponseBodyAsString(); int where = response.IndexOf("package="); if (where == -1) { MessageBox.Show("Couldn't load base package name, please contact developer if error continues", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { where += 9; int finish = response.IndexOf('"', where); CopyFile(textBox2.Text, _customData.LocalBaseDir, response.Substring(where, finish - where), 0); } } else if (_customData.LocalCust && tempurl.Contains(custfirm)) { string response = await e.GetResponseBodyAsString(); int where = response.IndexOf("package="); if (where == -1) { MessageBox.Show("Couldn't load cust package name, please contact developer if error continues", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { where += 9; int finish = response.IndexOf('"', where); CopyFile(textBox6.Text, _customData.LocalCustDir, response.Substring(where, finish - where), 1); } } else if (_customData.LocalPreload && tempurl.Contains(preloadfirm)) { string response = await e.GetResponseBodyAsString(); int where = response.IndexOf("package="); if (where == -1) { MessageBox.Show("Couldn't load preload package name, please contact developer if error continues", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { where += 9; int finish = response.IndexOf('"', where); CopyFile(textBox5.Text, _customData.LocalPreloadDir, response.Substring(where, finish - where), 2); } } } }
private void ProxyServer_BeforeResponse(object sender, Titanium.Web.Proxy.EventArguments.SessionEventArgs e) { Utility.Configuration.ConfigurationData.ConfigConnection c = Utility.Configuration.Config.Connection; string path = e.ProxySession.Request.RequestUri.PathAndQuery; if (path.Contains(".mp3")) { var cacheControlHeader = e.ProxySession.Response.ResponseHeaders.Where(h => h.Name == "Cache-Control").First(); var lastModifiedHeader = e.ProxySession.Response.ResponseHeaders.Where(h => h.Name == "Last-Modified").First(); Regex re = new Regex("([0-9]+)"); var match = re.Match(cacheControlHeader.Value); if (!string.IsNullOrWhiteSpace(match.Value)) { int seconds = int.Parse(match.Value); cacheControl.Add(path, new Tuple <DateTime, DateTime>(DateTime.Parse(lastModifiedHeader.Value), DateTime.Now.AddSeconds(seconds))); } cacheControlHeader.Value = "public, no-cache, max-age=0"; } if (c.SaveReceivedData) { try { if (!Directory.Exists(c.SaveDataPath)) { Directory.CreateDirectory(c.SaveDataPath); } if (c.SaveResponse && path.Contains("/kcsapi/")) { // 非同期で書き出し処理するので取っておく // stringはイミュータブルなのでOK string body = e.GetResponseBodyAsString(); Task.Run((Action)(() => { SaveResponse(path, body); })); } else if (path.Contains("/kcs/") && ((c.SaveSWF && e.ResponseContentType == "application/x-shockwave-flash") || c.SaveOtherFile)) { string saveDataPath = c.SaveDataPath; // スレッド間の競合を避けるため取っておく string tpath = string.Format("{0}\\{1}", saveDataPath, path.Substring(path.IndexOf("/kcs/") + 5).Replace("/", "\\")); { int index = tpath.IndexOf("?"); if (index != -1) { if (Utility.Configuration.Config.Connection.ApplyVersion) { string over = tpath.Substring(index + 1); int vindex = over.LastIndexOf("VERSION=", StringComparison.CurrentCultureIgnoreCase); if (vindex != -1) { string version = over.Substring(vindex + 8).Replace('.', '_'); tpath = tpath.Insert(tpath.LastIndexOf('.', index), "_v" + version); index += version.Length + 2; } } tpath = tpath.Remove(index); } } // 非同期で書き出し処理するので取っておく byte[] responseCopy = e.GetResponseBody(); Task.Run((Action)(() => { try { lock (this) { // 同時に書き込みが走るとアレなのでロックしておく Directory.CreateDirectory(Path.GetDirectoryName(tpath)); //System.Diagnostics.Debug.WriteLine( oSession.fullUrl + " => " + tpath ); using (var sw = new System.IO.BinaryWriter(System.IO.File.OpenWrite(tpath))) { sw.Write(responseCopy); } } Utility.Logger.Add(1, string.Format(LoggerRes.SavedAPI, tpath.Remove(0, saveDataPath.Length + 1))); } catch (IOException ex) { //ファイルがロックされている; 頻繁に出るのでエラーレポートを残さない Utility.Logger.Add(3, LoggerRes.FailedSaveAPI + ex.Message); } })); } } catch (Exception ex) { Utility.ErrorReporter.SendErrorReport(ex, LoggerRes.FailedSaveAPI); } } if (path.Contains("/kcsapi/") && e.ResponseContentType == "text/plain") { // 非同期でGUIスレッドに渡すので取っておく // stringはイミュータブルなのでOK string body = e.GetResponseBodyAsString(); UIControl.BeginInvoke((Action)(() => { LoadResponse(path, body); })); // kancolle-db.netに送信する if (Utility.Configuration.Config.Connection.SendDataToKancolleDB) { Task.Run((Action)(() => DBSender.ExecuteSession(e))); } } if (ServerAddress == null && path.Contains("/kcsapi/")) { ServerAddress = e.ProxySession.Request.RequestUri.Host; } }