Working SMB file share
This commit is contained in:
parent
7789923672
commit
76b20570ec
11 changed files with 187 additions and 7 deletions
|
|
@ -27,7 +27,7 @@ var lsCmd = &cobra.Command{
|
|||
|
||||
fmt.Print(string(lsblkOut))
|
||||
return 0
|
||||
}))
|
||||
}, nil))
|
||||
|
||||
return nil
|
||||
},
|
||||
|
|
|
|||
33
cmd/run.go
33
cmd/run.go
|
|
@ -4,9 +4,11 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"github.com/AlexSSD7/linsk/vm"
|
||||
"github.com/sethvargo/go-password/password"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
|
@ -19,6 +21,12 @@ var runCmd = &cobra.Command{
|
|||
vmMountDevName := args[1]
|
||||
fsType := args[2]
|
||||
|
||||
networkSharePort, err := getClosestAvailPort(9000)
|
||||
if err != nil {
|
||||
slog.Error("Failed to get closest available host port for network file share", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// TODO: `slog` library prints entire stack traces for errors which makes reading errors challenging.
|
||||
|
||||
os.Exit(runVM(args[0], func(ctx context.Context, i *vm.Instance, fm *vm.FileManager) int {
|
||||
|
|
@ -31,10 +39,31 @@ var runCmd = &cobra.Command{
|
|||
return 1
|
||||
}
|
||||
|
||||
fmt.Println("Mounted! Now sleeping")
|
||||
sharePWD, err := password.Generate(16, 10, 0, false, false)
|
||||
if err != nil {
|
||||
slog.Error("Failed to generate ephemeral password for network file share", "error", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
shareURI := "smb://linsk:" + sharePWD + "@127.0.0.1:" + fmt.Sprint(networkSharePort)
|
||||
|
||||
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: SMB\nServer Address: smb://127.0.0.1:%v\nUsername: linsk\nPassword: %v\n\nShare URI: %v\n================\n", networkSharePort, sharePWD, shareURI)
|
||||
|
||||
err = fm.StartSMB([]byte(sharePWD))
|
||||
if err != nil {
|
||||
slog.Error("Failed to start SMB server", "error", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
slog.Info("Started the network share successfully", "type", "smb")
|
||||
|
||||
<-ctx.Done()
|
||||
return 0
|
||||
}))
|
||||
}, []vm.PortForwardingConfig{{
|
||||
HostIP: net.ParseIP("127.0.0.1"), // TODO: Make this changeable.
|
||||
HostPort: networkSharePort,
|
||||
VMPort: 445,
|
||||
}}))
|
||||
|
||||
return nil
|
||||
},
|
||||
|
|
|
|||
|
|
@ -103,7 +103,7 @@ var shellCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
return 0
|
||||
}))
|
||||
}, nil))
|
||||
|
||||
return nil
|
||||
},
|
||||
|
|
|
|||
37
cmd/utils.go
37
cmd/utils.go
|
|
@ -2,6 +2,8 @@ package cmd
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"os/user"
|
||||
|
|
@ -36,7 +38,7 @@ func doRootCheck() {
|
|||
}
|
||||
}
|
||||
|
||||
func runVM(passthroughArg string, fn func(context.Context, *vm.Instance, *vm.FileManager) int) int {
|
||||
func runVM(passthroughArg string, fn func(context.Context, *vm.Instance, *vm.FileManager) int, forwardPorts []vm.PortForwardingConfig) int {
|
||||
doRootCheck()
|
||||
|
||||
var passthroughConfig []vm.USBDevicePassthroughConfig
|
||||
|
|
@ -48,6 +50,10 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.Instance, *vm.Fil
|
|||
var forwardPortsConfig []vm.PortForwardingConfig
|
||||
|
||||
for i, fp := range strings.Split(forwardPortsFlagStr, ",") {
|
||||
if fp == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
fpc, err := vm.ParsePortForwardString(fp)
|
||||
if err != nil {
|
||||
slog.Error("Failed to parse port forward string", "index", i, "value", fp, "error", err)
|
||||
|
|
@ -57,6 +63,8 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.Instance, *vm.Fil
|
|||
forwardPortsConfig = append(forwardPortsConfig, fpc)
|
||||
}
|
||||
|
||||
forwardPortsConfig = append(forwardPortsConfig, forwardPorts...)
|
||||
|
||||
// TODO: Alpine image should be downloaded from somewhere.
|
||||
vi, err := vm.NewInstance(slog.Default().With("caller", "vm"), "alpine-img/alpine.qcow2", passthroughConfig, vmDebugFlag, forwardPortsConfig)
|
||||
if err != nil {
|
||||
|
|
@ -144,3 +152,30 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.Instance, *vm.Fil
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getClosestAvailPort(port uint16) (uint16, error) {
|
||||
for i := port; i < 65535; i++ {
|
||||
ln, err := net.Listen("tcp", ":"+fmt.Sprint(i))
|
||||
if err != nil {
|
||||
if opErr, ok := err.(*net.OpError); ok {
|
||||
if sysErr, ok := opErr.Err.(*os.SyscallError); ok {
|
||||
if sysErr.Err == syscall.EADDRINUSE {
|
||||
// The port is in use.
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0, errors.Wrapf(err, "net listen (port %v)", port)
|
||||
}
|
||||
|
||||
err = ln.Close()
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "close ephemeral listener")
|
||||
}
|
||||
|
||||
return i, nil
|
||||
}
|
||||
|
||||
return 0, fmt.Errorf("no available port found")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue