8000 chore: switch to new wgtunnel via tunnelsdk by deansheather · Pull Request #6489 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

chore: switch to new wgtunnel via tunnelsdk #6489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 22, 2023
Merged
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
8000
54 changes: 14 additions & 40 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import (
"github.com/coder/coder/provisionersdk"
sdkproto "github.com/coder/coder/provisionersdk/proto"
"github.com/coder/coder/tailnet"
"github.com/coder/wgtunnel/tunnelsdk"
)

// ReadGitAuthProvidersFromEnv is provided for compatibility purposes with the
Expand Down Expand Up @@ -538,34 +539,25 @@ flags, and YAML configuration. The precedence is as follows:
return xerrors.Errorf("configure http client: %w", err)
}

var (
ctxTunnel, closeTunnel = context.WithCancel(ctx)
tunnel *devtunnel.Tunnel
tunnelErr <-chan error
)
defer closeTunnel()

// If the access URL is empty, we attempt to run a reverse-proxy
// tunnel to make the initial setup really simple.
var (
tunnel *tunnelsdk.Tunnel
tunnelDone <-chan struct{} = make(chan struct{}, 1)
)
if cfg.AccessURL.String() == "" {
cmd.Printf("Opening tunnel so workspaces can connect to your deployment. For production scenarios, specify an external access URL\n")
tunnel, tunnelErr, err = devtunnel.New(ctxTunnel, logger.Named("devtunnel"))
tunnel, err = devtunnel.New(ctx, logger.Named("devtunnel"), cfg.WgtunnelHost.String())
if err != nil {
return xerrors.Errorf("create tunnel: %w", err)
}
err = cfg.AccessURL.Set(tunnel.URL)
if err != nil {
return xerrors.Errorf("set access url: %w", err)
}
defer tunnel.Close()
tunnelDone = tunnel.Wait()
cfg.AccessURL = clibase.URL(*tunnel.URL)

if cfg.WildcardAccessURL.String() == "" {
u, err := parseURL(tunnel.URL)
if err != nil {
return xerrors.Errorf("parse tunnel url: %w", err)
}

// Suffixed wildcard access URL.
u, err = url.Parse(fmt.Sprintf("*--%s", u.Hostname()))
u, err := url.Parse(fmt.Sprintf("*--%s", tunnel.URL.Hostname()))
if err != nil {
return xerrors.Errorf("parse wildcard url: %w", err)
}
Expand Down Expand Up @@ -1089,10 +1081,8 @@ flags, and YAML configuration. The precedence is as follows:
_, _ = fmt.Fprintln(cmd.OutOrStdout(), cliui.Styles.Bold.Render(
"Interrupt caught, gracefully exiting. Use ctrl+\\ to force quit",
))
case exitErr = <-tunnelErr:
if exitErr == nil {
exitErr = xerrors.New("dev tunnel closed unexpectedly")
}
case <-tunnelDone:
exitErr = xerrors.New("dev tunnel closed unexpectedly")
case exitErr = <-errCh:
}
if exitErr != nil && !xerrors.Is(exitErr, context.Canceled) {
Expand Down Expand Up @@ -1161,8 +1151,8 @@ flags, and YAML configuration. The precedence is as follows:
// Close tunnel after we no longer have in-flight connections.
if tunnel != nil {
cmd.Println("Waiting for tunnel to close...")
closeTunnel()
<-tunnelErr
_ = tunnel.Close()
<-tunnel.Wait()
cmd.Println("Done waiting for tunnel")
}

Expand Down Expand Up @@ -1240,22 +1230,6 @@ flags, and YAML configuration. The precedence is as follows:
return root
}

// parseURL parses a string into a URL.
func parseURL(u string) (*url.URL, error) {
hasScheme := strings.HasPrefix(u, "http:") || strings.HasPrefix(u, "https:")

if !hasScheme {
return nil, xerrors.Errorf("URL %q must have a scheme of either http or https", u)
}

parsed, err := url.Parse(u)
if err != nil {
return nil, err
}

return parsed, nil
}

// isLocalURL returns true if the hostname of the provided URL appears 8000 to
// resolve to a loopback address.
func isLocalURL(ctx context.Context, u *url.URL) (bool, error) {
Expand Down
3 changes: 3 additions & 0 deletions coderd/apidoc/docs.go

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

3 changes: 3 additions & 0 deletions coderd/apidoc/swagger.json

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

50 changes: 37 additions & 13 deletions coderd/devtunnel/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/go-ping/ping"
"golang.org/x/exp/slices"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"

"github.com/coder/coder/cryptorand"
)
Expand All @@ -19,13 +20,11 @@ type Region struct {
}

type Node struct {
ID int `json:"id"`
RegionID int `json:"region_id"`
HostnameHTTPS string `json:"hostname_https"`
HostnameWireguard string `json:"hostname_wireguard"`
WireguardPort uint16 `json:"wireguard_port"`
ID int `json:"id"`
RegionID int `json:"region_id"`
HostnameHTTPS string `json:"hostname_https"`

AvgLatency time.Duration `json:"avg_latency"`
AvgLatency time.Duration `json:"-"`
}

var Regions = []Region{
Expand All @@ -34,28 +33,53 @@ var Regions = []Region{
LocationName: "US East Pittsburgh",
Nodes: []Node{
{
ID: 1,
RegionID: 0,
HostnameHTTPS: "pit-1.try.coder.app",
HostnameWireguard: "pit-1.try.coder.app",
WireguardPort: 55551,
ID: 1,
RegionID: 0,
HostnameHTTPS: "pit-1.try.coder.app",
},
},
},
}

func FindClosestNode() (Node, error) {
// Nodes returns a list of nodes to use for the tunnel. It will pick a random
// node from each region.
//
// If a customNode is provided, it will be returned as the only node with ID
// 9999.
func Nodes(customTunnelHost string) ([]Node, error) {
nodes := []Node{}

if customTunnelHost != "" {
return []Node{
{
ID: 9999,
RegionID: 9999,
HostnameHTTPS: customTunnelHost,
},
}, nil
}

for _, region := range Regions {
// Pick a random node from each region.
i, err := cryptorand.Intn(len(region.Nodes))
if err != nil {
return Node{}, err
return []Node{}, err
}
nodes = append(nodes, region.Nodes[i])
}

return nodes, nil
}

// FindClosestNode pings each node and returns the one with the lowest latency.
func FindClosestNode(nodes []Node) (Node, error) {
if len(nodes) == 0 {
return Node{}, xerrors.New("no wgtunnel nodes")
}

// Copy the nodes so we don't mutate the original.
nodes = append([]Node{}, nodes...)

var (
nodesMu sync.Mutex
eg = errgroup.Group{}
Expand Down
Loading
0