From 2e3510aec7aa7bd811d319a097c6dea2203cba7f Mon Sep 17 00:00:00 2001 From: cadl Date: Mon, 16 Oct 2023 18:07:42 +0800 Subject: [PATCH 1/3] feat: support remote backend server --- proxy.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/proxy.go b/proxy.go index f33b372..1858742 100644 --- a/proxy.go +++ b/proxy.go @@ -18,14 +18,15 @@ func main() { } func main2() error { - if len(os.Args) != 5 { - return fmt.Errorf("Expected 4 args: usage: proxy ") + if len(os.Args) != 6 { + return fmt.Errorf("Expected 5 args: usage: proxy ") } 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 { @@ -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, } From 7cc2cfad1c6075392ee96095fe1e864d8401b2dd Mon Sep 17 00:00:00 2001 From: cadl Date: Mon, 16 Oct 2023 18:54:31 +0800 Subject: [PATCH 2/3] chore: dockerize --- Dockerfile | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..987c67a --- /dev/null +++ b/Dockerfile @@ -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"] From bfe96e1d7b520ef0c73778707ac79b3484720f4f Mon Sep 17 00:00:00 2001 From: cadl Date: Mon, 16 Oct 2023 18:58:07 +0800 Subject: [PATCH 3/3] doc: update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 878dc74..a1d2bfc 100644 --- a/README.md +++ b/README.md @@ -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