add http3 reverse proxy
This commit is contained in:
66
listener/http3/conn.go
Normal file
66
listener/http3/conn.go
Normal file
@ -0,0 +1,66 @@
|
||||
package http3
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
mdata "github.com/go-gost/core/metadata"
|
||||
)
|
||||
|
||||
// a dummy HTTP3 server conn used by HTTP3 handler
|
||||
type conn struct {
|
||||
md mdata.Metadata
|
||||
r *http.Request
|
||||
w http.ResponseWriter
|
||||
laddr net.Addr
|
||||
raddr net.Addr
|
||||
closed chan struct{}
|
||||
}
|
||||
|
||||
func (c *conn) Read(b []byte) (n int, err error) {
|
||||
return 0, &net.OpError{Op: "read", Net: "http3", Source: nil, Addr: nil, Err: errors.New("read not supported")}
|
||||
}
|
||||
|
||||
func (c *conn) Write(b []byte) (n int, err error) {
|
||||
return 0, &net.OpError{Op: "write", Net: "http3", Source: nil, Addr: nil, Err: errors.New("write not supported")}
|
||||
}
|
||||
|
||||
func (c *conn) Close() error {
|
||||
select {
|
||||
case <-c.closed:
|
||||
default:
|
||||
close(c.closed)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) LocalAddr() net.Addr {
|
||||
return c.laddr
|
||||
}
|
||||
|
||||
func (c *conn) RemoteAddr() net.Addr {
|
||||
return c.raddr
|
||||
}
|
||||
|
||||
func (c *conn) SetDeadline(t time.Time) error {
|
||||
return &net.OpError{Op: "set", Net: "http3", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
|
||||
}
|
||||
|
||||
func (c *conn) SetReadDeadline(t time.Time) error {
|
||||
return &net.OpError{Op: "set", Net: "http3", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
|
||||
}
|
||||
|
||||
func (c *conn) SetWriteDeadline(t time.Time) error {
|
||||
return &net.OpError{Op: "set", Net: "http3", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
|
||||
}
|
||||
|
||||
func (c *conn) Done() <-chan struct{} {
|
||||
return c.closed
|
||||
}
|
||||
|
||||
// Metadata implements metadata.Metadatable interface.
|
||||
func (c *conn) Metadata() mdata.Metadata {
|
||||
return c.md
|
||||
}
|
99
listener/http3/h3/listener.go
Normal file
99
listener/http3/h3/listener.go
Normal file
@ -0,0 +1,99 @@
|
||||
package h3
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/go-gost/core/listener"
|
||||
"github.com/go-gost/core/logger"
|
||||
md "github.com/go-gost/core/metadata"
|
||||
admission "github.com/go-gost/x/admission/wrapper"
|
||||
xnet "github.com/go-gost/x/internal/net"
|
||||
pht_util "github.com/go-gost/x/internal/util/pht"
|
||||
limiter "github.com/go-gost/x/limiter/traffic/wrapper"
|
||||
metrics "github.com/go-gost/x/metrics/wrapper"
|
||||
"github.com/go-gost/x/registry"
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.ListenerRegistry().Register("h3", NewListener)
|
||||
}
|
||||
|
||||
type http3Listener struct {
|
||||
addr net.Addr
|
||||
server *pht_util.Server
|
||||
logger logger.Logger
|
||||
md metadata
|
||||
options listener.Options
|
||||
}
|
||||
|
||||
func NewListener(opts ...listener.Option) listener.Listener {
|
||||
options := listener.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
return &http3Listener{
|
||||
logger: options.Logger,
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *http3Listener) Init(md md.Metadata) (err error) {
|
||||
if err = l.parseMetadata(md); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
network := "udp"
|
||||
if xnet.IsIPv4(l.options.Addr) {
|
||||
network = "udp4"
|
||||
}
|
||||
l.addr, err = net.ResolveUDPAddr(network, l.options.Addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
l.server = pht_util.NewHTTP3Server(
|
||||
l.options.Addr,
|
||||
&quic.Config{
|
||||
KeepAlivePeriod: l.md.keepAlivePeriod,
|
||||
HandshakeIdleTimeout: l.md.handshakeTimeout,
|
||||
MaxIdleTimeout: l.md.maxIdleTimeout,
|
||||
Versions: []quic.VersionNumber{
|
||||
quic.Version1,
|
||||
},
|
||||
MaxIncomingStreams: int64(l.md.maxStreams),
|
||||
},
|
||||
pht_util.TLSConfigServerOption(l.options.TLSConfig),
|
||||
pht_util.BacklogServerOption(l.md.backlog),
|
||||
pht_util.PathServerOption(l.md.authorizePath, l.md.pushPath, l.md.pullPath),
|
||||
pht_util.LoggerServerOption(l.options.Logger),
|
||||
)
|
||||
|
||||
go func() {
|
||||
if err := l.server.ListenAndServe(); err != nil {
|
||||
l.logger.Error(err)
|
||||
}
|
||||
}()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (l *http3Listener) Accept() (conn net.Conn, err error) {
|
||||
conn, err = l.server.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
conn = metrics.WrapConn(l.options.Service, conn)
|
||||
conn = admission.WrapConn(l.options.Admission, conn)
|
||||
conn = limiter.WrapConn(l.options.TrafficLimiter, conn)
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
func (l *http3Listener) Addr() net.Addr {
|
||||
return l.addr
|
||||
}
|
||||
|
||||
func (l *http3Listener) Close() (err error) {
|
||||
return l.server.Close()
|
||||
}
|
75
listener/http3/h3/metadata.go
Normal file
75
listener/http3/h3/metadata.go
Normal file
@ -0,0 +1,75 @@
|
||||
package h3
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
mdata "github.com/go-gost/core/metadata"
|
||||
mdutil "github.com/go-gost/core/metadata/util"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultAuthorizePath = "/authorize"
|
||||
defaultPushPath = "/push"
|
||||
defaultPullPath = "/pull"
|
||||
defaultBacklog = 128
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
authorizePath string
|
||||
pushPath string
|
||||
pullPath string
|
||||
backlog int
|
||||
|
||||
// QUIC config options
|
||||
keepAlivePeriod time.Duration
|
||||
maxIdleTimeout time.Duration
|
||||
handshakeTimeout time.Duration
|
||||
maxStreams int
|
||||
}
|
||||
|
||||
func (l *http3Listener) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
authorizePath = "authorizePath"
|
||||
pushPath = "pushPath"
|
||||
pullPath = "pullPath"
|
||||
|
||||
keepAlive = "keepAlive"
|
||||
keepAlivePeriod = "ttl"
|
||||
handshakeTimeout = "handshakeTimeout"
|
||||
maxIdleTimeout = "maxIdleTimeout"
|
||||
maxStreams = "maxStreams"
|
||||
|
||||
backlog = "backlog"
|
||||
)
|
||||
|
||||
l.md.authorizePath = mdutil.GetString(md, authorizePath)
|
||||
if !strings.HasPrefix(l.md.authorizePath, "/") {
|
||||
l.md.authorizePath = defaultAuthorizePath
|
||||
}
|
||||
l.md.pushPath = mdutil.GetString(md, pushPath)
|
||||
if !strings.HasPrefix(l.md.pushPath, "/") {
|
||||
l.md.pushPath = defaultPushPath
|
||||
}
|
||||
l.md.pullPath = mdutil.GetString(md, pullPath)
|
||||
if !strings.HasPrefix(l.md.pullPath, "/") {
|
||||
l.md.pullPath = defaultPullPath
|
||||
}
|
||||
|
||||
l.md.backlog = mdutil.GetInt(md, backlog)
|
||||
if l.md.backlog <= 0 {
|
||||
l.md.backlog = defaultBacklog
|
||||
}
|
||||
|
||||
if mdutil.GetBool(md, keepAlive) {
|
||||
l.md.keepAlivePeriod = mdutil.GetDuration(md, keepAlivePeriod)
|
||||
if l.md.keepAlivePeriod <= 0 {
|
||||
l.md.keepAlivePeriod = 10 * time.Second
|
||||
}
|
||||
}
|
||||
l.md.handshakeTimeout = mdutil.GetDuration(md, handshakeTimeout)
|
||||
l.md.maxIdleTimeout = mdutil.GetDuration(md, maxIdleTimeout)
|
||||
l.md.maxStreams = mdutil.GetInt(md, maxStreams)
|
||||
|
||||
return
|
||||
}
|
@ -2,30 +2,32 @@ package http3
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/go-gost/core/listener"
|
||||
"github.com/go-gost/core/logger"
|
||||
md "github.com/go-gost/core/metadata"
|
||||
admission "github.com/go-gost/x/admission/wrapper"
|
||||
xnet "github.com/go-gost/x/internal/net"
|
||||
pht_util "github.com/go-gost/x/internal/util/pht"
|
||||
limiter "github.com/go-gost/x/limiter/traffic/wrapper"
|
||||
metrics "github.com/go-gost/x/metrics/wrapper"
|
||||
mdx "github.com/go-gost/x/metadata"
|
||||
"github.com/go-gost/x/registry"
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/lucas-clemente/quic-go/http3"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.ListenerRegistry().Register("http3", NewListener)
|
||||
registry.ListenerRegistry().Register("h3", NewListener)
|
||||
}
|
||||
|
||||
type http3Listener struct {
|
||||
server *http3.Server
|
||||
addr net.Addr
|
||||
server *pht_util.Server
|
||||
cqueue chan net.Conn
|
||||
errChan chan error
|
||||
logger logger.Logger
|
||||
md metadata
|
||||
options listener.Options
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewListener(opts ...listener.Option) listener.Listener {
|
||||
@ -53,9 +55,10 @@ func (l *http3Listener) Init(md md.Metadata) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
l.server = pht_util.NewHTTP3Server(
|
||||
l.options.Addr,
|
||||
&quic.Config{
|
||||
l.server = &http3.Server{
|
||||
Addr: l.options.Addr,
|
||||
TLSConfig: l.options.TLSConfig,
|
||||
QuicConfig: &quic.Config{
|
||||
KeepAlivePeriod: l.md.keepAlivePeriod,
|
||||
HandshakeIdleTimeout: l.md.handshakeTimeout,
|
||||
MaxIdleTimeout: l.md.maxIdleTimeout,
|
||||
@ -64,11 +67,11 @@ func (l *http3Listener) Init(md md.Metadata) (err error) {
|
||||
},
|
||||
MaxIncomingStreams: int64(l.md.maxStreams),
|
||||
},
|
||||
pht_util.TLSConfigServerOption(l.options.TLSConfig),
|
||||
pht_util.BacklogServerOption(l.md.backlog),
|
||||
pht_util.PathServerOption(l.md.authorizePath, l.md.pushPath, l.md.pullPath),
|
||||
pht_util.LoggerServerOption(l.options.Logger),
|
||||
)
|
||||
Handler: http.HandlerFunc(l.handleFunc),
|
||||
}
|
||||
|
||||
l.cqueue = make(chan net.Conn, l.md.backlog)
|
||||
l.errChan = make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
if err := l.server.ListenAndServe(); err != nil {
|
||||
@ -80,15 +83,15 @@ func (l *http3Listener) Init(md md.Metadata) (err error) {
|
||||
}
|
||||
|
||||
func (l *http3Listener) Accept() (conn net.Conn, err error) {
|
||||
conn, err = l.server.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
var ok bool
|
||||
select {
|
||||
case conn = <-l.cqueue:
|
||||
case err, ok = <-l.errChan:
|
||||
if !ok {
|
||||
err = listener.ErrClosed
|
||||
}
|
||||
}
|
||||
|
||||
conn = metrics.WrapConn(l.options.Service, conn)
|
||||
conn = admission.WrapConn(l.options.Admission, conn)
|
||||
conn = limiter.WrapConn(l.options.TrafficLimiter, conn)
|
||||
return conn, nil
|
||||
return
|
||||
}
|
||||
|
||||
func (l *http3Listener) Addr() net.Addr {
|
||||
@ -96,5 +99,36 @@ func (l *http3Listener) Addr() net.Addr {
|
||||
}
|
||||
|
||||
func (l *http3Listener) Close() (err error) {
|
||||
return l.server.Close()
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
|
||||
select {
|
||||
case <-l.errChan:
|
||||
default:
|
||||
err = l.server.Close()
|
||||
l.errChan <- err
|
||||
close(l.errChan)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *http3Listener) handleFunc(w http.ResponseWriter, r *http.Request) {
|
||||
raddr, _ := net.ResolveTCPAddr("tcp", r.RemoteAddr)
|
||||
conn := &conn{
|
||||
laddr: l.addr,
|
||||
raddr: raddr,
|
||||
closed: make(chan struct{}),
|
||||
md: mdx.NewMetadata(map[string]any{
|
||||
"r": r,
|
||||
"w": w,
|
||||
}),
|
||||
}
|
||||
select {
|
||||
case l.cqueue <- conn:
|
||||
default:
|
||||
l.logger.Warnf("connection queue is full, client %s discarded", r.RemoteAddr)
|
||||
return
|
||||
}
|
||||
|
||||
<-conn.Done()
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package http3
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
mdata "github.com/go-gost/core/metadata"
|
||||
@ -9,17 +8,11 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
defaultAuthorizePath = "/authorize"
|
||||
defaultPushPath = "/push"
|
||||
defaultPullPath = "/pull"
|
||||
defaultBacklog = 128
|
||||
defaultBacklog = 128
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
authorizePath string
|
||||
pushPath string
|
||||
pullPath string
|
||||
backlog int
|
||||
backlog int
|
||||
|
||||
// QUIC config options
|
||||
keepAlivePeriod time.Duration
|
||||
@ -30,10 +23,6 @@ type metadata struct {
|
||||
|
||||
func (l *http3Listener) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
authorizePath = "authorizePath"
|
||||
pushPath = "pushPath"
|
||||
pullPath = "pullPath"
|
||||
|
||||
keepAlive = "keepAlive"
|
||||
keepAlivePeriod = "ttl"
|
||||
handshakeTimeout = "handshakeTimeout"
|
||||
@ -43,19 +32,6 @@ func (l *http3Listener) parseMetadata(md mdata.Metadata) (err error) {
|
||||
backlog = "backlog"
|
||||
)
|
||||
|
||||
l.md.authorizePath = mdutil.GetString(md, authorizePath)
|
||||
if !strings.HasPrefix(l.md.authorizePath, "/") {
|
||||
l.md.authorizePath = defaultAuthorizePath
|
||||
}
|
||||
l.md.pushPath = mdutil.GetString(md, pushPath)
|
||||
if !strings.HasPrefix(l.md.pushPath, "/") {
|
||||
l.md.pushPath = defaultPushPath
|
||||
}
|
||||
l.md.pullPath = mdutil.GetString(md, pullPath)
|
||||
if !strings.HasPrefix(l.md.pullPath, "/") {
|
||||
l.md.pullPath = defaultPullPath
|
||||
}
|
||||
|
||||
l.md.backlog = mdutil.GetInt(md, backlog)
|
||||
if l.md.backlog <= 0 {
|
||||
l.md.backlog = defaultBacklog
|
||||
|
Reference in New Issue
Block a user