Skip to content

Agent Mesh

sambox review

Antonio Ojeab90cb0ac1f56
diff --git a/cmd/nano-init/main.go b/cmd/nano-init/main.go
index 4f4039a4..73c7f792 100644
--- a/cmd/nano-init/main.go
+++ b/cmd/nano-init/main.go
@@ -97,7 +97,8 @@ func main() {
return
default:
log.Printf("TCP Accept error: %v", err)
- return
+ time.Sleep(50 * time.Millisecond)
+ continue
}
}
go handleConnection(conn, udsPath)
@@ -288,7 +289,8 @@ func startDNSSpoofer(ctx context.Context, dnsAddr string) error {
return
default:
log.Printf("DNS Server read error: %v", err)
- return
+ time.Sleep(50 * time.Millisecond)
+ continue
}
}
@@ -319,7 +321,7 @@ func startDNSSpoofer(ctx context.Context, dnsAddr string) error {
Body: &dnsmessage.AResource{A: [4]byte{127, 0, 0, 1}},
})
case dnsmessage.TypeAAAA:
- ip := net.ParseIP("127.0.0.1").To16()
+ ip := net.ParseIP("::1").To16()
var aaaa [16]byte
copy(aaaa[:], ip)
resp.Answers = append(resp.Answers, dnsmessage.Resource{
@@ -371,7 +373,7 @@ func setupPID1Duties(agentPid int, exitChan chan<- syscall.WaitStatus) {
signal.Notify(sigCh, syscall.SIGCHLD)
go func() {
- for range sigCh {
+ reap := func() {
for {
var status syscall.WaitStatus
pid, err := syscall.Wait4(-1, &status, syscall.WNOHANG, nil)
@@ -386,6 +388,11 @@ func setupPID1Duties(agentPid int, exitChan chan<- syscall.WaitStatus) {
}
}
}
+ // Initial reap pass to catch any processes that exited before signal.Notify was registered
+ reap()
+ for range sigCh {
+ reap()
+ }
}()
}
diff --git a/cmd/nano-init/main_test.go b/cmd/nano-init/main_test.go
index 9df901bb..ada75405 100644
--- a/cmd/nano-init/main_test.go
+++ b/cmd/nano-init/main_test.go
@@ -187,7 +187,7 @@ func TestDNSSpoofer(t *testing.T) {
t.Fatalf("Expected AAAAResource body type")
}
- expectedAAAA := net.ParseIP("127.0.0.1").To16()
+ expectedAAAA := net.ParseIP("::1").To16()
var expectedAAAABytes [16]byte
copy(expectedAAAABytes[:], expectedAAAA)
diff --git a/cmd/sam-box/main.go b/cmd/sam-box/main.go
index 59694462..523c52e6 100644
--- a/cmd/sam-box/main.go
+++ b/cmd/sam-box/main.go
@@ -315,9 +315,7 @@ func main() {
if err := os.Remove(udsPathFlag); err != nil && !os.IsNotExist(err) {
logger.Fatalf("Failed to remove existing UDS file: %v", err)
}
- oldUmask := syscall.Umask(0177)
listener, err := net.Listen("unix", udsPathFlag)
- syscall.Umask(oldUmask)
if err != nil {
logger.Fatalf("Failed to listen on UDS socket: %v", err)
}
diff --git a/internal/sambox/gateway.go b/internal/sambox/gateway.go
index 0e6ee3bc..d85cc9fd 100644
--- a/internal/sambox/gateway.go
+++ b/internal/sambox/gateway.go
@@ -26,6 +26,7 @@ import (
"errors"
"fmt"
"io"
+ "log"
"math/big"
"net"
"net/http"
@@ -245,7 +246,12 @@ func (g *Gateway) Serve(listener net.Listener) error {
case <-tlsListener.closed:
return nil
default:
- return err
+ if errors.Is(err, net.ErrClosed) {
+ return nil
+ }
+ log.Printf("Accept error: %v; retrying in 50ms", err)
+ time.Sleep(50 * time.Millisecond)
+ continue
}
}
@@ -292,8 +298,8 @@ func (g *Gateway) handleConnection(rawConn net.Conn, tlsListener *channelListene
}
}
-func (g *Gateway) handleHTTPConnection(conn net.Conn, tlsListener *channelListener) {
- req, err := http.ReadRequest(bufio.NewReader(conn))
+func (g *Gateway) handleHTTPConnection(conn *bufferedConn, tlsListener *channelListener) {
+ req, err := http.ReadRequest(conn.r)
if err != nil {
_ = conn.Close()
return
@@ -402,7 +408,7 @@ func (g *Gateway) upgradeToTLS(rawConn net.Conn, sni string, tlsListener *channe
type bufferedConn struct {
net.Conn
- r io.Reader
+ r *bufio.Reader
}
func (c *bufferedConn) Read(b []byte) (int, error) {