Comments (7)
in tls/conn.go, after reading the handshake around line 1356, c.readRecord returns no data available.
With the other clients, this method returns data
from nbio.
found the problem I think but I don't have a fix yet and I have to sign-off. If you look at the pcap, the client cipher comes in with the encrypted handshake data in the same packet. I believe your code isn't able to handle when the handle shake comes in with the encrypted data. With other clients the encrypted handshake data comes in, in a separate packet.
from nbio.
I have built an exe and reproduced it, will debug it.
from nbio.
also failed with gorilla server on win10:
set GOHOSTARCH=amd64
set GOHOSTOS=windows
go version go1.16 windows/amd64
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{}
func echo(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
for {
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
err = c.WriteMessage(mt, message)
if err != nil {
log.Println("write:", err)
break
}
fmt.Println("onEcho:", string(message))
}
}
func main() {
addr := "localhost:8888"
mux := &http.ServeMux{}
mux.HandleFunc("/ws", echo)
server := http.Server{
Addr: addr,
Handler: mux,
}
log.Println("running on:", addr)
log.Fatalln(server.ListenAndServeTLS("server.crt", "server.key"))
}
when I debug, the tls handshake succeeded for golang, but got the dwError = 12175(ERROR_WINHTTP_SECURE_FAILURE) for c++.
what's your windows version?
did you get the same error 12175(ERROR_WINHTTP_SECURE_FAILURE)?
from nbio.
I was testing with a letsencrypt server cert so the windows client accepted it. I figured out how to disable the server cert verification in the c++ client app
to disabled the server cert verification add
DWORD dwSSLFlag;
dwSSLFlag = SECURITY_FLAG_IGNORE_UNKNOWN_CA;
dwSSLFlag |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
dwSSLFlag |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
dwSSLFlag |= SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE;
WinHttpSetOption(hRequestHandle, WINHTTP_OPTION_SECURITY_FLAGS,
&dwSSLFlag, sizeof(dwSSLFlag));
after hRequestHandle is initialized.
here is the complete code
// winhttpExample.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#pragma comment(lib, "winhttp")
#include <Windows.h>
#include <WinHttp.h>
#include <stdio.h>
int __cdecl wmain()
{
DWORD dwError = ERROR_SUCCESS;
BOOL fStatus = FALSE;
HINTERNET hSessionHandle = NULL;
HINTERNET hConnectionHandle = NULL;
HINTERNET hRequestHandle = NULL;
HINTERNET hWebSocketHandle = NULL;
BYTE rgbCloseReasonBuffer[123];
BYTE rgbBuffer[1024];
BYTE* pbCurrentBufferPointer = rgbBuffer;
DWORD dwBufferLength = ARRAYSIZE(rgbBuffer);
DWORD dwBytesTransferred = 0;
DWORD dwCloseReasonLength = 0;
USHORT usStatus = 0;
WINHTTP_WEB_SOCKET_BUFFER_TYPE eBufferType;
INTERNET_PORT Port = 8888;
const WCHAR* pcwszServerName = L"localhost";
const WCHAR* pcwszPath = L"/ws";
const WCHAR* pcwszMessage = L"Hello world";
const DWORD cdwMessageLength = ARRAYSIZE(L"Hello world") * sizeof(WCHAR);
wprintf(L"program started\n");
//
// Create session, connection and request handles.
//
hSessionHandle = WinHttpOpen(L"WebSocket sample",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
NULL,
NULL,
0);
if (hSessionHandle == NULL)
{
dwError = GetLastError();
goto quit;
}
wprintf(L"session handle constructed\n");
WinHttpSetOption(hSessionHandle, WINHTTP_OPTION_CLIENT_CERT_CONTEXT, NULL, 0);
hConnectionHandle = WinHttpConnect(hSessionHandle,
pcwszServerName,
Port,
0);
if (hConnectionHandle == NULL)
{
dwError = GetLastError();
wprintf(L"connection failed %d\n", dwError);
goto quit;
}
wprintf(L"connected\n");
hRequestHandle = WinHttpOpenRequest(hConnectionHandle,
L"GET",
pcwszPath,
NULL,
NULL,
NULL,
WINHTTP_FLAG_SECURE);
// 0);
if (hRequestHandle == NULL)
{
dwError = GetLastError();
goto quit;
}
DWORD dwSSLFlag;
dwSSLFlag = SECURITY_FLAG_IGNORE_UNKNOWN_CA;
dwSSLFlag |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
dwSSLFlag |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
dwSSLFlag |= SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE;
WinHttpSetOption(hRequestHandle, WINHTTP_OPTION_SECURITY_FLAGS,
&dwSSLFlag, sizeof(dwSSLFlag));
wprintf(L"open request successs\n");
//
// Request protocol upgrade from http to websocket.
//
#pragma prefast(suppress:6387, "WINHTTP_OPTION_UPGRADE_TO_WEB_SOCKET does not take any arguments.")
fStatus = WinHttpSetOption(hRequestHandle,
WINHTTP_OPTION_UPGRADE_TO_WEB_SOCKET,
NULL,
0);
if (!fStatus)
{
dwError = GetLastError();
goto quit;
}
//
// Perform websocket handshake by sending a request and receiving server's response.
// Application may specify additional headers if needed.
//
fStatus = WinHttpSendRequest(hRequestHandle,
WINHTTP_NO_ADDITIONAL_HEADERS,
0,
NULL,
0,
0,
0);
if (!fStatus)
{
dwError = GetLastError();
goto quit;
}
wprintf(L"upgraded \n");
fStatus = WinHttpReceiveResponse(hRequestHandle, 0);
if (!fStatus)
{
dwError = GetLastError();
goto quit;
}
wprintf(L"receive response\n");
//
// Application should check what is the HTTP status code returned by the server and behave accordingly.
// WinHttpWebSocketCompleteUpgrade will fail if the HTTP status code is different than 101.
//
hWebSocketHandle = WinHttpWebSocketCompleteUpgrade(hRequestHandle, NULL);
if (hWebSocketHandle == NULL)
{
dwError = GetLastError();
goto quit;
}
wprintf(L"upgrade complete\n");
//
// The request handle is not needed anymore. From now on we will use the websocket handle.
//
WinHttpCloseHandle(hRequestHandle);
hRequestHandle = NULL;
wprintf(L"Succesfully upgraded to websocket protocol\n");
//
// Send and receive data on the websocket protocol.
//
dwError = WinHttpWebSocketSend(hWebSocketHandle,
WINHTTP_WEB_SOCKET_BINARY_MESSAGE_BUFFER_TYPE,
(PVOID)pcwszMessage,
cdwMessageLength);
if (dwError != ERROR_SUCCESS)
{
goto quit;
}
wprintf(L"Sent message to the server: '%s'\n", pcwszMessage);
do
{
if (dwBufferLength == 0)
{
dwError = ERROR_NOT_ENOUGH_MEMORY;
goto quit;
}
dwError = WinHttpWebSocketReceive(hWebSocketHandle,
pbCurrentBufferPointer,
dwBufferLength,
&dwBytesTransferred,
&eBufferType);
if (dwError != ERROR_SUCCESS)
{
goto quit;
}
//
// If we receive just part of the message restart the receive operation.
//
pbCurrentBufferPointer += dwBytesTransferred;
dwBufferLength -= dwBytesTransferred;
} while (eBufferType == WINHTTP_WEB_SOCKET_BINARY_FRAGMENT_BUFFER_TYPE);
//
// We expected server just to echo single binary message.
//
if (eBufferType != WINHTTP_WEB_SOCKET_BINARY_MESSAGE_BUFFER_TYPE)
{
wprintf(L"Unexpected buffer type\n");
dwError = ERROR_INVALID_PARAMETER;
goto quit;
}
wprintf(L"Received message from the server: '%.*s'\n", dwBufferLength, (WCHAR*)rgbBuffer);
//
// Gracefully close the connection.
//
dwError = WinHttpWebSocketClose(hWebSocketHandle,
WINHTTP_WEB_SOCKET_SUCCESS_CLOSE_STATUS,
NULL,
0);
if (dwError != ERROR_SUCCESS)
{
goto quit;
}
//
// Check close status returned by the server.
//
dwError = WinHttpWebSocketQueryCloseStatus(hWebSocketHandle,
&usStatus,
rgbCloseReasonBuffer,
ARRAYSIZE(rgbCloseReasonBuffer),
&dwCloseReasonLength);
if (dwError != ERROR_SUCCESS)
{
goto quit;
}
wprintf(L"The server closed the connection with status code: '%d' and reason: '%.*S'\n",
(int)usStatus,
dwCloseReasonLength,
rgbCloseReasonBuffer);
quit:
if (hRequestHandle != NULL)
{
WinHttpCloseHandle(hRequestHandle);
hRequestHandle = NULL;
}
if (hWebSocketHandle != NULL)
{
WinHttpCloseHandle(hWebSocketHandle);
hWebSocketHandle = NULL;
}
if (hConnectionHandle != NULL)
{
WinHttpCloseHandle(hConnectionHandle);
hConnectionHandle = NULL;
}
if (hSessionHandle != NULL)
{
WinHttpCloseHandle(hSessionHandle);
hSessionHandle = NULL;
}
if (dwError != ERROR_SUCCESS)
{
wprintf(L"Application failed with error: %u\n", dwError);
return -1;
}
return 0;
}
this client works with your gorrilla websocket example above with out error for me
from nbio.
fixed: lesismal/llib@d120a1b
from nbio.
thank you
from nbio.
Related Issues (20)
- 请问多个包合并读取应该如何处理? HOT 3
- How to check that websocket conn is open or closed? can we export the internal variable of isClosed? HOT 7
- AsyncRead 代码疑惑 HOT 9
- Few potential race conditions when dynamically adding/removing conns and shutdown HOT 5
- 如何增加序列化和反序列化的函数? HOT 3
- how to use with so reuseport option for the http example? HOT 3
- one more question. thx for the prompt reply, how well does it scale as a client? HOT 7
- just tested the performance of nbio tcp https on multicore system. it's great! but... how to do h2? HOT 1
- am i asking for a lot? pushing for tcp performance and nbios hit the mark but... HOT 18
- curious about the "total success" HOT 28
- how to increase the eventloop from the tls server example? HOT 5
- only 2,000 req/s with redis client example but using tls server example modification, i can get 25,000 req/s. what's wrong? HOT 2
- 请教大佬,在很短的时间里,向同一个conn推送多次数据 HOT 7
- 使用udp发送数据,会存在丢包! HOT 13
- Retract v1.5.4 HOT 6
- how to set so reuseport option for server? HOT 1
- 1.5.6 仍可能存在潜在bug HOT 29
- What is the principle of realizing 10 million connections to a single server? HOT 1
- WS客户端发送超高压缩率内容,可导致服务器OOM HOT 14
- onClose 没有调用 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nbio.