Skip to content

Commit

Permalink
source a .conf file with skywire-cli config gen (#1644)
Browse files Browse the repository at this point in the history
* include accidentally omitted

* revert skywire-cli ut to previous state

* update go.mod ; make format check
  • Loading branch information
0pcom committed Jul 9, 2023
1 parent c36d0b3 commit b03f7a5
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 108 deletions.
196 changes: 167 additions & 29 deletions cmd/skywire-cli/commands/config/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,18 @@ func init() {
genConfigCmd.Flags().BoolVar(&isAll, "all", false, "show all flags")
genConfigCmd.Flags().StringVar(&binPath, "binpath", scriptExecString("${BINPATH}"), "set bin_path\033[0m")
gHiddenFlags = append(gHiddenFlags, "binpath")
// genConfigCmd.Flags().StringVar(&addSkysocksClientSrv, "proxyclientpk", scriptExecString("${PROXYCLIENTPK}"), "set server public key for proxy client")
// genConfigCmd.Flags().BoolVar(&proxyClientAutostart, "startproxyclient", scriptExecBool("${STARTPROXYCLIENT:-false}"), "autostart proxy client")
// genConfigCmd.Flags().BoolVar(&disableProxyServerAutostart, "noproxyserver", scriptExecBool("${NOPROXYSERVER:-false}"), "disable autostart of proxy server")
// genConfigCmd.Flags().StringVar(&proxyServerPass, "proxyserverpass", "", "set password for the proxy server")
// genConfigCmd.Flags().StringVar(&proxyClientPass, "proxyclientpass", "", "set password for the proxy client to access the proxy server (if needed)")
// // TODO: Password for accessing proxy client
// genConfigCmd.Flags().StringVar(&setVPNClientKillswitch, "killsw", "", "vpn client killswitch")
// genConfigCmd.Flags().StringVar(&addVPNClientSrv, "addvpn", "", "set vpn server public key for vpn client")
// genConfigCmd.Flags().StringVar(&addVPNClientPasscode, "vpnpass", "", "password for vpn client to access the vpn server (if needed)")
// genConfigCmd.Flags().StringVar(&addVPNServerPasscode, "vpnserverpass", "", "set password to the vpn server")
// genConfigCmd.Flags().StringVar(&setVPNServerSecure, "secure", "", "change secure mode status of vpn server")
// genConfigCmd.Flags().StringVar(&setVPNServerNetIfc, "netifc", "", "VPN Server network interface (detected: "+getInterfaceNames()+")")
genConfigCmd.Flags().StringVar(&addSkysocksClientSrv, "proxyclientpk", scriptExecString("${PROXYCLIENTPK}"), "set server public key for proxy client")
genConfigCmd.Flags().BoolVar(&enableProxyClientAutostart, "startproxyclient", scriptExecBool("${STARTPROXYCLIENT:-false}"), "autostart proxy client")
genConfigCmd.Flags().BoolVar(&disableProxyServerAutostart, "noproxyserver", scriptExecBool("${NOPROXYSERVER:-false}"), "disable autostart of proxy server")
genConfigCmd.Flags().StringVar(&proxyServerPass, "proxyserverpass", scriptExecString("${PROXYSEVERPASS}"), "set proxy server password")
genConfigCmd.Flags().StringVar(&proxyClientPass, "proxyclientpass", scriptExecString("${PROXYCLIENTPASS}"), "password for the proxy client to access the server (if needed)")
// TODO: Password for accessing proxy client
genConfigCmd.Flags().StringVar(&setVPNClientKillswitch, "killsw", scriptExecString("${VPNKS}"), "vpn client killswitch")
genConfigCmd.Flags().StringVar(&addVPNClientSrv, "addvpn", scriptExecString("${ADDVPNPK}"), "set vpn server public key for vpn client")
genConfigCmd.Flags().StringVar(&addVPNClientPasscode, "vpnpass", scriptExecString("${VPNCLIENTPASS}"), "password for vpn client to access the vpn server (if needed)")
genConfigCmd.Flags().StringVar(&addVPNServerPasscode, "vpnserverpass", scriptExecString("${VPNSEVERPASS}"), "set password to the vpn server")
genConfigCmd.Flags().StringVar(&setVPNServerSecure, "secure", scriptExecString("${VPNSEVERSECURE}"), "change secure mode status of vpn server")
genConfigCmd.Flags().StringVar(&setVPNServerNetIfc, "netifc", scriptExecString("${VPNSEVERNETIFC}"), "VPN Server network interface (detected: "+getInterfaceNames()+")")
genConfigCmd.Flags().BoolVarP(&isEnvs, "envs", "q", false, "show the environmental variable settings")
gHiddenFlags = append(gHiddenFlags, "envs")
genConfigCmd.Flags().BoolVar(&noFetch, "nofetch", false, "do not fetch the services from the service conf url")
Expand Down Expand Up @@ -497,24 +497,23 @@ var genConfigCmd = &cobra.Command{
log.WithError(err).Error("Failed to fetch servers\n")
log.Warn("Falling back on hardcoded servers")
}
} else {
// nil error from client.Get
if res.Body != nil {
defer res.Body.Close() //nolint
}
body, err := io.ReadAll(res.Body)
if err != nil {
log.WithError(err).Fatal("Failed to read response\n")
}
//fill in services struct with the response
err = json.Unmarshal(body, &services)
if err != nil {
log.WithError(err).Fatal("Failed to unmarshal json response\n")
}
if !isStdout {
log.Infof("Fetched service endpoints from '%s'", serviceConfURL)
}
}
if res.Body != nil {
defer res.Body.Close() //nolint
}
body, err := io.ReadAll(res.Body)
if err != nil {
log.WithError(err).Fatal("Failed to read response\n")
}
//fill in services struct with the response
err = json.Unmarshal(body, &services)
if err != nil {
log.WithError(err).Fatal("Failed to unmarshal json response\n")
}
if !isStdout {
log.Infof("Fetched service endpoints from '%s'", serviceConfURL)
}

// reset the state of isStdout
isStdout = wasStdout
}
Expand Down Expand Up @@ -990,6 +989,79 @@ var genConfigCmd = &cobra.Command{
newConfLauncherApps := append(conf.Launcher.Apps, exampleApps...)
conf.Launcher.Apps = newConfLauncherApps
}

if addVPNServerPasscode != "" {
changeAppsConfig(conf, "vpn-server", "--passcode", addVPNServerPasscode)
}
if setVPNServerNetIfc != "" {
changeAppsConfig(conf, "vpn-server", "--netifc", setVPNServerNetIfc)
}
switch setVPNServerSecure {
case "true":
changeAppsConfig(conf, "vpn-server", "--secure", setVPNServerSecure)
case "false":
changeAppsConfig(conf, "vpn-server", "--secure", setVPNServerSecure)
}
switch setVPNServerAutostart {
case "true":
for i, app := range conf.Launcher.Apps {
if app.Name == "vpn-server" {
conf.Launcher.Apps[i].AutoStart = true
}
}
case "false":
for i, app := range conf.Launcher.Apps {
if app.Name == "vpn-server" {
conf.Launcher.Apps[i].AutoStart = false
}
}
}

switch setVPNClientKillswitch {
case "true":
changeAppsConfig(conf, "vpn-client", "--killswitch", setVPNClientKillswitch)
case "false":
changeAppsConfig(conf, "vpn-client", "--killswitch", setVPNClientKillswitch)
}
if addVPNClientSrv != "" {
keyParsed, err := coinCipher.PubKeyFromHex(strings.TrimSpace(addVPNClientSrv))
if err != nil {
log.WithError(err).Fatalf("Failed to parse hypervisor private key: %s.", addVPNClientSrv)
}
changeAppsConfig(conf, "vpn-client", "--srv", keyParsed.Hex())
}

if addVPNClientPasscode != "" {
changeAppsConfig(conf, "vpn-client", "--passcode", addVPNClientPasscode)
}
if addSkysocksClientSrv != "" {
keyParsed, err := coinCipher.PubKeyFromHex(strings.TrimSpace(addSkysocksClientSrv))
if err != nil {
logger.WithError(err).Fatalf("Failed to parse public key: %s.", addSkysocksClientSrv)
}
changeAppsConfig(conf, "skysocks-client", "--srv", keyParsed.Hex())
}
if proxyServerPass != "" {
changeAppsConfig(conf, "skysocks", "--passcode", proxyServerPass)
}
if proxyClientPass != "" {
changeAppsConfig(conf, "skysocks-client", "--passcode", proxyClientPass)
}

if disableProxyServerAutostart {
for i, app := range conf.Launcher.Apps {
if app.Name == "skysocks" {
conf.Launcher.Apps[i].AutoStart = false
}
}
}
if enableProxyClientAutostart {
for i, app := range conf.Launcher.Apps {
if app.Name == "skysocks-client" {
conf.Launcher.Apps[i].AutoStart = true
}
}
}
if isHypervisor {
// Disable hypervisor UI authentication --disable-auth flag
if isDisableAuth {
Expand Down Expand Up @@ -1152,6 +1224,40 @@ const envfileLinux = `#
#-- Set app bin_path
#BINPATH='./apps'
#-- Set server public key for proxy client to connect to
#PROXYCLIENTPK=''
#-- Enable autostart of the proxy client
#STARTPROXYCLIENT=true
#-- Disable autostart of proxy server
#NOPROXYSERVER=true
#-- Set a password for the proxy server
#PROXYSEVERPASS=''
#-- Password for the proxy client to access the server (if password is set for the server)
#PROXYCLIENTPASS=''
#-- Set VPN client killswitch
#VPNKS=true
#-- Set vpn server public key for the vpn client to use
#ADDVPNPK=''
#-- Password for vpn client to access the server (if password is set forthe server)
#VPNCLIENTPASS=''
#-- Set password to the vpn server
#VPNSEVERPASS=''
#-- Change secure mode status of vpn server
#VPNSEVERSECURE=''
#-- Set VPN Server network interface
#VPNSEVERNETIFC=''
`
const envfileWindows = `#
# C:\ProgramData\skywire.ps1
Expand Down Expand Up @@ -1218,4 +1324,36 @@ const envfileWindows = `#
#-- Set app bin_path
#$BINPATH='./apps'
#-- Set server public key for proxy client to connect to
#$PROXYCLIENTPK=''
#-- Enable autostart of the proxy client
#$STARTPROXYCLIENT=true
#-- Disable autostart of proxy server
#$NOPROXYSERVER=true
#-- Set a password for the proxy server
#$PROXYSEVERPASS=''
#-- Password for the proxy client to access the server (if password is set for the server)
#$PROXYCLIENTPASS=''
#-- Set VPN client killswitch
#$VPNKS=true
#-- Set vpn server public key for the vpn client to use
#$ADDVPNPK=''
#-- Password for vpn client to access the server (if password is set forthe server)
#$VPNCLIENTPASS=''
#-- Set password to the vpn server
#$VPNSEVERPASS=''
#-- Change secure mode status of vpn server
#$VPNSEVERSECURE=''
#-- Set VPN Server network interface
#$VPNSEVERNETIFC=''
`
142 changes: 71 additions & 71 deletions cmd/skywire-cli/commands/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,83 +15,83 @@ import (

var logger = logging.MustGetLogger("skywire-cli")

// proxyClientAutostart bool //nolint Note: pending implementation for config gen
// disableProxyServerAutostart bool //nolint Note: pending implementation for config gen
// proxyServerPass string //nolint Note: pending implementation for config gen
// proxyClientPass string //nolint Note: pending implementation for config gen
var (
conf = new(visorconfig.V1)
dmsgHTTPServersList = &visorconfig.DmsgHTTPServers{
Test: visorconfig.DmsgHTTPServersData{DMSGServers: []*disc.Entry{}},
Prod: visorconfig.DmsgHTTPServersData{DMSGServers: []*disc.Entry{}},
}
noFetch bool
noDefaults bool
stcprPort int
sudphPort int
sk cipher.SecKey
output string
confPath string
configName string //nolint Note: configName used, but golangci-lint marked it unused in wrong
isStdout bool
isRegen bool
isRetainHypervisors bool
isTestEnv bool
pText string
isPkgEnv bool
isUsrEnv bool
isHypervisor bool
hypervisorPKs string
dmsgptyWlPKs string
surveyWhitelistPKs string
routeSetupNodes string
transportSetupPKs string
isDmsgHTTP bool
isVpnServerEnable bool
isDisableAuth bool
isEnableAuth bool
selectedOS string
disableApps string
isBestProtocol bool
serviceConfURL string
services *visorconfig.Services
isForce bool
isHide bool
isAll bool
isOutUnset bool
ver string
isRoot = visorconfig.IsRoot()
svcConf = strings.ReplaceAll(utilenv.ServiceConfAddr, "http://", "") //visorconfig.DefaultServiceConfAddr
testConf = strings.ReplaceAll(utilenv.TestServiceConfAddr, "http://", "") //visorconfig.DefaultServiceConfAddr
gHiddenFlags []string
uHiddenFlags []string
binPath string
logLevel string
isPkg bool
input string
isUpdateEndpoints bool
addHypervisorPKs string
isResetHypervisor bool
setVPNClientKillswitch string
addVPNClientSrv string
addVPNClientPasscode string
isResetVPNclient bool
addVPNServerPasscode string
setVPNServerSecure string
setVPNServerAutostart string
setVPNServerNetIfc string
isResetVPNServer bool
addSkysocksClientSrv string
isResetSkysocksClient bool
skysocksPasscode string
isResetSkysocks bool
setPublicAutoconnect string
minHops int
isUsr bool
isPublic bool
disablePublicAutoConn bool
isDisplayNodeIP bool
addExampleApps bool
noFetch bool
noDefaults bool
stcprPort int
sudphPort int
sk cipher.SecKey
output string
confPath string
configName string //nolint Note: configName used, but golangci-lint marked it unused in wrong
isStdout bool
isRegen bool
isRetainHypervisors bool
isTestEnv bool
pText string
isPkgEnv bool
isUsrEnv bool
isHypervisor bool
hypervisorPKs string
dmsgptyWlPKs string
surveyWhitelistPKs string
routeSetupNodes string
transportSetupPKs string
isDmsgHTTP bool
isVpnServerEnable bool
isDisableAuth bool
isEnableAuth bool
selectedOS string
disableApps string
isBestProtocol bool
serviceConfURL string
services *visorconfig.Services
isForce bool
isHide bool
isAll bool
isOutUnset bool
ver string
isRoot = visorconfig.IsRoot()
svcConf = strings.ReplaceAll(utilenv.ServiceConfAddr, "http://", "") //visorconfig.DefaultServiceConfAddr
testConf = strings.ReplaceAll(utilenv.TestServiceConfAddr, "http://", "") //visorconfig.DefaultServiceConfAddr
gHiddenFlags []string
uHiddenFlags []string
binPath string
logLevel string
isPkg bool
input string
isUpdateEndpoints bool
addHypervisorPKs string
isResetHypervisor bool
setVPNClientKillswitch string
addVPNClientSrv string
addVPNClientPasscode string
isResetVPNclient bool
addVPNServerPasscode string
setVPNServerSecure string
setVPNServerAutostart string
setVPNServerNetIfc string
isResetVPNServer bool
addSkysocksClientSrv string
isResetSkysocksClient bool
skysocksPasscode string
isResetSkysocks bool
setPublicAutoconnect string
minHops int
isUsr bool
isPublic bool
disablePublicAutoConn bool
isDisplayNodeIP bool
addExampleApps bool
enableProxyClientAutostart bool
disableProxyServerAutostart bool
proxyServerPass string
proxyClientPass string
)

// RootCmd contains commands that interact with the config of local skywire-visor
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ require (
github.com/lib/pq v1.10.7
github.com/orandin/lumberjackrus v1.0.1
github.com/pterm/pterm v0.12.49
github.com/skycoin/dmsg v1.3.0-rc1.0.20230609193507-b02bb8f7d1bc
github.com/skycoin/skywire-utilities v0.0.0-20230601232053-0abbc9604fbc
github.com/skycoin/dmsg v1.3.11
github.com/skycoin/skywire-utilities v0.0.0-20230609191923-1e678802e17e
github.com/skycoin/systray v1.10.0
github.com/spf13/pflag v1.0.5
github.com/zcalusic/sysinfo v0.9.5
Expand Down
Loading

0 comments on commit b03f7a5

Please sign in to comment.