Skip to content

Commit

Permalink
proxy: pick up a random port and bind to the correct port
Browse files Browse the repository at this point in the history
This PR changes the address to bind locally. Instead of binding to an
hardcoded port, we now let the OS pick up a random port for us. In order
to make this work, we had to introduce a new method to the `sql-proxy`
repo (planetscale/sql-proxy#52).

Both `pscale shell` and `pscale connect` now automatically binds to a
random port.

closes planetscale/surfaces#111
  • Loading branch information
asbiaidw5 authored and fatih committed Mar 2, 2021
1 parent 5c621ec commit bb938d7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 18 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/pkg/browser v0.0.0-20201112035734-206646e67786
github.com/pkg/errors v0.9.1
github.com/planetscale/planetscale-go v0.9.0
github.com/planetscale/sql-proxy v0.1.3
github.com/planetscale/sql-proxy v0.1.4
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ github.com/planetscale/planetscale-go v0.9.0 h1:EY2BQNyBAWx8rjk5ecqcmulKyT8zheWa
github.com/planetscale/planetscale-go v0.9.0/go.mod h1:R+07GStW2oGSfeTwWoplnimSEFzxIzEofOHgPFxArg0=
github.com/planetscale/sql-proxy v0.1.3 h1:u0HA78rkasas2JOHEyA21LAtIx+HVU6W7yPeS1XJRow=
github.com/planetscale/sql-proxy v0.1.3/go.mod h1:Z1F1p5YmoBV0mkbfApaFA0zYv0Ngxm4GT4VnvtCgoro=
github.com/planetscale/sql-proxy v0.1.4 h1:RBTxrEXiF08W8GHTuHQ1Hn9aM79ibLSFZ3yX/dwsF/c=
github.com/planetscale/sql-proxy v0.1.4/go.mod h1:lTxdxxilj2oOgVsZ5c1Eyjo/3XTEv0GZ8E2A33y6We8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
Expand Down
31 changes: 24 additions & 7 deletions internal/cmd/connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ argument:
}
}

const localProxyAddr = "127.0.0.1"
localAddr := localProxyAddr + ":0"
if flags.localAddr != "" {
localAddr = flags.localAddr
}

proxyOpts := proxy.Options{
CertSource: proxyutil.NewRemoteCertSource(client),
LocalAddr: flags.localAddr,
LocalAddr: localAddr,
RemoteAddr: flags.remoteAddr,
Instance: fmt.Sprintf("%s/%s/%s", cfg.Organization, database, branch),
}
Expand All @@ -74,16 +80,27 @@ argument:
proxyOpts.Logger = zap.NewNop()
}

fmt.Printf("Secure connection to databases %s and branch %s is established!.\n\nLocal address to connect your application: %s (press ctrl-c to quit)",
cmdutil.BoldBlue(database),
cmdutil.BoldBlue(branch),
cmdutil.BoldBlue(flags.localAddr))

p, err := proxy.NewClient(proxyOpts)
if err != nil {
return fmt.Errorf("couldn't create proxy client: %s", err)
}

go func() {
// this is blocking and will only return once p.Run() below is
// invoked
addr, err := p.LocalAddr()
if err != nil {
fmt.Printf("failed getting local addr: %s\n", err)
return
}

fmt.Printf("Secure connection to databases %s and branch %s is established!.\n\nLocal address to connect your application: %s (press ctrl-c to quit)",
cmdutil.BoldBlue(database),
cmdutil.BoldBlue(branch),
cmdutil.BoldBlue(addr.String()),
)
}()

// TODO(fatih): replace with signal.NotifyContext once Go 1.16 is released
// https://go-review.googlesource.com/c/go/+/219640
ctx = sigutil.WithSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
Expand All @@ -92,7 +109,7 @@ argument:
}

cmd.PersistentFlags().StringVar(&cfg.Organization, "org", cfg.Organization, "The organization for the current user")
cmd.PersistentFlags().StringVar(&flags.localAddr, "local-addr", "127.0.0.1:3307",
cmd.PersistentFlags().StringVar(&flags.localAddr, "local-addr", "",
"Local address to bind and listen for connections")
cmd.PersistentFlags().StringVar(&flags.remoteAddr, "remote-addr", "",
"PlanetScale Database remote network address. By default the remote address is populated automatically from the PlanetScale API.")
Expand Down
30 changes: 21 additions & 9 deletions internal/cmd/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"syscall"
Expand All @@ -21,11 +22,6 @@ import (
"go.uber.org/zap"
)

const (
localProxyPort = "3307"
localProxyAddr = "127.0.0.1"
)

func ShellCmd(cfg *config.Config) *cobra.Command {
var flags struct {
localAddr string
Expand Down Expand Up @@ -79,9 +75,15 @@ second argument:
}
}

const localProxyAddr = "127.0.0.1"
localAddr := localProxyAddr + ":0"
if flags.localAddr != "" {
localAddr = flags.localAddr
}

proxyOpts := proxy.Options{
CertSource: proxyutil.NewRemoteCertSource(client),
LocalAddr: flags.localAddr,
LocalAddr: localAddr,
RemoteAddr: flags.remoteAddr,
Instance: fmt.Sprintf("%s/%s/%s", cfg.Organization, database, branch),
}
Expand Down Expand Up @@ -123,10 +125,20 @@ second argument:
return err
}

addr, err := p.LocalAddr()
if err != nil {
return err
}

host, port, err := net.SplitHostPort(addr.String())
if err != nil {
return err
}

mysqlArgs := []string{
fmt.Sprintf("--defaults-extra-file=%s", tmpFile),
"-h", localProxyAddr,
"-P", localProxyPort,
"-h", host,
"-P", port,
}

m := &mysql{}
Expand All @@ -138,7 +150,7 @@ second argument:

cmd.PersistentFlags().StringVar(&cfg.Organization, "org", cfg.Organization, "The organization for the current user")
cmd.PersistentFlags().StringVar(&flags.localAddr, "local-addr",
localProxyAddr+":"+localProxyPort, "Local address to bind and listen for connections")
"", "Local address to bind and listen for connections. By default the proxy binds to 127.0.0.1 with a random port.")
cmd.PersistentFlags().StringVar(&flags.remoteAddr, "remote-addr", "",
"PlanetScale Database remote network address. By default the remote address is populated automatically from the PlanetScale API.")
cmd.PersistentFlags().BoolVar(&flags.debug, "debug", false, "enable debug mode")
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/planetscale/sql-proxy/proxy/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ github.com/pkg/errors
# github.com/planetscale/planetscale-go v0.9.0
## explicit
github.com/planetscale/planetscale-go/planetscale
# github.com/planetscale/sql-proxy v0.1.3
# github.com/planetscale/sql-proxy v0.1.4
## explicit
github.com/planetscale/sql-proxy/proxy
github.com/planetscale/sql-proxy/sigutil
Expand Down

0 comments on commit bb938d7

Please sign in to comment.