@@ -2,6 +2,7 @@ package dbconnect
22
33import (
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().
1617func 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}
0 commit comments