Skip to content

Commit 9f0506e

Browse files
andystimeclaude
andcommitted
Fix connection parsing to strip null bytes from connection file
The connection file uses a binary format with null-padded fields. Strip null bytes (\x00) when parsing IP, port, and PID to prevent sending invalid data to server. Example file content: "192.168.2.9\0\0\0\0\0,44800,51840" Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent f180532 commit 9f0506e

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

internal/agent/agent.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -808,19 +808,24 @@ func (a *Agent) readConnectionsFromDir() (map[string][]string, error) {
808808
connections := make(map[string][]string)
809809

810810
klog.V(5).Infof("Reading connections from directory: %s", a.connectionsDir)
811+
klog.Infof("[DEBUG] Scanning connections directory: %s", a.connectionsDir)
811812

812813
entries, err := os.ReadDir(a.connectionsDir)
813814
if err != nil {
814815
if os.IsNotExist(err) {
815816
klog.V(5).Infof("Connections directory does not exist yet: %s", a.connectionsDir)
817+
klog.Infof("[DEBUG] Connections directory does not exist: %s", a.connectionsDir)
816818
return connections, nil // No connections directory yet
817819
}
820+
klog.Infof("[DEBUG] Failed to read connections directory %s: %v", a.connectionsDir, err)
818821
return nil, fmt.Errorf("failed to read connections directory: %w", err)
819822
}
820823

821824
klog.V(5).Infof("Found %d entries in connections directory", len(entries))
825+
klog.Infof("[DEBUG] Found %d file entries in directory: %s", len(entries), a.connectionsDir)
822826

823827
for _, entry := range entries {
828+
klog.Infof("[DEBUG] Processing entry: name=%s, isDir=%v", entry.Name(), entry.IsDir())
824829
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".txt") {
825830
continue
826831
}
@@ -832,15 +837,20 @@ func (a *Agent) readConnectionsFromDir() (map[string][]string, error) {
832837
}
833838

834839
// Read connection lines from worker's file
835-
connLines, err := a.readWorkerConnectionFile(filepath.Join(a.connectionsDir, entry.Name()))
840+
filePath := filepath.Join(a.connectionsDir, entry.Name())
841+
klog.Infof("[DEBUG] Reading connection file for worker %s: %s", workerID, filePath)
842+
connLines, err := a.readWorkerConnectionFile(filePath)
836843
if err != nil {
837844
klog.V(4).Infof("Failed to read connection file for worker %s: %v", workerID, err)
845+
klog.Infof("[DEBUG] Failed to read connection file for worker %s: %v", workerID, err)
838846
continue
839847
}
840848

849+
klog.Infof("[DEBUG] Worker %s: Read %d connection line(s) from file", workerID, len(connLines))
841850
if len(connLines) > 0 {
842851
connections[workerID] = connLines
843852
klog.V(4).Infof("Worker %s has %d active connection(s): %v", workerID, len(connLines), connLines)
853+
klog.Infof("[DEBUG] Worker %s has %d active connection(s): %v", workerID, len(connLines), connLines)
844854
}
845855
}
846856

@@ -948,15 +958,23 @@ func parseConnectionsToAPI(connectionLines []string) []api.ConnectionInfo {
948958
if len(parts) < 1 || parts[0] == "" {
949959
continue
950960
}
951-
clientIP := strings.TrimSpace(parts[0])
961+
// Trim whitespace and null bytes from IP address
962+
clientIP := strings.Trim(strings.TrimSpace(parts[0]), "\x00")
963+
if clientIP == "" {
964+
continue
965+
}
952966
var clientPort, clientPID int
953967
if len(parts) >= 2 {
954-
if port, err := strconv.Atoi(strings.TrimSpace(parts[1])); err == nil {
968+
// Trim whitespace and null bytes from port
969+
portStr := strings.Trim(strings.TrimSpace(parts[1]), "\x00")
970+
if port, err := strconv.Atoi(portStr); err == nil {
955971
clientPort = port
956972
}
957973
}
958974
if len(parts) >= 3 {
959-
if pid, err := strconv.Atoi(strings.TrimSpace(parts[2])); err == nil {
975+
// Trim whitespace and null bytes from PID
976+
pidStr := strings.Trim(strings.TrimSpace(parts[2]), "\x00")
977+
if pid, err := strconv.Atoi(pidStr); err == nil {
960978
clientPID = pid
961979
}
962980
}

0 commit comments

Comments
 (0)