Skip to content

Commit

Permalink
Added root CA config
Browse files Browse the repository at this point in the history
  • Loading branch information
myleshorton committed Feb 5, 2025
1 parent 2be938c commit 48427de
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions kindling.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package kindling

import (
"context"
"crypto/x509"
"embed"
"encoding/pem"
"fmt"
"io"
"log/slog"
Expand Down Expand Up @@ -31,6 +33,7 @@ type httpDialer func(ctx context.Context, addr string) (http.RoundTripper, error
type kindling struct {
httpDialers []httpDialer
logWriter io.Writer
rootCA string
}

// Make sure that kindling implements the Kindling interface.
Expand Down Expand Up @@ -75,10 +78,10 @@ func WithDomainFronting(configURL, countryCode string) Option {
}
}

// WithDoHTunnel is a functional option that enables DNS over HTTPS (DoH) tunneling for the Kindling.
func WithDoHTunnel() Option {
// WithRootCA pins the root CA to use for TLS.
func WithRootCA(rootCA string) Option {
return func(k *kindling) {

k.rootCA = rootCA
}
}

Expand Down Expand Up @@ -149,7 +152,7 @@ func (k *kindling) newSmartHTTPDialer(domains ...string) (httpDialer, error) {
}
return k.newTransportWithDialContext(func(ctx context.Context, network, addr string) (net.Conn, error) {
return streamConn, nil
}), nil
})
}, nil
}

Expand All @@ -165,18 +168,31 @@ func (k *kindling) newSmartHTTPTransport(domains ...string) (*http.Transport, er
return nil, fmt.Errorf("failed to dial stream: %v", err)
}
return streamConn, nil
}), nil
})
}

func (k *kindling) newTransportWithDialContext(dialContext func(ctx context.Context, network, addr string) (net.Conn, error)) *http.Transport {
return &http.Transport{
func (k *kindling) newTransportWithDialContext(dialContext func(ctx context.Context, network, addr string) (net.Conn, error)) (*http.Transport, error) {
tr := &http.Transport{
DialContext: dialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 20 * time.Second,
ExpectContinueTimeout: 4 * time.Second,
}
if k.rootCA != "" {
block, _ := pem.Decode([]byte(k.rootCA))
if block == nil {
return nil, fmt.Errorf("failed to decode root CA PEM block")
}
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(block.Bytes) {
log.Error("Failed to append root CA to pool")
return nil, fmt.Errorf("failed to append root CA to pool")
}
tr.TLSClientConfig.RootCAs = certPool
}
return tr, nil
}

//go:embed smart_dialer_config.yml
Expand Down

0 comments on commit 48427de

Please sign in to comment.