diff options
| author | Jeff Carr <[email protected]> | 2025-03-17 05:08:46 -0500 | 
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-03-17 05:08:46 -0500 | 
| commit | b96f3ce26dc518c165f1939be1f5b7ed6f2e8701 (patch) | |
| tree | 16dae8d9ca39dd94350e91f392e9caca95f1c0b0 | |
| parent | 24962f3524ab05bb990dd8f6f7c876b02c279880 (diff) | |
attempt to drain the sockets
| -rw-r--r-- | Makefile | 6 | ||||
| -rw-r--r-- | main.go | 47 | 
2 files changed, 48 insertions, 5 deletions
@@ -3,9 +3,9 @@  VERSION = $(shell git describe --tags)  BUILDTIME = $(shell date +%Y.%m.%d_%H%M) -all: proto build +all: proto install  	@#./gus --config /etc/gus/gus.text -	./gus --me +	gus --me  gocui: build  	./gus --gui gocui --config /etc/gus/gus.text >/tmp/gocui.log 2>&1 @@ -14,7 +14,7 @@ build: goimports vet  	GO111MODULE=off go build -v -x \  		-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" -install: +install: goimports vet  	GO111MODULE=off go install -v -x \  		-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}" @@ -14,6 +14,8 @@ import (  	sync "sync"  	"time" +	"github.com/svent/go-nbreader" +  	"github.com/google/uuid"  	"go.wit.com/dev/alexflint/arg"  	"go.wit.com/gui" @@ -254,6 +256,14 @@ func ioCopy(clientConn, targetConn net.Conn) {  	defer clientConn.Close()  	defer targetConn.Close() +	/* +		if !argv.UseME { +			// Drain existing data +			drainConnection(clientConn) +			drainConnection(targetConn) +		} +	*/ +  	var wg sync.WaitGroup  	wg.Add(2) @@ -287,8 +297,7 @@ func connectME() {  	}  	localport := "25910" -	// where := "104.48.38.253:25910" -	where := "104.48.38.253:8081" +	where := argv.URL  	dest, err := net.Dial("tcp", where)  	if err != nil {  		log.Printf("Failed to connect to %s %v\n", where, err) @@ -356,3 +365,37 @@ func getFQDN(hostname string) (string, error) {  	return "", fmt.Errorf("no FQDN found for hostname: %s", hostname)  } + +// drainConnection reads and discards any pending data before copying +func drainConnection(conn net.Conn) { +	// Create a non-blocking reader with a timeout of 1 second +	reader := nbreader.NewNBReader(conn, 1024, nbreader.Timeout(time.Second)) + +	buffer := make([]byte, 1024) + +	var count int + +	for { +		// Attempt to read data from the reader +		n, err := reader.Read(buffer) +		if err != nil { +			log.Info("error was:", err) +			// Handle error, such as timeout or end of stream +			break +		} + +		if n > 0 { +			log.Info("chunk size was", n, "buffer =", string(buffer)) +			count = 0 +			continue +		} + +		count += 1 +		if count > 3 { +			log.Info("buffer is drained") +			return +		} + +		// Process the read data (buffer[:n]) +	} +}  | 
