feat(tsconnect/wasm): add upgradeTLS to wrapped conns
Adds an upgradeTLS method to the conn objects returned by dial/accept, wrapping the existing net.Conn with crypto/tls in place (STARTTLS / FTPS AUTH TLS style). Client mode reuses the dialTLS options (serverName, insecureSkipVerify, caCerts), now factored into tlsClientConfigFromJS; server mode takes isServer with certPem/keyPem. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XfRgMke8p7QDxD5QM5thxH
This commit is contained in:
@@ -889,22 +889,18 @@ func (i *jsIPN) listen(network, addr string) js.Value {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *jsIPN) dialTLS(addr string, opts js.Value) js.Value {
|
// tlsClientConfigFromJS builds a client tls.Config from optional JS options
|
||||||
return makePromise(func() (any, error) {
|
// (serverName, insecureSkipVerify, caCerts). defaultServerName may be empty
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
// (STARTTLS upgrade case), in which case serverName must be provided unless
|
||||||
defer cancel()
|
// insecureSkipVerify is set.
|
||||||
|
//
|
||||||
host, _, err := net.SplitHostPort(addr)
|
// On wasm there's no system root pool, so default to the baked-in
|
||||||
if err != nil {
|
// LetsEncrypt roots (which is what `tailscale cert` uses for tailnet
|
||||||
return nil, fmt.Errorf("invalid address %q: %w", addr, err)
|
// HTTPS endpoints). Callers can override with caCerts (PEM) or bypass
|
||||||
}
|
// entirely with insecureSkipVerify.
|
||||||
|
func tlsClientConfigFromJS(defaultServerName string, opts js.Value) (*tls.Config, error) {
|
||||||
// On wasm there's no system root pool, so default to the
|
|
||||||
// baked-in LetsEncrypt roots (which is what `tailscale cert`
|
|
||||||
// uses for tailnet HTTPS endpoints). Callers can override with
|
|
||||||
// caCerts (PEM) or bypass entirely with insecureSkipVerify.
|
|
||||||
cfg := &tls.Config{
|
cfg := &tls.Config{
|
||||||
ServerName: host,
|
ServerName: defaultServerName,
|
||||||
RootCAs: bakedroots.Get(),
|
RootCAs: bakedroots.Get(),
|
||||||
}
|
}
|
||||||
if !opts.IsUndefined() && !opts.IsNull() {
|
if !opts.IsUndefined() && !opts.IsNull() {
|
||||||
@@ -922,6 +918,26 @@ func (i *jsIPN) dialTLS(addr string, opts js.Value) js.Value {
|
|||||||
cfg.RootCAs = pool
|
cfg.RootCAs = pool
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if cfg.ServerName == "" && !cfg.InsecureSkipVerify {
|
||||||
|
return nil, fmt.Errorf("serverName is required unless insecureSkipVerify is set")
|
||||||
|
}
|
||||||
|
return cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *jsIPN) dialTLS(addr string, opts js.Value) js.Value {
|
||||||
|
return makePromise(func() (any, error) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
host, _, err := net.SplitHostPort(addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid address %q: %w", addr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg, err := tlsClientConfigFromJS(host, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
rawConn, err := i.dialer.UserDial(ctx, "tcp", addr)
|
rawConn, err := i.dialer.UserDial(ctx, "tcp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1464,6 +1480,46 @@ func wrapConn(conn net.Conn) map[string]any {
|
|||||||
"remoteAddr": js.FuncOf(func(this js.Value, args []js.Value) any {
|
"remoteAddr": js.FuncOf(func(this js.Value, args []js.Value) any {
|
||||||
return conn.RemoteAddr().String()
|
return conn.RemoteAddr().String()
|
||||||
}),
|
}),
|
||||||
|
// upgradeTLS wraps the conn in TLS in place (STARTTLS-style) and
|
||||||
|
// returns a new wrapped conn sharing the same underlying net.Conn;
|
||||||
|
// the old handle must not be used afterward. On handshake failure
|
||||||
|
// the underlying conn is closed. With isServer, certPem and keyPem
|
||||||
|
// are required; otherwise client options as in dialTLS apply.
|
||||||
|
"upgradeTLS": js.FuncOf(func(this js.Value, args []js.Value) any {
|
||||||
|
opts := js.Undefined()
|
||||||
|
if len(args) > 0 {
|
||||||
|
opts = args[0]
|
||||||
|
}
|
||||||
|
return makePromise(func() (any, error) {
|
||||||
|
var tlsConn *tls.Conn
|
||||||
|
hasOpts := !opts.IsUndefined() && !opts.IsNull()
|
||||||
|
if hasOpts && opts.Get("isServer").Type() == js.TypeBoolean && opts.Get("isServer").Bool() {
|
||||||
|
certPem := opts.Get("certPem")
|
||||||
|
keyPem := opts.Get("keyPem")
|
||||||
|
if certPem.Type() != js.TypeString || keyPem.Type() != js.TypeString {
|
||||||
|
return nil, fmt.Errorf("upgradeTLS: certPem and keyPem are required when isServer is set")
|
||||||
|
}
|
||||||
|
cert, err := tls.X509KeyPair([]byte(certPem.String()), []byte(keyPem.String()))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("upgradeTLS: parsing cert/key: %w", err)
|
||||||
|
}
|
||||||
|
tlsConn = tls.Server(conn, &tls.Config{Certificates: []tls.Certificate{cert}})
|
||||||
|
} else {
|
||||||
|
cfg, err := tlsClientConfigFromJS("", opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tlsConn = tls.Client(conn, cfg)
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := tlsConn.HandshakeContext(ctx); err != nil {
|
||||||
|
conn.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return wrapConn(tlsConn), nil
|
||||||
|
})
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user