forked from longtao.wu/droboticflow
106 lines
3.0 KiB
Go
106 lines
3.0 KiB
Go
// query-lab-bridge is an isolated-fixture transport, never a production service.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"io"
|
|
"log"
|
|
"net"
|
|
"net/netip"
|
|
"os"
|
|
"os/signal"
|
|
"sync"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
func allowedFlow(listen, upstream, node, actualUID, expectedUID string) bool {
|
|
if (node != "10.189.16.4" && node != "10.189.16.33") || actualUID == "" || actualUID != expectedUID {
|
|
return false
|
|
}
|
|
l, le := netip.ParseAddrPort(listen)
|
|
u, ue := netip.ParseAddrPort(upstream)
|
|
if le != nil || ue != nil {
|
|
return false
|
|
}
|
|
private := netip.MustParsePrefix("10.189.16.0/24")
|
|
return l == netip.MustParseAddrPort("127.0.0.1:27229") && private.Contains(u.Addr()) && u.Port() == 27130 ||
|
|
node == "10.189.16.4" && private.Contains(l.Addr()) && l.Port() == 27130 && u == netip.MustParseAddrPort("127.0.0.1:27129")
|
|
}
|
|
|
|
// Do not embed TCPConn: promoted ReadFrom/WriteTo would bypass the deadlines
|
|
// when io.Copy selects its optimized interfaces.
|
|
type boundedConn struct{ conn *net.TCPConn }
|
|
|
|
func (c boundedConn) Read(p []byte) (int, error) {
|
|
_ = c.conn.SetReadDeadline(time.Now().Add(time.Minute))
|
|
return c.conn.Read(p)
|
|
}
|
|
func (c boundedConn) Write(p []byte) (int, error) {
|
|
_ = c.conn.SetWriteDeadline(time.Now().Add(time.Minute))
|
|
return c.conn.Write(p)
|
|
}
|
|
|
|
func main() {
|
|
listen := flag.String("listen", "", "fixed isolated listen address")
|
|
upstream := flag.String("upstream", "", "fixed isolated query address")
|
|
uid := flag.String("pod-uid", "", "preflight Pod identity")
|
|
flag.Parse()
|
|
if !allowedFlow(*listen, *upstream, os.Getenv("NODE_NAME"), os.Getenv("POD_UID"), *uid) {
|
|
log.Fatal("isolated transport identity or address rejected")
|
|
}
|
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
|
|
defer stop()
|
|
listener, err := net.Listen("tcp4", *listen)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer listener.Close()
|
|
go func() { <-ctx.Done(); _ = listener.Close() }()
|
|
var workers sync.WaitGroup
|
|
defer workers.Wait()
|
|
slots := make(chan struct{}, 128)
|
|
for ctx.Err() == nil {
|
|
conn, err := listener.Accept()
|
|
if err != nil {
|
|
if ctx.Err() != nil || errors.Is(err, net.ErrClosed) {
|
|
return
|
|
}
|
|
log.Fatal("isolated accept failed")
|
|
}
|
|
select {
|
|
case slots <- struct{}{}:
|
|
default:
|
|
_ = conn.Close()
|
|
continue
|
|
}
|
|
workers.Add(1)
|
|
go func(conn net.Conn) {
|
|
defer workers.Done()
|
|
defer func() { <-slots }()
|
|
defer conn.Close()
|
|
dialer := net.Dialer{Timeout: 3 * time.Second}
|
|
remote, err := dialer.DialContext(ctx, "tcp4", *upstream)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer remote.Close()
|
|
a, b := conn.(*net.TCPConn), remote.(*net.TCPConn)
|
|
for _, c := range []*net.TCPConn{a, b} {
|
|
_ = c.SetReadBuffer(64 << 10)
|
|
_ = c.SetWriteBuffer(64 << 10)
|
|
}
|
|
localCtx, cancel := context.WithCancel(ctx)
|
|
defer cancel()
|
|
go func() { <-localCtx.Done(); _ = a.Close(); _ = b.Close() }()
|
|
done := make(chan struct{})
|
|
go func() { _, _ = io.Copy(boundedConn{b}, boundedConn{a}); _ = b.CloseWrite(); close(done) }()
|
|
_, _ = io.Copy(boundedConn{a}, boundedConn{b})
|
|
_ = a.CloseWrite()
|
|
<-done
|
|
}(conn)
|
|
}
|
|
}
|