2023-08-25 16:54:58 +01:00
package cmd
import (
"context"
"fmt"
"log/slog"
2023-08-26 16:26:35 +01:00
"net"
2023-08-26 11:27:38 +01:00
"os"
2023-08-25 16:54:58 +01:00
2023-08-26 09:16:52 +01:00
"github.com/AlexSSD7/linsk/vm"
2023-08-26 16:26:35 +01:00
"github.com/sethvargo/go-password/password"
2023-08-25 16:54:58 +01:00
"github.com/spf13/cobra"
)
var runCmd = & cobra . Command {
2023-08-29 10:37:52 +01:00
Use : "run" ,
Short : "Start a VM and expose a file share." ,
Args : cobra . ExactArgs ( 3 ) ,
2023-08-27 15:30:51 +01:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
2023-08-25 16:54:58 +01:00
vmMountDevName := args [ 1 ]
fsType := args [ 2 ]
2023-08-29 10:00:12 +01:00
ftpPassivePortCount := uint16 ( 9 )
networkSharePort , err := getClosestAvailPortWithSubsequent ( 9000 , 10 )
2023-08-26 16:26:35 +01:00
if err != nil {
2023-08-29 10:59:50 +01:00
slog . Error ( "Failed to get closest available host port for network file share" , "error" , err . Error ( ) )
2023-08-26 16:26:35 +01:00
os . Exit ( 1 )
}
2023-08-29 10:00:12 +01:00
ports := [ ] vm . PortForwardingRule { {
HostIP : net . ParseIP ( "127.0.0.1" ) , // TODO: Make this changeable.
HostPort : networkSharePort ,
VMPort : 21 ,
} }
for i := uint16 ( 0 ) ; i < ftpPassivePortCount ; i ++ {
p := networkSharePort + 1 + i
ports = append ( ports , vm . PortForwardingRule {
HostIP : net . ParseIP ( "127.0.0.1" ) , // TODO: Make this changeable.
HostPort : p ,
VMPort : p ,
} )
}
2023-08-25 19:55:11 +01:00
// TODO: `slog` library prints entire stack traces for errors which makes reading errors challenging.
2023-08-27 13:44:57 +01:00
os . Exit ( runVM ( args [ 0 ] , func ( ctx context . Context , i * vm . VM , fm * vm . FileManager ) int {
2023-08-29 10:59:50 +01:00
slog . Info ( "Mounting the device" , "dev" , vmMountDevName , "fs" , fsType , "luks" , luksFlag )
2023-08-25 19:55:11 +01:00
err := fm . Mount ( vmMountDevName , vm . MountOptions {
FSType : fsType ,
LUKS : luksFlag ,
} )
2023-08-25 16:54:58 +01:00
if err != nil {
2023-08-29 10:59:50 +01:00
slog . Error ( "Failed to mount the disk inside the VM" , "error" , err . Error ( ) )
2023-08-26 11:27:38 +01:00
return 1
2023-08-25 16:54:58 +01:00
}
2023-08-26 16:26:35 +01:00
sharePWD , err := password . Generate ( 16 , 10 , 0 , false , false )
if err != nil {
2023-08-29 10:59:50 +01:00
slog . Error ( "Failed to generate ephemeral password for network file share" , "error" , err . Error ( ) )
2023-08-26 16:26:35 +01:00
return 1
}
2023-08-29 10:00:12 +01:00
shareURI := "ftp://linsk:" + sharePWD + "@localhost:" + fmt . Sprint ( networkSharePort )
2023-08-26 16:26:35 +01:00
2023-08-29 10:00:12 +01:00
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: FTP\nServer Address: ftp://localhost:%v\nUsername: linsk\nPassword: %v\n\nShare URI: %v\n================\n" , networkSharePort , sharePWD , shareURI )
2023-08-26 16:26:35 +01:00
2023-08-29 10:00:12 +01:00
err = fm . StartFTP ( [ ] byte ( sharePWD ) , networkSharePort + 1 , ftpPassivePortCount )
2023-08-26 16:26:35 +01:00
if err != nil {
2023-08-29 10:59:50 +01:00
slog . Error ( "Failed to start FTP server" , "error" , err . Error ( ) )
2023-08-26 16:26:35 +01:00
return 1
}
2023-08-29 10:00:12 +01:00
slog . Info ( "Started the network share successfully" , "type" , "ftp" )
2023-08-26 16:26:35 +01:00
2023-08-25 16:54:58 +01:00
<- ctx . Done ( )
2023-08-26 11:27:38 +01:00
return 0
2023-08-29 10:00:12 +01:00
} , ports , unrestrictedNetworkingFlag ) )
2023-08-25 16:54:58 +01:00
} ,
}
2023-08-25 19:55:11 +01:00
var luksFlag bool
func init ( ) {
runCmd . Flags ( ) . BoolVarP ( & luksFlag , "luks" , "l" , false , "Use cryptsetup to open a LUKS volume (password will be prompted)" )
}