/// <summary> /// Initializes a new instance of the PCHCServer class /// with the specified osted Cache Mode Listen Port and IPAddress type. /// </summary> /// <param name="transferProtocol">The transport type will be used</param> /// <param name="hostedCacheModeListenPort"> /// The specified BranchCache service's listen port in hosted cache mode. /// </param> /// <param name="ipaddressType">The IP address type.</param> /// <param name="logger">The logger.</param> public PCHCServer( TransferProtocol transferProtocol, int hostedCacheModeListenPort, IPAddressType ipaddressType, ILogPrinter logger) { if (logger == null) { throw new ArgumentNullException("logger", "The input parameter \"logger\" is null."); } this.logger = logger; this.httpServerTransport = new HttpServerTransport( transferProtocol, hostedCacheModeListenPort, ipaddressType, PCHCRESOURCE, logger); this.httpServerTransport.HttpRequestEventHandle += new EventHandler <HttpRequestEventArg>(this.ReceiveHttpRequest); this.logger.AddDebug(@"Register the delegate EventHandle with generic parameter HttpRequestEverntArg and the handle method is ReceiveHttpRequest."); }
/// <summary> /// Initializes a new instance of the PccrtpServer class with specified logger instance. /// </summary> /// <param name="listenPort">Indicates the PCCRTP server's listen port.</param> /// <param name="addressType">Indicates the PCCRTP server's address type is Ipv4 or Ipv6.</param> public PccrtpServer(int listenPort, IPAddressType addressType) { this.httpServerTransport = new HttpServerTransport( TransferProtocol.HTTP, listenPort, addressType, null); this.httpServerTransport.HttpRequestEventHandle += new EventHandler <HttpRequestEventArg>(this.OnHttpRequestMessageReceived); }
/// <summary> /// Initializes a new instance of the HttpServerTransport class. /// </summary> /// <param name="transferProtocol">The transport type will be used</param> /// <param name="listenPort">The listen port.</param> /// <param name="ipaddressType">The IP address type.</param> /// <param name="resource">The resource.</param> public HttpServerTransport( TransferProtocol transferProtocol, int listenPort, IPAddressType ipaddressType, string resource) { this.httpListener = new HttpListener(); this.httpListener.Prefixes.Add( this.GetListenPrefix(transferProtocol, listenPort, resource)); }
/// <summary> /// Initializes a new instance of the PccrtpServer class with specified logger instance. /// </summary> /// <param name="listenPort">Indicates the PCCRTP server's listen port.</param> /// <param name="addressType">Indicates the PCCRTP server's address type is Ipv4 or Ipv6.</param> /// <param name="logger">An instance of ILogPrinter.</param> public PccrtpServer(int listenPort, IPAddressType addressType, ILogPrinter logger) { if (logger == null) { throw new ArgumentNullException("logger", "The input parameter \"logger\" is null."); } this.logger = logger; this.httpServerTransport = new HttpServerTransport( TransferProtocol.HTTP, listenPort, addressType, null, this.logger); this.httpServerTransport.HttpRequestEventHandle += new EventHandler <HttpRequestEventArg>(this.OnHttpRequestMessageReceived); }
/// <summary> /// Initializes a new instance of the <see cref="PccrrServer"/> class. /// </summary> /// <param name="serverPort">The server port.</param> /// <param name="pccrrPath">The pccrr path.</param> /// <param name="addressType">The IP address type.</param> public PccrrServer( int serverPort, string pccrrPath, IPAddressType addressType) { if (pccrrPath == null) { throw new ArgumentNullException("pccrrPath"); } this.serverPort = serverPort; this.url = pccrrPath; this.addressType = addressType; this.httpServerTransport = new HttpServerTransport(TransferProtocol.HTTP, serverPort, addressType, pccrrPath); this.httpServerTransport.HttpRequestEventHandle += new EventHandler <HttpRequestEventArg>(this.HttpServerTransport_ReceiveFrom); }
/// <summary> /// Initializes a new instance of the PccrtpServer class with specified logger instance. /// </summary> /// <param name="listenPort">Indicates the PCCRTP server's listen port.</param> /// <param name="addressType">Indicates the PCCRTP server's address type is Ipv4 or Ipv6.</param> /// <param name="logger">An instance of ILogPrinter.</param> public PccrtpServer(int listenPort, IPAddressType addressType, ILogPrinter logger) { if (logger == null) { throw new ArgumentNullException("logger", "The input parameter \"logger\" is null."); } this.logger = logger; this.httpServerTransport = new HttpServerTransport( TransferProtocol.HTTP, listenPort, addressType, null, this.logger); this.httpServerTransport.HttpRequestEventHandle += new EventHandler<HttpRequestEventArg>(this.OnHttpRequestMessageReceived); }
/// <summary> /// Initializes a new instance of the HttpServerTransport class. /// </summary> /// <param name="transferProtocol">The transport type will be used</param> /// <param name="listenPort">The listen port.</param> /// <param name="ipaddressType">The IP address type.</param> /// <param name="resource">The resource.</param> /// <param name="logger">The logger.</param> public HttpServerTransport( TransferProtocol transferProtocol, int listenPort, IPAddressType ipaddressType, string resource, ILogPrinter logger) { if (logger == null) { throw new ArgumentNullException("logger", "The input parameter \"logger\" is null."); } this.logger = logger; this.httpListener = new HttpListener(); string prefix = this.GetListenPrefix(transferProtocol, listenPort, resource); this.httpListener.Prefixes.Add(prefix); this.logger.AddDebug(string.Format( "Initialize the http server transport and add prefix: {0} to http listener.", prefix)); }
public DaemonUtil(string daemonName, CancellationToken cancel = default) : base(cancel) { if (daemonName._IsEmpty()) { throw new ArgumentNullException(nameof(daemonName)); } daemonName = daemonName._NonNullTrim(); try { // 起動パラメータ this.Params = new OneLineParams(GlobalDaemonStateManager.StartupArguments); if (Params._HasKey(Consts.DaemonArgKeys.StartLogFileBrowser)) { // Log Browser で利用されるべきポート番号の決定 int httpPort = Params._GetFirstValueOrDefault(Consts.DaemonArgKeys.LogFileBrowserPort, StrComparer.IgnoreCaseComparer)._ToInt(); if (httpPort == 0) { httpPort = Util.GenerateDynamicListenableTcpPortWithSeed(Env.DnsFqdnHostName + "_seed_daemonutil_logbrowser_http" + Env.AppRootDir + "@" + daemonName); } int httpsPort = Params._GetFirstValueOrDefault(Consts.DaemonArgKeys.LogFileBrowserPort, StrComparer.IgnoreCaseComparer)._ToInt(); if (httpsPort == 0) { httpsPort = Util.GenerateDynamicListenableTcpPortWithSeed(Env.DnsFqdnHostName + "_seed_daemonutil_logbrowser_https" + Env.AppRootDir + "@" + daemonName, excludePorts: httpPort._SingleArray()); } // Log Browser 用の CertVault の作成 CertVault certVault = new CertVault(PP.Combine(Env.AppLocalDir, "Config/DaemonUtil_LogBrowser/CertVault"), new CertVaultSettings(defaultSetting: EnsureSpecial.Yes) { UseAcme = false }); DisposeList.Add(certVault); // Log Browser の起動 HttpServerOptions httpServerOptions = new HttpServerOptions { UseStaticFiles = false, UseSimpleBasicAuthentication = false, HttpPortsList = httpPort._SingleList(), HttpsPortsList = httpsPort._SingleList(), DebugKestrelToConsole = false, UseKestrelWithIPACoreStack = true, AutomaticRedirectToHttpsIfPossible = false, LocalHostOnly = false, UseGlobalCertVault = false, // Disable Global CertVault DisableHiveBasedSetting = true, // Disable Hive based settings ServerCertSelector = certVault.X509CertificateSelectorForHttpsServerNoAcme, DenyRobots = true, // Deny robots }; LogBrowserOptions browserOptions = new LogBrowserOptions( Env.AppRootDir, systemTitle: $"{Env.DnsFqdnHostName}", clientIpAcl: (ip) => { // 接続元 IP アドレスの種類を取得 IPAddressType type = ip._GetIPAddressType(); if (type.Bit(IPAddressType.GlobalIp)) { // 接続元がグローバル IP の場合 if (GlobalDaemonStateManager.IsDaemonClientLocalIpAddressGlobal == false) { // DaemonCenter との接続にプライベート IP を利用している場合: 接続拒否 return(false); } } // それ以外の場合: 接続許可 return(true); } ); DisposeList.Add(LogBrowserHttpServerBuilder.StartServer(httpServerOptions, new LogBrowserHttpServerOptions(browserOptions, "/" + GlobalDaemonStateManager.DaemonSecret))); GlobalDaemonStateManager.FileBrowserHttpsPortNumber = httpsPort; } } catch (Exception ex) { ex._Debug(); this._DisposeSafe(); throw; } }
/// <summary> /// Initializes a new instance of the PCHCServer class /// with the specified osted Cache Mode Listen Port and IPAddress type. /// </summary> /// <param name="transferProtocol">The transport type will be used</param> /// <param name="hostedCacheModeListenPort"> /// The specified BranchCache service's listen port in hosted cache mode. /// </param> /// <param name="ipaddressType">The IP address type.</param> public PCHCServer(TransferProtocol transferProtocol, int hostedCacheModeListenPort, IPAddressType ipaddressType) { this.httpServerTransport = new HttpServerTransport( transferProtocol, hostedCacheModeListenPort, ipaddressType, PCHCRESOURCE); this.httpServerTransport.HttpRequestEventHandle += new EventHandler <HttpRequestEventArg>(this.ReceiveHttpRequest); }
/// <summary> /// Initializes a new instance of the PCHCServer class /// with the specified osted Cache Mode Listen Port and IPAddress type. /// </summary> /// <param name="transferProtocol">The transport type will be used</param> /// <param name="hostedCacheModeListenPort"> /// The specified BranchCache service's listen port in hosted cache mode. /// </param> /// <param name="ipaddressType">The IP address type.</param> /// <param name="logger">The logger.</param> public PCHCServer( TransferProtocol transferProtocol, int hostedCacheModeListenPort, IPAddressType ipaddressType, ILogPrinter logger) { if (logger == null) { throw new ArgumentNullException("logger", "The input parameter \"logger\" is null."); } this.logger = logger; this.httpServerTransport = new HttpServerTransport( transferProtocol, hostedCacheModeListenPort, ipaddressType, PCHCRESOURCE, logger); this.httpServerTransport.HttpRequestEventHandle += new EventHandler<HttpRequestEventArg>(this.ReceiveHttpRequest); this.logger.AddDebug(@"Register the delegate EventHandle with generic parameter HttpRequestEverntArg and the handle method is ReceiveHttpRequest."); }
/// <summary> /// Initializes a new instance of the PCHCServer class /// with the specified osted Cache Mode Listen Port and IPAddress type. /// </summary> /// <param name="transferProtocol">The transport type will be used</param> /// <param name="hostedCacheModeListenPort"> /// The specified BranchCache service's listen port in hosted cache mode. /// </param> /// <param name="ipaddressType">The IP address type.</param> public PCHCServer(TransferProtocol transferProtocol, int hostedCacheModeListenPort, IPAddressType ipaddressType) { this.httpServerTransport = new HttpServerTransport( transferProtocol, hostedCacheModeListenPort, ipaddressType, PCHCRESOURCE); this.httpServerTransport.HttpRequestEventHandle += new EventHandler<HttpRequestEventArg>(this.ReceiveHttpRequest); }
/// <summary> /// 获取访问者的IP地址 /// </summary> /// <returns></returns> public static string GetVisitorIPAddress(IPAddressType type) { string result = string.Empty; if (HttpContext.Current != null) { var sv = HttpContext.Current.Request.ServerVariables; result = sv["HTTP_X_FORWARDED_FOR1"]; if (string.IsNullOrEmpty(result) || result.ToLower().IndexOf("unknown") > 0) { result = sv["HTTP_XOR_FWARDED_FOR"]; if (string.IsNullOrEmpty(result) || result.ToLower().IndexOf("unknown") > 0) { result = sv["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(result) || result.ToLower().IndexOf("unknown") > 0) { result = sv["HTTP_FOX_RWARDED_FOR"]; if (string.IsNullOrEmpty(result) || result.ToLower().IndexOf("unknown") > 0) { result = sv["HTTP_XOR_FWARDED_FOR"]; if (string.IsNullOrEmpty(result) || result.ToLower().IndexOf("unknown") > 0) { result = sv["REMOTE_ADDR"]; if (string.IsNullOrEmpty(result) || result.ToLower().IndexOf("unknown") > 0) { result = HttpContext.Current.Request.UserHostAddress; } } } } } } } if (!string.IsNullOrEmpty(result)) { //可能有代理 if (result.IndexOf(".") == -1) //没有“.”肯定是非IPv4格式 { result = string.Empty; } else { if (result.IndexOf(",") != -1) { //有“,”,估计多个代理。取第一个不是内网的IP。 result = result.Replace(" ", "").Replace("'", ""); string[] temparyip = result.Split(",;".ToCharArray()); switch (type) { case IPAddressType.FULL: return result; case IPAddressType.LAST: return temparyip[temparyip.Length - 1]; case IPAddressType.WAN: for (int i = 0; i < temparyip.Length; i++) { if (temparyip[i].Substring(0, 3) != "10." && temparyip[i].Substring(0, 7) != "192.168" && temparyip[i].Substring(0, 7) != "172.16.") { return temparyip[i]; //找到不是内网的地址 } } break; } } else { return result; } } } return result; }
/// <summary> /// Initializes a new instance of the PccrtpServer class with specified logger instance. /// </summary> /// <param name="listenPort">Indicates the PCCRTP server's listen port.</param> /// <param name="addressType">Indicates the PCCRTP server's address type is Ipv4 or Ipv6.</param> public PccrtpServer(int listenPort, IPAddressType addressType) { this.httpServerTransport = new HttpServerTransport( TransferProtocol.HTTP, listenPort, addressType, null); this.httpServerTransport.HttpRequestEventHandle += new EventHandler<HttpRequestEventArg>(this.OnHttpRequestMessageReceived); }
/// <summary> /// Initializes a new instance of the <see cref="PccrrServer"/> class. /// </summary> /// <param name="serverPort">The server port.</param> /// <param name="pccrrPath">The pccrr path.</param> /// <param name="addressType">The IP address type.</param> public PccrrServer( int serverPort, string pccrrPath, IPAddressType addressType) { if (pccrrPath == null) { throw new ArgumentNullException("pccrrPath"); } this.serverPort = serverPort; this.url = pccrrPath; this.addressType = addressType; this.httpServerTransport = new HttpServerTransport(TransferProtocol.HTTP, serverPort, addressType, pccrrPath); this.httpServerTransport.HttpRequestEventHandle += new EventHandler<HttpRequestEventArg>(this.HttpServerTransport_ReceiveFrom); }
/// <summary> /// Initializes a new instance of the HttpServerTransport class. /// </summary> /// <param name="transferProtocol">The transport type will be used</param> /// <param name="listenPort">The listen port.</param> /// <param name="ipaddressType">The IP address type.</param> /// <param name="resource">The resource.</param> public HttpServerTransport( TransferProtocol transferProtocol, int listenPort, IPAddressType ipaddressType, string resource) { this.httpListener = new HttpListener(); this.httpListener.Prefixes.Add( this.GetListenPrefix(transferProtocol, listenPort, resource)); }
/// <summary> /// Initializes a new instance of the HttpServerTransport class. /// </summary> /// <param name="transferProtocol">The transport type will be used</param> /// <param name="listenPort">The listen port.</param> /// <param name="ipaddressType">The IP address type.</param> /// <param name="resource">The resource.</param> /// <param name="logger">The logger.</param> public HttpServerTransport( TransferProtocol transferProtocol, int listenPort, IPAddressType ipaddressType, string resource, ILogPrinter logger) { if (logger == null) { throw new ArgumentNullException("logger", "The input parameter \"logger\" is null."); } this.logger = logger; this.httpListener = new HttpListener(); string prefix = this.GetListenPrefix(transferProtocol, listenPort, resource); this.httpListener.Prefixes.Add(prefix); this.logger.AddDebug(string.Format( "Initialize the http server transport and add prefix: {0} to http listener.", prefix)); }