Skip to content

Commit 0e63bba

Browse files
committed
feat: change name to dbproxy, keep listening for requests on connect, force manually set port
1 parent d306435 commit 0e63bba

12 files changed

Lines changed: 73 additions & 54 deletions

File tree

cmd/dbproxy/cmd_test.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

cmd/dbproxy/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
"github.com/icewolf/cloudflare-db-proxy/dbconnect"
7+
"github.com/urfave/cli/v2"
8+
)
9+
10+
func main() {
11+
app := &cli.App{}
12+
app.Name = "dbproxy"
13+
app.Usage = "Standalone implementation of Cloudflare's db-connect"
14+
app.UsageText = "dbproxy [global options] [command] [command options]"
15+
app.Commands = commands()
16+
17+
_ = app.Run(os.Args)
18+
}
19+
20+
func commands() []*cli.Command {
21+
cmds := []*cli.Command{dbconnect.Cmd()}
22+
return cmds
23+
}
Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dbconnect
22

33
import (
44
"context"
5+
"fmt"
56
"log"
67
"net"
78
"strconv"
@@ -15,28 +16,20 @@ import (
1516
// The tunnel package is responsible for appending this to tunnel.Commands().
1617
func Cmd() *cli.Command {
1718
return &cli.Command{
18-
Category: "Database Connect (ALPHA) - Deprecated",
19-
Name: "db-connect",
20-
Usage: "deprecated: Access your SQL database from Cloudflare Workers or the browser",
19+
Category: "Database Proxy (db-connect)",
20+
Name: "start",
21+
Usage: "Access your SQL database from Cloudflare Workers or the browser",
2122
ArgsUsage: " ",
2223
Description: `
23-
This feature has been deprecated.
24-
Please see:
25-
26-
cloudflared access tcp --help
27-
28-
for setting up database connections to the cloudflare edge.
29-
30-
3124
Creates a connection between your database and the Cloudflare edge.
3225
Now you can execute SQL commands anywhere you can send HTTPS requests.
3326
3427
Connect your database with any of the following commands, you can also try the "playground" without a database:
3528
36-
cloudflared db-connect --url postgres://user:pass@localhost?sslmode=disable \
29+
dbproxy start --url postgres://user:pass@localhost?sslmode=disable \
3730
--auth-domain mysite.cloudflareaccess.com --application-aud my-access-policy-tag
38-
cloudflared db-connect --url mysql://localhost --insecure
39-
cloudflared db-connect --playground
31+
dbproxy start --url mysql://localhost --insecure
32+
dbproxy start --playground
4033
4134
Requests should be authenticated using Cloudflare Access, learn more about how to enable it here:
4235
@@ -48,10 +41,10 @@ func Cmd() *cli.Command {
4841
Usage: "URL to the database (eg. postgres://user:pass@localhost?sslmode=disable)",
4942
EnvVars: []string{"TUNNEL_URL"},
5043
}),
51-
altsrc.NewStringFlag(&cli.StringFlag{
52-
Name: "hostname",
53-
Usage: "Hostname to accept commands over HTTPS (eg. sql.mysite.com)",
54-
EnvVars: []string{"TUNNEL_HOSTNAME"},
44+
altsrc.NewIntFlag(&cli.IntFlag{
45+
Name: "port",
46+
Usage: "Port on which to expose proxy",
47+
EnvVars: []string{"TUNNEL_PORT"},
5548
}),
5649
altsrc.NewStringFlag(&cli.StringFlag{
5750
Name: "auth-domain",
@@ -119,6 +112,10 @@ func CmdBefore(c *cli.Context) error {
119112
return nil
120113
}
121114

115+
if !c.IsSet("port") {
116+
log.Fatal("must specify --port, so tunnel can reliably connect to proxy on every run")
117+
}
118+
122119
// Ensure that secure configurations specify a hostname, domain, and tag for JWT validation.
123120
if !c.IsSet("auth-domain") || !c.IsSet("application-aud") {
124121
log.Fatal("must specify --auth-domain and --application-aud unless you want to run in --insecure mode")
@@ -151,15 +148,14 @@ func CmdAction(c *cli.Context) error {
151148
// Since the Proxy should only talk to the tunnel daemon, find the next available
152149
// localhost port and start to listen to requests.
153150
go func() {
154-
err := proxy.Start(ctx, "127.0.0.1:", listenerC)
151+
addr := fmt.Sprintf("127.0.0.1:%d", c.Int("port"))
152+
err := proxy.Start(ctx, addr, listenerC)
155153
if err != nil {
156154
log.Fatal(err)
157155
}
158156
}()
159157

160-
// Block until the the Proxy is online, retreive its address, and change the url to point to it.
161-
// This is effectively "handing over" control to the tunnel package so it can run the tunnel daemon.
162-
c.Set("url", "https://"+(<-listenerC).Addr().String())
163-
164-
return nil
158+
for {
159+
log.Printf("Connected. Listening for requests at %s.", (<-listenerC).Addr().String())
160+
}
165161
}

dbconnect/cmd_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dbconnect
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/urfave/cli/v2"
9+
)
10+
11+
func TestCmd(t *testing.T) {
12+
tests := [][]string{
13+
{"dbproxy", "start", "--port", "1234", "--playground"},
14+
{"dbproxy", "start", "--port", "1234", "--playground", "--hostname", "sql.mysite.com"},
15+
{"dbproxy", "start", "--port", "1234", "--url", "sqlite3::memory:?cache=shared", "--insecure"},
16+
{"dbproxy", "start", "--port", "1234", "--url", "sqlite3::memory:?cache=shared", "--hostname", "sql.mysite.com", "--auth-domain", "mysite.cloudflareaccess.com", "--application-aud", "aud"},
17+
}
18+
19+
app := &cli.App{
20+
Name: "dbproxy",
21+
Commands: []*cli.Command{Cmd()},
22+
}
23+
24+
for _, test := range tests {
25+
assert.NoError(t, app.Run(test))
26+
}
27+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)