From d961fb6aeaa62056fccf145f4159c8eec37d3f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renaud=20Gue=CC=81rin?= Date: Wed, 16 Oct 2024 22:26:47 +0100 Subject: [PATCH 1/2] Include user/password in share URL --- share/afp.go | 2 +- share/ftp.go | 2 +- share/smb.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/share/afp.go b/share/afp.go index 33bab32..4f24e3b 100644 --- a/share/afp.go +++ b/share/afp.go @@ -53,5 +53,5 @@ func (b *AFPBackend) Apply(sharePWD string, vc *VMShareContext) (string, error) return "", errors.Wrap(err, "start afp server") } - return "afp://" + net.JoinHostPort(b.listenIP.String(), fmt.Sprint(b.sharePort)) + "/linsk", nil + return "afp://linsk:" + sharePWD + "@" + net.JoinHostPort(b.listenIP.String(), fmt.Sprint(b.sharePort)) + "/linsk", nil } diff --git a/share/ftp.go b/share/ftp.go index adfae9e..1be88de 100644 --- a/share/ftp.go +++ b/share/ftp.go @@ -73,5 +73,5 @@ func (b *FTPBackend) Apply(sharePWD string, vc *VMShareContext) (string, error) return "", errors.Wrap(err, "start ftp server") } - return "ftp://" + b.extIP.String() + ":" + fmt.Sprint(b.sharePort), nil + return "ftp://linsk:" + sharePWD + "@" + b.extIP.String() + ":" + fmt.Sprint(b.sharePort), nil } diff --git a/share/smb.go b/share/smb.go index d54ab63..3ef406b 100644 --- a/share/smb.go +++ b/share/smb.go @@ -77,12 +77,12 @@ func (b *SMBBackend) Apply(sharePWD string, vc *VMShareContext) (string, error) var shareURL string switch { case b.sharePort != nil: - shareURL = "smb://" + net.JoinHostPort(b.listenIP.String(), fmt.Sprint(*b.sharePort)) + "/linsk" + shareURL = "smb://linsk:" + sharePWD + "@" + net.JoinHostPort(b.listenIP.String(), fmt.Sprint(*b.sharePort)) + "/linsk" case vc.NetTapCtx != nil: if osspecifics.IsWindows() { shareURL = `\\` + strings.ReplaceAll(vc.NetTapCtx.Net.GuestIP.String(), ":", "-") + ".ipv6-literal.net" + `\linsk` } else { - shareURL = "smb://" + net.JoinHostPort(vc.NetTapCtx.Net.GuestIP.String(), fmt.Sprint(smbPort)) + "/linsk" + shareURL = "smb://linsk:" + sharePWD + "@" + net.JoinHostPort(vc.NetTapCtx.Net.GuestIP.String(), fmt.Sprint(smbPort)) + "/linsk" } default: return "", fmt.Errorf("no port forwarding and net tap configured") From 4ac6a79b1ab9c54f9bb9170c3d7dbb13510ef5c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renaud=20Gue=CC=81rin?= Date: Sat, 2 Nov 2024 01:36:37 +0000 Subject: [PATCH 2/2] Restore original shareURL and add fullURL --- cmd/run.go | 16 ++++++++++++++-- share/afp.go | 9 ++++++--- share/backend.go | 2 +- share/ftp.go | 11 +++++++---- share/smb.go | 21 +++++++++++++-------- 5 files changed, 41 insertions(+), 18 deletions(-) diff --git a/cmd/run.go b/cmd/run.go index aa7ec62..17a4aa3 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -101,7 +101,7 @@ var runCmd = &cobra.Command{ lg := slog.With("backend", shareBackendFlag) - shareURI, err := backend.Apply(sharePWD, &share.VMShareContext{ + shareURL, fullURL, err := backend.Apply(sharePWD, &share.VMShareContext{ Instance: i, FileManager: fm, NetTapCtx: tapCtx, @@ -113,7 +113,19 @@ var runCmd = &cobra.Command{ lg.Info("Started the network share successfully") - fmt.Fprintf(os.Stderr, "===========================\n[Network File Share Config]\nThe network file share was started. Please use the credentials below to connect to the file server.\n\nType: "+strings.ToUpper(shareBackendFlag)+"\nURL: %v\nUsername: linsk\nPassword: %v\n===========================\n", shareURI, sharePWD) + fmt.Fprintf(os.Stderr, "===========================\n"+ + "[Network File Share Config]\n"+ + "The network file share was started. Please use the credentials below to connect to the file server.\n\n"+ + "Type: %v\n"+ + "URL: %v\n"+ + "Username: linsk\n"+ + "Password: %v\n", strings.ToUpper(shareBackendFlag), shareURL, sharePWD) + + if fullURL != "" { + fmt.Fprintf(os.Stderr, "Full URL: %v\n", fullURL) + } + + fmt.Fprintf(os.Stderr, "===========================\n") ctxWait := true diff --git a/share/afp.go b/share/afp.go index 4f24e3b..ac0d8ca 100644 --- a/share/afp.go +++ b/share/afp.go @@ -47,11 +47,14 @@ func NewAFPBackend(uc *UserConfiguration) (Backend, *VMShareOptions, error) { }, nil } -func (b *AFPBackend) Apply(sharePWD string, vc *VMShareContext) (string, error) { +func (b *AFPBackend) Apply(sharePWD string, vc *VMShareContext) (string, string, error) { err := vc.FileManager.StartAFP(sharePWD) if err != nil { - return "", errors.Wrap(err, "start afp server") + return "", "", errors.Wrap(err, "start afp server") } - return "afp://linsk:" + sharePWD + "@" + net.JoinHostPort(b.listenIP.String(), fmt.Sprint(b.sharePort)) + "/linsk", nil + shareURL := "afp://" + net.JoinHostPort(b.listenIP.String(), fmt.Sprint(b.sharePort)) + "/linsk" + fullURL := "afp://linsk:" + sharePWD + "@" + net.JoinHostPort(b.listenIP.String(), fmt.Sprint(b.sharePort)) + "/linsk" + + return shareURL, fullURL, nil } diff --git a/share/backend.go b/share/backend.go index 056f5fd..a724f1b 100644 --- a/share/backend.go +++ b/share/backend.go @@ -19,7 +19,7 @@ package share type NewBackendFunc func(uc *UserConfiguration) (Backend, *VMShareOptions, error) type Backend interface { - Apply(sharePWD string, vc *VMShareContext) (string, error) + Apply(sharePWD string, vc *VMShareContext) (shareURL string, fullURL string, err error) } var backends = map[string]NewBackendFunc{ diff --git a/share/ftp.go b/share/ftp.go index 1be88de..c490919 100644 --- a/share/ftp.go +++ b/share/ftp.go @@ -63,15 +63,18 @@ func NewFTPBackend(uc *UserConfiguration) (Backend, *VMShareOptions, error) { }, nil } -func (b *FTPBackend) Apply(sharePWD string, vc *VMShareContext) (string, error) { +func (b *FTPBackend) Apply(sharePWD string, vc *VMShareContext) (string, string, error) { if vc.NetTapCtx != nil { - return "", fmt.Errorf("net taps are unsupported in ftp") + return "", "", fmt.Errorf("net taps are unsupported in ftp") } err := vc.FileManager.StartFTP(sharePWD, b.sharePort+1, b.passivePortCount, b.extIP) if err != nil { - return "", errors.Wrap(err, "start ftp server") + return "", "", errors.Wrap(err, "start ftp server") } - return "ftp://linsk:" + sharePWD + "@" + b.extIP.String() + ":" + fmt.Sprint(b.sharePort), nil + shareURL := "ftp://" + net.JoinHostPort(b.extIP.String(), fmt.Sprint(b.sharePort)) + fullURL := "ftp://linsk:" + sharePWD + "@" + net.JoinHostPort(b.extIP.String(), fmt.Sprint(b.sharePort)) + + return shareURL, fullURL, nil } diff --git a/share/smb.go b/share/smb.go index 3ef406b..e890ced 100644 --- a/share/smb.go +++ b/share/smb.go @@ -60,33 +60,38 @@ func NewSMBBackend(uc *UserConfiguration) (Backend, *VMShareOptions, error) { }, nil } -func (b *SMBBackend) Apply(sharePWD string, vc *VMShareContext) (string, error) { +func (b *SMBBackend) Apply(sharePWD string, vc *VMShareContext) (string, string, error) { if b.sharePort != nil && vc.NetTapCtx != nil { - return "", fmt.Errorf("conflict: configured to use a forwarded port but a net tap configuration was detected") + return "", "", fmt.Errorf("conflict: configured to use a forwarded port but a net tap configuration was detected") } if b.sharePort == nil && vc.NetTapCtx == nil { - return "", fmt.Errorf("no net tap configuration found") + return "", "", fmt.Errorf("no net tap configuration found") } err := vc.FileManager.StartSMB(sharePWD) if err != nil { - return "", errors.Wrap(err, "start smb server") + return "", "", errors.Wrap(err, "start smb server") } var shareURL string switch { case b.sharePort != nil: - shareURL = "smb://linsk:" + sharePWD + "@" + net.JoinHostPort(b.listenIP.String(), fmt.Sprint(*b.sharePort)) + "/linsk" + shareURL = "smb://" + net.JoinHostPort(b.listenIP.String(), fmt.Sprint(*b.sharePort)) + "/linsk" case vc.NetTapCtx != nil: if osspecifics.IsWindows() { shareURL = `\\` + strings.ReplaceAll(vc.NetTapCtx.Net.GuestIP.String(), ":", "-") + ".ipv6-literal.net" + `\linsk` } else { - shareURL = "smb://linsk:" + sharePWD + "@" + net.JoinHostPort(vc.NetTapCtx.Net.GuestIP.String(), fmt.Sprint(smbPort)) + "/linsk" + shareURL = "smb://" + net.JoinHostPort(vc.NetTapCtx.Net.GuestIP.String(), fmt.Sprint(smbPort)) + "/linsk" } default: - return "", fmt.Errorf("no port forwarding and net tap configured") + return "", "", fmt.Errorf("no port forwarding and net tap configured") } - return shareURL, nil + if osspecifics.IsWindows() { + return shareURL, "", nil // No full URL on Windows + } + + fullURL := strings.Replace(shareURL, "smb://", "smb://linsk:"+sharePWD+"@", 1) + return shareURL, fullURL, nil }