Skip to content

Commit

Permalink
Improved models
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Oct 8, 2024
1 parent f60575b commit 3e74bb6
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 343 deletions.
240 changes: 47 additions & 193 deletions v2rayN/ServiceLib/Handler/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace ServiceLib.Handler
/// </summary>
public class ConfigHandler
{
private static string configRes = Global.ConfigFileName;
private static readonly object objLock = new();
private static readonly string _configRes = Global.ConfigFileName;
private static readonly object _objLock = new();

#region ConfigHandler

Expand All @@ -21,39 +21,29 @@ public class ConfigHandler
/// <returns></returns>
public static int LoadConfig(ref Config? config)
{
//载入配置文件
var result = Utils.LoadResource(Utils.GetConfigPath(configRes));
var result = Utils.LoadResource(Utils.GetConfigPath(_configRes));
if (Utils.IsNotEmpty(result))
{
//转成Json
config = JsonUtils.Deserialize<Config>(result);
}
else
{
if (File.Exists(Utils.GetConfigPath(configRes)))
if (File.Exists(Utils.GetConfigPath(_configRes)))
{
Logging.SaveLog("LoadConfig Exception");
return -1;
}
}

if (config == null)
{
config = new Config
{
};
}
if (config.coreBasicItem == null)
config ??= new Config();

config.coreBasicItem ??= new()
{
config.coreBasicItem = new()
{
logEnabled = false,
loglevel = "warning",
muxEnabled = false,
};
}
logEnabled = false,
loglevel = "warning",
muxEnabled = false,
};

//本地监听
if (config.inbound == null)
{
config.inbound = new List<InItem>();
Expand All @@ -75,55 +65,38 @@ public static int LoadConfig(ref Config? config)
config.inbound[0].protocol = EInboundProtocol.socks.ToString();
}
}
if (config.routingBasicItem == null)
config.routingBasicItem ??= new()
{
config.routingBasicItem = new()
{
enableRoutingAdvanced = true
};
}
//路由规则
enableRoutingAdvanced = true
};

if (Utils.IsNullOrEmpty(config.routingBasicItem.domainStrategy))
{
config.routingBasicItem.domainStrategy = Global.DomainStrategies[0];//"IPIfNonMatch";
}
//if (Utile.IsNullOrEmpty(config.domainMatcher))
//{
// config.domainMatcher = "linear";
//}

//kcp
if (config.kcpItem == null)
config.kcpItem ??= new KcpItem
{
config.kcpItem = new KcpItem
{
mtu = 1350,
tti = 50,
uplinkCapacity = 12,
downlinkCapacity = 100,
readBufferSize = 2,
writeBufferSize = 2,
congestion = false
};
}
if (config.grpcItem == null)
mtu = 1350,
tti = 50,
uplinkCapacity = 12,
downlinkCapacity = 100,
readBufferSize = 2,
writeBufferSize = 2,
congestion = false
};
config.grpcItem ??= new GrpcItem
{
config.grpcItem = new GrpcItem
{
idle_timeout = 60,
health_check_timeout = 20,
permit_without_stream = false,
initial_windows_size = 0,
};
}
if (config.tunModeItem == null)
idle_timeout = 60,
health_check_timeout = 20,
permit_without_stream = false,
initial_windows_size = 0,
};
config.tunModeItem ??= new TunModeItem
{
config.tunModeItem = new TunModeItem
{
enableTun = false,
mtu = 9000,
};
}
enableTun = false,
mtu = 9000,
};
config.guiItem ??= new()
{
enableStatistics = false,
Expand All @@ -150,19 +123,13 @@ public static int LoadConfig(ref Config? config)
}
}

if (config.constItem == null)
{
config.constItem = new ConstItem();
}
config.constItem ??= new ConstItem();
if (Utils.IsNullOrEmpty(config.constItem.defIEProxyExceptions))
{
config.constItem.defIEProxyExceptions = Global.IEProxyExceptions;
}

if (config.speedTestItem == null)
{
config.speedTestItem = new();
}
config.speedTestItem ??= new();
if (config.speedTestItem.speedTestTimeout < 10)
{
config.speedTestItem.speedTestTimeout = 10;
Expand All @@ -183,34 +150,19 @@ public static int LoadConfig(ref Config? config)
xudpProxyUDP443 = "reject"
};

if (config.mux4SboxItem == null)
config.mux4SboxItem ??= new()
{
config.mux4SboxItem = new()
{
protocol = Global.SingboxMuxs[0],
max_connections = 8
};
}
protocol = Global.SingboxMuxs[0],
max_connections = 8
};

if (config.hysteriaItem == null)
config.hysteriaItem ??= new()
{
config.hysteriaItem = new()
{
up_mbps = 100,
down_mbps = 100
};
}
up_mbps = 100,
down_mbps = 100
};
config.clashUIItem ??= new();

if (config.systemProxyItem == null)
{
config.systemProxyItem = new()
{
systemProxyExceptions = config.systemProxyExceptions,
systemProxyAdvancedProtocol = config.systemProxyAdvancedProtocol,
};
}

config.systemProxyItem ??= new();
config.webDavItem ??= new();

return 0;
Expand All @@ -234,12 +186,12 @@ public static int SaveConfig(Config config, bool reload = true)
/// <param name="config"></param>
private static void ToJsonFile(Config config)
{
lock (objLock)
lock (_objLock)
{
try
{
//save temp file
var resPath = Utils.GetConfigPath(configRes);
var resPath = Utils.GetConfigPath(_configRes);
var tempPath = $"{resPath}_temp";
if (JsonUtils.ToFile(config, tempPath) != 0)
{
Expand All @@ -260,104 +212,6 @@ private static void ToJsonFile(Config config)
}
}

//public static int ImportOldGuiConfig(Config config, string fileName)
//{
// var result = Utils.LoadResource(fileName);
// if (Utils.IsNullOrEmpty(result))
// {
// return -1;
// }

// var configOld = JsonUtils.Deserialize<ConfigOld>(result);
// if (configOld == null)
// {
// return -1;
// }

// var subItem = JsonUtils.Deserialize<List<SubItem>>(JsonUtils.Serialize(configOld.subItem));
// foreach (var it in subItem)
// {
// if (Utils.IsNullOrEmpty(it.id))
// {
// it.id = Utils.GetGUID(false);
// }
// SQLiteHelper.Instance.Replace(it);
// }

// var profileItems = JsonUtils.Deserialize<List<ProfileItem>>(JsonUtils.Serialize(configOld.vmess));
// foreach (var it in profileItems)
// {
// if (Utils.IsNullOrEmpty(it.indexId))
// {
// it.indexId = Utils.GetGUID(false);
// }
// SQLiteHelper.Instance.Replace(it);
// }

// foreach (var it in configOld.routings)
// {
// if (it.locked)
// {
// continue;
// }
// var routing = JsonUtils.Deserialize<RoutingItem>(JsonUtils.Serialize(it));
// foreach (var it2 in it.rules)
// {
// it2.id = Utils.GetGUID(false);
// }
// routing.ruleNum = it.rules.Count;
// routing.ruleSet = JsonUtils.Serialize(it.rules, false);

// if (Utils.IsNullOrEmpty(routing.id))
// {
// routing.id = Utils.GetGUID(false);
// }
// SQLiteHelper.Instance.Replace(routing);
// }

// config = JsonUtils.Deserialize<Config>(JsonUtils.Serialize(configOld));

// if (config.coreBasicItem == null)
// {
// config.coreBasicItem = new()
// {
// logEnabled = configOld.logEnabled,
// loglevel = configOld.loglevel,
// muxEnabled = configOld.muxEnabled,
// };
// }

// if (config.routingBasicItem == null)
// {
// config.routingBasicItem = new()
// {
// enableRoutingAdvanced = configOld.enableRoutingAdvanced,
// domainStrategy = configOld.domainStrategy
// };
// }

// if (config.guiItem == null)
// {
// config.guiItem = new()
// {
// enableStatistics = configOld.enableStatistics,
// keepOlderDedupl = configOld.keepOlderDedupl,
// ignoreGeoUpdateCore = configOld.ignoreGeoUpdateCore,
// autoUpdateInterval = configOld.autoUpdateInterval,
// checkPreReleaseUpdate = configOld.checkPreReleaseUpdate,
// enableSecurityProtocolTls13 = configOld.enableSecurityProtocolTls13,
// trayMenuServersLimit = configOld.trayMenuServersLimit,
// };
// }

// GetDefaultServer(config);
// GetDefaultRouting(config);
// SaveConfig(config);
// LoadConfig(ref config);

// return 0;
//}

#endregion ConfigHandler

#region Server
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/Handler/CoreHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void LoadCore(ProfileItem? node)
public int LoadCoreConfigSpeedtest(List<ServerTestItem> selecteds)
{
int pid = -1;
var coreType = selecteds.Exists(t => t.configType == EConfigType.Hysteria2 || t.configType == EConfigType.TUIC || t.configType == EConfigType.WireGuard) ? ECoreType.sing_box : ECoreType.Xray;
var coreType = selecteds.Exists(t => t.ConfigType == EConfigType.Hysteria2 || t.ConfigType == EConfigType.TUIC || t.ConfigType == EConfigType.WireGuard) ? ECoreType.sing_box : ECoreType.Xray;
string configPath = Utils.GetConfigPath(Global.CoreSpeedtestConfigFileName);
if (CoreConfigHandler.GenerateClientSpeedtestConfig(_config, configPath, selecteds, coreType, out string msg) != 0)
{
Expand Down
2 changes: 1 addition & 1 deletion v2rayN/ServiceLib/Handler/Fmt/VmessFmt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class VmessFmt : BaseFmt
result = result[Global.ProtocolShares[EConfigType.VMess].Length..];
result = Utils.Base64Decode(result);

//转成Json

VmessQRCode? vmessQRCode = JsonUtils.Deserialize<VmessQRCode>(result);
if (vmessQRCode == null)
{
Expand Down
16 changes: 8 additions & 8 deletions v2rayN/ServiceLib/Models/ClashConnectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
{
public class ClashConnectionModel
{
public string id { get; set; }
public string network { get; set; }
public string type { get; set; }
public string host { get; set; }
public string? id { get; set; }
public string? network { get; set; }
public string? type { get; set; }
public string? host { get; set; }
public ulong upload { get; set; }
public ulong download { get; set; }
public string uploadTraffic { get; set; }
public string downloadTraffic { get; set; }
public string? uploadTraffic { get; set; }
public string? downloadTraffic { get; set; }
public double time { get; set; }
public string elapsed { get; set; }
public string chain { get; set; }
public string? elapsed { get; set; }
public string? chain { get; set; }
}
}
Loading

0 comments on commit 3e74bb6

Please sign in to comment.