mirror of
https://github.com/fosrl/gerbil.git
synced 2026-03-22 12:54:30 -05:00
Compare commits
4 Commits
proxy-lock
...
proxy-cont
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5eacbb7239 | ||
|
|
d21c09c84f | ||
|
|
28c65b950c | ||
|
|
1643d71905 |
@@ -18,6 +18,7 @@ import (
|
||||
|
||||
"github.com/fosrl/gerbil/logger"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
// RouteRecord represents a routing configuration
|
||||
@@ -72,7 +73,9 @@ type SNIProxy struct {
|
||||
}
|
||||
|
||||
type activeTunnel struct {
|
||||
conns map[net.Conn]struct{}
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
count int // protected by activeTunnelsLock
|
||||
}
|
||||
|
||||
// readOnlyConn is a wrapper for io.Reader that implements net.Conn
|
||||
@@ -588,30 +591,32 @@ func (p *SNIProxy) handleConnection(clientConn net.Conn) {
|
||||
}
|
||||
}
|
||||
|
||||
// Track this tunnel by SNI
|
||||
// Track this tunnel by SNI using context for cancellation
|
||||
p.activeTunnelsLock.Lock()
|
||||
tunnel, ok := p.activeTunnels[hostname]
|
||||
if !ok {
|
||||
tunnel = &activeTunnel{conns: make(map[net.Conn]struct{})}
|
||||
ctx, cancel := context.WithCancel(p.ctx)
|
||||
tunnel = &activeTunnel{ctx: ctx, cancel: cancel}
|
||||
p.activeTunnels[hostname] = tunnel
|
||||
}
|
||||
tunnel.conns[actualClientConn] = struct{}{}
|
||||
tunnel.count++
|
||||
tunnelCtx := tunnel.ctx
|
||||
p.activeTunnelsLock.Unlock()
|
||||
|
||||
defer func() {
|
||||
// Remove this conn from active tunnels - O(1) with map
|
||||
p.activeTunnelsLock.Lock()
|
||||
if tunnel, ok := p.activeTunnels[hostname]; ok {
|
||||
delete(tunnel.conns, actualClientConn)
|
||||
if len(tunnel.conns) == 0 {
|
||||
tunnel.count--
|
||||
if tunnel.count == 0 {
|
||||
tunnel.cancel()
|
||||
if p.activeTunnels[hostname] == tunnel {
|
||||
delete(p.activeTunnels, hostname)
|
||||
}
|
||||
}
|
||||
p.activeTunnelsLock.Unlock()
|
||||
}()
|
||||
|
||||
// Start bidirectional data transfer
|
||||
p.pipe(actualClientConn, targetConn, clientReader)
|
||||
// Start bidirectional data transfer with tunnel context
|
||||
p.pipe(tunnelCtx, actualClientConn, targetConn, clientReader)
|
||||
}
|
||||
|
||||
// getRoute retrieves routing information for a hostname
|
||||
@@ -747,47 +752,36 @@ func (p *SNIProxy) selectStickyEndpoint(clientAddr string, endpoints []string) s
|
||||
}
|
||||
|
||||
// pipe handles bidirectional data transfer between connections
|
||||
func (p *SNIProxy) pipe(clientConn, targetConn net.Conn, clientReader io.Reader) {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
func (p *SNIProxy) pipe(ctx context.Context, clientConn, targetConn net.Conn, clientReader io.Reader) {
|
||||
g, gCtx := errgroup.WithContext(ctx)
|
||||
|
||||
// closeOnce ensures we only close connections once
|
||||
var closeOnce sync.Once
|
||||
closeConns := func() {
|
||||
closeOnce.Do(func() {
|
||||
// Close both connections to unblock any pending reads
|
||||
clientConn.Close()
|
||||
targetConn.Close()
|
||||
})
|
||||
}
|
||||
// Close connections when context cancels to unblock io.Copy operations
|
||||
context.AfterFunc(gCtx, func() {
|
||||
clientConn.Close()
|
||||
targetConn.Close()
|
||||
})
|
||||
|
||||
// Copy data from client to target (using the buffered reader)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
defer closeConns()
|
||||
|
||||
// Use a large buffer for better performance
|
||||
// Copy data from client to target
|
||||
g.Go(func() error {
|
||||
buf := make([]byte, 32*1024)
|
||||
_, err := io.CopyBuffer(targetConn, clientReader, buf)
|
||||
if err != nil && err != io.EOF {
|
||||
logger.Debug("Copy client->target error: %v", err)
|
||||
}
|
||||
}()
|
||||
return err
|
||||
})
|
||||
|
||||
// Copy data from target to client
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
defer closeConns()
|
||||
|
||||
// Use a large buffer for better performance
|
||||
g.Go(func() error {
|
||||
buf := make([]byte, 32*1024)
|
||||
_, err := io.CopyBuffer(clientConn, targetConn, buf)
|
||||
if err != nil && err != io.EOF {
|
||||
logger.Debug("Copy target->client error: %v", err)
|
||||
}
|
||||
}()
|
||||
return err
|
||||
})
|
||||
|
||||
wg.Wait()
|
||||
g.Wait()
|
||||
}
|
||||
|
||||
// GetCacheStats returns cache statistics
|
||||
@@ -803,46 +797,34 @@ func (p *SNIProxy) ClearCache() {
|
||||
|
||||
// UpdateLocalSNIs updates the local SNIs and invalidates cache for changed domains
|
||||
func (p *SNIProxy) UpdateLocalSNIs(fullDomains []string) {
|
||||
newSNIs := make(map[string]struct{}, len(fullDomains))
|
||||
newSNIs := make(map[string]struct{})
|
||||
for _, domain := range fullDomains {
|
||||
newSNIs[domain] = struct{}{}
|
||||
// Invalidate any cached route for this domain
|
||||
p.cache.Delete(domain)
|
||||
}
|
||||
|
||||
// Get old SNIs with read lock to compute diff outside write lock
|
||||
p.localSNIsLock.RLock()
|
||||
oldSNIs := p.localSNIs
|
||||
p.localSNIsLock.RUnlock()
|
||||
|
||||
// Compute removed SNIs outside the lock
|
||||
// Update localSNIs
|
||||
p.localSNIsLock.Lock()
|
||||
removed := make([]string, 0)
|
||||
for sni := range oldSNIs {
|
||||
for sni := range p.localSNIs {
|
||||
if _, stillLocal := newSNIs[sni]; !stillLocal {
|
||||
removed = append(removed, sni)
|
||||
}
|
||||
}
|
||||
|
||||
// Swap with minimal write lock hold time
|
||||
p.localSNIsLock.Lock()
|
||||
p.localSNIs = newSNIs
|
||||
p.localSNIsLock.Unlock()
|
||||
|
||||
// Invalidate cache for new domains (cache is thread-safe)
|
||||
for domain := range newSNIs {
|
||||
p.cache.Delete(domain)
|
||||
}
|
||||
|
||||
logger.Debug("Updated local SNIs, added %d, removed %d", len(newSNIs), len(removed))
|
||||
|
||||
// Terminate tunnels for removed SNIs
|
||||
// Terminate tunnels for removed SNIs via context cancellation
|
||||
if len(removed) > 0 {
|
||||
p.activeTunnelsLock.Lock()
|
||||
for _, sni := range removed {
|
||||
if tunnel, ok := p.activeTunnels[sni]; ok {
|
||||
for conn := range tunnel.conns {
|
||||
conn.Close()
|
||||
}
|
||||
tunnel.cancel()
|
||||
delete(p.activeTunnels, sni)
|
||||
logger.Debug("Closed tunnels for SNI target change: %s", sni)
|
||||
logger.Debug("Cancelled tunnel context for SNI target change: %s", sni)
|
||||
}
|
||||
}
|
||||
p.activeTunnelsLock.Unlock()
|
||||
|
||||
Reference in New Issue
Block a user