mirror of
https://github.com/go-i2p/go-meta-listener.git
synced 2025-09-07 10:47:44 -04:00
Compare commits
4 Commits
ab41cecda5
...
6b30e62ee4
Author | SHA1 | Date | |
---|---|---|---|
![]() |
6b30e62ee4 | ||
![]() |
c0524805f7 | ||
![]() |
b692f9c58f | ||
![]() |
509c265a3d |
2
Makefile
Normal file
2
Makefile
Normal file
@@ -0,0 +1,2 @@
|
||||
fmt:
|
||||
find . -name '*.go' -exec gofumpt -s -extra -w {} \;
|
@@ -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
|
||||
}
|
||||
|
@@ -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)
|
||||
|
||||
|
Reference in New Issue
Block a user