mirror of
https://github.com/2dust/v2rayN.git
synced 2025-04-19 21:52:25 +00:00
up grpc state
This commit is contained in:
parent
3154df1bde
commit
d0ca8b2996
7 changed files with 65 additions and 85 deletions
|
@ -129,7 +129,7 @@ namespace v2rayN.Forms
|
||||||
{
|
{
|
||||||
e.Cancel = true;
|
e.Cancel = true;
|
||||||
|
|
||||||
statistics?.saveToFile();
|
statistics?.SaveToFile();
|
||||||
|
|
||||||
HideForm();
|
HideForm();
|
||||||
return;
|
return;
|
||||||
|
@ -137,7 +137,7 @@ namespace v2rayN.Forms
|
||||||
if (e.CloseReason == CloseReason.ApplicationExitCall)
|
if (e.CloseReason == CloseReason.ApplicationExitCall)
|
||||||
{
|
{
|
||||||
ConfigHandler.SaveConfig(ref config);
|
ConfigHandler.SaveConfig(ref config);
|
||||||
statistics?.saveToFile();
|
statistics?.SaveToFile();
|
||||||
statistics?.Close();
|
statistics?.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -168,7 +168,7 @@ namespace v2rayN.Forms
|
||||||
Utils.SaveLog("Windows shutdown UnsetProxy");
|
Utils.SaveLog("Windows shutdown UnsetProxy");
|
||||||
//CloseV2ray();
|
//CloseV2ray();
|
||||||
ConfigHandler.ToJsonFile(config);
|
ConfigHandler.ToJsonFile(config);
|
||||||
statistics?.saveToFile();
|
statistics?.SaveToFile();
|
||||||
ProxySetting.UnsetProxy();
|
ProxySetting.UnsetProxy();
|
||||||
m.Result = (IntPtr)1;
|
m.Result = (IntPtr)1;
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -146,7 +146,7 @@ namespace v2rayN
|
||||||
public const string InboundProxyTagName = "proxy";
|
public const string InboundProxyTagName = "proxy";
|
||||||
public const string Loopback = "127.0.0.1";
|
public const string Loopback = "127.0.0.1";
|
||||||
public const string InboundAPIProtocal = "dokodemo-door";
|
public const string InboundAPIProtocal = "dokodemo-door";
|
||||||
public const uint InboundAPIPort = 10805;
|
|
||||||
public enum StatisticsFreshRate
|
public enum StatisticsFreshRate
|
||||||
{
|
{
|
||||||
quick = 1000,
|
quick = 1000,
|
||||||
|
@ -185,6 +185,11 @@ namespace v2rayN
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static int pacPort { get; set; }
|
public static int pacPort { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
public static int statePort { get; set; }
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,13 @@
|
||||||
using System;
|
using Grpc.Core;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Net;
|
||||||
using System.Text.RegularExpressions;
|
using System.Net.Sockets;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
|
||||||
using v2rayN.Mode;
|
using v2rayN.Mode;
|
||||||
using v2rayN.Protos.Statistics;
|
using v2rayN.Protos.Statistics;
|
||||||
|
|
||||||
using Grpc.Core;
|
|
||||||
|
|
||||||
namespace v2rayN.Handler
|
namespace v2rayN.Handler
|
||||||
{
|
{
|
||||||
class StatisticsHandler
|
class StatisticsHandler
|
||||||
|
@ -63,19 +60,24 @@ namespace v2rayN.Handler
|
||||||
Statistic.Add(statistic);
|
Statistic.Add(statistic);
|
||||||
}
|
}
|
||||||
|
|
||||||
loadFromFile();
|
LoadFromFile();
|
||||||
|
|
||||||
grpcInit();
|
GrpcInit();
|
||||||
|
|
||||||
workThread_ = new Thread(new ThreadStart(run));
|
workThread_ = new Thread(new ThreadStart(Run));
|
||||||
workThread_.Start();
|
workThread_.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void grpcInit()
|
private void GrpcInit()
|
||||||
{
|
{
|
||||||
channel_ = new Channel($"127.0.0.1:{Global.InboundAPIPort}", ChannelCredentials.Insecure);
|
if (channel_ == null)
|
||||||
channel_.ConnectAsync();
|
{
|
||||||
client_ = new StatsService.StatsServiceClient(channel_);
|
Global.statePort = GetFreePort();
|
||||||
|
|
||||||
|
channel_ = new Channel($"127.0.0.1:{Global.statePort}", ChannelCredentials.Insecure);
|
||||||
|
channel_.ConnectAsync();
|
||||||
|
client_ = new StatsService.StatsServiceClient(channel_);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Close()
|
public void Close()
|
||||||
|
@ -92,7 +94,7 @@ namespace v2rayN.Handler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run()
|
public void Run()
|
||||||
{
|
{
|
||||||
while (!exitFlag_)
|
while (!exitFlag_)
|
||||||
{
|
{
|
||||||
|
@ -120,7 +122,7 @@ namespace v2rayN.Handler
|
||||||
down = 0;
|
down = 0;
|
||||||
|
|
||||||
//TODO: parse output
|
//TODO: parse output
|
||||||
parseOutput(res.Stat, out up, out down);
|
ParseOutput(res.Stat, out up, out down);
|
||||||
|
|
||||||
Up = up;
|
Up = up;
|
||||||
Down = down;
|
Down = down;
|
||||||
|
@ -139,10 +141,9 @@ namespace v2rayN.Handler
|
||||||
if (UpdateUI)
|
if (UpdateUI)
|
||||||
updateFunc_(TotalUp, TotalDown, Up, Down, Statistic);
|
updateFunc_(TotalUp, TotalDown, Up, Down, Statistic);
|
||||||
}
|
}
|
||||||
|
|
||||||
Thread.Sleep(config_.statisticsFreshRate);
|
|
||||||
channel_.ConnectAsync();
|
|
||||||
}
|
}
|
||||||
|
Thread.Sleep(config_.statisticsFreshRate);
|
||||||
|
channel_.ConnectAsync();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -151,7 +152,7 @@ namespace v2rayN.Handler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void parseOutput(Google.Protobuf.Collections.RepeatedField<Stat> source, out ulong up, out ulong down)
|
public void ParseOutput(Google.Protobuf.Collections.RepeatedField<Stat> source, out ulong up, out ulong down)
|
||||||
{
|
{
|
||||||
|
|
||||||
up = 0; down = 0;
|
up = 0; down = 0;
|
||||||
|
@ -189,7 +190,7 @@ namespace v2rayN.Handler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveToFile()
|
public void SaveToFile()
|
||||||
{
|
{
|
||||||
if (!Directory.Exists(logPath_))
|
if (!Directory.Exists(logPath_))
|
||||||
{
|
{
|
||||||
|
@ -249,7 +250,7 @@ namespace v2rayN.Handler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadFromFile()
|
public void LoadFromFile()
|
||||||
{
|
{
|
||||||
if (!Directory.Exists(logPath_)) return;
|
if (!Directory.Exists(logPath_)) return;
|
||||||
|
|
||||||
|
@ -397,5 +398,25 @@ namespace v2rayN.Handler
|
||||||
Utils.SaveLog(ex.Message, ex);
|
Utils.SaveLog(ex.Message, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int GetFreePort()
|
||||||
|
{
|
||||||
|
int defaultPort = 28123;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// TCP stack please do me a favor
|
||||||
|
TcpListener l = new TcpListener(IPAddress.Loopback, 0);
|
||||||
|
l.Start();
|
||||||
|
var port = ((IPEndPoint)l.LocalEndpoint).Port;
|
||||||
|
l.Stop();
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// in case access denied
|
||||||
|
Utils.SaveLog(ex.Message, ex);
|
||||||
|
return defaultPort;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -631,26 +631,28 @@ namespace v2rayN.Handler
|
||||||
v2rayConfig.stats = new Stats();
|
v2rayConfig.stats = new Stats();
|
||||||
|
|
||||||
apiObj.tag = tag;
|
apiObj.tag = tag;
|
||||||
apiObj.services = services.ToList();
|
apiObj.services = services.ToList();
|
||||||
v2rayConfig.api = apiObj;
|
v2rayConfig.api = apiObj;
|
||||||
|
|
||||||
policySystemSetting.statsInboundDownlink = true;
|
policySystemSetting.statsInboundDownlink = true;
|
||||||
policySystemSetting.statsInboundUplink = true;
|
policySystemSetting.statsInboundUplink = true;
|
||||||
policyObj.system = policySystemSetting;
|
policyObj.system = policySystemSetting;
|
||||||
v2rayConfig.policy = policyObj;
|
v2rayConfig.policy = policyObj;
|
||||||
if(!v2rayConfig.inbounds.Exists(item => { return item.tag == tag; }))
|
|
||||||
|
if (!v2rayConfig.inbounds.Exists(item => { return item.tag == tag; }))
|
||||||
{
|
{
|
||||||
var apiInbound = new Mode.Inbounds();
|
var apiInbound = new Mode.Inbounds();
|
||||||
var apiInboundSettings = new Mode.Inboundsettings();
|
var apiInboundSettings = new Mode.Inboundsettings();
|
||||||
apiInbound.tag = tag;
|
apiInbound.tag = tag;
|
||||||
apiInbound.listen = Global.Loopback;
|
apiInbound.listen = Global.Loopback;
|
||||||
apiInbound.port = config.port();
|
apiInbound.port = Global.statePort;
|
||||||
apiInbound.protocol = Global.InboundAPIProtocal;
|
apiInbound.protocol = Global.InboundAPIProtocal;
|
||||||
apiInboundSettings.address = Global.Loopback;
|
apiInboundSettings.address = Global.Loopback;
|
||||||
apiInbound.settings = apiInboundSettings;
|
apiInbound.settings = apiInboundSettings;
|
||||||
v2rayConfig.inbounds.Add(apiInbound);
|
v2rayConfig.inbounds.Add(apiInbound);
|
||||||
}
|
}
|
||||||
if(!v2rayConfig.routing.rules.Exists(item => { return item.outboundTag == tag; }))
|
|
||||||
|
if (!v2rayConfig.routing.rules.Exists(item => { return item.outboundTag == tag; }))
|
||||||
{
|
{
|
||||||
var apiRoutingRule = new Mode.RulesItem();
|
var apiRoutingRule = new Mode.RulesItem();
|
||||||
apiRoutingRule.inboundTag = tag;
|
apiRoutingRule.inboundTag = tag;
|
||||||
|
@ -1153,14 +1155,14 @@ namespace v2rayN.Handler
|
||||||
vmessItem.network = Global.DefaultNetwork;
|
vmessItem.network = Global.DefaultNetwork;
|
||||||
vmessItem.headerType = Global.None;
|
vmessItem.headerType = Global.None;
|
||||||
|
|
||||||
|
|
||||||
vmessItem.configVersion = Utils.ToInt(vmessQRCode.v);
|
vmessItem.configVersion = Utils.ToInt(vmessQRCode.v);
|
||||||
vmessItem.remarks = Utils.ToString(vmessQRCode.ps);
|
vmessItem.remarks = Utils.ToString(vmessQRCode.ps);
|
||||||
vmessItem.address = Utils.ToString(vmessQRCode.add);
|
vmessItem.address = Utils.ToString(vmessQRCode.add);
|
||||||
vmessItem.port = Utils.ToInt(vmessQRCode.port);
|
vmessItem.port = Utils.ToInt(vmessQRCode.port);
|
||||||
vmessItem.id = Utils.ToString(vmessQRCode.id);
|
vmessItem.id = Utils.ToString(vmessQRCode.id);
|
||||||
vmessItem.alterId = Utils.ToInt(vmessQRCode.aid);
|
vmessItem.alterId = Utils.ToInt(vmessQRCode.aid);
|
||||||
|
|
||||||
if (!Utils.IsNullOrEmpty(vmessQRCode.net))
|
if (!Utils.IsNullOrEmpty(vmessQRCode.net))
|
||||||
{
|
{
|
||||||
vmessItem.network = vmessQRCode.net;
|
vmessItem.network = vmessQRCode.net;
|
||||||
|
@ -1169,7 +1171,7 @@ namespace v2rayN.Handler
|
||||||
{
|
{
|
||||||
vmessItem.headerType = vmessQRCode.type;
|
vmessItem.headerType = vmessQRCode.type;
|
||||||
}
|
}
|
||||||
|
|
||||||
vmessItem.requestHost = Utils.ToString(vmessQRCode.host);
|
vmessItem.requestHost = Utils.ToString(vmessQRCode.host);
|
||||||
vmessItem.path = Utils.ToString(vmessQRCode.path);
|
vmessItem.path = Utils.ToString(vmessQRCode.path);
|
||||||
vmessItem.streamSecurity = Utils.ToString(vmessQRCode.tls);
|
vmessItem.streamSecurity = Utils.ToString(vmessQRCode.tls);
|
||||||
|
|
|
@ -21,40 +21,14 @@ namespace v2rayN.Mode
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<Outbounds> outbounds { get; set; }
|
public List<Outbounds> outbounds { get; set; }
|
||||||
|
|
||||||
/// 网速统计
|
|
||||||
/// 使用v2ray api功能
|
|
||||||
///
|
|
||||||
/// routing->rules 需要加上这一条
|
|
||||||
/// {
|
|
||||||
/// "inboundTag": [
|
|
||||||
/// "api"
|
|
||||||
/// ],
|
|
||||||
/// "outboundTag": "api",
|
|
||||||
/// "type": "field"
|
|
||||||
/// }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 统计需要, 空对象
|
/// 统计需要, 空对象
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Stats stats { get; set; }
|
public Stats stats { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 需要tag和services
|
|
||||||
/// "api": {
|
|
||||||
/// "tag": "api",
|
|
||||||
/// "services": [
|
|
||||||
/// "StatsService"
|
|
||||||
/// ]
|
|
||||||
/// }
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public API api { get; set; }
|
public API api { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// policy 都设置为true;
|
|
||||||
/// "system": {
|
|
||||||
/// "statsInboundUplink": true,
|
|
||||||
/// "statsInboundDownlink": true
|
|
||||||
/// }
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Policy policy;
|
public Policy policy;
|
||||||
|
|
||||||
|
|
|
@ -33,4 +33,4 @@ using System.Runtime.InteropServices;
|
||||||
// 方法是按如下所示使用“*”:
|
// 方法是按如下所示使用“*”:
|
||||||
//[assembly: AssemblyVersion("1.0.*")]
|
//[assembly: AssemblyVersion("1.0.*")]
|
||||||
//[assembly: AssemblyVersion("1.0.0")]
|
//[assembly: AssemblyVersion("1.0.0")]
|
||||||
[assembly: AssemblyFileVersion("2.37")]
|
[assembly: AssemblyFileVersion("2.38")]
|
||||||
|
|
|
@ -1,17 +1,4 @@
|
||||||
{
|
{
|
||||||
"stats": {},
|
|
||||||
"api": {
|
|
||||||
"tag": "api",
|
|
||||||
"services": [
|
|
||||||
"StatsService"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"policy": {
|
|
||||||
"system": {
|
|
||||||
"statsInboundUplink": true,
|
|
||||||
"statsInboundDownlink": true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"log": {
|
"log": {
|
||||||
"access": "",
|
"access": "",
|
||||||
"error": "",
|
"error": "",
|
||||||
|
@ -39,16 +26,7 @@
|
||||||
"tls"
|
"tls"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
{
|
|
||||||
"listen": "127.0.0.1",
|
|
||||||
"port": 10805,
|
|
||||||
"protocol": "dokodemo-door",
|
|
||||||
"settings": {
|
|
||||||
"address": "127.0.0.1"
|
|
||||||
},
|
|
||||||
"tag": "api"
|
|
||||||
}
|
|
||||||
],
|
],
|
||||||
"outbounds": [{
|
"outbounds": [{
|
||||||
"tag": "proxy",
|
"tag": "proxy",
|
||||||
|
|
Loading…
Reference in a new issue