From 771c39a29ce8cea74e66e9e3c371acd748d0b0d3 Mon Sep 17 00:00:00 2001 From: WeebDataHoarder <57538841+WeebDataHoarder@users.noreply.github.com> Date: Fri, 10 Jun 2022 10:46:55 +0200 Subject: [PATCH] Added HTTP/TLS versions, update quic-go --- go.mod | 7 +++---- go.sum | 14 ++++++-------- httputils/fasthttp.go | 26 +++++++++++++++++++++++++- httputils/nethttp.go | 25 ++++++++++++++++++++++++- httputils/server.go | 28 ++++++++++++++++------------ 5 files changed, 74 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 1168183..5be84de 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/cloudflare/circl v1.1.0 github.com/dgrr/http2 v0.3.5 github.com/ipfs/go-cid v0.2.0 - github.com/lucas-clemente/quic-go v0.27.2 + github.com/lucas-clemente/quic-go v0.27.1-0.20220609183220-6fbc6d951a40 github.com/multiformats/go-multihash v0.1.0 github.com/valyala/fasthttp v1.37.0 ) @@ -35,12 +35,11 @@ require ( github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fastrand v1.1.0 // indirect golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect - golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect + golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect golang.org/x/net v0.0.0-20220607020251-c690dde0001d // indirect golang.org/x/sys v0.0.0-20220608164250-635b8c9b7f68 // indirect golang.org/x/text v0.3.7 // indirect - golang.org/x/tools v0.1.10 // indirect - golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect + golang.org/x/tools v0.1.11 // indirect gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect lukechampine.com/blake3 v1.1.7 // indirect ) diff --git a/go.sum b/go.sum index 8ee57cf..8b09951 100644 --- a/go.sum +++ b/go.sum @@ -91,8 +91,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/lucas-clemente/quic-go v0.27.2 h1:zsMwwniyybb8B/UDNXRSYee7WpQJVOcjQEGgpw2ikXs= -github.com/lucas-clemente/quic-go v0.27.2/go.mod h1:vXgO/11FBSKM+js1NxoaQ/bPtVFYfB7uxhfHXyMhl1A= +github.com/lucas-clemente/quic-go v0.27.1-0.20220609183220-6fbc6d951a40 h1:TmAcxgYB4Vnpa8QfljlEFYv8BHfjrkZlTzQAvCSRTKU= +github.com/lucas-clemente/quic-go v0.27.1-0.20220609183220-6fbc6d951a40/go.mod h1:vXgO/11FBSKM+js1NxoaQ/bPtVFYfB7uxhfHXyMhl1A= github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI= github.com/mailru/easyjson v0.0.0-20190312143242-1de009706dbe/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/marten-seemann/qpack v0.2.1 h1:jvTsT/HpCn2UZJdP+UUB53FfUUgeOyG5K1ns0OJOGVs= @@ -218,8 +218,8 @@ golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTk golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o= -golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -303,14 +303,12 @@ golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBn golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= -golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20= -golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E= +golang.org/x/tools v0.1.11 h1:loJ25fNOEhSXfHrpoGj91eCUThwdNX6u24rO1xnNteY= +golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f h1:uF6paiQQebLeSXkrTqHqz0MXhXXS1KgF41eUdBNvxK0= -golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.1.0/go.mod h1:UGEZY7KEX120AnNLIHFMKIo4obdJhkp2tPbaPlQx13Y= diff --git a/httputils/fasthttp.go b/httputils/fasthttp.go index c6529a1..936c9b1 100644 --- a/httputils/fasthttp.go +++ b/httputils/fasthttp.go @@ -62,13 +62,37 @@ func (c *FastHTTPContext) GetRequestTime() time.Time { } func (c *FastHTTPContext) GetTLSServerName() string { - return c.ctx.TLSConnectionState().ServerName + if state := c.ctx.TLSConnectionState(); state != nil { + return state.ServerName + } + + return "" } func (c *FastHTTPContext) GetHost() string { return string(c.ctx.Request.Host()) } +func (c *FastHTTPContext) GetProtocol() string { + return string(c.ctx.Request.Header.Protocol()) +} + +func (c *FastHTTPContext) GetTLSVersion() uint16 { + if state := c.ctx.TLSConnectionState(); state != nil { + return state.Version + } + + return 0 +} + +func (c *FastHTTPContext) GetTLSCipher() uint16 { + if state := c.ctx.TLSConnectionState(); state != nil { + return state.CipherSuite + } + + return 0 +} + func (c *FastHTTPContext) GetRequestHeader(name string) string { return string(c.ctx.Request.Header.Peek(name)) } diff --git a/httputils/nethttp.go b/httputils/nethttp.go index c22bce4..f884e7c 100644 --- a/httputils/nethttp.go +++ b/httputils/nethttp.go @@ -62,13 +62,36 @@ func (c *NetHTTPContext) GetRequestTime() time.Time { } func (c *NetHTTPContext) GetTLSServerName() string { - return c.httpRequest.TLS.ServerName + if c.httpRequest.TLS != nil { + return c.httpRequest.TLS.ServerName + } + return "" } func (c *NetHTTPContext) GetHost() string { return c.httpRequest.Host } +func (c *NetHTTPContext) GetProtocol() string { + return c.httpRequest.Proto +} + +func (c *NetHTTPContext) GetTLSVersion() uint16 { + if c.httpRequest.TLS != nil { + return c.httpRequest.TLS.Version + } + + return 0 +} + +func (c *NetHTTPContext) GetTLSCipher() uint16 { + if c.httpRequest.TLS != nil { + return c.httpRequest.TLS.CipherSuite + } + + return 0 +} + func (c *NetHTTPContext) GetRequestHeader(name string) string { return c.httpRequest.Header.Get(name) diff --git a/httputils/server.go b/httputils/server.go index 3c6f59d..9dfb5de 100644 --- a/httputils/server.go +++ b/httputils/server.go @@ -4,6 +4,7 @@ import ( "crypto/tls" "git.gammaspectra.live/S.O.N.G/MakyuuIchaival/tlsutils" "github.com/dgrr/http2" + "github.com/lucas-clemente/quic-go" "github.com/lucas-clemente/quic-go/http3" "github.com/valyala/fasthttp" "io" @@ -42,6 +43,9 @@ type RequestContext interface { GetTLSServerName() string GetBody() io.Reader GetHost() string + GetProtocol() string + GetTLSVersion() uint16 + GetTLSCipher() uint16 GetRequestHeader(name string) string GetResponseHeader(name string) string AddResponseHeader(name string, value string) @@ -178,19 +182,19 @@ func (server *Server) Serve() { } s := &http3.Server{ - Server: &http.Server{ - ReadTimeout: server.ReadTimeout, - WriteTimeout: server.WriteTimeout, - IdleTimeout: server.IdleTimeout, - Addr: server.ListenAddress, - TLSConfig: server.TLSConfig.QUICConfig, - Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if server.Debug { - log.Print("Received HTTP/3 request") - } - server.Handler(handler(w, r)) - }), + Addr: server.ListenAddress, + QuicConfig: &quic.Config{ + HandshakeIdleTimeout: server.ReadTimeout, + MaxIdleTimeout: server.IdleTimeout, + KeepAlivePeriod: server.IdleTimeout, }, + TLSConfig: server.TLSConfig.QUICConfig, + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if server.Debug { + log.Print("Received HTTP/3 request") + } + server.Handler(handler(w, r)) + }), } //TODO HACK: SetQuicHeaders does not work before server starts