/// <summary> /// 指定ポートで Listening を開始する。 /// Shutdown() を呼び出さずに2回目の Startup() を呼び出した場合、InvalidOperationException が発生する。 /// </summary> /// <param name="listeningPort">Listeningするポート。</param> /// <param name="useIpV6">falseの場合、127.0.0.1で待ち受ける。trueの場合、::1で待ち受ける。既定false。</param> /// <param name="isSetProxyInProcess">trueの場合、プロセス内IEプロキシの設定を実施し、HTTP通信をNekoxyに向ける。既定true。</param> public static void Startup(int listeningPort, bool useIpV6 = false, bool isSetProxyInProcess = true) { if (server != null) { throw new InvalidOperationException("Calling Startup() twice without calling Shutdown() is not permitted."); } TransparentProxyLogic.AfterSessionComplete += InvokeAfterSessionComplete; TransparentProxyLogic.AfterReadRequestHeaders += InvokeAfterReadRequestHeaders; TransparentProxyLogic.AfterReadResponseHeaders += InvokeAfterReadResponseHeaders; ListeningPort = listeningPort; try { if (isSetProxyInProcess) { WinInetUtil.SetProxyInProcessForNekoxy(listeningPort); } server = new TcpServer(listeningPort, useIpV6); server.Start(TransparentProxyLogic.CreateProxy); server.InitListenFinished.WaitOne(); if (server.InitListenException != null) { throw server.InitListenException; } } catch (Exception) { Shutdown(); throw; } }
/// <summary> /// 指定ポートで Listening を開始する。 /// Shutdown() を呼び出さずに2回目の Startup() を呼び出した場合、InvalidOperationException が発生する。 /// </summary> /// <param name="listeningPort">Listeningするポート。</param> /// <param name="useIpV6">falseの場合、127.0.0.1で待ち受ける。trueの場合、::1で待ち受ける。既定false。</param> /// <param name="isSetProxyInProcess">trueの場合、プロセス内IEプロキシの設定を実施し、HTTP通信をNekoxyに向ける。既定true。</param> public static void Startup(int listeningPort, bool useIpV6 = false, bool isSetProxyInProcess = true) { if (IsInListening) { throw new InvalidOperationException("Calling Startup() twice without calling Shutdown() is not permitted."); } FiddlerApplication.BeforeRequest += setUpstreamProxyHandler; FiddlerApplication.AfterSessionComplete += raiseAfterSessionComplete; FiddlerApplication.RequestHeadersAvailable += raiseRequestHeadersAvailable; FiddlerApplication.ResponseHeadersAvailable += raiseResponseHeadersAvailable; ListeningPort = listeningPort; try { if (isSetProxyInProcess) { WinInetUtil.SetProxyInProcessForNekoxy(listeningPort); } readGatewayConfig(); FiddlerApplication.Startup(listeningPort, FiddlerCoreStartupFlags.ChainToUpstreamGateway); } catch (Exception) { Shutdown(); throw; } }
private static void readGatewayConfig() { if (UpstreamProxyConfig.Type == ProxyConfigType.DirectAccess) { useHttpGateway = false; useHttpsGateway = false; return; } string httpHost = ""; int httpPort = 0; string httpsHost = ""; int httpsPort = 0; if (UpstreamProxyConfig.Type == ProxyConfigType.SpecificProxy) { httpHost = UpstreamProxyConfig.SpecificProxyHost; httpPort = UpstreamProxyConfig.SpecificProxyPort; httpsHost = httpHost; httpsPort = httpPort; } else { httpHost = WinInetUtil.GetSystemHttpProxyHost(); httpPort = WinInetUtil.GetSystemHttpProxyPort(); if (httpPort == ListeningPort && httpHost.IsLoopbackHost()) { httpHost = ""; } httpsHost = WinInetUtil.GetSystemHttpsProxyHost(); httpsPort = WinInetUtil.GetSystemHttpsProxyPort(); if (httpsPort == ListeningPort && httpsHost.IsLoopbackHost()) { httpsHost = ""; } } useHttpGateway = !string.IsNullOrEmpty(httpHost); useHttpsGateway = !string.IsNullOrEmpty(httpsHost); if (useHttpGateway) { httpGateway = httpHost.Contains(":") ? string.Format("[{0}]:{1}", httpHost, httpPort) : string.Format("{0}:{1}", httpHost, httpPort); } if (useHttpsGateway) { httpsGateway = httpsHost.Contains(":") ? string.Format("[{0}]:{1}", httpsHost, httpsPort) : string.Format("{0}:{1}", httpsHost, httpsPort); } }
private void SetUpstreamProxy() { this.RelayHttpProxyHost = null; this.RelayHttpProxyPort = 80; var config = UpstreamProxyConfig; if (config.Type == ProxyConfigType.DirectAccess) { return; } if (config.Type == ProxyConfigType.SpecificProxy) { this.RelayHttpProxyHost = string.IsNullOrWhiteSpace(config.SpecificProxyHost) ? null : config.SpecificProxyHost; this.RelayHttpProxyPort = config.SpecificProxyPort; return; } // システムプロキシ利用(既定) var requestUri = this.GetEffectiveRequestUri(); if (requestUri != null) { var systemProxyConfig = WebRequest.GetSystemWebProxy(); if (systemProxyConfig.IsBypassed(requestUri)) { return; //ダイレクトアクセス } var systemProxy = systemProxyConfig.GetProxy(requestUri); this.RelayHttpProxyHost = !systemProxy.IsOwnProxy() ? systemProxy.Host : null; this.RelayHttpProxyPort = systemProxy.Port; } else { //リクエストURIをうまく組み立てられなかった場合、自動構成は諦めて通常のプロキシ設定を適用 var systemProxyHost = WinInetUtil.GetSystemHttpProxyHost(); var systemProxyPort = WinInetUtil.GetSystemHttpProxyPort(); if (systemProxyPort == HttpProxy.ListeningPort && systemProxyHost.IsLoopbackHost()) { return; //自身が指定されていた場合上流には指定しない } this.RelayHttpProxyHost = systemProxyHost; this.RelayHttpProxyPort = systemProxyPort; } }
private void SetUpstreamProxy() { this.RelayHttpProxyHost = null; this.RelayHttpProxyPort = 80; var config = UpstreamProxyConfig; if (config.Type == ProxyConfigType.DirectAccess) { return; } if (config.Type == ProxyConfigType.SpecificProxy) { this.RelayHttpProxyHost = string.IsNullOrWhiteSpace(config.SpecificProxyHost) ? null : config.SpecificProxyHost; this.RelayHttpProxyPort = config.SpecificProxyPort; return; } var requestUri = this.GetEffectiveRequestUri(); if (requestUri != null) { var systemProxyConfig = WebRequest.GetSystemWebProxy(); if (systemProxyConfig.IsBypassed(requestUri)) { return; } var systemProxy = systemProxyConfig.GetProxy(requestUri); this.RelayHttpProxyHost = !systemProxy.IsOwnProxy() ? systemProxy.Host : null; this.RelayHttpProxyPort = systemProxy.Port; } else { var systemProxyHost = WinInetUtil.GetSystemHttpProxyHost(); var systemProxyPort = WinInetUtil.GetSystemHttpProxyPort(); if (systemProxyPort == HttpProxy.ListeningPort && systemProxyHost.IsLoopbackHost()) { return; } this.RelayHttpProxyHost = systemProxyHost; this.RelayHttpProxyPort = systemProxyPort; } }
public static void Startup(int listeningPort, bool useIpV6 = false, bool isSetIEProxySettings = true) { FiddlerApplication.AfterSessionComplete += TranslateSession; FiddlerApplication.BeforeRequest += ConfigProxy; try { if (isSetIEProxySettings) { WinInetUtil.SetProxyInProcessByUrlmon(listeningPort); var systemProxyHost = WinInetUtil.GetSystemHttpProxyHost(); var systemProxyPort = WinInetUtil.GetSystemHttpProxyPort(); if (systemProxyPort != listeningPort || systemProxyHost.IsLoopbackHost()) { _systemProxyHost = systemProxyHost; _systemProxyPort = systemProxyPort; } } FiddlerApplication.Startup(listeningPort, FiddlerCoreStartupFlags.OptimizeThreadPool); } catch (Exception) { Shutdown(); throw; } }