Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
EzrealJ committed Apr 26, 2022
1 parent 04adc0d commit 10e0eb1
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 77 deletions.
4 changes: 2 additions & 2 deletions FastGithub.Configuration/GlobalListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public static class GlobalListener
/// <summary>
/// http端口
/// </summary>
public static int HttpPort { get; } = false ? GetAvailableTcpPort(80) : GetAvailableTcpPort(3880);
public static int HttpPort { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(80) : GetAvailableTcpPort(3880);

/// <summary>
/// https端口
/// </summary>
public static int HttpsPort { get; } = false ? GetAvailableTcpPort(443) : GetAvailableTcpPort(38443);
public static int HttpsPort { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(443) : GetAvailableTcpPort(38443);

/// <summary>
/// 获取已监听的端口
Expand Down
29 changes: 15 additions & 14 deletions FastGithub.DomainResolve/DnscryptProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public DnscryptProxy(ILogger<DnscryptProxy> logger)
this.exeFilePath = Path.Combine(PATH, false ? $"{NAME}.exe" : NAME);
this.tomlFilePath = Path.Combine(PATH, $"{NAME}.toml");
}

private void LogErrorWindowsNoAdmin(Exception ex)
{
logger.LogError(ex, "处理服务异常,没有管理员权限时,这种情况是正常的");
}
/// <summary>
/// 启动dnscrypt-proxy
/// </summary>
Expand Down Expand Up @@ -79,17 +82,7 @@ private async Task StartCoreAsync(CancellationToken cancellationToken)
await TomlUtil.SetLogLevelAsync(this.tomlFilePath, 6, cancellationToken);
await TomlUtil.SetLBStrategyAsync(this.tomlFilePath, "ph", cancellationToken);
await TomlUtil.SetMinMaxTTLAsync(this.tomlFilePath, TimeSpan.FromMinutes(1d), TimeSpan.FromMinutes(2d), cancellationToken);

if (false && Environment.UserInteractive == false)
{
ServiceInstallUtil.StopAndDeleteService(this.serviceName);
ServiceInstallUtil.InstallAndStartService(this.serviceName, this.exeFilePath, ServiceStartType.SERVICE_DEMAND_START);
this.process = Process.GetProcessesByName(this.processName).FirstOrDefault(item => item.SessionId == 0);
}
else
{
this.process = StartDnscryptProxy();
}
this.process = StartDnscryptProxy();

if (this.process != null)
{
Expand All @@ -106,9 +99,17 @@ public void Stop()
{
try
{
if (false && Environment.UserInteractive == false)
if (OperatingSystem.IsWindows() && Environment.UserInteractive == false)
{
ServiceInstallUtil.StopAndDeleteService(this.serviceName);
try
{
ServiceInstallUtil.StopAndDeleteService(this.serviceName);
}
catch (Exception ex)
{

LogErrorWindowsNoAdmin(ex);
}
}

if (this.process != null && this.process.HasExited == false)
Expand Down
2 changes: 1 addition & 1 deletion FastGithub.HttpServer/HttpReverseProxyMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private bool TryGetDomainConfig(HostString host, [MaybeNullWhen(false)] out Doma
}

// 未配置的域名,但仍然被解析到本机ip的域名
if (false && IsDomain(host.Host))
if (OperatingSystem.IsWindows() && IsDomain(host.Host))
{
this.logger.LogWarning($"域名{host.Host}可能已经被DNS污染,如果域名为本机域名,请解析为非回环IP");
domainConfig = defaultDomainConfig;
Expand Down
37 changes: 3 additions & 34 deletions FastGithub.HttpServer/KestrelServerOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,6 @@ public static void ListenHttpProxy(this KestrelServerOptions kestrel)
logger.LogInformation($"已监听http://localhost:{httpProxyPort},http代理服务启动完成");
}

/// <summary>
/// 监听ssh反向代理
/// </summary>
/// <param name="kestrel"></param>
public static void ListenSshReverseProxy(this KestrelServerOptions kestrel)
{
var sshPort = GlobalListener.SshPort;
kestrel.ListenLocalhost(sshPort, listen =>
{
listen.UseFlowAnalyze();
listen.UseConnectionHandler<GithubSshReverseProxyHandler>();
});

kestrel.GetLogger().LogInformation($"已监听ssh://localhost:{sshPort},github的ssh反向代理服务启动完成");
}

/// <summary>
/// 监听git反向代理
/// </summary>
/// <param name="kestrel"></param>
public static void ListenGitReverseProxy(this KestrelServerOptions kestrel)
{
var gitPort = GlobalListener.GitPort;
kestrel.ListenLocalhost(gitPort, listen =>
{
listen.UseFlowAnalyze();
listen.UseConnectionHandler<GithubGitReverseProxyHandler>();
});

kestrel.GetLogger().LogInformation($"已监听git://localhost:{gitPort},github的git反向代理服务启动完成");
}

/// <summary>
/// 监听http反向代理
Expand All @@ -86,7 +55,7 @@ public static void ListenHttpReverseProxy(this KestrelServerOptions kestrel)
var httpPort = GlobalListener.HttpPort;
kestrel.ListenLocalhost(httpPort);

if (false)
if (OperatingSystem.IsWindows())
{
kestrel.GetLogger().LogInformation($"已监听http://localhost:{httpPort},http反向代理服务启动完成");
}
Expand All @@ -106,7 +75,7 @@ public static void ListenHttpsReverseProxy(this KestrelServerOptions kestrel)
var httpsPort = GlobalListener.HttpsPort;
kestrel.ListenLocalhost(httpsPort, listen =>
{
if (false)
if (OperatingSystem.IsWindows())
{
listen.UseFlowAnalyze();
}
Expand All @@ -116,7 +85,7 @@ public static void ListenHttpsReverseProxy(this KestrelServerOptions kestrel)
});
});

if (false)
if (OperatingSystem.IsWindows())
{
var logger = kestrel.GetLogger();
logger.LogInformation($"已监听https://localhost:{httpsPort},https反向代理服务启动完成");
Expand Down
5 changes: 2 additions & 3 deletions FastGithub/AppHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
/// <returns></returns>
private async Task CheckFastGithubProxyAsync(CancellationToken cancellationToken)
{
if (false == false)
{

try
{
if (await this.UseFastGithubProxyAsync() == false)
Expand All @@ -80,7 +79,7 @@ private async Task CheckFastGithubProxyAsync(CancellationToken cancellationToken
{
this.logger.LogWarning("尝试获取代理信息失败");
}
}

}


Expand Down
20 changes: 14 additions & 6 deletions FastGithub/ConsoleUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Serilog;
using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

Expand Down Expand Up @@ -28,15 +29,22 @@ static class ConsoleUtil
/// <returns></returns>
public static bool DisableQuickEdit()
{
if (false)
try
{
var hwnd = GetStdHandle(STD_INPUT_HANDLE);
if (GetConsoleMode(hwnd, out uint mode))
if (OperatingSystem.IsWindows())
{
mode &= ~ENABLE_QUICK_EDIT;
return SetConsoleMode(hwnd, mode);
var hwnd = GetStdHandle(STD_INPUT_HANDLE);
if (GetConsoleMode(hwnd, out uint mode))
{
mode &= ~ENABLE_QUICK_EDIT;
return SetConsoleMode(hwnd, mode);
}
}
}
catch (Exception ex)
{
Log.Error(ex, "禁用快速编辑模式,没有管理员权限时,这种情况是正常的");
}

return false;
}
Expand Down
10 changes: 1 addition & 9 deletions FastGithub/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,8 @@ public static IHostBuilder CreateHostBuilder(string[] args)
kestrel.NoLimit();
kestrel.ListenHttpsReverseProxy();
kestrel.ListenHttpReverseProxy();
kestrel.ListenHttpProxy();

if (false)
{
kestrel.ListenSshReverseProxy();
kestrel.ListenGitReverseProxy();
}
else
{
kestrel.ListenHttpProxy();
}
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion FastGithub/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private static bool UseCommand(ILogger logger)
var action = cmd == Command.Start ? "启动" : "停止";
try
{
if (false)
if (OperatingSystem.IsWindows())
{
UseCommandAtWindows(cmd);
}
Expand Down
9 changes: 2 additions & 7 deletions FastGithub/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Serilog;
using System;

namespace FastGithub
Expand Down Expand Up @@ -33,18 +34,13 @@ public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppOptions>(this.Configuration);
services.Configure<FastGithubOptions>(this.Configuration.GetSection(nameof(FastGithub)));

services.AddConfiguration();
services.AddDomainResolve();
services.AddHttpClient();
services.AddReverseProxy();
services.AddFlowAnalyze();
services.AddHostedService<AppHostedService>();

if (false)
{
services.AddPacketIntercept();
}

}

/// <summary>
Expand All @@ -58,7 +54,6 @@ public void Configure(IApplicationBuilder app)
{
appBuilder.UseHttpProxy();
});

app.MapWhen(context => context.Connection.LocalPort != httpProxyPort, appBuilder =>
{
appBuilder.UseRequestLogging();
Expand Down

0 comments on commit 10e0eb1

Please sign in to comment.