Skip to content

Better privacy by fragmenting SNI clientHello #24

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions cmd/tlsrouter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ func (c *Config) Match(hostname string) (string, bool) {
}

for _, r := range c.routes {
if r.match.MatchString(hostname) {
return r.backend, r.proxyInfo
matches := r.match.FindStringSubmatchIndex(hostname)
if matches != nil {
result := r.match.ExpandString(nil, r.backend, hostname, matches)
return string(result), r.proxyInfo
}
}
return "", false
Expand Down
80 changes: 75 additions & 5 deletions cmd/tlsrouter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,55 @@ package main

import (
"bytes"
"context"
"crypto/tls"
"flag"
"fmt"
"io"
"log"
"net"
"strings"
"sync"
"time"
)

var (
cfgFile = flag.String("conf", "", "configuration file")
listen = flag.String("listen", ":443", "listening port")
helloTimeout = flag.Duration("hello-timeout", 3*time.Second, "how long to wait for the TLS ClientHello")
cfgFile = flag.String("conf", "", "configuration file")
listen = flag.String("listen", ":443", "listening port")
helloTimeout = flag.Duration("hello-timeout", 3*time.Second, "how long to wait for the TLS ClientHello")
helloMss = flag.Int64("hello-mss", 0, "how many bytes to fragment/segment the TLS ClientHello")
resolverAddress = flag.String("dns", "", "address of the dns resolver")
resolverNetwork = flag.String("dns-net", "", "protocol for the dns resolver (e.g. \"tcp-tls\" or \"tcp\" or \"udp\")")
)

// BackendDialer with timeout
var BackendDialer = &net.Dialer{
Timeout: 15 * time.Second,
Resolver: &net.Resolver{
PreferGo: true,
Dial: dialDNSResolver,
},
}

// ResolverDialer with timeout
var ResolverDialer = &net.Dialer{
Timeout: 10 * time.Second,
}

// ResolverTLSConfig for DNS-over-TLS
var ResolverTLSConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.X25519, tls.CurveP521, tls.CurveP384, tls.CurveP256},
CipherSuites: []uint16{
// tls.TLS_CHACHA20_POLY1305_SHA256,
// tls.TLS_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
},
}

func main() {
flag.Parse()

Expand Down Expand Up @@ -142,7 +176,7 @@ func (c *Conn) proxy() {
}

c.logf("routing %q to %q", c.hostname, c.backend)
backend, err := net.DialTimeout("tcp", c.backend, 10*time.Second)
backend, err := BackendDialer.Dial("tcp", c.backend)
if err != nil {
c.internalError("failed to dial backend %q for %q: %s", c.backend, c.hostname, err)
return
Expand All @@ -168,7 +202,20 @@ func (c *Conn) proxy() {

// Replay the piece of the handshake we had to read to do the
// routing, then blindly proxy any other bytes.
if _, err = io.Copy(c.backendConn, &handshakeBuf); err != nil {
if *helloMss == 0 {
_, err = io.Copy(c.backendConn, &handshakeBuf)
} else {
for {
_, err = io.CopyN(c.backendConn, &handshakeBuf, *helloMss)
if err != nil {
if err == io.EOF {
err = nil
}
break
}
}
}
if err != nil {
c.internalError("failed to replay handshake to %q: %s", c.backend, err)
return
}
Expand All @@ -189,3 +236,26 @@ func proxy(wg *sync.WaitGroup, a, b net.Conn) {
btcp.CloseWrite()
atcp.CloseRead()
}

func dialDNSResolver(ctx context.Context, network, address string) (net.Conn, error) {
if *resolverNetwork != "" {
network = *resolverNetwork
}
if *resolverAddress != "" {
address = *resolverAddress
}

useTLS := strings.HasPrefix(network, "tcp") && strings.HasSuffix(network, "-tls")
if useTLS {
network = strings.TrimSuffix(network, "-tls")
if !strings.Contains(address, ":") {
address += ":853"
}
return tls.DialWithDialer(ResolverDialer, network, address, ResolverTLSConfig)
}

if !strings.Contains(address, ":") {
address += ":53"
}
return ResolverDialer.DialContext(ctx, network, address)
}