4 Commits

Author SHA1 Message Date
eyedeekay
6b30e62ee4 Close other listeners if any listener fails 2025-05-05 15:45:51 -04:00
eyedeekay
c0524805f7 Makefile, fmt 2025-05-05 15:34:08 -04:00
eyedeekay
b692f9c58f See if it's a TLS issue... 2025-05-05 15:33:09 -04:00
eyedeekay
509c265a3d add more logging to AddHeaders 2025-05-05 15:28:59 -04:00
3 changed files with 32 additions and 3 deletions

2
Makefile Normal file
View File

@@ -0,0 +1,2 @@
fmt:
find . -name '*.go' -exec gofumpt -s -extra -w {} \;

View File

@@ -14,6 +14,8 @@ var (
ErrListenerClosed = errors.New("listener is closed")
// ErrNoListeners is returned when the meta listener has no active listeners
ErrNoListeners = errors.New("no active listeners")
// ErrInternalListenerFailure is returned when an internal listener fails
ErrInternalListenerFailure = errors.New("Internal listener error, shutting down metalistener for restart")
)
// MetaListener implements the net.Listener interface and manages multiple
@@ -127,12 +129,19 @@ func (ml *MetaListener) handleListener(id string, listener net.Listener) {
ml.mu.RUnlock()
if stillExists {
// Only report error if this listener hasn't been removed
// Create a combined error with both the standard message and original error details
combinedErr := fmt.Errorf("%w: listener %s error - %v",
ErrInternalListenerFailure, id, err)
// Send the combined error to notify Accept() calls
select {
case ml.errCh <- fmt.Errorf("listener %s error: %w", id, err):
case ml.errCh <- combinedErr:
default:
// Don't block if no one is reading errors
}
// Then close all listeners
go ml.Close()
}
return
}

View File

@@ -2,6 +2,7 @@ package mirror
import (
"bufio"
"crypto/tls"
"log"
"net"
"net/http"
@@ -17,6 +18,8 @@ func AddHeaders(conn net.Conn, headers map[string]string) net.Conn {
// if the request is not an HTTP request, return the connection as is
req, err := http.ReadRequest(bufio.NewReader(conn))
if err != nil {
log.Println("Error reading request:", err)
// if the request is not an HTTP request, return the connection as is
return conn
}
log.Println("Adding headers to connection:", req.Method, req.URL)
@@ -26,9 +29,11 @@ func AddHeaders(conn net.Conn, headers map[string]string) net.Conn {
}
// write the request back to the connection
if err := req.Write(conn); err != nil {
log.Println("Error writing request:", err)
// if there is an error writing the request, return the connection as is
return conn
}
// return the connection with the headers added
// If all goes well, return the connection with the headers added
return conn
}
@@ -39,14 +44,27 @@ func (ml *Mirror) Accept() (net.Conn, error) {
// Accept a connection from the listener
conn, err := ml.MetaListener.Accept()
if err != nil {
log.Println("Error accepting connection:", err)
return nil, err
}
// Check if the connection is a TLS connection
if tlsConn, ok := conn.(*tls.Conn); ok {
// If it is a TLS connection, perform the handshake
if err := tlsConn.Handshake(); err != nil {
log.Println("Error performing TLS handshake:", err)
return nil, err
}
// If the handshake is successful, get the underlying connection
conn = tlsConn.NetConn()
}
host := map[string]string{
"Host": ml.MetaListener.Addr().String(),
"X-Forwarded-For": conn.RemoteAddr().String(),
"X-Forwarded-Proto": "http",
}
// Add headers to the connection
conn = AddHeaders(conn, host)