diff --git a/README.md b/README.md index 26dea5c..e592384 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,8 @@ Download from releases or build it and place it to `~/.local/bin/proxfix`. ```bash PROXYFIX=proxy-fix-linux-$( [ "$(uname -m)" = "aarch64" ] && echo "arm64" || echo "amd64" ) -wget https://github.com/domcloud/proxy-fix/releases/download/v0.2.4/$PROXYFIX.tar.gz -tar -xf $PROXYFIX.tar.gz && mv $PROXYFIX /usr/local/bin/proxfix && rm -rf $PROXYFIX* +wget https://github.com/domcloud/proxy-fix/releases/download/v0.2.5/$PROXYFIX.tar.gz +tar -xf $PROXYFIX.tar.gz && mv -f $PROXYFIX /usr/local/bin/proxfix && rm -rf $PROXYFIX* ``` ## Usage diff --git a/main.go b/main.go index f7c8f1a..1379d09 100644 --- a/main.go +++ b/main.go @@ -12,10 +12,14 @@ import ( ) var outDial string +var pid int func init() { + initProcess() +} + +func initProcess() { var err error - var pid int args := os.Args bg := os.Getenv("NOHUP") == "1" @@ -24,7 +28,7 @@ func init() { outDial, pid, err = checkExistingProcess() if err == nil { if isPortListening(outDial) && outDial == preferredDial { - fmt.Printf("Process is already running on %d\n", outDial) + fmt.Printf("Process is already running on %s\n", outDial) return } else { fmt.Printf("Killing stale process %d \n", pid) diff --git a/proxy.go b/proxy.go index 79d05c4..12c10a2 100644 --- a/proxy.go +++ b/proxy.go @@ -29,10 +29,17 @@ func (proxy *Proxy) handleConnection(clientConn net.Conn) { // Filter invalid headers filterInvalidHeaders(request.Header) + oldpid := pid // Connect to the destination server +retryonce: destConn, err := proxy.handleDial() if err != nil { + if !proxy.connected && oldpid == pid && !processExists(pid) { + fmt.Println("Process died, reinit") + initProcess() + goto retryonce + } return } defer destConn.Close() @@ -75,7 +82,7 @@ func handleHTTP(destConn net.Conn, clientConn net.Conn) { func (proxy *Proxy) handleDial() (destConn net.Conn, err error) { retries := 0 retry: - destConn, err = net.Dial("tcp", proxy.DialTarget) + destConn, err = net.DialTimeout("tcp", proxy.DialTarget, time.Second) if err != nil { if retries < MAX_RETRY && !proxy.connected { retries += 1 @@ -84,6 +91,8 @@ retry: goto retry } fmt.Printf("Error connecting to destination: %v\n", err) + proxy.connected = false + return } proxy.connected = true return