Skip to content
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

feat(remote access): Auto detect URL scheme when opening url based on name #423

Merged
merged 1 commit into from
Jan 22, 2025
Merged
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
30 changes: 28 additions & 2 deletions pkg/cmd/remoteaccess/server/server.manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type CmdServer struct {
listen string
configuration string
open bool
browserScheme string

*subcommand.SubCommand

Expand Down Expand Up @@ -57,6 +58,11 @@ func NewCmdServer(f *cmdutil.Factory) *CmdServer {
UserKnownHostsFile /dev/null
ProxyCommand c8y remoteaccess server --device %n --listen -
---

Note: When using the "--browser" flag, by default the URL scheme (e.g. http, https) will be
auto detected based on the Remote Access configuration's name. For example, if the configuration
name has the "https:" prefix, then http will be used, otherwise http will be used. This aligns
with the naming convention used in https://github.com/Cumulocity-IoT/cumulocity-remote-access-cloud-http-proxy
`),
Example: heredoc.Doc(`
$ c8y remoteaccess server --device 12345
Expand All @@ -73,6 +79,9 @@ func NewCmdServer(f *cmdutil.Factory) *CmdServer {

$ c8y remoteaccess server --device 12345 --configuration "*rugpi*" --browser
Start a local proxy and match on the configuration using wildcards, then open the browser to the endpoint

$ c8y remoteaccess server --device 12345 --configuration "*rugpi*" --browser --scheme https
Start a local proxy and match on the configuration using wildcards, then open the browser to the endpoint and force usage of https
`),
RunE: ccmd.RunE,
}
Expand All @@ -82,9 +91,11 @@ func NewCmdServer(f *cmdutil.Factory) *CmdServer {
cmd.Flags().StringVar(&ccmd.listen, "listen", "127.0.0.1:0", "Listen. unix:///run/example.sock")
cmd.Flags().StringVar(&ccmd.configuration, "configuration", "", "Remote Access Configuration. Accepts wildcards")
cmd.Flags().BoolVar(&ccmd.open, "browser", false, "Open the endpoint in a browser (if available)")
cmd.Flags().StringVar(&ccmd.browserScheme, "scheme", "auto", "URL scheme to use when opening the address in a browser, e.g. http, https or auto")

completion.WithOptions(
cmd,
completion.WithValidateSet("scheme", "http", "https", "auto"),
completion.WithDevice("device", func() (*c8y.Client, error) { return ccmd.factory.Client() }),
completion.WithRemoteAccessPassthroughConfiguration("configuration", "device", func() (*c8y.Client, error) { return ccmd.factory.Client() }),
)
Expand Down Expand Up @@ -185,13 +196,27 @@ func (n *CmdServer) RunE(cmd *cobra.Command, args []string) error {
host = "127.0.0.1"
}

// Set the scheme to be used when opening a browser
switch n.browserScheme {
case "auto":
// Align to convention used by
// https://github.com/Cumulocity-IoT/cumulocity-remote-access-cloud-http-proxy
n.browserScheme = "http"
if strings.HasPrefix(craConfig.Name, "https:") {
n.browserScheme = "https"
} else if strings.HasPrefix(craConfig.Name, "http:") {
n.browserScheme = "http"
}
}

type ServerInfo struct {
Port string
Host string
Device string
LocalAddress string
User string
CumulocityURL string
Scheme string
}

messageTmpl := heredoc.Doc(`
Expand All @@ -203,7 +228,7 @@ func (n *CmdServer) RunE(cmd *cobra.Command, args []string) error {

SSH: ssh -p {{.Port}} {{.User}}@{{.Host}}

Website: http://{{.LocalAddress}}
Website: {{.Scheme}}://{{.LocalAddress}}

Press ctrl-c to shutdown the server
`)
Expand All @@ -216,11 +241,12 @@ func (n *CmdServer) RunE(cmd *cobra.Command, args []string) error {
LocalAddress: localAddress,
Device: device,
User: "<device_username>",
Scheme: n.browserScheme,
})

if n.open {
go func() {
targetURL := fmt.Sprintf("http://%s:%s", host, port)
targetURL := fmt.Sprintf("%s://%s:%s", n.browserScheme, host, port)
if err := n.factory.Browser.Browse(targetURL); err != nil {
cfg.Logger.Warnf("%s", err)
}
Expand Down
Loading