Skip to content

Commit 6dab8e9

Browse files
committed
添加加密支持
1 parent 1f985a2 commit 6dab8e9

8 files changed

Lines changed: 100 additions & 49 deletions

File tree

README.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
# [ColoryrWork](https://github.com/Coloryr/ColoryrWork)
22
![ico](./ColoryrWork.png)
33

4-
2.0.0版本正在进行中
5-
6-
一个多功能服务器/应用框架
4+
**2.0.0版本制作正在进行中**
75

86
## ColoryrServer
9-
在线动态编译.多功能.服务器框架
7+
在线动态编译.多功能.应用服务器框架
108
**服务器框架内不包含任何业务代码,需要用户自行编写**
119

1210
![截图](./doc/pic/work.png)
1311

1412
- 这是一个中型服务器
15-
- 支持Linux和Windows下运行
16-
- 可以对接Mysql\Redis\MsSql\Oracle数据库
17-
- 自带Http\WebSocket\Socket\Mqtt支持
18-
- 可以添加Ssl证书
19-
- 可以对接[ColorMirai](https://github.com/Coloryr/ColorMirai) QQ机器人
13+
- 支持`Linux`下运行
14+
- 可以对接`Mysql\Redis\MsSql\Oracle\Sqlite`数据库
15+
- 自带`Http\WebSocket\Socket\Mqtt\Netty`支持
16+
- 可以添加`Ssl证书`
17+
- 可以对接[ColorMirai](https://github.com/Coloryr/ColorMirai)QQ机器人
2018
- 占用内存极少
2119
- 可以配置端口反向代理和域名反向代理
2220
- 业务代码修改无需重启
23-
- 可以自己添加DLL库,并在端口文件中调用
21+
- 可以自己添加DLL库,并在代码文件中调用
22+
- 自带`Vue项目`编译与上线
2423

2524
[理论性能测试](./doc/test.md)
2625

@@ -29,9 +28,6 @@
2928
[目录结构与文件信息](./doc/config.md)
3029

3130
[业务代码编写](./doc/code.md)
32-
```
33-
注:net6正在生产环境测试中,目前暂未发现问题
34-
```
3531

3632
## ColoryrApp(在计划中)
3733
动态加载App

doc/config.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
"Heads": {}
2222
}
2323
},
24-
//是否启用SSL
25-
"Ssl": false,
26-
//SSL配置
24+
//是否启用Http的SSL
25+
"UseSsl": false,
26+
//Http的SSL配置
2727
"Ssls": {
2828
"default": {
29-
"SslLocal": "./test.sfx",
30-
"SslPassword": "123456"
29+
"Ssl": "./test.sfx",
30+
"Password": "123456"
3131
}
3232
},
3333
//域名转发设置
@@ -48,8 +48,13 @@
4848
},
4949
//WebSocket服务器地址
5050
"WebSocket": {
51-
"IP": "0.0.0.0",
52-
"Port": 25557
51+
"UseSsl": false,
52+
"Ssl": "",
53+
"Password": "",
54+
"Socket": {
55+
"IP": "0.0.0.0",
56+
"Port": 25557
57+
}
5358
},
5459
//机器人链接地址
5560
"Robot": {
@@ -159,6 +164,13 @@
159164
}
160165
```
161166

167+
## 加密证书配置
168+
169+
修改`Ssl`为证书位置
170+
修改`Password`为证书密码
171+
修改`UseSsl`为true,重启服务器
172+
加密版本为`Tls1.3`,需要你的证书支持才行
173+
162174
## 登录数据库
163175

164176
管理员数据,登录信息储存在`ColoryrServer\Login.db`
@@ -200,3 +212,4 @@
200212
- `FileRam`内存数据库固化
201213
- `Libs`额外CLR库,可以用于Dll调用,只能在关闭服务器期间更换
202214
- `Notes`导出的说明信息
215+

src/ColoryrServer/ASP/ASPConfig.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
namespace ColoryrServer.ASP
77
{
8-
internal record Ssl
8+
internal record SslObj
99
{
10-
public string SslLocal { get; set; }
11-
public string SslPassword { get; set; }
10+
public string Ssl { get; set; }
11+
public string Password { get; set; }
1212
}
1313
internal record ASPConfig : MainConfig
1414
{
@@ -17,8 +17,8 @@ internal record ASPConfig : MainConfig
1717
/// </summary>
1818
public List<SocketConfig> Http { get; set; }
1919
public Dictionary<string, RouteConfigObj> Routes { get; set; }
20-
public bool Ssl { get; set; }
21-
public Dictionary<string, Ssl> Ssls { get; set; }
20+
public bool UseSsl { get; set; }
21+
public Dictionary<string, SslObj> Ssls { get; set; }
2222
public Dictionary<string, RouteConfigObj> UrlRoutes { get; set; }
2323
public bool RouteEnable { get; set; }
2424
public bool NoInput { get; set; }
@@ -53,15 +53,15 @@ public override void Start()
5353
}
5454
},
5555
RouteEnable = false,
56-
Ssl = false,
56+
UseSsl = false,
5757
Ssls = new()
5858
{
5959
{
6060
"default",
6161
new()
6262
{
63-
SslLocal = "./test.sfx",
64-
SslPassword = "123456"
63+
Ssl = "./test.sfx",
64+
Password = "123456"
6565
}
6666
}
6767
},
@@ -92,8 +92,14 @@ public override void Start()
9292
},
9393
WebSocket = new()
9494
{
95-
IP = "0.0.0.0",
96-
Port = 25557
95+
Ssl = "",
96+
Password = "",
97+
UseSsl = false,
98+
Socket = new()
99+
{
100+
IP = "0.0.0.0",
101+
Port = 25557
102+
}
97103
},
98104
Robot = new()
99105
{

src/ColoryrServer/ASP/Program.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static void Main()
4444
var builder = WebApplication.CreateBuilder();
4545
builder.Logging.ClearProviders();
4646
builder.Logging.AddProvider(new ColoryrLoggerProvider());
47-
if (Config.Ssl)
47+
if (Config.UseSsl)
4848
{
4949
builder.Services.AddCertificateForwarding(options =>
5050
{
@@ -54,8 +54,7 @@ public static void Main()
5454
if (!string.IsNullOrWhiteSpace(headerValue))
5555
{
5656
byte[] bytes = StringToByteArray(headerValue);
57-
var clientCertificate = new X509Certificate2(bytes);
58-
return clientCertificate;
57+
return new X509Certificate2(bytes);
5958
}
6059
return null;
6160
};
@@ -66,11 +65,11 @@ public static void Main()
6665
i.ServerCertificateSelector = ASPServer.Ssl));
6766
foreach (var item in Config.Ssls)
6867
{
69-
if (File.Exists(item.Value.SslLocal))
68+
if (File.Exists(item.Value.Ssl))
7069
{
7170
try
7271
{
73-
var ssl = new X509Certificate2(item.Value.SslLocal, item.Value.SslPassword);
72+
var ssl = new X509Certificate2(item.Value.Ssl, item.Value.Password);
7473
if (item.Key == "default")
7574
DefaultSsl = ssl;
7675
else
@@ -83,15 +82,15 @@ public static void Main()
8382
}
8483
else
8584
{
86-
ServerMain.LogError($"SSL证书找不到:{item.Value.SslLocal}");
85+
ServerMain.LogError($"SSL证书找不到:{item.Value.Ssl}");
8786
}
8887
}
8988
}
9089
string[] urls = new string[Config.Http.Count];
9190
for (int a = 0; a < Config.Http.Count; a++)
9291
{
9392
var item = Config.Http[a];
94-
urls[a] = $"{(Config.Ssl ? https : http)}://{item.IP}:{item.Port}/";
93+
urls[a] = $"{(Config.UseSsl ? https : http)}://{item.IP}:{item.Port}/";
9594
ServerMain.LogOut($"Http服务器监听{item.IP}:{item.Port}");
9695
}
9796

src/ColoryrServer/Core/FileSystem/ConfigUtil.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public abstract record MainConfig
1212
/// <summary>
1313
/// WebSocket配置
1414
/// </summary>
15-
public SocketConfig WebSocket { get; set; }
15+
public SslConfigObj WebSocket { get; set; }
1616
/// <summary>
1717
/// Reboot配置
1818
/// </summary>
@@ -40,7 +40,7 @@ public abstract record MainConfig
4040
/// <summary>
4141
/// MQTT配置
4242
/// </summary>
43-
public MqttConfigObj MqttConfig { get; set; }
43+
public SslConfigObj MqttConfig { get; set; }
4444
/// <summary>
4545
/// 任务配置
4646
/// </summary>
@@ -63,7 +63,7 @@ public abstract record MainConfig
6363
public CodeConfigObj CodeSetting { get; set; }
6464
}
6565

66-
public record MqttConfigObj
66+
public record SslConfigObj
6767
{
6868
/// <summary>
6969
/// 使用SSL证书

src/ColoryrServer/Core/Http/PostBuild/PostServerConfig.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static SocketObj GetSocketConfig()
6363
{
6464
Socket = ServerMain.Config.Socket,
6565
Mqtt = ServerMain.Config.MqttConfig.Socket,
66-
WebSocket = ServerMain.Config.WebSocket
66+
WebSocket = ServerMain.Config.WebSocket.Socket
6767
};
6868
}
6969
public static ReMessage WebSetSocket(BuildOBJ json)
@@ -110,8 +110,8 @@ public static ReMessage WebSetSocket(BuildOBJ json)
110110
}
111111
else if (json.Text is "WebSocket")
112112
{
113-
ServerMain.Config.WebSocket.IP = ip;
114-
ServerMain.Config.WebSocket.Port = port;
113+
ServerMain.Config.WebSocket.Socket.IP = ip;
114+
ServerMain.Config.WebSocket.Socket.Port = port;
115115
ServerMain.ConfigUtil.Save();
116116
return new()
117117
{

src/ColoryrServer/Core/PortServer/PortMqttServer.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using MQTTnet.Server;
66
using System.Net;
77
using System.Security.Authentication;
8+
using System.Security.Cryptography;
89
using System.Security.Cryptography.X509Certificates;
910
using System.Text;
1011
using System.Threading.Tasks;
@@ -22,10 +23,18 @@ public static async void Start()
2223
.WithDefaultEndpointPort(ServerMain.Config.MqttConfig.Socket.Port);
2324
if (ServerMain.Config.MqttConfig.UseSsl)
2425
{
25-
optionsBuilder = optionsBuilder.WithEncryptionSslProtocol(SslProtocols.Tls13)
26-
.WithEncryptionCertificate(
27-
new X509Certificate2(ServerMain.Config.MqttConfig.Ssl,
28-
ServerMain.Config.MqttConfig.Password));
26+
try
27+
{
28+
optionsBuilder = optionsBuilder.WithEncryptionSslProtocol(SslProtocols.Tls13)
29+
.WithEncryptionCertificate(new X509Certificate2(ServerMain.Config.MqttConfig.Ssl,
30+
ServerMain.Config.MqttConfig.Password));
31+
ServerMain.LogOut($"Mqtt服务器加载SSL证书{ServerMain.Config.MqttConfig.Ssl}");
32+
}
33+
catch (CryptographicException e)
34+
{
35+
ServerMain.LogError($"Mqtt服务器加载SSL证书{ServerMain.Config.MqttConfig.Ssl}错误");
36+
ServerMain.LogError(e);
37+
}
2938
}
3039
ServerMain.LogOut($"Mqtt服务器监听{ServerMain.Config.MqttConfig.Socket.IP}:{ServerMain.Config.MqttConfig.Socket.Port}");
3140
MqttServer = new MqttFactory().CreateMqttServer(optionsBuilder.Build());

src/ColoryrServer/Core/PortServer/PortWebSocket.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
using ColoryrServer.Core.DllManager;
22
using ColoryrServer.SDK;
33
using Fleck;
4+
using Org.BouncyCastle.Crypto.Tls;
45
using System;
56
using System.Collections.Generic;
7+
using System.IO;
68
using System.Linq;
9+
using System.Security.Authentication;
10+
using System.Security.Cryptography;
11+
using System.Security.Cryptography.X509Certificates;
712

813
namespace ColoryrServer.Core.PortServer;
914

@@ -65,8 +70,31 @@ public static void Start()
6570
{
6671
ServerMain.LogOut("WebScoket服务器正在启动");
6772
FleckLog.Level = LogLevel.Error;
68-
Server = new WebSocketServer("ws://" + ServerMain.Config.WebSocket.IP + ":" + ServerMain.Config.WebSocket.Port);
69-
ServerMain.LogOut($"WebScoket监听{ServerMain.Config.WebSocket.IP}:{ServerMain.Config.WebSocket.Port}");
73+
string url = ServerMain.Config.WebSocket.Socket.IP + ":" + ServerMain.Config.WebSocket.Socket.Port;
74+
if (ServerMain.Config.WebSocket.UseSsl && File.Exists(ServerMain.Config.WebSocket.Ssl))
75+
{
76+
try
77+
{
78+
var ssl = new X509Certificate2(ServerMain.Config.WebSocket.Ssl, ServerMain.Config.WebSocket.Password);
79+
Server = new WebSocketServer("wss://" + url)
80+
{
81+
EnabledSslProtocols = SslProtocols.Tls13,
82+
Certificate = ssl
83+
};
84+
ServerMain.LogOut($"WebScoket使用SSL证书{ServerMain.Config.WebSocket.Ssl}");
85+
}
86+
catch (CryptographicException e)
87+
{
88+
ServerMain.LogError($"WebScoket使用SSL证书{ServerMain.Config.WebSocket.Ssl}错误");
89+
ServerMain.LogError(e);
90+
}
91+
}
92+
else
93+
{
94+
Server = new WebSocketServer("ws://" + url);
95+
}
96+
97+
ServerMain.LogOut($"WebScoket监听{url}");
7098
Server.Start(Socket =>
7199
{
72100
Socket.OnOpen = () =>

0 commit comments

Comments
 (0)