From b96f3ce26dc518c165f1939be1f5b7ed6f2e8701 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Mon, 17 Mar 2025 05:08:46 -0500 Subject: [PATCH] attempt to drain the sockets --- Makefile | 6 +++--- main.go | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 1b8cb25..1caa794 100644 --- a/Makefile +++ b/Makefile @@ -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}" diff --git a/main.go b/main.go index 42dffc3..c40a1ce 100644 --- a/main.go +++ b/main.go @@ -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]) + } +}