示例#1
0
        // 1 つの SSL 接続試行を処理する非同期関数
        async Task PerformOneAsync(SslCertCollectorItem e, CancellationToken cancel = default)
        {
            using (ConnSock sock = await TcpIp.ConnectAsync(new TcpConnectParam(IPAddress.Parse(e.IpAddress), e.Port, connectTimeout: 5000), cancel))
            {
                using (SslSock ssl = await sock.SslStartClientAsync(new PalSslClientAuthenticationOptions(e.SniHostName, true)))
                {
                    ILayerInfoSsl      sslInfo = ssl.Info.Ssl;
                    PalX509Certificate cert    = sslInfo.RemoteCertificate !;

                    Certificate cert2 = cert.PkiCertificate;

                    e.CertIssuer  = cert2.CertData.IssuerDN.ToString()._MakeAsciiOneLinePrintableStr();
                    e.CertSubject = cert2.CertData.SubjectDN.ToString()._MakeAsciiOneLinePrintableStr();

                    e.CertFqdnList  = cert2.HostNameList.Select(x => x.HostName)._Combine(",")._MakeAsciiOneLinePrintableStr();
                    e.CertHashSha1  = cert2.DigestSHA1Str;
                    e.CertNotAfter  = cert2.CertData.NotAfter;
                    e.CertNotBefore = cert2.CertData.NotBefore;

                    // 無視リストに含まれないものだけを出力
                    if (Consts.Strings.AutoEnrollCertificateSubjectInStrList.Where(x => e.CertIssuer._InStr(x, true)).Any() == false)
                    {
                        this.ResultList.Add(e);
                    }

                    $"OK: {e.SniHostName}:{e.Port} => {e._ObjectToJson(compact: true)}"._Print();
                }
            }
        }
        public static async Task <SslSock> SslStartClientAsync(this ConnSock baseSock, PalSslClientAuthenticationOptions sslOptions, CancellationToken cancel = default)
        {
            SslSock ret = new SslSock(baseSock);

            await ret.StartSslClientAsync(sslOptions, cancel);

            return(ret);
        }
        protected sealed override async Task TcpAcceptedImplAsync(NetTcpListenerPort listener, ConnSock s)
        {
            using (SslSock ssl = new SslSock(s))
            {
                await ssl.StartSslServerAsync(this.Options.SslServerAuthenticationOptions, s.GrandCancel);

                ssl.UpdateSslSessionInfo();

                await SslAcceptedImplAsync(listener, ssl);
            }
        }
 protected abstract Task SslAcceptedImplAsync(NetTcpListenerPort listener, SslSock sock);
示例#5
0
        public static async Task <WebSocket> ConnectAsync(string uri, WebSocketConnectOptions?options = null, CancellationToken cancel = default)
        {
            if (options == null)
            {
                options = new WebSocketConnectOptions();
            }

            Uri u = new Uri(uri);

            int  port   = 0;
            bool useSsl = false;

            if (u.Scheme._IsSamei("ws"))
            {
                port = Consts.Ports.Http;
            }
            else if (u.Scheme._IsSamei("wss"))
            {
                port   = Consts.Ports.Https;
                useSsl = true;
            }
            else
            {
                throw new ArgumentException($"uri \"{uri}\" is not a WebSocket address.");
            }

            if (u.IsDefaultPort == false)
            {
                port = u.Port;
            }

            ConnSock tcpSock = await LocalNet.ConnectIPv4v6DualAsync(new TcpConnectParam(u.Host, port, connectTimeout : options.WebSocketOptions.TimeoutOpen, dnsTimeout : options.WebSocketOptions.TimeoutOpen), cancel);

            try
            {
                ConnSock targetSock = tcpSock;

                try
                {
                    if (useSsl)
                    {
                        SslSock sslSock = new SslSock(tcpSock);
                        try
                        {
                            options.SslOptions.TargetHost = u.Host;

                            await sslSock.StartSslClientAsync(options.SslOptions, cancel);

                            targetSock = sslSock;
                        }
                        catch
                        {
                            sslSock._DisposeSafe();
                            throw;
                        }
                    }

                    WebSocket webSock = new WebSocket(targetSock, options.WebSocketOptions);

                    await webSock.StartWebSocketClientAsync(uri, cancel);

                    return(webSock);
                }
                catch
                {
                    targetSock.Dispose();
                    throw;
                }
            }
            catch
            {
                tcpSock._DisposeSafe();
                throw;
            }
        }
        protected override async Task SslAcceptedImplAsync(NetTcpListenerPort listener, SslSock sock)
        {
            using (PipeStream st = sock.GetStream())
            {
                sock.AttachHandle.SetStreamReceiveTimeout(this.Options.RecvTimeout);

                int magicNumber = await st.ReceiveSInt32Async();

                if (magicNumber != MagicNumber)
                {
                    throw new ApplicationException($"Invalid magicNumber = 0x{magicNumber:X}");
                }

                int clientVersion = await st.ReceiveSInt32Async();

                MemoryBuffer <byte> sendBuffer = new MemoryBuffer <byte>();
                sendBuffer.WriteSInt32(MagicNumber);
                sendBuffer.WriteSInt32(ServerVersion);
                await st.SendAsync(sendBuffer);

                SizedDataQueue <Memory <byte> > standardLogQueue = new SizedDataQueue <Memory <byte> >();
                try
                {
                    while (true)
                    {
                        if (standardLogQueue.CurrentTotalSize >= CoresConfig.LogProtocolSettings.BufferingSizeThresholdPerServer || st.IsReadyToReceive(sizeof(int)) == false)
                        {
                            var list = standardLogQueue.GetList();
                            standardLogQueue.Clear();
                            await LogDataReceivedInternalAsync(sock.EndPointInfo.RemoteIP._NonNullTrim(), list);
                        }

                        LogProtocolDataType type = (LogProtocolDataType)await st.ReceiveSInt32Async();

                        switch (type)
                        {
                        case LogProtocolDataType.StandardLog:
                        {
                            int size = await st.ReceiveSInt32Async();

                            if (size > CoresConfig.LogProtocolSettings.MaxDataSize)
                            {
                                throw new ApplicationException($"size > MaxDataSize. size = {size}");
                            }

                            Memory <byte> data = new byte[size];

                            await st.ReceiveAllAsync(data);

                            standardLogQueue.Add(data, data.Length);

                            break;
                        }

                        case LogProtocolDataType.KeepAlive:
                            break;

                        default:
                            throw new ApplicationException("Invalid LogProtocolDataType");
                        }
                    }
                }
                finally
                {
                    await LogDataReceivedInternalAsync(sock.EndPointInfo.RemoteIP._NonNullTrim(), standardLogQueue.GetList());
                }
            }
        }