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" }
}
}
}
};- Please note that tun inbound requires your app to have administrator privileges.
- Always set
Route.AutoDetectInterfaceto 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
}
};- In order to use this library for running
sing-boxyou first need to obtain it's executable from its original repository. - You need to have the path to this executable and pass it to
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://[email protected]: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://[email protected]: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));- ClashApi can be used for accessing Traffic info, Logs,
sing-box's internal url tester, selector outbound and more. - in order to be able to use this api, you first need to populate the
Experimental.ClashApiproperty in yourSingBoxConfig.
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");- the
GetLogsmethod returns anIAsyncEnumerable<LogInfo> - the
LogInfoclass contains two properties:Levelwhich indicates log level andPayloadwhich is a line of logs. - you can pass a
CancelationTokenfor 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}");
}- the
GetTrafficmethod returns anIAsyncEnumerable<TrafficInfo> - the
TrafficInfoclass contains two properties:UpandDown, which indicate the traffic used the the past second inBytes. - you can pass a
CancelationTokenfor 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}");
}nameparameter is the same asTagin Outbound configuration.
var delayInfo = await clashApi.GetProxyDelay(name: "out-1", timeout: 1000, url: "http://cp.cloudflare.com");
Console.WriteLine(delayInfo.Delay);