Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.20 as builder
WORKDIR /app
COPY . .

RUN CGO_ENABLED=0 go build ./proxy.go

FROM alpine:latest

COPY --from=builder /app/proxy /app/proxy

ENV BACKEND_PORT=3306
ENV BACKEND_HOST=localhost
ENV PROXY_PORT=3307

CMD ["sh", "-c", "/app/proxy ${PROXY_PORT} ${BACKEND_HOST} ${BACKEND_PORT} /etc/server-cert.pem /etc/server-key.pem"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ MySQL proxy that terminates TLS before proxying connection to MySQL server (with

```bash
$ go build proxy.go
$ ./proxy 3306 3307 ./server.crt ./server.key
$ ./proxy 3306 127.0.0.1 3307 ./server.crt ./server.key
```

## Protocol
Expand Down
13 changes: 7 additions & 6 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ func main() {
}

func main2() error {
if len(os.Args) != 5 {
return fmt.Errorf("Expected 4 args: usage: proxy <proxy-port> <server-port> <server-cert-path> <server-key-path>")
if len(os.Args) != 6 {
return fmt.Errorf("Expected 5 args: usage: proxy <proxy-port> <server-addr> <server-port> <server-cert-path> <server-key-path>")
}

port := os.Args[1]
targetPort := os.Args[2]
serverCertPath := os.Args[3]
serverKeyPath := os.Args[4]
targetAddr := os.Args[2]
targetPort := os.Args[3]
serverCertPath := os.Args[4]
serverKeyPath := os.Args[5]

serverCert, err := tls.LoadX509KeyPair(serverCertPath, serverKeyPath)
if err != nil {
Expand All @@ -37,7 +38,7 @@ func main2() error {

proxy := MysqlProxy{
listenAddr: fmt.Sprintf("0.0.0.0:%s", port),
serverAddr: fmt.Sprintf("localhost:%s", targetPort),
serverAddr: fmt.Sprintf("%s:%s", targetAddr, targetPort),
tlsConfig: tlsConfig,
debug: false,
}
Expand Down