示例#1
0
 private void Application_Startup(object sender, StartupEventArgs e)
 {
     RemoteObject = null;
     if (File.Exists("./settings.xml"))
     {
         XmlSerializer xser = new XmlSerializer(typeof(AppSetting));
         using (StreamReader sr = new StreamReader("./settings.xml", System.Text.Encoding.UTF8))
         {
             try
             {
                 Setting = (AppSetting)xser.Deserialize(sr);
             }
             catch (InvalidOperationException) { }
         }
     }
     if (!Check.WaitOne(0, false) && !Setting.duplication)
     {
         using (Check)
         {
             dupli = true;
             if (Setting.ShowWindow)
             {
                 try
                 {
                     IpcClientChannel client = new IpcClientChannel();
                     ChannelServices.RegisterChannel(client, true);
                     RemoteObject = (SendObject)Activator.GetObject(typeof(SendObject), "ipc://2chApiProxyIPC/ShowWindow");
                     RemoteObject.SendMessage(true);
                 }
                 catch (RemotingException) { }
             }
             System.Windows.Application.Current.Shutdown();
             return;
         }
     }
     this.Exit += (esender, ee) =>
     {
         if (Check != null)
         {
             using (Check)
             {
                 try
                 {
                     Check.ReleaseMutex();
                 }
                 catch (ApplicationException)
                 {
                     Check.Close();
                 }
             }
         }
     };
 }
示例#2
0
        public void Init()
        {
            var fuctory = new ModelFuctory();

            //プロクシ処理用クラス初期化
            try
            {
                DatProxy = new DatProxy(Appkey, HMkey, UserAgent1, UserAgent0, UserAgent2, RouninID, RouninPW, ProxyAddress)
                {
                    APIMediator   = fuctory.CreateAPIMediator(),
                    HtmlConverter = fuctory.CreateHtmlConverter()
                };
            }
            catch (System.IO.FileLoadException)
            {
                using (TaskTrayIcon)
                {
                    System.Windows.MessageBox.Show("FiddlerCore4.dllのバージョンが違うか必要なdllが無いようです。zipファイルに同梱のすべてのdllを上書きコピーしてください。", "dll読み込みエラー");
                    Microsoft.Win32.SystemEvents.PowerModeChanged -= new Microsoft.Win32.PowerModeChangedEventHandler(PowermodeChanged);
                    System.Windows.Application.Current.Shutdown();
                    return;
                }
            }
            //書き込み板毎設定の読み込み
            if (File.Exists("./BoardSettings.yaml"))
            {
                using (var stream = File.OpenText("./BoardSettings.yaml"))
                {
                    try
                    {
                        var deserializer = new YamlDotNet.Serialization.Deserializer();
                        DatProxy.BoardSettings = deserializer.Deserialize <Dictionary <string, BoardSettings> >(stream);
                    }
                    catch (Exception err)
                    {
                        this.SystemLog = "YAMLファイルの書式が間違っているようです。\n" + err.ToString();
                    }
                }
            }
            DatProxy.BoardSettings ??= new Dictionary <string, BoardSettings>();
            this.SystemLog = $"{DatProxy.BoardSettings.Count()}板分の設定を読み込みました。";
            if (DatProxy.BoardSettings.ContainsKey("2chapiproxy_default") == false)
            {
                // ファイルが無いかデフォルト設定が無い時、JaneStyleの設定を使用
                var def = new BoardSettings {
                    UserAgent = "Monazilla/1.00 JaneStyle/4.00 Windows/10.0.19041", SetOekaki = false, KeepAlive = false
                };
                def.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                def.Headers.Add("Accept-Encoding", "gzip, identity");
                def.Headers.Add("ContentType", "application/x-www-form-urlencoded");
                DatProxy.BoardSettings.Add("2chapiproxy_default", def);
                this.SystemLog = "書き込みのデフォルト設定としてJaneStyleのものを使用します";
            }

            //外部コードのコンパイル
            if (CEExternalRead)
            {
                CEResultView = DatProxy.HtmlConverter.Compile(CESrcfilePath);
            }

            //設定の適用、プロクシクラス
            DatProxy.AllowWANAccese  = WANAccess;
            DatProxy.user            = WANID;
            DatProxy.pw              = WANPW;
            DatProxy.WriteUA         = UserAgent3;
            DatProxy.GetHTML         = KakotoHTML;
            DatProxy.OfflawRokkaPerm = OfflawRokkaPermutation;
            DatProxy.CangeUARetry    = ChangeUARetry;
            DatProxy.SocksPoxy       = Socks4aProxy;
            DatProxy.gZipRes         = gZipResponse;
            DatProxy.ChunkRes        = ChunkedResponse;
            DatProxy.OnlyORPerm      = OnlyORPerm;
            DatProxy.CRReplace       = CRReplace;
            DatProxy.KakolinkPerm    = KakolinkPermutation;
            DatProxy.AllUAReplace    = (UserAgent3 == "") ? (false) : (AllUAReplace);
            DatProxy.BeLogin         = BeLogin;
            DatProxy.SetReferrer     = SetReferrer;

            //設定の適用、APIアクセスクラス
            DatProxy.APIMediator.AppKey       = this.Appkey;
            DatProxy.APIMediator.HMKey        = this.HMkey;
            DatProxy.APIMediator.SidUA        = this.UserAgent0;
            DatProxy.APIMediator.X2chUA       = this.UserAgent1;
            DatProxy.APIMediator.DatUA        = this.UserAgent2;
            DatProxy.APIMediator.RouninID     = this.RouninID;
            DatProxy.APIMediator.RouninPW     = this.RouninPW;
            DatProxy.APIMediator.ProxyAddress = this.ProxyAddress;
            DatProxy.UpdateAsync();

            //設定の適用、html変換クラス
            DatProxy.HtmlConverter.UserAgent              = _UserAgent4;
            DatProxy.HtmlConverter.ProxyAddress           = _ProxyAddress;
            DatProxy.HtmlConverter.IsDifferenceDetect     = !_AllReturn;
            DatProxy.HtmlConverter.IsAliveCheckSkip       = _SkipAliveCheck;
            DatProxy.HtmlConverter.Is5chURIReplace        = _Replace5chURI;
            DatProxy.HtmlConverter.IsHttpsReplace         = _ReplaceHttpsLink;
            DatProxy.HtmlConverter.IsExternalConverterUse = _CEExternalRead;

            //エラー通知用コールバック登録
            DatProxy.APIMediator.PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName == nameof(DatProxy.APIMediator.CurrentError))
                {
                    //ここで取得しておく
                    string error = DatProxy.APIMediator.CurrentError;

                    App.Current.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        this.SystemLog = error;
                    }));
                }
            };

            DatProxy.HtmlConverter.PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName == nameof(DatProxy.HtmlConverter.CurrentError))
                {
                    //ここで取得しておく
                    string error = DatProxy.HtmlConverter.CurrentError;

                    App.Current.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        this.SystemLog = error;
                    }));
                }
            };

            //自動開始処理
            if (Setting.AutoStart)
            {
                int pnum = (Setting.AutoSelect) ? (0) : (Setting.PortNumber);
                pnum = DatProxy.Start(pnum);
                if (pnum != 0)
                {
                    SystemLog  = "開始、ポート番号:" + pnum;
                    NowStart   = true;
                    PortNumber = pnum;
                }
                else
                {
                    SystemLog = "既に使用中のポートみたいです、別のポートを指定して下さい。";
                    DatProxy.End();
                }
            }
            //同時起動と終了を設定
            if (!String.IsNullOrEmpty(Setting.SenburaPath))
            {
                bool kage = true;
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = Setting.SenburaPath;
                if (System.IO.Path.GetFileName(Setting.SenburaPath) != "kage.exe")
                {
                    kage = false;
                    p.EnableRaisingEvents = true;
                    p.Exited += (sender, e) =>
                    {
                        App.Current.Dispatcher.BeginInvoke((Action)(() => { if (Setting.SyncEnd)
                                                                            {
                                                                                BeforeShutdown();
                                                                            }
                                                                    }), null);
                    };
                }
                try
                {
                    p.Start();
                    m_SenburaPID = p.Id;
                    SystemLog    = Setting.SenburaPath + " を起動";
                    m_SyncStart  = true;
                    //かちゅ~しゃの場合の終了同期処理の追加
                    if (kage)
                    {
                        System.Diagnostics.Process[] plist = null;
                        for (int i = 0; i < 50; ++i)
                        {
                            plist = System.Diagnostics.Process.GetProcessesByName("Katjusha");
                            if (plist.Length != 0)
                            {
                                break;
                            }
                            System.Threading.Thread.Sleep(100);
                        }
                        plist[0].EnableRaisingEvents = true;
                        plist[0].Exited += (sender, e) =>
                        {
                            App.Current.Dispatcher.BeginInvoke((Action)(() => { if (Setting.SyncEnd)
                                                                                {
                                                                                    BeforeShutdown();
                                                                                }
                                                                        }), null);
                        };
                        m_SenburaPID = plist[0].Id;
                    }
                }
                catch (Exception err)
                {
                    SystemLog = Setting.SenburaPath + " の起動に失敗\n" + err.ToString();
                }
            }
            Setting.change = false;
            //プロセス間通信のサーバ登録
            try
            {
                IpcServerChannel server = new IpcServerChannel("2chApiProxyIPC");
                ChannelServices.RegisterChannel(server, true);
                RemoteObject         = new SendObject();
                SendObject.OnTrance += new SendObject.CallEventHandler((e) =>
                {
                    if (RemoteObject.first)
                    {
                        RemoteObject.first = false;
                        App.Current.Dispatcher.BeginInvoke((Action)(() => { if (e)
                                                                            {
                                                                                ActivateWindow();
                                                                            }
                                                                    }));
                    }
                    else
                    {
                        RemoteObject.first = true;
                    }
                });
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(SendObject), "ShowWindow", WellKnownObjectMode.Singleton);
            }
            catch (Exception) { }
            SystemLog = "2chAPIProxy起動";
        }
示例#3
0
        public void Init()
        {
            var fuctory = new ModelFuctory();

            //プロクシ処理用クラス初期化
            try
            {
                DatProxy = new DatProxy(Appkey, HMkey, UserAgent1, UserAgent0, UserAgent2, RouninID, RouninPW, ProxyAddress)
                {
                    APIMediator   = fuctory.CreateAPIMediator(),
                    HtmlConverter = fuctory.CreateHtmlConverter()
                };
            }
            catch (System.IO.FileLoadException)
            {
                using (TaskTrayIcon)
                {
                    System.Windows.MessageBox.Show("FiddlerCore4.dllのバージョンが違うか必要なdllが無いようです。zipファイルに同梱のすべてのdllを上書きコピーしてください。", "dll読み込みエラー");
                    Microsoft.Win32.SystemEvents.PowerModeChanged -= new Microsoft.Win32.PowerModeChangedEventHandler(PowermodeChanged);
                    System.Windows.Application.Current.Shutdown();
                    return;
                }
            }
            //書き込みヘッダ定義ファイルの読み込み
            Dictionary <String, String> header = new Dictionary <string, string>();

            if (File.Exists("./WriteRequestHeader.txt"))
            {
                using (StreamReader sr = new StreamReader("./WriteRequestHeader.txt", System.Text.Encoding.UTF8))
                {
                    while (sr.EndOfStream == false)
                    {
                        try
                        {
                            string line = sr.ReadLine();
                            if (line.IndexOf("//") == 0 || string.IsNullOrEmpty(line) == true)
                            {
                                continue;
                            }
                            var pair = line.Split(':');
                            if (pair.Length == 0)
                            {
                                continue;
                            }
                            if (pair.Length == 1)
                            {
                                header.Add(pair[0], "");
                            }
                            else
                            {
                                header.Add(pair[0], pair[1]);
                            }
                        }
                        catch (Exception err)
                        {
                            System.Diagnostics.Debug.WriteLine("●ヘッダ定義の読み込み中のエラー\n" + err.ToString());
                        }
                    }
                }
            }
            DatProxy.WriteHeader = header;
            if (header.Count == 0)
            {
                this.SystemLog = "書き込み時には内部定義ヘッダを使用します";
            }
            else
            {
                this.SystemLog = "書き込み時には外部定義ヘッダを使用します";
            }

            //外部コードのコンパイル
            if (CEExternalRead)
            {
                CEResultView = DatProxy.HtmlConverter.Compile(CESrcfilePath);
            }

            //設定の適用、プロクシクラス
            DatProxy.AllowWANAccese  = WANAccess;
            DatProxy.user            = WANID;
            DatProxy.pw              = WANPW;
            DatProxy.WriteUA         = UserAgent3;
            DatProxy.GetHTML         = KakotoHTML;
            DatProxy.OfflawRokkaPerm = OfflawRokkaPermutation;
            DatProxy.CangeUARetry    = ChangeUARetry;
            DatProxy.SocksPoxy       = Socks4aProxy;
            DatProxy.gZipRes         = gZipResponse;
            DatProxy.ChunkRes        = ChunkedResponse;
            DatProxy.OnlyORPerm      = OnlyORPerm;
            DatProxy.CRReplace       = CRReplace;
            DatProxy.KakolinkPerm    = KakolinkPermutation;
            DatProxy.AllUAReplace    = (UserAgent3 == "") ? (false) : (AllUAReplace);
            DatProxy.BeLogin         = BeLogin;

            //設定の適用、APIアクセスクラス
            DatProxy.APIMediator.AppKey       = this.Appkey;
            DatProxy.APIMediator.HMKey        = this.HMkey;
            DatProxy.APIMediator.SidUA        = this.UserAgent0;
            DatProxy.APIMediator.X2chUA       = this.UserAgent1;
            DatProxy.APIMediator.DatUA        = this.UserAgent2;
            DatProxy.APIMediator.RouninID     = this.RouninID;
            DatProxy.APIMediator.RouninPW     = this.RouninPW;
            DatProxy.APIMediator.ProxyAddress = this.ProxyAddress;
            DatProxy.UpdateAsync();

            //設定の適用、html変換クラス
            DatProxy.HtmlConverter.UserAgent              = _UserAgent4;
            DatProxy.HtmlConverter.ProxyAddress           = _ProxyAddress;
            DatProxy.HtmlConverter.IsDifferenceDetect     = !_AllReturn;
            DatProxy.HtmlConverter.IsAliveCheckSkip       = _SkipAliveCheck;
            DatProxy.HtmlConverter.Is5chURIReplace        = _Replace5chURI;
            DatProxy.HtmlConverter.IsHttpsReplace         = _ReplaceHttpsLink;
            DatProxy.HtmlConverter.IsExternalConverterUse = _CEExternalRead;

            //エラー通知用コールバック登録
            DatProxy.APIMediator.PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName == nameof(DatProxy.APIMediator.CurrentError))
                {
                    //ここで取得しておく
                    string error = DatProxy.APIMediator.CurrentError;

                    App.Current.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        this.SystemLog = error;
                    }));
                }
            };

            DatProxy.HtmlConverter.PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName == nameof(DatProxy.HtmlConverter.CurrentError))
                {
                    //ここで取得しておく
                    string error = DatProxy.HtmlConverter.CurrentError;

                    App.Current.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        this.SystemLog = error;
                    }));
                }
            };

            //自動開始処理
            if (Setting.AutoStart)
            {
                int pnum = (Setting.AutoSelect) ? (0) : (Setting.PortNumber);
                pnum = DatProxy.Start(pnum);
                if (pnum != 0)
                {
                    SystemLog  = "開始、ポート番号:" + pnum;
                    NowStart   = true;
                    PortNumber = pnum;
                }
                else
                {
                    SystemLog = "既に使用中のポートみたいです、別のポートを指定して下さい。";
                    DatProxy.End();
                }
            }
            //同時起動と終了を設定
            if (!String.IsNullOrEmpty(Setting.SenburaPath))
            {
                bool kage = true;
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = Setting.SenburaPath;
                if (System.IO.Path.GetFileName(Setting.SenburaPath) != "kage.exe")
                {
                    kage = false;
                    p.EnableRaisingEvents = true;
                    p.Exited += (sender, e) =>
                    {
                        App.Current.Dispatcher.BeginInvoke((Action)(() => { if (Setting.SyncEnd)
                                                                            {
                                                                                BeforeShutdown();
                                                                            }
                                                                    }), null);
                    };
                }
                try
                {
                    p.Start();
                    m_SenburaPID = p.Id;
                    SystemLog    = Setting.SenburaPath + " を起動";
                    m_SyncStart  = true;
                    //かちゅ~しゃの場合の終了同期処理の追加
                    if (kage)
                    {
                        System.Diagnostics.Process[] plist = null;
                        for (int i = 0; i < 50; ++i)
                        {
                            plist = System.Diagnostics.Process.GetProcessesByName("Katjusha");
                            if (plist.Length != 0)
                            {
                                break;
                            }
                            System.Threading.Thread.Sleep(100);
                        }
                        plist[0].EnableRaisingEvents = true;
                        plist[0].Exited += (sender, e) =>
                        {
                            App.Current.Dispatcher.BeginInvoke((Action)(() => { if (Setting.SyncEnd)
                                                                                {
                                                                                    BeforeShutdown();
                                                                                }
                                                                        }), null);
                        };
                        m_SenburaPID = plist[0].Id;
                    }
                }
                catch (Exception err)
                {
                    SystemLog = Setting.SenburaPath + " の起動に失敗\n" + err.ToString();
                }
            }
            Setting.change = false;
            //プロセス間通信のサーバ登録
            try
            {
                IpcServerChannel server = new IpcServerChannel("2chApiProxyIPC");
                ChannelServices.RegisterChannel(server, true);
                RemoteObject         = new SendObject();
                SendObject.OnTrance += new SendObject.CallEventHandler((e) =>
                {
                    if (RemoteObject.first)
                    {
                        RemoteObject.first = false;
                        App.Current.Dispatcher.BeginInvoke((Action)(() => { if (e)
                                                                            {
                                                                                ActivateWindow();
                                                                            }
                                                                    }));
                    }
                    else
                    {
                        RemoteObject.first = true;
                    }
                });
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(SendObject), "ShowWindow", WellKnownObjectMode.Singleton);
            }
            catch (Exception) { }
            SystemLog = "2chAPIProxy起動";
        }