|
@@ -1,111 +1,109 @@
|
|
|
-using System.Net;
|
|
|
-using System.Security.Cryptography;
|
|
|
+using System.Security.Cryptography;
|
|
|
using System.Text;
|
|
|
using PCC.App.Security;
|
|
|
using PCC.App.Tpm;
|
|
|
using PCC.Common.AssemblyInject.Interfaces;
|
|
|
using PCC.Common.EventBus;
|
|
|
-using PCC.Common.Networking;
|
|
|
+using PCC.DevShared.Configuration;
|
|
|
|
|
|
namespace PCC.DevServer;
|
|
|
|
|
|
internal class DevServerApp(
|
|
|
- DevServerPccConfigManager configManager,
|
|
|
+ PccDevConfigManagerBase configManager,
|
|
|
+ IPeerInfoProvider peerInfoProvider,
|
|
|
TrustedPeerManager tpm,
|
|
|
IEventBus eventBus,
|
|
|
- ILogger<DevServerApp> logger,
|
|
|
- ILogger<KestrelTcpServer> ktsLogger) : IAssemblyInjectSyncInitStarStop
|
|
|
+ ILogger<DevServerApp> logger) : IAssemblyInjectSyncInitStarStop
|
|
|
{
|
|
|
- private KestrelTcpServer _tcpServer;
|
|
|
+ private ILocalPeerInfo? _local;
|
|
|
+ private IRemotePeerInfo? _remote;
|
|
|
|
|
|
public void Init()
|
|
|
{
|
|
|
logger.LogInformation("init");
|
|
|
- //eventBus.Subscript<TrustedPeerManager.TPM_EVT_PEER_IX>(OnIncome);
|
|
|
- //eventBus.Subscript<TrustedPeerManager.TPM_EVT_PEER_RX>(OnRx);
|
|
|
- //eventBus.Subscript<TrustedPeerManager.TPM_EVT_PEER_DX>(OnDx);
|
|
|
- //eventBus.Subscript<TrustedPeerManager.TPM_EVT_PEER_XX>(OnXx);
|
|
|
+
|
|
|
+ eventBus.Subscript<TPM_EVT_LISTENER_STATUS_CHANGED>(OnListenerStatusChanged);
|
|
|
+ eventBus.Subscript<TPM_EVT_INBOUND_CON_ACCEPTED>(OnInboundAccepted);
|
|
|
+ eventBus.Subscript<TPM_EVT_INBOUND_CON_STATUS_CHANGED>(OnInboundStatusChanged);
|
|
|
+ eventBus.Subscript<TPM_EVT_INBOUND_CON_ERROR>(OnInboundConnectionError);
|
|
|
+ eventBus.Subscript<TPM_EVT_INBOUND_RX>(OnInboundRx);
|
|
|
+ eventBus.Subscript<TPM_EVT_INBOUND_TX>(OnInboundTx);
|
|
|
}
|
|
|
|
|
|
public void Start()
|
|
|
{
|
|
|
logger.LogInformation("starting...");
|
|
|
|
|
|
- byte[] myPri, myPub;
|
|
|
if (configManager.Instance.MyKeyPrivate == null || configManager.Instance.MyKeyPublic == null)
|
|
|
{
|
|
|
logger.LogInformation("Generating 8192bit RSA key... be patient, may take up to 30 seconds...");
|
|
|
- (myPub, myPri) = RsaUtility.GeneratePKCS1(8192);
|
|
|
+ var (myPub, myPri) = RsaUtility.GeneratePKCS1(8192);
|
|
|
configManager.Instance.MyKeyPrivate = Convert.ToBase64String(myPri, Base64FormattingOptions.None);
|
|
|
configManager.Instance.MyKeyPublic = Convert.ToBase64String(myPub, Base64FormattingOptions.None);
|
|
|
configManager.Save();
|
|
|
- logger.LogInformation("RSA key generated and saved.");
|
|
|
+ logger.LogInformation("RSA key generated and saved. Please fill conf and RESTART.");
|
|
|
}
|
|
|
- else
|
|
|
+
|
|
|
+
|
|
|
+ _local = peerInfoProvider.PeerInfo.FirstOrDefault();
|
|
|
+ if (_local != null)
|
|
|
{
|
|
|
- myPri = Convert.FromBase64String(configManager.Instance.MyKeyPrivate);
|
|
|
- myPub = Convert.FromBase64String(configManager.Instance.MyKeyPublic);
|
|
|
+ _remote = _local.TrustedRemotePeers.FirstOrDefault();
|
|
|
+
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ private void OnInboundTx(TPM_EVT_INBOUND_TX obj)
|
|
|
+ {
|
|
|
+ logger.LogInformation($"IB Tx L{obj.LocalPeerId} R{obj.RemotePeerId} {Encoding.UTF8.GetString(obj.payload.Span)}");
|
|
|
|
|
|
- //eventBus.Publish(new TrustedPeerManager.TPM_EVT_CMD_INIT(myPub, myPri));
|
|
|
-
|
|
|
- //logger.LogInformation("Your public key for share to trusted peer: " + configManager.Instance.MyKeyPublic);
|
|
|
-
|
|
|
- //if (configManager.Instance.ListenPort == null || false == IPAddress.TryParse(configManager.Instance.ListenAddress, out _))
|
|
|
- //{
|
|
|
- // logger.LogError($"Missing config `{nameof(configManager.Instance.ListenPort)}' or `{nameof(configManager.Instance.ListenAddress)}', HALT");
|
|
|
- // return;
|
|
|
- //}
|
|
|
-
|
|
|
- //if (configManager.Instance.TrustPeerKeyPub == null)
|
|
|
- //{
|
|
|
- // logger.LogError($"Missing config `{nameof(configManager.Instance.TrustPeerKeyPub)}', please obtain from trusted peer, HALT");
|
|
|
- // return;
|
|
|
- //}
|
|
|
-
|
|
|
- ////warn just example for server
|
|
|
- //tpm.AddPeer(Convert.FromBase64String(configManager.Instance.TrustPeerKeyPub), "", 0);
|
|
|
-
|
|
|
- //_tcpServer = new KestrelTcpServer(configManager.Instance.ListenAddress, configManager.Instance.ListenPort.Value, tpm.HandleIncomingPeerAsync, ktsLogger);
|
|
|
- //Task.Run(async () =>
|
|
|
- //{
|
|
|
- // try
|
|
|
- // {
|
|
|
- // await _tcpServer.StartAsync();
|
|
|
- // }
|
|
|
- // catch (Exception e)
|
|
|
- // {
|
|
|
- // logger.LogError(e, "Start TCP server");
|
|
|
- // }
|
|
|
- //});
|
|
|
}
|
|
|
|
|
|
- //private void OnIncome(TrustedPeerManager.TPM_EVT_PEER_IX obj)
|
|
|
- //{
|
|
|
- // logger.LogInformation("Income:" + obj.PeerId);
|
|
|
- //}
|
|
|
+ private void OnInboundRx(TPM_EVT_INBOUND_RX obj)
|
|
|
+ {
|
|
|
+ logger.LogInformation($"IB Rx L{obj.LocalPeerId} R{obj.RemotePeerId} {Encoding.UTF8.GetString(obj.payload.Span)}");
|
|
|
|
|
|
- //private void OnRx(TrustedPeerManager.TPM_EVT_PEER_RX obj)
|
|
|
- //{
|
|
|
- // logger.LogInformation($"Rx from <{obj.PeerId}>");
|
|
|
- // logger.LogInformation($"Rx content: {Encoding.UTF8.GetString(obj.payload.Span)}");
|
|
|
- // tpm.SendToPeer(obj.PeerId, SHA256.HashData(obj.payload.Span));
|
|
|
- //}
|
|
|
+ tpm.SendToPeer(_local.PeerId, obj.RemotePeerId, SHA256.HashData(obj.payload.Span));
|
|
|
+ }
|
|
|
|
|
|
- //private void OnDx(TrustedPeerManager.TPM_EVT_PEER_DX obj)
|
|
|
- //{
|
|
|
- // logger.LogInformation("Disconnected:" + obj.PeerId);
|
|
|
- //}
|
|
|
+ private void OnInboundConnectionError(TPM_EVT_INBOUND_CON_ERROR obj)
|
|
|
+ {
|
|
|
+ logger.LogError(obj.Exception, $"IB ERR L{obj.LocalPeerId} R{obj.RemotePeerId} C{obj.ConnectionId}");
|
|
|
+ }
|
|
|
|
|
|
- //private void OnXx(TrustedPeerManager.TPM_EVT_PEER_XX obj)
|
|
|
- //{
|
|
|
- // logger.LogWarning($"有内鬼,终止交易! {obj.Kind} {obj.PeerId}");
|
|
|
- //}
|
|
|
+ private void OnInboundStatusChanged(TPM_EVT_INBOUND_CON_STATUS_CHANGED obj)
|
|
|
+ {
|
|
|
+ switch (obj.Status)
|
|
|
+ {
|
|
|
+ case TPM_EVT_INBOUND_CON_STATUS.HANDSHAKE_FAIL_NOT_TRUSTED:
|
|
|
+ case TPM_EVT_INBOUND_CON_STATUS.SECURE_ALERT_REPLAY_ATTACK_DETECT:
|
|
|
+ logger.LogError($"IB ST {obj.Status} L{obj.LocalPeerId} R{obj.RemotePeerId} C{obj.ConnectionId}");
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ case TPM_EVT_INBOUND_CON_STATUS.INVALID:
|
|
|
+ case TPM_EVT_INBOUND_CON_STATUS.SECURE_WARN_TIMESTAMP_SKEW:
|
|
|
+ logger.LogWarning($"IB ST {obj.Status} L{obj.LocalPeerId} R{obj.RemotePeerId} C{obj.ConnectionId}");
|
|
|
+ break;
|
|
|
+
|
|
|
+ case TPM_EVT_INBOUND_CON_STATUS.HANDSHAKE_OK:
|
|
|
+ case TPM_EVT_INBOUND_CON_STATUS.DISCONNECTED:
|
|
|
+ logger.LogInformation($"IB ST {obj.Status} L{obj.LocalPeerId} R{obj.RemotePeerId} C{obj.ConnectionId}");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void OnInboundAccepted(TPM_EVT_INBOUND_CON_ACCEPTED obj)
|
|
|
+ {
|
|
|
+ logger.LogInformation($"Inbound accepted {obj.RemoteEndpoint} {obj.ConnectionId}");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void OnListenerStatusChanged(TPM_EVT_LISTENER_STATUS_CHANGED obj)
|
|
|
+ {
|
|
|
+ logger.LogInformation($"Listener {obj.Status} {obj.Exception?.Message}");
|
|
|
+ }
|
|
|
|
|
|
public void Stop()
|
|
|
{
|
|
|
- // logger.LogInformation("stop");
|
|
|
- // _ = _tcpServer.StopAsync(); //FAF
|
|
|
- // eventBus.Publish(new TrustedPeerManager.TPM_EVT_CMD_SHUTDOWN());
|
|
|
}
|
|
|
}
|