Skip to content

Commit

Permalink
enforce timeout for http header processing
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Jan 10, 2019
1 parent c889ae6 commit 6146366
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
8 changes: 5 additions & 3 deletions transport/internet/http/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"strings"
"time"

"v2ray.com/core/common"
"v2ray.com/core/common/net"
Expand Down Expand Up @@ -105,9 +106,10 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti
}

server := &http.Server{
Addr: serial.Concat(address, ":", port),
TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")),
Handler: listener,
Addr: serial.Concat(address, ":", port),
TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")),
Handler: listener,
ReadHeaderTimeout: time.Second * 4,
}

listener.server = server
Expand Down
21 changes: 12 additions & 9 deletions transport/internet/websocket/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type requestHandler struct {
var upgrader = &websocket.Upgrader{
ReadBufferSize: 4 * 1024,
WriteBufferSize: 4 * 1024,
HandshakeTimeout: time.Second * 8,
HandshakeTimeout: time.Second * 4,
}

func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
Expand All @@ -50,6 +50,7 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req

type Listener struct {
sync.Mutex
server http.Server
listener net.Listener
config *Config
addConn internet.ConnHandler
Expand All @@ -74,8 +75,17 @@ func ListenWS(ctx context.Context, address net.Address, port net.Port, streamSet
listener: listener,
}

l.server = http.Server{
Handler: &requestHandler{
path: wsSettings.GetNormalizedPath(),
ln: l,
},
ReadHeaderTimeout: time.Second * 4,
MaxHeaderBytes: 2048,
}

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))
}
}()
Expand All @@ -99,13 +109,6 @@ func listenTCP(ctx context.Context, address net.Address, port net.Port, tlsConfi
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().
func (ln *Listener) Addr() net.Addr {
return ln.listener.Addr()
Expand Down

0 comments on commit 6146366

Please sign in to comment.