NoSugarNet/Sample/NoSugarNet.ServerCli/Config.cs

71 lines
2.5 KiB
C#
Raw Normal View History

using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
2024-01-22 15:08:43 +08:00
namespace NoSugarNet.ServerCli
{
public class ConfigDataModel
{
public int ServerPort { get; set; }
2024-01-25 17:17:16 +08:00
public int CompressAdapterType { get; set; }
public List<ConfigDataModel_Single> TunnelList { get; set; }
}
public class ConfigDataModel_Single
{
public string ServerLocalTargetIP { get; set; }
public int ServerLocalTargetPort { get; set; }
public int ClientLocalPort { get; set; }
}
2024-01-22 15:08:43 +08:00
public static class Config
{
public static ConfigDataModel cfg;
2024-01-22 15:08:43 +08:00
public static bool LoadConfig()
{
try
{
string path = System.Environment.CurrentDirectory + "//config.cfg";
if (!File.Exists(path))
2024-01-22 15:08:43 +08:00
{
ConfigDataModel sampleCfg = new ConfigDataModel
2024-01-22 15:08:43 +08:00
{
ServerPort = 1000,
TunnelList = new List<ConfigDataModel_Single>()
2024-01-22 15:08:43 +08:00
{
new ConfigDataModel_Single(){ ServerLocalTargetIP = "127.0.0.1",ServerLocalTargetPort=3389,ClientLocalPort = 10001},
new ConfigDataModel_Single(){ ServerLocalTargetIP = "127.0.0.1",ServerLocalTargetPort=3389,ClientLocalPort = 10002}
}
};
string jsonString = JsonSerializer.Serialize(sampleCfg, new JsonSerializerOptions()
2024-01-22 15:08:43 +08:00
{
// 整齐打印
WriteIndented = true,
//重新编码,解决中文乱码问题
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
});
System.IO.File.WriteAllText(path, jsonString, Encoding.UTF8);
Console.WriteLine("未找到配置,已生成模板,请浏览" + path);
return false;
2024-01-22 15:08:43 +08:00
}
StreamReader sr = new StreamReader(path, Encoding.Default);
String jsonstr = sr.ReadToEnd();
cfg = JsonSerializer.Deserialize<ConfigDataModel>(jsonstr);
2024-01-22 15:08:43 +08:00
sr.Close();
if (cfg?.TunnelList.Count > 0)
2024-01-22 15:08:43 +08:00
return true;
else
return false;
}
catch (Exception ex)
{
Console.WriteLine("配置文件异常:" + ex.ToString());
return false;
}
}
}
}