![]() |
VOOZH | about |
dotnet add package SingBoxLib --version 1.2.1
NuGet\Install-Package SingBoxLib -Version 1.2.1
<PackageReference Include="SingBoxLib" Version="1.2.1" />
<PackageVersion Include="SingBoxLib" Version="1.2.1" />Directory.Packages.props
<PackageReference Include="SingBoxLib" />Project file
paket add SingBoxLib --version 1.2.1
#r "nuget: SingBoxLib, 1.2.1"
#:package SingBoxLib@1.2.1
#addin nuget:?package=SingBoxLib&version=1.2.1Install as a Cake Addin
#tool nuget:?package=SingBoxLib&version=1.2.1Install as a Cake Tool
<div align="center">
Configure and run sing-box with ease.
Based on sing-box's official documentation.
</div>
for more configuration examples, including Route Rules or Dns configuration, please refer to sing-box's official documentation.
var config = new SingBoxConfig
{
Inbounds = new()
{
new MixedInbound
{
Listen = "127.0.0.1",
ListenPort = 2080
}
},
Outbounds = new()
{
new TrojanOutbound
{
Server = "yourserver.server",
Port = 443,
Password = "my top secret password!",
Transport = new GrpcTransport
{
ServiceName = "grpcSeviceNameGoesHere",
},
Tls = new()
{
Enabled = true,
ServerName = "sniGoesHere",
Alpn = new() { "listOfAplnsGoHere" }
}
}
}
};
Route.AutoDetectInterface to true when using tun inbound.var config = new SingBoxConfig
{
... Outbounds, etc....
Inbounds = new()
{
new TunInbound
{
InterfaceName = "myTunInterface",
INet4Address = "172.19.0.1/30",
Stack = TunStacks.System,
Mtu = 1500,
AutoRoute = true,
}
},
Route = new()
{
AutoDetectInterface = true
}
};
sing-box you first need to obtain it's executable from its original repository.SingBoxWrapper's constructor.var wrapper = new SingBoxWrapper("sing-box.exe");
if you need sing-box's logs, you need to subscribe to the Onlog event:
wrapper.OnLog += (sender, log) =>
{
Console.WriteLine(log);
};
you can pass a CancellationToken to the SingBoxWrapper.StartAsync method, if you cancel it, the sing-box proccess will end. this parameter is optional.
// config is a SingBoxConfig instance.
var cts = new CancelationTokenSource();
await wrapper.StartAsync(config,cts.Token);
At this moment the following formats are supported (the sing-box itself can support more protocols):
VMess VLess Shadowsocks Trojan Socks Http Hysteria2 Tuic
var myProfileUrl = "trojan://myLovelyPassword@myserver.server:443?security=tls&sni=mySni&type=grpc&serviceName=myGrpcPath#MyTrojanServer";
var myProfile = ProfileParser.ParseProfileUrl(myProfileUrl);
// convert to outbound and use directly in sing-box config:
var myOutbound = myProfile.ToOutboundConfig();
Additionally you can convert parsed profiles back to string url:
var myProfileUrl = myProfile.ToProfileUrl();
You can use UrlTester and ParallelUrlTester classes to easily test if the proxies are healthy and valid
var myProfileUrl = "trojan://myLovelyPassword@myserver.server:443?security=tls&sni=mySni&type=grpc&serviceName=myGrpcPath#MyTrojanServer";
var myProfile = ProfileParser.ParseProfileUrl(myProfileUrl);
var urlTester = new UrlTester(
new SingBoxWrapper("sing-box-path"),
// local port
2080,
// timeout in miliseconds
3000,
// retry count (will still do the retries even if proxy works, returns fastest result)
5,
// url to test using the proxy, defauts to http://cp.cloudflare.com, optional
null
);
var testResult = await urlTester.TestAsync(myProfile);
Console.WriteLine($"Success: {testResult.Success}, Delay: {testResult.Delay}");
Parallel:
var parallelTester = new ParallelUrlTester(
new SingBoxWrapper("sing-box-path"),
// A list of open local ports, must be equal or bigger than total test thread count
// make sure they are not occupied by other applications running on your system
new int[] { 2080, 2081, 2082, 2083, 2084, 2085 },
// max number of concurrent testing
6,
// timeout in miliseconds
3000,
// retry count (will still do the retries even if proxy works, returns fastest result)
5,
// url to test using the proxy, defauts to http://cp.cloudflare.com, optional
null);
List<ProfileItem> profilesToTest = GetMyProfilesFormSomewhere();
var results = new ConcurrentBag<UrlTestResult>();
await parallelTester.ParallelTestAsync(profilesToTest, new Progress<UrlTestResult>((result =>
{
results.Add(result);
})), default(CancellationToken));
sing-box's internal url tester, selector outbound and more.Experimental.ClashApi property in your SingBoxConfig.var config = new SingBoxConfig
{
... other config parameters....
Experimental = new()
{
ClashApi = new()
{
ExternalController = "127.0.0.1:9090",
}
}
};
After running the sing-box's proccess with the above changes to your config, you shoud create an instance of ClashApiWrapper:
using SingBoxLib.Runtime.Api.Clash;
....
var clashApi = new ClashApiWrapper("http://127.0.0.1:9090");
ClashApi:GetLogs method returns an IAsyncEnumerable<LogInfo>LogInfo class contains two properties: Level which indicates log level and Payload which is a line of logs.CancelationToken for when you want to stop getting more logs. this parameter is optional.await foreach(var logInfo in clashApi.GetLogs(cts.Token))
{
Console.WriteLine($"{logInfo.Level}>> {logInfo.Payload}");
}
ClashApi:GetTraffic method returns an IAsyncEnumerable<TrafficInfo>TrafficInfo class contains two properties: Up and Down, which indicate the traffic used the the past second in Bytes.CancelationToken for when you want to stop getting more traffic info. this parameter is optional.await foreach(var trafficInfo in clashApi.GetTraffic(cts.Token))
{
Console.WriteLine($"Up: {trafficInfo.Up}, Down: {trafficInfo.Down}");
}
ClashApi:name parameter is the same as Tag in Outbound configuration.var delayInfo = await clashApi.GetProxyDelay(name: "out-1", timeout: 1000, url: "http://cp.cloudflare.com");
Console.WriteLine(delayInfo.Delay);
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 1 NuGet packages that depend on SingBoxLib:
| Package | Downloads |
|---|---|
|
Pouyan.SingBox
quieckly to create a vpn client that's support v2ray protocols |
Showing the top 1 popular GitHub repositories that depend on SingBoxLib:
| Repository | Stars |
|---|---|
|
Mahdi0024/ProxyCollector
Automatically collects and tests v2ray proxies and puts them in github for everyone to use.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 1.2.1 | 4,573 | 1/7/2025 |
| 1.2.0 | 238 | 1/7/2025 |
| 1.1.11 | 33,217 | 12/1/2023 |
| 1.1.10 | 1,185 | 11/15/2023 |
| 1.1.9 | 535 | 11/15/2023 |
| 1.1.8 | 575 | 11/15/2023 |
| 1.1.7 | 574 | 11/15/2023 |
| 1.1.6 | 576 | 11/14/2023 |
| 1.1.5 | 572 | 11/14/2023 |
| 1.1.4 | 591 | 10/27/2023 |
| 1.1.2 | 670 | 10/17/2023 |
| 1.1.1 | 1,398 | 9/30/2023 |
| 1.1.0 | 637 | 9/25/2023 |
| 1.0.9 | 658 | 9/23/2023 |
| 1.0.8 | 1,157 | 9/20/2023 |
| 1.0.7 | 833 | 9/18/2023 |
| 1.0.6 | 668 | 9/18/2023 |
| 1.0.5 | 636 | 9/18/2023 |
| 1.0.4 | 675 | 9/18/2023 |
| 1.0.3 | 659 | 9/13/2023 |