attempt to drain the sockets

This commit is contained in:
Jeff Carr 2025-03-17 05:08:46 -05:00
parent 24962f3524
commit b96f3ce26d
2 changed files with 48 additions and 5 deletions

View File

@ -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}"

47
main.go
View File

@ -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])
}
}