mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 23:47:07 -05:00
Merge pull request #1793 from lucifer9/h2c
add server side h2c (http2 without TLS protection) support
This commit is contained in:
commit
aa3c337041
@ -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 {
|
||||
@ -102,17 +105,24 @@ 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{
|
||||
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
|
||||
go func() {
|
||||
@ -124,11 +134,17 @@ 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
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return listener, nil
|
||||
|
Loading…
Reference in New Issue
Block a user