Home > database >  https and websocket handler with different deadlines
https and websocket handler with different deadlines

Time:07-11

I have a toy proxy server that accepts connections on a port. I set some deadlines for read/write operations to avoid having too many idle connections from bad clients that fail to close properly.

The problem is that I would like to set a higher deadline for connections that are towards websockets (wss in particular). For plain http requests I can see the 101 Switching Protocols response but https/wss is trickier since I mostly do a io.CopyBuffer from src connection to dst connection and I don't see anything "websocket related" in the initial proxy connection in order to differentiate between https and wss and apply the proper deadline.

I've included a debug screen to such a request towards a wss:// demo server.

enter image description here

Any ideas?

CodePudding user response:

One cannot reliably distinguish between "normal" HTTP traffic and Websockets just from looking at the encrypted data.

One can try to do some heuristics by looking at traffic patterns, i.e. in which direction how many data are transferred in which time and with which idle times between data. Such heuristics can be based on the assumption that HTTP is a request response protocol with typically small requests shortly followed by larger responses, while Websockets can show arbitrary traffic patterns.

But arbitrary traffic patterns also means that Websockets might be used in a request response way too. (including request response though). Also, in some use cases the usage pattern for HTTP consists of mostly larger requests followed by small responses. Thus depending on the kind of application such heuristics might be successful or they might fail.

CodePudding user response:

It is always a good practice to define global Server timeout to make sure that resources are not locked forever. That timeout should be not smaller that longest timeout in all handlers.

DefaultServer = &http.Server{
    Handler:        http.TimeoutHandler(handler, wssTimeout, timeoutResponse),
...

}

In handler that processes http and wss requests, we need to set timeout dynamically.

func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request) {
    // Request Context is going to be cancelled if client's connection closes, the request is canceled (with HTTP/2), Server we created above time outed.
    // all code down the stack should respect that ctx. 
    ctx := r.Context()
    
    
    timeoit := httpTimeout
    if itIsWSS(r) {
       timeout = wssTimeout
    }
    
    ctx, cancel = cWithTimeout(ctx, timeout)
    defer cancel()
    
    // all code below to use ctx instead of context.Backgound()/TODO()
  • Related