예제 #1
0
        private void FiddlerApplication_BeforeRequest(Fiddler.Session oSession)
        {
            Utility.Configuration.ConfigurationData.ConfigConnection c = Utility.Configuration.Config.Connection;


            // 上流プロキシ設定
            if (c.UseUpstreamProxy)
            {
                oSession["X-OverrideGateway"] = string.Format("localhost:{0}", c.UpstreamProxyPort);
            }


            if (oSession.fullUrl.Contains("/kcsapi/"))
            {
                string url  = oSession.fullUrl;
                string body = oSession.GetRequestBodyAsString();

                //保存
                {
                    if (c.SaveReceivedData && c.SaveRequest)
                    {
                        Task.Run((Action)(() => {
                            SaveRequest(url, body);
                        }));
                    }
                }

                UIControl.BeginInvoke((Action)(() => { LoadRequest(url, body); }));
            }
        }
예제 #2
0
        /// <summary>
        /// 通信の受信を開始します。
        /// </summary>
        /// <param name="portID">受信に使用するポート番号。</param>
        /// <param name="UIControl">GUI スレッドで実行するためのオブジェクト。中身は何でもいい</param>
        /// <returns>実際に使用されるポート番号。</returns>
        public int Start(int portID, Control UIControl)
        {
            StopFiddler();
            StartFiddler();

            Utility.Configuration.ConfigurationData.ConfigConnection c = Utility.Configuration.Config.Connection;


            this.UIControl = UIControl;


            HttpProxy.Shutdown();
            try
            {
                HttpProxy.UpstreamProxyConfig = new ProxyConfig(ProxyConfigType.SpecificProxy, "127.0.0.1", FiddlerProxyPort);

                HttpProxy.Startup(portID, false, false);
                ProxyPort = portID;


                ProxyStarted();

                Utility.Logger.Add(2, string.Format("APIObserver: ポート {0} 番で受信を開始しました。", portID));
            }
            catch (Exception ex)
            {
                Utility.Logger.Add(3, "APIObserver: 受信開始に失敗しました。" + ex.Message);
                ProxyPort = 0;
            }


            return(ProxyPort);
        }
예제 #3
0
        /// <summary>
        /// 通信の受信を開始します。
        /// </summary>
        /// <param name="portID">受信に使用するポート番号。</param>
        /// <param name="UIControl">GUI スレッドで実行するためのオブジェクト。中身は何でもいい</param>
        /// <returns>実際に使用されるポート番号。</returns>
        public int Start(int portID, Control UIControl)
        {
            Utility.Configuration.ConfigurationData.ConfigConnection c = Utility.Configuration.Config.Connection;


            this.UIControl = UIControl;

            try {
                //if ( c.UseUpstreamProxy )
                //	HttpProxy.UpstreamProxyConfig = new ProxyConfig( ProxyConfigType.SpecificProxy, c.UpstreamProxyAddress, c.UpstreamProxyPort );
                //else if ( c.UseSystemProxy )
                //	HttpProxy.UpstreamProxyConfig = new ProxyConfig( ProxyConfigType.SystemProxy );
                //else
                //	HttpProxy.UpstreamProxyConfig = new ProxyConfig( ProxyConfigType.DirectAccess );

                //HttpProxy.Startup( portID, false, false );
                var endPoint = new ExplicitProxyEndPoint(System.Net.IPAddress.Any, portID, false);
                ProxyPort = portID;
                ProxyServer.AddEndPoint(endPoint);
                ProxyServer.Start();

                ProxyStarted();
                Utility.Logger.Add(2, string.Format(LoggerRes.APIObserverStarted, portID));
            } catch (Exception ex) {
                Utility.Logger.Add(3, LoggerRes.APIObserverFailed + ex.Message);
                ProxyPort = 0;
            }


            return(ProxyPort);
        }
예제 #4
0
        private void ProxyServer_BeforeRequest(object sender, Titanium.Web.Proxy.EventArguments.SessionEventArgs e)
        {
            var    request = e.ProxySession.Request;
            string path    = request.RequestUri.PathAndQuery;

            Utility.Configuration.ConfigurationData.ConfigConnection c = Utility.Configuration.Config.Connection;

            if (path.Contains("/kcsapi/"))
            {
                string body = e.GetRequestBodyAsString();

                //保存
                if (c.SaveReceivedData && c.SaveRequest)
                {
                    Task.Run((Action)(() => {
                        SaveRequest(path, body);
                    }));
                }
                UIControl.BeginInvoke((Action)(() => { LoadRequest(path, body); }));
            }
            if (path.Contains(".mp3"))
            {
                Logger.Add(1, $"Requesting audio: {path}");
                string[] substrings = path.Split('/');
                switch (substrings[3])
                {
                case "titlecall":
                    DialogueTranslator.Add(DialogueType.Titlecall, substrings[4], substrings[5].Split('.')[0]);
                    break;

                case "kc9999":
                    DialogueTranslator.Add(DialogueType.NPC, "npc", substrings[4].Split('.')[0]);
                    break;

                default:
                    DialogueTranslator.Add(DialogueType.Shipgirl, substrings[3].Substring(2), substrings[4].Split('.')[0]);
                    break;
                }
                if (request.RequestHeaders.Where(h => h.Name == "If-Modified-Since").Count() > 0)
                {
                    if (cacheControl.ContainsKey(path))
                    {
                        if (cacheControl[path].Item2 < DateTime.Now)
                        {
                            return;
                        }
                        else
                        {
                            e.NotModified(cacheControl[path].Item1);
                        }
                    }
                }
            }
        }
예제 #5
0
        /// <summary>
        /// 通信の受信を開始します。
        /// </summary>
        /// <param name="portID">受信に使用するポート番号。</param>
        /// <param name="UIControl">GUI スレッドで実行するためのオブジェクト。中身は何でもいい</param>
        /// <returns>実際に使用されるポート番号。</returns>
        public int Start(int portID, Control UIControl)
        {
            Utility.Configuration.ConfigurationData.ConfigConnection c = Utility.Configuration.Config.Connection;


            this.UIControl = UIControl;


            HttpProxy.Shutdown();
            try
            {
                if (c.UseUpstreamProxy)
                {
                    HttpProxy.UpstreamProxyConfig = new ProxyConfig(ProxyConfigType.SpecificProxy, c.UpstreamProxyAddress, c.UpstreamProxyPort);
                }
                else if (c.UseSystemProxy)
                {
                    HttpProxy.UpstreamProxyConfig = new ProxyConfig(ProxyConfigType.SystemProxy);
                }
                else
                {
                    HttpProxy.UpstreamProxyConfig = new ProxyConfig(ProxyConfigType.DirectAccess);
                }

                HttpProxy.Startup(portID, false, false);
                ProxyPort = portID;


                ProxyStarted();

                Utility.Logger.Add(2, string.Format("APIObserver: ポート {0} 番で受信を開始しました。", portID));
            }
            catch (Exception ex)
            {
                Utility.Logger.Add(3, "APIObserver: 受信開始に失敗しました。" + ex.Message);
                ProxyPort = 0;
            }


            return(ProxyPort);
        }
예제 #6
0
        private void FiddlerApplication_BeforeRequest(Fiddler.Session oSession)
        {
            Utility.Configuration.ConfigurationData.ConfigConnection c = Utility.Configuration.Config.Connection;


            // 上流プロキシ設定
            if (c.UseUpstreamProxy)
            {
                oSession["X-OverrideGateway"] = string.Format("{0}:{1}", c.UpstreamProxyAddress, c.UpstreamProxyPort);
            }


            if (oSession.fullUrl.Contains("/kcsapi/"))
            {
                string url  = oSession.fullUrl;
                string body = oSession.GetRequestBodyAsString();

                //保存
                {
                    if (c.SaveReceivedData && c.SaveRequest)
                    {
                        Task.Run((Action)(() => {
                            SaveRequest(url, body);
                        }));
                    }
                }

                UIControl.BeginInvoke((Action)(() => { LoadRequest(url, body); }));
            }

            // flash wmode & quality
            {
                if (oSession.fullUrl.Contains("/gadget/js/kcs_flash.js"))
                {
                    oSession.bBufferResponse = true;
                }
            }
        }
예제 #7
0
        private void FiddlerApplication_BeforeRequest(Fiddler.Session oSession)
        {
            Utility.Configuration.ConfigurationData.ConfigConnection c = Utility.Configuration.Config.Connection;

            if (c.UseUpstreamProxy)
            {
                oSession["X-OverrideGateway"] = string.Format("{0}:{1}", c.UpstreamProxyAddress, c.UpstreamProxyPort);
            }

            ObserverResult(p =>
            {
                try
                {
                    return(p.OnBeforeRequest(oSession));
                }
                catch (Exception oe)
                {
                    Logger.Add(3, string.Format("插件 {0}({1}) 执行 OnBeforeRequest 时出错!", p.MenuTitle, p.Version));
                    ErrorReporter.SendErrorReport(oe, p.MenuTitle);
                    return(false);
                }
            });
        }
예제 #8
0
        private void FiddlerApplication_AfterSessionComplete(Fiddler.Session oSession)
        {
            //保存
            {
                Utility.Configuration.ConfigurationData.ConfigConnection c = Utility.Configuration.Config.Connection;

                if (c.SaveReceivedData)
                {
                    try {
                        if (c.SaveResponse && oSession.fullUrl.Contains("/kcsapi/"))
                        {
                            // 非同期で書き出し処理するので取っておく
                            // stringはイミュータブルなのでOK
                            string url  = oSession.fullUrl;
                            string body = oSession.GetResponseBodyAsString();

                            Task.Run((Action)(() => {
                                SaveResponse(url, body);
                            }));
                        }
                        else if (oSession.fullUrl.Contains("/kcs/") &&
                                 ((c.SaveSWF && oSession.oResponse.MIMEType == "application/x-shockwave-flash") || c.SaveOtherFile))
                        {
                            string saveDataPath = c.SaveDataPath;                             // スレッド間の競合を避けるため取っておく
                            string tpath        = string.Format("{0}\\{1}", saveDataPath, oSession.fullUrl.Substring(oSession.fullUrl.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 = new byte[oSession.ResponseBody.Length];
                            Array.Copy(oSession.ResponseBody, responseCopy, oSession.ResponseBody.Length);

                            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("通信からファイル {0} を保存しました。", tpath.Remove(0, saveDataPath.Length + 1)));
                                } catch (IOException ex) {                                      //ファイルがロックされている; 頻繁に出るのでエラーレポートを残さない
                                    Utility.Logger.Add(3, "通信内容の保存に失敗しました。 " + ex.Message);
                                }
                            }));
                        }
                    } catch (Exception ex) {
                        Utility.ErrorReporter.SendErrorReport(ex, "通信内容の保存に失敗しました。");
                    }
                }
            }


            if (oSession.fullUrl.Contains("/kcsapi/") && oSession.oResponse.MIMEType == "text/plain")
            {
                // 非同期でGUIスレッドに渡すので取っておく
                // stringはイミュータブルなのでOK
                string url  = oSession.fullUrl;
                string body = oSession.GetResponseBodyAsString();
                UIControl.BeginInvoke((Action)(() => { LoadResponse(url, body); }));
            }



            if (ServerAddress == null)
            {
                string url = oSession.fullUrl;

                int idxb = url.IndexOf("/kcsapi/");

                if (idxb != -1)
                {
                    int idxa = url.LastIndexOf("/", idxb - 1);

                    ServerAddress = url.Substring(idxa + 1, idxb - idxa - 1);
                }
            }
        }
예제 #9
0
        void HttpProxy_AfterSessionComplete(Session session)
        {
            Utility.Configuration.ConfigurationData.ConfigConnection c = Utility.Configuration.Config.Connection;

            string baseurl = session.Request.PathAndQuery;

            //debug
            //Utility.Logger.Add( 1, baseurl );


            // request
            if (baseurl.Contains("/kcsapi/"))
            {
                string url  = baseurl;
                string body = session.Request.BodyAsString;

                //保存
                if (c.SaveReceivedData && c.SaveRequest)
                {
                    Task.Run((Action)(() => {
                        SaveRequest(url, body);
                    }));
                }


                UIControl.BeginInvoke((Action)(() => { LoadRequest(url, body); }));
            }



            //response
            //保存

            if (c.SaveReceivedData)
            {
                try {
                    if (!Directory.Exists(c.SaveDataPath))
                    {
                        Directory.CreateDirectory(c.SaveDataPath);
                    }


                    if (c.SaveResponse && baseurl.Contains("/kcsapi/"))
                    {
                        // 非同期で書き出し処理するので取っておく
                        // stringはイミュータブルなのでOK
                        string url  = baseurl;
                        string body = session.Response.BodyAsString;

                        Task.Run((Action)(() => {
                            SaveResponse(url, body);
                        }));
                    }
                    else if (baseurl.Contains("/kcs/") &&
                             ((c.SaveSWF && session.Response.MimeType == "application/x-shockwave-flash") || c.SaveOtherFile))
                    {
                        string saveDataPath = c.SaveDataPath;                         // スレッド間の競合を避けるため取っておく
                        string tpath        = string.Format("{0}\\{1}", saveDataPath, baseurl.Substring(baseurl.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 = new byte[session.Response.Body.Length];
                        Array.Copy(session.Response.Body, responseCopy, session.Response.Body.Length);

                        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("通信からファイル {0} を保存しました。", tpath.Remove(0, saveDataPath.Length + 1)));
                            } catch (IOException ex) {                                  //ファイルがロックされている; 頻繁に出るのでエラーレポートを残さない
                                Utility.Logger.Add(3, "通信内容の保存に失敗しました。 " + ex.Message);
                            }
                        }));
                    }
                } catch (Exception ex) {
                    Utility.ErrorReporter.SendErrorReport(ex, "通信内容の保存に失敗しました。");
                }
            }



            if (baseurl.Contains("/kcsapi/") && session.Response.MimeType == "text/plain")
            {
                // 非同期でGUIスレッドに渡すので取っておく
                // stringはイミュータブルなのでOK
                string url  = baseurl;
                string body = session.Response.BodyAsString;
                UIControl.BeginInvoke((Action)(() => { LoadResponse(url, body); }));

                // kancolle-db.netに送信する
                if (Utility.Configuration.Config.Connection.SendDataToKancolleDB)
                {
                    Task.Run((Action)(() => DBSender.ExecuteSession(session)));
                }
            }


            if (ServerAddress == null && baseurl.Contains("/kcsapi/"))
            {
                ServerAddress = session.Request.Headers.Host;
            }
        }
예제 #10
0
        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;
            }
        }