Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for HTTP/3 and WebTransport #428

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,24 @@ import (
"golang.org/x/sync/errgroup"
)

//Config represents a client configuration
// Config represents a client configuration
type Config struct {
Fingerprint string
Auth string
KeepAlive time.Duration
MaxRetryCount int
MaxRetryInterval time.Duration
Server string
Proxy string
Remotes []string
Headers http.Header
TLS TLSConfig
DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
Verbose bool
Fingerprint string
Auth string
KeepAlive time.Duration
MaxRetryCount int
MaxRetryInterval time.Duration
Server string
Proxy string
Remotes []string
Headers http.Header
TLS TLSConfig
DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
Verbose bool
PreferWebTransport bool
}

//TLSConfig for a Client
// TLSConfig for a Client
type TLSConfig struct {
SkipVerify bool
CA string
Expand All @@ -54,7 +55,7 @@ type TLSConfig struct {
ServerName string
}

//Client represents a client instance
// Client represents a client instance
type Client struct {
*cio.Logger
config *Config
Expand All @@ -69,7 +70,7 @@ type Client struct {
tunnel *tunnel.Tunnel
}

//NewClient creates a new client instance
// NewClient creates a new client instance
func NewClient(c *Config) (*Client, error) {
//apply default scheme
if !strings.HasPrefix(c.Server, "http") {
Expand All @@ -82,11 +83,13 @@ func NewClient(c *Config) (*Client, error) {
if err != nil {
return nil, err
}
//swap to websockets scheme
u.Scheme = strings.Replace(u.Scheme, "http", "ws", 1)
if !(c.PreferWebTransport && u.Scheme == "https") {
//swap to websockets scheme
u.Scheme = strings.Replace(u.Scheme, "http", "ws", 1)
}
//apply default port
if !regexp.MustCompile(`:\d+$`).MatchString(u.Host) {
if u.Scheme == "wss" {
if u.Scheme == "wss" || u.Scheme == "https" {
u.Host = u.Host + ":443"
} else {
u.Host = u.Host + ":80"
Expand All @@ -107,7 +110,7 @@ func NewClient(c *Config) (*Client, error) {
//set default log level
client.Logger.Info = c.Verbose
//configure tls
if u.Scheme == "wss" {
if u.Scheme == "wss" || u.Scheme == "https" {
tc := &tls.Config{}
if c.TLS.ServerName != "" {
tc.ServerName = c.TLS.ServerName
Expand Down Expand Up @@ -190,7 +193,7 @@ func NewClient(c *Config) (*Client, error) {
return client, nil
}

//Run starts client and blocks while connected
// Run starts client and blocks while connected
func (c *Client) Run() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -221,7 +224,7 @@ func (c *Client) verifyServer(hostname string, remote net.Addr, key ssh.PublicKe
return nil
}

//verifyLegacyFingerprint calculates and compares legacy MD5 fingerprints
// verifyLegacyFingerprint calculates and compares legacy MD5 fingerprints
func (c *Client) verifyLegacyFingerprint(key ssh.PublicKey) error {
bytes := md5.Sum(key.Marshal())
strbytes := make([]string, len(bytes))
Expand All @@ -236,7 +239,7 @@ func (c *Client) verifyLegacyFingerprint(key ssh.PublicKey) error {
return nil
}

//Start client and does not block
// Start client and does not block
func (c *Client) Start(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
c.stop = cancel
Expand Down Expand Up @@ -293,12 +296,12 @@ func (c *Client) setProxy(u *url.URL, d *websocket.Dialer) error {
return nil
}

//Wait blocks while the client is running.
// Wait blocks while the client is running.
func (c *Client) Wait() error {
return c.eg.Wait()
}

//Close manually stops the client
// Close manually stops the client
func (c *Client) Close() error {
if c.stop != nil {
c.stop()
Expand Down
63 changes: 47 additions & 16 deletions client/client_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"net"
"strings"
"time"

Expand All @@ -14,6 +15,9 @@ import (
"github.com/jpillora/chisel/share/cnet"
"github.com/jpillora/chisel/share/cos"
"github.com/jpillora/chisel/share/settings"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/http3"
"github.com/quic-go/webtransport-go"
"golang.org/x/crypto/ssh"
)

Expand Down Expand Up @@ -75,26 +79,53 @@ func (c *Client) connectionOnce(ctx context.Context) (connected bool, err error)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var conn net.Conn
//prepare dialer
d := websocket.Dialer{
HandshakeTimeout: settings.EnvDuration("WS_TIMEOUT", 45*time.Second),
Subprotocols: []string{chshare.ProtocolVersion},
TLSClientConfig: c.tlsConfig,
ReadBufferSize: settings.EnvInt("WS_BUFF_SIZE", 0),
WriteBufferSize: settings.EnvInt("WS_BUFF_SIZE", 0),
NetDialContext: c.config.DialContext,
}
//optional proxy
if p := c.proxyURL; p != nil {
if err := c.setProxy(p, &d); err != nil {
if c.config.PreferWebTransport && c.tlsConfig != nil {
c.Debugf("Preparing WebTransport dialer")
d := webtransport.Dialer{
RoundTripper: &http3.RoundTripper{
TLSClientConfig: c.tlsConfig,
QuicConfig: &quic.Config{
HandshakeIdleTimeout: settings.EnvDuration("WS_TIMEOUT", 45*time.Second),
},
},
}
//optional proxy
if p := c.proxyURL; p != nil {
return false, fmt.Errorf("proxying is not supported when using webtransport")
}
_, wtSession, err := d.Dial(ctx, c.server+"/wt", c.config.Headers)
if err != nil {
return false, err
}
wtStream, err := wtSession.OpenStream()
if err != nil {
return false, err
}
conn = cnet.NewWebTransportConn(wtStream)
} else {
c.Debugf("Preparing WebSocket dialer")
d := websocket.Dialer{
HandshakeTimeout: settings.EnvDuration("WS_TIMEOUT", 45*time.Second),
Subprotocols: []string{chshare.ProtocolVersion},
TLSClientConfig: c.tlsConfig,
ReadBufferSize: settings.EnvInt("WS_BUFF_SIZE", 0),
WriteBufferSize: settings.EnvInt("WS_BUFF_SIZE", 0),
NetDialContext: c.config.DialContext,
}
//optional proxy
if p := c.proxyURL; p != nil {
if err := c.setProxy(p, &d); err != nil {
return false, err
}
}
wsConn, _, err := d.DialContext(ctx, c.server, c.config.Headers)
if err != nil {
return false, err
}
conn = cnet.NewWebSocketConn(wsConn)
}
wsConn, _, err := d.DialContext(ctx, c.server, c.config.Headers)
if err != nil {
return false, err
}
conn := cnet.NewWebSocketConn(wsConn)
// perform SSH handshake on net.Conn
c.Debugf("Handshaking...")
sshConn, chans, reqs, err := ssh.NewClientConn(conn, "", c.sshConfig)
Expand Down
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ require (
github.com/jpillora/backoff v1.0.0
github.com/jpillora/requestlog v1.0.0
github.com/jpillora/sizestr v1.0.0
github.com/quic-go/quic-go v0.34.0
github.com/quic-go/webtransport-go v0.5.3
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e
golang.org/x/net v0.0.0-20210614182718-04defd469f4e
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/crypto v0.4.0
golang.org/x/net v0.7.0
golang.org/x/sync v0.1.0
)

replace github.com/quic-go/webtransport-go => github.com/FyraLabs/webtransport-go v0.0.0-20230521122645-2533f15702ba
Loading