Skip to content

Commit

Permalink
Detect process killed
Browse files Browse the repository at this point in the history
  • Loading branch information
willnode committed Oct 31, 2024
1 parent 349b239 commit 7019981
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -84,6 +91,8 @@ retry:
goto retry
}
fmt.Printf("Error connecting to destination: %v\n", err)
proxy.connected = false
return
}
proxy.connected = true
return
Expand Down

0 comments on commit 7019981

Please sign in to comment.