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

various small fixes #1151

Merged
merged 19 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
73 changes: 34 additions & 39 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ snapshot-clean: ## Cleans snapshot / release
rm -rf ./dist

host-apps: ## Build app
test -d apps && rm -r apps || true
mkdir -p ./apps
${OPTS} go build ${BUILD_OPTS} -o ./apps/ ./cmd/apps/skychat
${OPTS} go build ${BUILD_OPTS} -o ./apps/ ./cmd/apps/skysocks
Expand Down Expand Up @@ -180,6 +181,7 @@ host-apps-systray-windows:

# Static Apps
host-apps-static: ## Build app
test -d apps && rm -r apps || true
mkdir -p ./apps
${STATIC_OPTS} go build -trimpath --ldflags '-linkmode external -extldflags "-static" -buildid=' -o ./apps/ ./cmd/apps/skychat
${STATIC_OPTS} go build -trimpath --ldflags '-linkmode external -extldflags "-static" -buildid=' -o ./apps/ ./cmd/apps/skysocks
Expand Down Expand Up @@ -233,68 +235,61 @@ install-deps-ui: ## Install the UI dependencies
run: ## Run skywire visor with skywire-config.json, and start a browser if running a hypervisor
./skywire-visor -bc ./skywire-config.json

## Run skywire from source, without compiling binaries - requires skywire cloned
run-source:
## Prepare to run skywire from source, without compiling binaries
prepare:
test -d apps && rm -r apps || true
ln -s scripts/_apps apps
chmod +x apps/*
mkdir -p apps
ln ./scripts/_apps/skychat ./apps/
ln ./scripts/_apps/skysocks ./apps/
ln ./scripts/_apps/skysocks-client ./apps/
ln ./scripts/_apps/vpn-server ./apps/
ln ./scripts/_apps/vpn-client ./apps/
chmod +x ./apps/*
sudo echo "sudo cache"

prepare-systray: prepare
rm apps/vpn*
ln -f ./scripts/_apps/vpn-server-systray ./apps/vpn-server
ln -f ./scripts/_apps/vpn-client-systray ./apps/vpn-client

## Run skywire from source, without compiling binaries - requires skywire cloned
run-source: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -in | go run ./cmd/skywire-visor/skywire-visor.go -nb || true

## Run skywire from source, with vpn server enabled
run-systray: prepare-systray
go run -tags systray ./cmd/skywire-cli/skywire-cli.go config gen -ni | go run -tags systray ./cmd/skywire-visor/skywire-visor.go -nb || true

## Run skywire from source, without compiling binaries - requires skywire cloned
run-vpnsrv:
test -d apps && rm -r apps || true
ln -s scripts/_apps apps
chmod +x apps/*
sudo echo "sudo cache"
run-vpnsrv: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -in --servevpn | go run ./cmd/skywire-visor/skywire-visor.go -nb || true

## Run skywire from source with test endpoints
run-source-test:
test -d apps && rm -r apps || true
ln -s scripts/_apps apps
chmod +x apps/*
sudo echo "sudo cache"
run-source-test: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -nit | go run ./cmd/skywire-visor/skywire-visor.go -nb || true

## Run skywire from source, with vpn server enabled
run-vpnsrv-test:
test -d apps && rm -r apps || true
ln -s scripts/_apps apps
chmod +x apps/*
sudo echo "sudo cache"
run-vpnsrv-test: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -nit --servevpn | go run ./cmd/skywire-visor/skywire-visor.go -nb || true

## Run skywire from source, with vpn server enabled
run-systray-test: prepare-systray
go run -tags systray ./cmd/skywire-cli/skywire-cli.go config gen -nit | go run -tags systray ./cmd/skywire-visor/skywire-visor.go -nb || true

## Run skywire from source with dmsghttp config
run-source-dmsghttp:
test -d apps && rm -r apps || true
ln -s scripts/_apps apps
chmod +x apps/*
sudo echo "sudo cache"
run-source-dmsghttp: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -din | go run ./cmd/skywire-visor/skywire-visor.go -nb || true

## Run skywire from source with dmsghttp config and vpn server
run-vpnsrv-dmsghttp:
test -d apps && rm -r apps || true
ln -s scripts/_apps apps
chmod +x apps/*
sudo echo "sudo cache"
run-vpnsrv-dmsghttp: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -din --servevpn | go run ./cmd/skywire-visor/skywire-visor.go -nb || true

## Run skywire from source with dmsghttp config and test endpoints
run-source-dmsghttp-test:
test -d apps && rm -r apps || true
ln -s scripts/_apps apps
chmod +x apps/*
sudo echo "sudo cache"
run-source-dmsghttp-test: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -dint | go run ./cmd/skywire-visor/skywire-visor.go -nb || true

## Run skywire from source with dmsghttp config, vpn server, and test endpoints
run-vpnsrv-dmsghttp-test:
test -d apps && rm -r apps || true
ln -s scripts/_apps apps
chmod +x apps/*
sudo echo "sudo cache"
run-vpnsrv-dmsghttp-test: prepare
go run ./cmd/skywire-cli/skywire-cli.go config gen -dint --servevpn | go run ./cmd/skywire-visor/skywire-visor.go -nb || true

lint-ui: ## Lint the UI code
Expand Down
File renamed without changes.
134 changes: 117 additions & 17 deletions cmd/skywire-cli/commands/config/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"encoding/json"
"fmt"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"

"github.com/bitfield/script"
"github.com/sirupsen/logrus"
"github.com/skycoin/skycoin/src/util/logging"
"github.com/spf13/cobra"
Expand All @@ -29,6 +32,7 @@ var (
retainHypervisors bool
testEnv bool
pkgEnv bool
usrEnv bool
hypervisor bool
hypervisorPKs string
dmsgHTTP bool
Expand All @@ -47,6 +51,7 @@ var (
all bool
outunset bool
ver string
root bool
svcconf = strings.ReplaceAll(utilenv.ServiceConfAddr, "http://", "") //skyenv.DefaultServiceConfAddr
testconf = strings.ReplaceAll(utilenv.TestServiceConfAddr, "http://", "") //skyenv.DefaultServiceConfAddr
hiddenflags []string
Expand All @@ -58,30 +63,46 @@ func init() {
RootCmd.AddCommand(genConfigCmd)

genConfigCmd.Flags().StringVarP(&serviceConfURL, "url", "a", svcconf, "services conf")
hiddenflags = append(hiddenflags, "url")
genConfigCmd.Flags().BoolVarP(&bestProtocol, "bestproto", "b", false, "best protocol (dmsg | direct) based on location")
genConfigCmd.Flags().BoolVarP(&disableauth, "noauth", "c", false, "disable authentication for hypervisor UI")
hiddenflags = append(hiddenflags, "noauth")
genConfigCmd.Flags().BoolVarP(&dmsgHTTP, "dmsghttp", "d", false, "use dmsg connection to skywire services")
hiddenflags = append(hiddenflags, "dmsghttp")
genConfigCmd.Flags().BoolVarP(&enableauth, "auth", "e", false, "enable auth on hypervisor UI")
hiddenflags = append(hiddenflags, "auth")
genConfigCmd.Flags().BoolVarP(&force, "force", "f", false, "remove pre-existing config")
hiddenflags = append(hiddenflags, "force")
genConfigCmd.Flags().StringVarP(&disableApps, "disableapps", "g", "", "comma separated list of apps to disable")
hiddenflags = append(hiddenflags, "disableapps")
genConfigCmd.Flags().BoolVarP(&hypervisor, "ishv", "i", false, "local hypervisor configuration")
genConfigCmd.Flags().StringVarP(&hypervisorPKs, "hvpks", "j", "", "list of public keys to use as hypervisor")
genConfigCmd.Flags().StringVarP(&selectedOS, "os", "k", skyenv.OS, "(linux / macos / windows) paths")
hiddenflags = append(hiddenflags, "os")
genConfigCmd.Flags().BoolVarP(&stdout, "stdout", "n", false, "write config to stdout")
genConfigCmd.Flags().StringVarP(&output, "out", "o", skyenv.ConfigName, "output config")
genConfigCmd.Flags().BoolVarP(&pkgEnv, "package", "p", false, "use paths for package "+skyenv.SkywirePath)
hiddenflags = append(hiddenflags, "stdout")
genConfigCmd.Flags().StringVarP(&output, "out", "o", "", "output config: "+skyenv.ConfigName)
genConfigCmd.Flags().BoolVarP(&pkgEnv, "pkg", "p", false, "use paths for package: "+skyenv.SkywirePath)
genConfigCmd.Flags().BoolVarP(&usrEnv, "user", "u", false, "use paths for user space: "+skyenv.HomePath())
genConfigCmd.Flags().BoolVarP(&publicRPC, "publicrpc", "q", false, "allow rpc requests from LAN")
hiddenflags = append(hiddenflags, "publicrpc")
genConfigCmd.Flags().BoolVarP(&regen, "regen", "r", false, "re-generate existing config & retain keys")
genConfigCmd.Flags().VarP(&sk, "sk", "s", "a random key is generated if unspecified\n\r")
hiddenflags = append(hiddenflags, "sk")
genConfigCmd.Flags().BoolVarP(&testEnv, "testenv", "t", false, "use test deployment "+testconf)
hiddenflags = append(hiddenflags, "testenv")
genConfigCmd.Flags().BoolVarP(&vpnServerEnable, "servevpn", "v", false, "enable vpn server")
hiddenflags = append(hiddenflags, "servevpn")
genConfigCmd.Flags().BoolVarP(&hide, "hide", "w", false, "dont print the config to the terminal")
hiddenflags = append(hiddenflags, "hide")
genConfigCmd.Flags().BoolVarP(&retainHypervisors, "retainhv", "x", false, "retain existing hypervisors with regen")
hiddenflags = append(hiddenflags, "retainhv")
genConfigCmd.Flags().StringVar(&ver, "version", "", "custom version testing override")
hiddenflags = append(hiddenflags, "version")
genConfigCmd.Flags().BoolVar(&all, "all", false, "show all flags")
genConfigCmd.Flags().StringVar(&print, "print", "", "parse test ; read config from file & print")
hiddenflags = append(hiddenflags, "print")

hiddenflags = []string{"url", "print", "noauth", "dmsghttp", "auth", "force", "disableapps", "os", "stdout", "publicrpc", "sk", "testenv", "servevpn", "hide", "retainhv", "print", "version"}
for _, j := range hiddenflags {
genConfigCmd.Flags().MarkHidden(j) //nolint
}
Expand All @@ -104,6 +125,7 @@ var genConfigCmd = &cobra.Command{
//set default output filename
if output == "" {
outunset = true
confPath = skyenv.ConfigName
} else {
confPath = output
}
Expand All @@ -113,15 +135,23 @@ var genConfigCmd = &cobra.Command{
force = false
regen = false
}
//hide defeats the purpose of stdout.
if (stdout) && (hide) {
logger.Fatal("Use of mutually exclusive flags: -w --hide and -n --stdout")
}
//--force will delete a config, which excludes --regen
if (force) && (regen) {
logger.Fatal("Use of mutually exclusive flags: -f --force cannot override -r --regen")
}
var err error
//hide defeats the purpose of stdout.
if (stdout) && (hide) {
logger.Fatal("Use of mutually exclusive flags: -w --hide and -n --stdout")
// these flags overwrite each other
if (usrEnv) && (pkgEnv) {
logger.Fatal("Use of mutually exclusive flags: -u --user and -p --pkg")
}
//enable local hypervisor by default for user
if usrEnv {
hypervisor = true
}
var err error
if dmsgHTTP {
dmsgHTTPPath := skyenv.DMSGHTTPName
if pkgEnv {
Expand Down Expand Up @@ -172,16 +202,20 @@ var genConfigCmd = &cobra.Command{
//fetch the service endpoints
services = visorconfig.Fetch(mLog, serviceConfURL, stdout)
// skywire-cli config gen -ip || skywire-cli config gen -p
if !stdout && outunset && pkgEnv && (selectedOS == "linux") {
if hypervisor {
//default config hypervisor
configName = "skywire.json"
} else {
configName = "skywire-visor.json"
if !stdout && outunset && (selectedOS == "linux") {
if pkgEnv {
if hypervisor {
//default config hypervisor
configName = skyenv.Skywirejson
} else {
configName = skyenv.Skywirevisorjson
}
confPath = skyenv.SkywirePath + "/" + configName
}
if usrEnv {
confPath = skyenv.HomePath() + "/" + skyenv.ConfigName
}
confPath = skyenv.SkywirePath + "/" + configName
}

// Read in old config and obtain old secret key or generate a new random secret key
// and obtain old hypervisors (if any)
var sk cipher.SecKey
Expand All @@ -204,7 +238,7 @@ var genConfigCmd = &cobra.Command{
}

//create the conf
conf, err := visorconfig.MakeDefaultConfig(mLog, &sk, pkgEnv, testEnv, dmsgHTTP, hypervisor, output, hypervisorPKs, services)
conf, err := visorconfig.MakeDefaultConfig(mLog, &sk, usrEnv, pkgEnv, testEnv, dmsgHTTP, hypervisor, confPath, hypervisorPKs, services)
if err != nil {
logger.WithError(err).Fatal("Failed to create config.")
}
Expand All @@ -221,6 +255,46 @@ var genConfigCmd = &cobra.Command{
}
}
}
skywire := os.Args[0]
match := strings.Contains("/tmp/", skywire)
if (!stdout) || (!match) {
// Disable apps not found at bin_path with above exceptions for go run and stdout
if _, err := os.Stat(conf.Launcher.BinPath + "/" + "skychat"); err != nil {
if disableApps == "" {
disableApps = "skychat"
} else {
disableApps = disableApps + ",skychat"
}
}
if _, err := os.Stat(conf.Launcher.BinPath + "/" + "skysocks"); err != nil {
if disableApps == "" {
disableApps = "skysocks"
} else {
disableApps = disableApps + ",skysocks"
}
}
if _, err := os.Stat(conf.Launcher.BinPath + "/" + "skysocks-client"); err != nil {
if disableApps == "" {
disableApps = "skysocks-client"
} else {
disableApps = disableApps + ",skysocks-client"
}
}
if _, err := os.Stat(conf.Launcher.BinPath + "/" + "vpn-client"); err != nil {
if disableApps == "" {
disableApps = "vpn-client"
} else {
disableApps = disableApps + ",vpn-client"
}
}
if _, err := os.Stat(conf.Launcher.BinPath + "/" + "vpn-server"); err != nil {
if disableApps == "" {
disableApps = "vpn-server"
} else {
disableApps = disableApps + ",vpn-server"
}
}
}
// Disable apps listed on --disable-apps flag
if disableApps != "" {
apps := strings.Split(disableApps, ",")
Expand Down Expand Up @@ -251,7 +325,7 @@ var genConfigCmd = &cobra.Command{
}
}
// Check OS and enable auth windows or macos
if selectedOS == "windows" || selectedOS == "macos" {
if (selectedOS == "windows") || (selectedOS == "macos") {
if hypervisor {
conf.Hypervisor.EnableAuth = true
}
Expand All @@ -261,6 +335,32 @@ var genConfigCmd = &cobra.Command{
}
//don't write file with stdout
if !stdout {
thisUser, err := user.Current()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like userLvl or owner etc sounds better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like userLvl or owner etc sounds better.

if err != nil {
panic(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What errors could this return? Are they clear enough in themselves? Should we exit cleanly and log something here instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What errors could this return? Are they clear enough in themselves? Should we exit cleanly and log something here instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to really error here & Ive changed it to fail transparently. This is only checking for root and it will just not change the default from false to true.

}
if thisUser.Username == "root" {
root = true
}
//dont write config as root to non root owned dir & vice versa
if _, err = exec.LookPath("stat"); err == nil {

confPath1, _ := filepath.Split(confPath)
if confPath1 == "" {
confPath1 = "./"
}
owner, err := script.Exec(`stat -c '%U' ` + confPath1).String()
if err != nil {
logger.Error("cannot stat: " + confPath1)
}
if ((owner != "root") || (owner != "root\n")) && root {
logger.Fatal("declined writing config as root to directory not owned by root")
}
if !root && ((owner == "root") || (owner == "root\n")) {
logger.Fatal("Insufficient permissions to write to the specified path")
}
}

// Save config to file.
if err := conf.Flush(); err != nil {
logger.WithError(err).Fatal("Failed to flush config to file.")
Expand Down
2 changes: 1 addition & 1 deletion cmd/skywire-visor/commands/nosystray.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func runApp() {
runVisor()
runVisor(nil)
}

// setStopFunction sets the stop function
Expand Down
Loading