12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using System.Net.Security;
- using System.Security.Authentication;
- using System.Security.Cryptography.X509Certificates;
- using InABox.Core;
- using WebSocketSharp;
- using WebSocketSharp.Server;
- using Logger = InABox.Core.Logger;
- namespace InABox.Rpc
- {
- public class RpcServerSocketTransport : RpcServerTransport<RpcServerSocketConnection>
- {
- private WebSocketServer? _server;
- public static X509Certificate2? Certificate { get; set; }
-
- //public static void InitCertificate(string certificateFile) => Certificate = new X509Certificate2(certificateFile);
-
- public override bool IsSecure() => Certificate != null;
-
- public RpcServerSocketTransport(int port, X509Certificate2? certificate = null)
- {
-
- Certificate = certificate;
-
- _server = new WebSocketServer(port, Certificate != null);
- if (Certificate != null)
- {
- _server.SslConfiguration.ServerCertificate = Certificate;
- _server.SslConfiguration.ClientCertificateRequired = false;
- _server.SslConfiguration.CheckCertificateRevocation = false;
- _server.SslConfiguration.ClientCertificateValidationCallback = WSSCallback;
- _server.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls12;
- }
- _server?.AddWebSocketService<RpcServerSocketConnection>("/", (connection) =>
- {
- connection.Transport = this;
- //new RpcServerSocketConnection() { Transport = this };
- });
- }
- private bool WSSCallback(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslpolicyerrors)
- {
- return true;
- }
- public override void Start()
- {
- _server?.Start();
- }
- public override void Send(RpcServerSocketConnection connection, RpcMessage message)
- {
- connection.Send(message);
- }
- public override void Stop()
- {
- _server?.Stop();
- }
- public void ConnectionOpened(RpcServerSocketConnection connection)
- => DoOpen(connection);
- public void ConnectionException(RpcServerSocketConnection connection, Exception e)
- => DoException(connection, e);
- public void ConnectionClosed(RpcServerSocketConnection connection, CloseEventArgs e)
- => DoClose(connection, (e.Code == 1000) ? RpcTransportCloseEventType.Closed : RpcTransportCloseEventType.Error);
-
- }
- }
|