Example #1
0
        static void Net_Test6_DualStack_Client()
        {
            string hostname = "www.google.com";

            using (var tcp = LocalNet.ConnectIPv4v6Dual(new TcpConnectParam(hostname, 443, connectTimeout: 5 * 1000)))
            {
                tcp.Info.GetValue <ILayerInfoIpEndPoint>().RemoteIPAddress !.AddressFamily.ToString()._Print();

                using (SslSock ssl = new SslSock(tcp))
                {
                    var sslClientOptions = new PalSslClientAuthenticationOptions()
                    {
                        TargetHost = hostname,
                        ValidateRemoteCertificateProc = (cert) => { return(true); },
                    };

                    ssl.StartSslClient(sslClientOptions);

                    var st = ssl.GetStream();

                    var w = new StreamWriter(st);
                    var r = new StreamReader(st);

                    w.WriteLine("GET / HTTP/1.0");
                    w.WriteLine($"HOST: {hostname}");
                    w.WriteLine();
                    w.WriteLine();
                    w.Flush();

                    while (true)
                    {
                        string?s = r.ReadLine();
                        if (s == null)
                        {
                            break;
                        }

                        Con.WriteLine(s);
                    }
                }
            }
        }
Example #2
0
        static void Net_Test2_Ssl_Client()
        {
            string hostname = "www.google.co.jp";

            using (ConnSock sock = LocalNet.Connect(new TcpConnectParam(hostname, 443)))
            {
                using (SslSock ssl = new SslSock(sock))
                {
                    //ssl.StartPCapRecorder(new PCapFileEmitter(new PCapFileEmitterOptions(new FilePath(@"c:\tmp\190610\test1.pcapng", flags: FileFlags.AutoCreateDirectory), false)));
                    var sslClientOptions = new PalSslClientAuthenticationOptions()
                    {
                        TargetHost = hostname,
                        ValidateRemoteCertificateProc = (cert) => { return(true); },
                    };

                    ssl.StartSslClient(sslClientOptions);

                    var st = ssl.GetStream();

                    var w = new StreamWriter(st);
                    var r = new StreamReader(st);

                    w.WriteLine("GET / HTTP/1.0");
                    w.WriteLine($"HOST: {hostname}");
                    w.WriteLine();
                    w.WriteLine();
                    w.Flush();

                    while (true)
                    {
                        string?s = r.ReadLine();
                        if (s == null)
                        {
                            break;
                        }

                        Con.WriteLine(s);
                    }
                }
            }
        }
Example #3
0
            protected override async Task SslAcceptedImplAsync(NetTcpListenerPort listener, SslSock sock)
            {
                using (var stream = sock.GetStream())
                    using (var r = new StreamReader(stream))
                        using (var w = new StreamWriter(stream))
                        {
                            while (true)
                            {
                                string?recv = await r.ReadLineAsync();

                                if (recv == null)
                                {
                                    return;
                                }

                                Con.WriteLine(recv);

                                await w.WriteLineAsync("[" + recv + "]\r\n");

                                await w.FlushAsync();
                            }
                        }
            }
    protected override async Task SslAcceptedImplAsync(NetTcpListenerPort listener, SslSock sock)
    {
        using (PipeStream st = sock.GetStream())
        {
            await sock.AttachHandle.SetStreamReceiveTimeoutAsync(this.Options.RecvTimeout);

            int magicNumber = await st.ReceiveSInt32Async();

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

            int clientVersion = await st.ReceiveSInt32Async();

            var accessKeyData = await st.ReceiveAllAsync(256);

            if (accessKeyData._GetString_UTF8(untilNullByte: true)._IsSame(this.Options.AccessKey) == false)
            {
                throw new ApplicationException($"Invalid access key");
            }

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

            SizedDataQueue <Memory <byte> > standardDataQueue = new SizedDataQueue <Memory <byte> >();
            try
            {
                while (true)
                {
                    if (standardDataQueue.CurrentTotalSize >= CoresConfig.DataVaultProtocolSettings.BufferingSizeThresholdPerServer || st.IsReadyToReceive(sizeof(int)) == false)
                    {
                        var list = standardDataQueue.GetList();
                        standardDataQueue.Clear();
                        await DataVaultDataReceivedInternalAsync(list);
                    }

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

                    switch (type)
                    {
                    case DataVaultProtocolDataType.StandardData:
                    {
                        int size = await st.ReceiveSInt32Async();

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

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

                        await st.ReceiveAllAsync(data);

                        standardDataQueue.Add(data, data.Length);

                        break;
                    }

                    case DataVaultProtocolDataType.KeepAlive:
                        break;

                    default:
                        throw new ApplicationException("Invalid DataVaultProtocolDataType");
                    }
                }
            }
            finally
            {
                await DataVaultDataReceivedInternalAsync(standardDataQueue.GetList());
            }
        }
    }