Skip to content

Commit

Permalink
测试附带gost实现socks代理自动转http
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqinxuancai committed Nov 14, 2024
1 parent 5897b59 commit b88361a
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .github/workflows/dotnet-core-desktop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ jobs:
- run: powershell Expand-Archive -Path .\x86.zip -DestinationPath .\x86
- run: powershell New-Item -ItemType Directory -Force -Path ".\build\x86"
- run: powershell Copy-Item -Path ".\x86\version.dll" -Destination ".\build\x86"

- run: powershell curl https://github.com/ginuerzh/gost/releases/download/v2.12.0/gost_2.12.0_windows_amd64.zip -o gost.zip
- run: powershell Expand-Archive -Path .\gost.zip -DestinationPath .\gost
- run: powershell Copy-Item -Path ".\gost\gost.exe" -Destination ".\build"

- run: powershell Copy-Item -Path "$env:GITHUB_WORKSPACE\DiscordProxyStart\bin\Release\net8.0\win-x64\publish\DiscordProxyStart.exe" -Destination ".\build"
- run: powershell Copy-Item -Path "$env:GITHUB_WORKSPACE\DiscordProxyStart\bin\Release\net8.0\win-x64\publish\DiscordProxyStart.pdb" -Destination ".\build"
Expand Down
5 changes: 5 additions & 0 deletions DiscordProxyStart/DiscordProxyStart.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\.github\workflows\dotnet-core-desktop.yml">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>

</ItemGroup>
</Project>
84 changes: 84 additions & 0 deletions DiscordProxyStart/Services/GostManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using DiscordProxyStart.Utils;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;


namespace DiscordProxyStart.Services
{

public class GostManager
{

private static GostManager _instance;

Check warning on line 14 in DiscordProxyStart/Services/GostManager.cs

View workflow job for this annotation

GitHub Actions / Dotnet Build

Non-nullable field '_instance' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
private static readonly object _lock = new object();

private readonly string gostPath;

public static GostManager Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
_instance = new GostManager($@"{AppContext.BaseDirectory}gost.exe");
}
}
}
return _instance;
}
}

public GostManager(string gostPath)
{
this.gostPath = gostPath;
}

private void KillExistingGostProcess()
{
var processes = Process.GetProcessesByName("gost");
foreach (var process in processes)
{
try
{
string processPath = Path.GetFullPath(process.MainModule?.FileName ?? string.Empty);

if (string.Equals(processPath, gostPath, StringComparison.OrdinalIgnoreCase))
{
process.Kill();
process.WaitForExit();
}
}
catch (Exception ex)
{
SimpleLogger.Instance.Info(@$"[KillExistingGostProcessError]{ex}");
}
}
}

public void StartProxy(string socksProxyAddress, int localHttpPort)
{
KillExistingGostProcess();
var startInfo = new ProcessStartInfo
{
FileName = gostPath,
Arguments = $"-L=http://127.0.0.1:{localHttpPort} -F={socksProxyAddress}",
UseShellExecute = false,
CreateNoWindow = true,
};

Debug.WriteLine(startInfo.Arguments);
var currentProcess = new Process { StartInfo = startInfo };
currentProcess.Start();
//currentProcess.WaitForExit();
Thread.Sleep(3000);
}
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DiscordProxyStart.Utils;
using DiscordProxyStart.Services;
using DiscordProxyStart.Utils;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
Expand All @@ -20,6 +21,9 @@ public static void Start()

var iniPath = Path.Combine(AppContext.BaseDirectory, "Config.ini");




//如果配置文件中有配置
var setupPath = SetupPathHelper.GetInstallPath("Discord");
if (File.Exists(iniPath))
Expand Down Expand Up @@ -74,6 +78,9 @@ private static void PortableStart(string exePath)
SimpleLogger.Instance.Info("正在准备启动...");
var appPath = GetAppPath(setupPath);

Check warning on line 79 in DiscordProxyStart/Services/WinStartManager.cs

View workflow job for this annotation

GitHub Actions / Dotnet Build

Possible null reference argument for parameter 'setupPath' in '(List<string> allPaths, string lastVersionPath) WinStartManager.GetAppPath(string setupPath)'.
var proxy = GetProxy();

//

var copyResult = CopyVersionDll(setupPath);

//TODO 自动gost转换socks代理?
Expand Down Expand Up @@ -202,6 +209,16 @@ private static string GetProxy()
throw new Exception("Config.ini中未设置代理地址");
}

//如果代理是socks开头,则启用Gost来中转,同时可支持账号密码
if (proxy.ToLower().StartsWith("socks"))
{
//代理需要转发
GostManager.Instance.StartProxy(proxy, 63121);
return "http://127.0.0.1:63121";
}



return proxy.Replace("\"", "").Trim();
}

Expand Down

0 comments on commit b88361a

Please sign in to comment.