enforce timeout for http header processing

pull/1511/head v4.11.0
Darien Raymond 2019-01-10 10:40:51 +01:00
parent c889ae6e8c
commit 6146366a4a
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 17 additions and 12 deletions

View File

@ -5,6 +5,7 @@ import (
"io" "io"
"net/http" "net/http"
"strings" "strings"
"time"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
@ -108,6 +109,7 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti
Addr: serial.Concat(address, ":", port), Addr: serial.Concat(address, ":", port),
TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")), TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")),
Handler: listener, Handler: listener,
ReadHeaderTimeout: time.Second * 4,
} }
listener.server = server listener.server = server

View File

@ -25,7 +25,7 @@ type requestHandler struct {
var upgrader = &websocket.Upgrader{ var upgrader = &websocket.Upgrader{
ReadBufferSize: 4 * 1024, ReadBufferSize: 4 * 1024,
WriteBufferSize: 4 * 1024, WriteBufferSize: 4 * 1024,
HandshakeTimeout: time.Second * 8, HandshakeTimeout: time.Second * 4,
} }
func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
@ -50,6 +50,7 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
type Listener struct { type Listener struct {
sync.Mutex sync.Mutex
server http.Server
listener net.Listener listener net.Listener
config *Config config *Config
addConn internet.ConnHandler addConn internet.ConnHandler
@ -74,8 +75,17 @@ func ListenWS(ctx context.Context, address net.Address, port net.Port, streamSet
listener: listener, listener: listener,
} }
l.server = http.Server{
Handler: &requestHandler{
path: wsSettings.GetNormalizedPath(),
ln: l,
},
ReadHeaderTimeout: time.Second * 4,
MaxHeaderBytes: 2048,
}
go func() { go func() {
if err := l.serve(); err != nil { if err := l.server.Serve(l.listener); err != nil {
newError("failed to serve http for WebSocket").Base(err).AtWarning().WriteToLog(session.ExportIDToError(ctx)) newError("failed to serve http for WebSocket").Base(err).AtWarning().WriteToLog(session.ExportIDToError(ctx))
} }
}() }()
@ -99,13 +109,6 @@ func listenTCP(ctx context.Context, address net.Address, port net.Port, tlsConfi
return listener, nil return listener, nil
} }
func (ln *Listener) serve() error {
return http.Serve(ln.listener, &requestHandler{
path: ln.config.GetNormalizedPath(),
ln: ln,
})
}
// Addr implements net.Listener.Addr(). // Addr implements net.Listener.Addr().
func (ln *Listener) Addr() net.Addr { func (ln *Listener) Addr() net.Addr {
return ln.listener.Addr() return ln.listener.Addr()