Compare commits

...

2 Commits

Author SHA1 Message Date
Bel LaPointe
5db6a4360e maybe 2026-02-03 20:37:00 -07:00
Bel LaPointe
057c5d0d25 gotta swap to tcp conns 2026-02-03 20:27:45 -07:00
7 changed files with 372 additions and 0 deletions

26
go.mod Normal file
View File

@@ -0,0 +1,26 @@
module red-apter
go 1.24.0
toolchain go1.24.12
require (
github.com/lib/pq v1.11.1
github.com/redis/go-redis/v9 v9.17.3
modernc.org/sqlite v1.44.3
)
require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect
golang.org/x/sys v0.37.0 // indirect
modernc.org/libc v1.67.6 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.11.0 // indirect
)

65
go.sum Normal file
View File

@@ -0,0 +1,65 @@
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/lib/pq v1.11.1 h1:wuChtj2hfsGmmx3nf1m7xC2XpK6OtelS2shMY+bGMtI=
github.com/lib/pq v1.11.1/go.mod h1:/p+8NSbOcwzAEI7wiMXFlgydTwcgTr3OSKMsD2BitpA=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/redis/go-redis/v9 v9.17.3 h1:fN29NdNrE17KttK5Ndf20buqfDZwGNgoUr9qjl1DQx4=
github.com/redis/go-redis/v9 v9.17.3/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 h1:mgKeJMpvi0yx/sU5GsxQ7p6s2wtOnGAHZWCHUM4KGzY=
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546/go.mod h1:j/pmGrbnkbPtQfxEe5D0VQhZC6qKbfKifgD0oM7sR70=
golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA=
golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w=
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=
golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs=
modernc.org/cc/v4 v4.27.1 h1:9W30zRlYrefrDV2JE2O8VDtJ1yPGownxciz5rrbQZis=
modernc.org/cc/v4 v4.27.1/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
modernc.org/ccgo/v4 v4.30.1 h1:4r4U1J6Fhj98NKfSjnPUN7Ze2c6MnAdL0hWw6+LrJpc=
modernc.org/ccgo/v4 v4.30.1/go.mod h1:bIOeI1JL54Utlxn+LwrFyjCx2n2RDiYEaJVSrgdrRfM=
modernc.org/fileutil v1.3.40 h1:ZGMswMNc9JOCrcrakF1HrvmergNLAmxOPjizirpfqBA=
modernc.org/fileutil v1.3.40/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc=
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
modernc.org/gc/v3 v3.1.1 h1:k8T3gkXWY9sEiytKhcgyiZ2L0DTyCQ/nvX+LoCljoRE=
modernc.org/gc/v3 v3.1.1/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY=
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
modernc.org/libc v1.67.6 h1:eVOQvpModVLKOdT+LvBPjdQqfrZq+pC39BygcT+E7OI=
modernc.org/libc v1.67.6/go.mod h1:JAhxUVlolfYDErnwiqaLvUqc8nfb2r6S6slAgZOnaiE=
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
modernc.org/sqlite v1.44.3 h1:+39JvV/HWMcYslAwRxHb8067w+2zowvFOUrOWIy9PjY=
modernc.org/sqlite v1.44.3/go.mod h1:CzbrU2lSB1DKUusvwGz7rqEKIq+NUd8GWuBBZDs9/nA=
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=

17
main.go Normal file
View File

@@ -0,0 +1,17 @@
package main
import (
"context"
"os/signal"
"red-apter/src"
"syscall"
)
func main() {
ctx, can := signal.NotifyContext(context.Background(), syscall.SIGINT)
defer can()
if err := src.Main(ctx); err != nil {
panic(err)
}
}

119
src/adapt.go Normal file
View File

@@ -0,0 +1,119 @@
package src
import (
"bufio"
"bytes"
"context"
"fmt"
"hash/crc32"
"io"
"log"
"net"
"strconv"
)
func adapt(ctx context.Context, config Config, conn net.Conn) error {
reader := bufio.NewReader(conn)
for ctx.Err() == nil {
if done, err := func() (bool, error) {
raw, message, err := readMessage(reader)
log.Printf("%q", raw)
if err != nil {
if err == io.EOF {
return true, nil
}
return true, err
}
if len(message) > 0 {
hashKey := message[max(0, len(message)-1)].(string)
hash := int(crc32.ChecksumIEEE([]byte(hashKey)))
forward := config.forwards[hash%len(config.forwards)]
forwardCon := forward.Get()
if forwardCon == nil {
return true, io.EOF
}
forwardConn := forwardCon.(net.Conn)
if _, err := forwardConn.Write(raw); err != nil {
return true, err
}
replyer := bufio.NewReader(forwardConn)
raw, _, err := readMessage(replyer)
if err != nil {
return true, err
}
log.Printf("%q", raw)
if _, err := conn.Write(raw); err != nil {
return true, err
}
}
return false, nil
}(); err != nil {
return err
} else if done {
return nil
}
}
return io.EOF
}
func readMessage(reader *bufio.Reader) ([]byte, []any, error) {
w := bytes.NewBuffer(nil)
firstLine, _, err := reader.ReadLine()
w.Write(firstLine)
if err != nil {
return w.Bytes(), nil, err
}
firstLine = bytes.TrimSuffix(firstLine, []byte("\r\n"))
if len(firstLine) == 0 {
return w.Bytes(), nil, nil
}
switch firstLine[0] {
case '+': // simple string, like +OK
return w.Bytes(), []any{string(firstLine[1:])}, nil
case '-': // simple error, like -message
return w.Bytes(), nil, fmt.Errorf("error: %s", firstLine[1:])
case ':': // number, like /[+-][0-9]+/
firstLine = bytes.TrimPrefix(firstLine[1:], []byte("+"))
n, err := strconv.Atoi(string(firstLine[1:]))
return w.Bytes(), []any{n}, err
case '$': // long string, like $-1 for nil, like $LEN\r\nSTRING\r\n
if firstLine[1] == '-' {
return w.Bytes(), []any{nil}, nil
}
nextLine, _, err := reader.ReadLine()
w.Write(nextLine)
nextLine = bytes.TrimSuffix(nextLine, []byte("\r\n"))
return w.Bytes(), []any{string(nextLine)}, err
case '*': // array, like *-1 for nil, like *4 for [1,2,3,4]
n, err := strconv.Atoi(string(firstLine[1:]))
if err != nil {
return w.Bytes(), nil, err
} else if n == -1 {
return w.Bytes(), nil, nil
}
var result []any
for i := 0; i < n; i++ {
moreBytes, more, err := readMessage(reader)
w.Write(moreBytes)
if err != nil {
return w.Bytes(), nil, err
}
result = append(result, more...)
}
return w.Bytes(), result, nil
case '_': // nil
return w.Bytes(), nil, nil
case '#': // boolean, like #t or #f
return w.Bytes(), []any{firstLine[1] == 't'}, nil
case ',': // double
log.Fatal("not impl")
}
log.Fatalf("not impl: %q", firstLine)
return w.Bytes(), nil, nil
}

73
src/config.go Normal file
View File

@@ -0,0 +1,73 @@
package src
import (
"context"
"encoding/json"
"fmt"
"net"
"os"
"slices"
"strings"
"sync"
)
type Config struct {
Listen string `json:"LISTEN"`
Forwards string `json:"FORWARDS"`
forwards []*sync.Pool
}
func NewConfig(ctx context.Context) (Config, error) {
config := Config{
Listen: ":10000",
Forwards: "",
}
b, _ := json.Marshal(config)
var m map[string]any
if err := json.Unmarshal(b, &m); err != nil {
return config, err
}
for k := range m {
if v := os.Getenv(k); v != "" {
m[k] = v
}
}
b2, _ := json.Marshal(m)
if err := json.Unmarshal(b2, &config); err != nil {
return config, err
}
forwards := strings.Split(config.Forwards, ",")
forwards = slices.DeleteFunc(forwards, func(s string) bool { return s == "" })
if len(forwards) == 0 {
return config, fmt.Errorf("at least one $FORWARD required")
}
config.forwards = make([]*sync.Pool, len(forwards))
for i := range forwards {
config.forwards[i] = &sync.Pool{
New: func() any {
v, err := (&net.Dialer{}).DialContext(ctx, "tcp", forwards[i])
if err != nil {
return nil
}
return v
},
}
}
return config, nil
}
func (c Config) Close() {
for i := range c.forwards {
if c.forwards[i] != nil {
c.forwards[i].New = nil
for {
got := c.forwards[i].Get()
if got != nil {
got.(net.Conn).Close()
}
}
}
}
}

53
src/listen.go Normal file
View File

@@ -0,0 +1,53 @@
package src
import (
"context"
"log"
"net"
"sync"
)
func listen(ctx context.Context, config Config) error {
wg := &sync.WaitGroup{}
defer wg.Wait()
listener, err := net.Listen("tcp", config.Listen)
if err != nil {
return err
}
defer listener.Close()
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
listener.Close()
}()
for ctx.Err() == nil {
conn, err := listener.Accept()
if err != nil {
log.Println("error accepting:", err)
} else {
wg.Add(1)
go func() {
defer wg.Done()
handle(ctx, config, conn)
}()
}
}
return ctx.Err()
}
func handle(ctx context.Context, config Config, conn net.Conn) {
if err := _handle(ctx, config, conn); err != nil {
log.Println("error handling:", err)
}
}
func _handle(ctx context.Context, config Config, conn net.Conn) error {
defer conn.Close()
return adapt(ctx, config, conn)
}

19
src/main.go Normal file
View File

@@ -0,0 +1,19 @@
package src
import (
"context"
)
func Main(ctx context.Context) error {
config, err := NewConfig(ctx)
if err != nil {
return err
}
defer config.Close()
if err := listen(ctx, config); err != nil && ctx.Err() == nil {
return err
}
return nil
}