add webtransport tunnel
This commit is contained in:
@ -29,32 +29,27 @@ type metadata struct {
|
||||
|
||||
func (d *http3Dialer) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
authorizePath = "authorizePath"
|
||||
pushPath = "pushPath"
|
||||
pullPath = "pullPath"
|
||||
host = "host"
|
||||
|
||||
keepAlive = "keepAlive"
|
||||
keepAlive = "keepalive"
|
||||
keepAlivePeriod = "ttl"
|
||||
handshakeTimeout = "handshakeTimeout"
|
||||
maxIdleTimeout = "maxIdleTimeout"
|
||||
maxStreams = "maxStreams"
|
||||
)
|
||||
|
||||
d.md.authorizePath = mdutil.GetString(md, authorizePath)
|
||||
d.md.authorizePath = mdutil.GetString(md, "pht.authorizePath", "authorizePath")
|
||||
if !strings.HasPrefix(d.md.authorizePath, "/") {
|
||||
d.md.authorizePath = defaultAuthorizePath
|
||||
}
|
||||
d.md.pushPath = mdutil.GetString(md, pushPath)
|
||||
d.md.pushPath = mdutil.GetString(md, "pht.pushPath", "pushPath")
|
||||
if !strings.HasPrefix(d.md.pushPath, "/") {
|
||||
d.md.pushPath = defaultPushPath
|
||||
}
|
||||
d.md.pullPath = mdutil.GetString(md, pullPath)
|
||||
d.md.pullPath = mdutil.GetString(md, "pht.pullPath", "pullPath")
|
||||
if !strings.HasPrefix(d.md.pullPath, "/") {
|
||||
d.md.pullPath = defaultPullPath
|
||||
}
|
||||
|
||||
d.md.host = mdutil.GetString(md, host)
|
||||
d.md.host = mdutil.GetString(md, "host")
|
||||
if !md.IsExists(keepAlive) || mdutil.GetBool(md, keepAlive) {
|
||||
d.md.keepAlivePeriod = mdutil.GetDuration(md, keepAlivePeriod)
|
||||
if d.md.keepAlivePeriod <= 0 {
|
||||
|
58
dialer/http3/wt/client.go
Normal file
58
dialer/http3/wt/client.go
Normal file
@ -0,0 +1,58 @@
|
||||
package wt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
|
||||
"github.com/go-gost/core/logger"
|
||||
wt_util "github.com/go-gost/x/internal/util/wt"
|
||||
wt "github.com/quic-go/webtransport-go"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
host string
|
||||
path string
|
||||
header http.Header
|
||||
dialer *wt.Dialer
|
||||
session *wt.Session
|
||||
log logger.Logger
|
||||
}
|
||||
|
||||
func (c *Client) Dial(ctx context.Context, addr string) (net.Conn, error) {
|
||||
ok := false
|
||||
if c.session != nil {
|
||||
select {
|
||||
case <-c.session.Context().Done():
|
||||
default:
|
||||
ok = true
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
url := url.URL{
|
||||
Scheme: "https",
|
||||
Host: c.host,
|
||||
Path: c.path,
|
||||
}
|
||||
resp, session, err := c.dialer.Dial(ctx, url.String(), c.header)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if c.log.IsLevelEnabled(logger.TraceLevel) {
|
||||
dump, _ := httputil.DumpResponse(resp, false)
|
||||
c.log.Trace(string(dump))
|
||||
}
|
||||
|
||||
c.session = session
|
||||
}
|
||||
|
||||
stream, err := c.session.OpenStream()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return wt_util.Conn(c.session, stream), nil
|
||||
}
|
111
dialer/http3/wt/dialer.go
Normal file
111
dialer/http3/wt/dialer.go
Normal file
@ -0,0 +1,111 @@
|
||||
package wt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"github.com/go-gost/core/dialer"
|
||||
md "github.com/go-gost/core/metadata"
|
||||
"github.com/go-gost/x/registry"
|
||||
"github.com/quic-go/quic-go"
|
||||
"github.com/quic-go/quic-go/http3"
|
||||
wt "github.com/quic-go/webtransport-go"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.DialerRegistry().Register("wt", NewDialer)
|
||||
}
|
||||
|
||||
type wtDialer struct {
|
||||
clients map[string]*Client
|
||||
clientMutex sync.Mutex
|
||||
md metadata
|
||||
options dialer.Options
|
||||
}
|
||||
|
||||
func NewDialer(opts ...dialer.Option) dialer.Dialer {
|
||||
options := dialer.Options{}
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
return &wtDialer{
|
||||
clients: make(map[string]*Client),
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *wtDialer) Init(md md.Metadata) (err error) {
|
||||
if err = d.parseMetadata(md); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Multiplex implements dialer.Multiplexer interface.
|
||||
func (d *wtDialer) Multiplex() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (d *wtDialer) Dial(ctx context.Context, addr string, opts ...dialer.DialOption) (conn net.Conn, err error) {
|
||||
d.clientMutex.Lock()
|
||||
defer d.clientMutex.Unlock()
|
||||
|
||||
client := d.clients[addr]
|
||||
if client == nil {
|
||||
var options dialer.DialOptions
|
||||
for _, opt := range opts {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
host := d.md.host
|
||||
if host == "" {
|
||||
host = options.Host
|
||||
}
|
||||
if h, _, _ := net.SplitHostPort(host); h != "" {
|
||||
host = h
|
||||
}
|
||||
|
||||
client = &Client{
|
||||
log: d.options.Logger,
|
||||
host: host,
|
||||
path: d.md.path,
|
||||
header: d.md.header,
|
||||
dialer: &wt.Dialer{
|
||||
RoundTripper: &http3.RoundTripper{
|
||||
TLSClientConfig: d.options.TLSConfig,
|
||||
Dial: func(ctx context.Context, adr string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
|
||||
// d.options.Logger.Infof("dial: %s, %s, %s", addr, adr, host)
|
||||
udpAddr, err := net.ResolveUDPAddr("udp", addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
udpConn, err := options.NetDialer.Dial(ctx, "udp", "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return quic.DialEarly(ctx, udpConn.(net.PacketConn), udpAddr, tlsCfg, cfg)
|
||||
},
|
||||
QuicConfig: &quic.Config{
|
||||
KeepAlivePeriod: d.md.keepAlivePeriod,
|
||||
HandshakeIdleTimeout: d.md.handshakeTimeout,
|
||||
MaxIdleTimeout: d.md.maxIdleTimeout,
|
||||
/*
|
||||
Versions: []quic.VersionNumber{
|
||||
quic.Version1,
|
||||
},
|
||||
*/
|
||||
MaxIncomingStreams: int64(d.md.maxStreams),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
d.clients[addr] = client
|
||||
}
|
||||
|
||||
return client.Dial(ctx, addr)
|
||||
}
|
62
dialer/http3/wt/metadata.go
Normal file
62
dialer/http3/wt/metadata.go
Normal file
@ -0,0 +1,62 @@
|
||||
package wt
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
mdata "github.com/go-gost/core/metadata"
|
||||
mdutil "github.com/go-gost/core/metadata/util"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultPath = "/wt"
|
||||
defaultKeepalivePeriod = 15 * time.Second
|
||||
)
|
||||
|
||||
type metadata struct {
|
||||
host string
|
||||
path string
|
||||
header http.Header
|
||||
|
||||
// QUIC config options
|
||||
keepAlivePeriod time.Duration
|
||||
maxIdleTimeout time.Duration
|
||||
handshakeTimeout time.Duration
|
||||
maxStreams int
|
||||
}
|
||||
|
||||
func (d *wtDialer) parseMetadata(md mdata.Metadata) (err error) {
|
||||
const (
|
||||
keepAlive = "keepalive"
|
||||
keepAlivePeriod = "ttl"
|
||||
handshakeTimeout = "handshakeTimeout"
|
||||
maxIdleTimeout = "maxIdleTimeout"
|
||||
maxStreams = "maxStreams"
|
||||
)
|
||||
|
||||
d.md.host = mdutil.GetString(md, "wt.host", "host")
|
||||
d.md.path = mdutil.GetString(md, "wt.path", "path")
|
||||
if d.md.path == "" {
|
||||
d.md.path = defaultPath
|
||||
}
|
||||
|
||||
if !md.IsExists(keepAlive) || mdutil.GetBool(md, keepAlive) {
|
||||
d.md.keepAlivePeriod = mdutil.GetDuration(md, keepAlivePeriod)
|
||||
if d.md.keepAlivePeriod <= 0 {
|
||||
d.md.keepAlivePeriod = 10 * time.Second
|
||||
}
|
||||
}
|
||||
d.md.handshakeTimeout = mdutil.GetDuration(md, handshakeTimeout)
|
||||
d.md.maxIdleTimeout = mdutil.GetDuration(md, maxIdleTimeout)
|
||||
d.md.maxStreams = mdutil.GetInt(md, maxStreams)
|
||||
|
||||
if m := mdutil.GetStringMapString(md, "wt.header", "header"); len(m) > 0 {
|
||||
h := http.Header{}
|
||||
for k, v := range m {
|
||||
h.Add(k, v)
|
||||
}
|
||||
d.md.header = h
|
||||
}
|
||||
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user