Skip to content

Commit

Permalink
Merge pull request #1793 from lucifer9/h2c
Browse files Browse the repository at this point in the history
add server side h2c (http2 without TLS protection) support
  • Loading branch information
xiaokangwang committed Jul 10, 2019
2 parents 3deb5f2 + e6bf431 commit aa3c337
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions transport/internet/http/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"v2ray.com/core/common/signal/done"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/tls"

"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)

type Listener struct {
Expand Down Expand Up @@ -102,16 +105,23 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti
config: *httpSettings,
}

var server *http.Server
config := tls.ConfigFromStreamSettings(streamSettings)
if config == nil {
return nil, newError("TLS must be enabled for http transport.").AtWarning()
}
h2s := &http2.Server{}

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

listener.server = server
Expand All @@ -124,10 +134,16 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti
newError("failed to listen on", address, ":", port).Base(err).WriteToLog(session.ExportIDToError(ctx))
return
}

err = server.ServeTLS(tcpListener, "", "")
if err != nil {
newError("stoping serving TLS").Base(err).WriteToLog(session.ExportIDToError(ctx))
if config == nil {
err = server.Serve(tcpListener)
if err != nil {
newError("stoping serving H2C").Base(err).WriteToLog(session.ExportIDToError(ctx))
}
} else {
err = server.ServeTLS(tcpListener, "", "")
if err != nil {
newError("stoping serving TLS").Base(err).WriteToLog(session.ExportIDToError(ctx))
}
}
}()

Expand Down

0 comments on commit aa3c337

Please sign in to comment.