forked from pathwar/pathwar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: setup pwctl bridge with config file and passphrases
- Loading branch information
Showing
9 changed files
with
186 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
entrypoint/out/ | ||
vendor/ | ||
*~ | ||
*# | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package hypervisor | ||
|
||
import "math/rand" | ||
|
||
// from https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go | ||
|
||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
const ( | ||
letterIdxBits = 6 // 6 bits to represent a letter index | ||
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits | ||
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits | ||
) | ||
|
||
func randString(n int) string { | ||
b := make([]byte, n) | ||
// A rand.Int63() generates 63 random bits, enough for letterIdxMax letters! | ||
for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; { | ||
if remain == 0 { | ||
cache, remain = rand.Int63(), letterIdxMax | ||
} | ||
if idx := int(cache & letterIdxMask); idx < len(letterBytes) { | ||
b[i] = letterBytes[idx] | ||
i-- | ||
} | ||
cache >>= letterIdxBits | ||
remain-- | ||
} | ||
|
||
return string(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/out/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
|
||
pwctlconfig "pathwar.pw/pwctl/config" | ||
) | ||
|
||
func getConfig() (*pwctlconfig.Config, error) { | ||
// load config file | ||
configJSON, err := ioutil.ReadFile("/pwctl.json") | ||
if err != nil { | ||
return nil, err | ||
} | ||
var config pwctlconfig.Config | ||
if err := json.Unmarshal(configJSON, &config); err != nil { | ||
return nil, err | ||
} | ||
return &config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package config | ||
|
||
import "encoding/json" | ||
|
||
type Config struct { | ||
Passphrases []string | ||
} | ||
|
||
func (c Config) String() string { | ||
out, _ := json.Marshal(c) | ||
return string(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func main() { | ||
rootCmd := &cobra.Command{ | ||
Use: os.Args[0], | ||
} | ||
rootCmd.PersistentFlags().BoolP("help", "h", false, "print usage") | ||
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { | ||
return nil | ||
} | ||
rootCmd.AddCommand(&cobra.Command{ | ||
Use: "entrypoint", | ||
Args: cobra.MinimumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
// FIXME: prepare the level | ||
// FIXME: add a self-destruct mode that allow having root access only at runtime | ||
// FIXME: lock to block other commands | ||
binary, err := exec.LookPath(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
env := os.Environ() | ||
if err := syscall.Exec(binary, args, env); err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
}) | ||
rootCmd.AddCommand(&cobra.Command{ | ||
Use: "env", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
for _, line := range os.Environ() { | ||
fmt.Println(line) | ||
} | ||
return nil | ||
}, | ||
}) | ||
rootCmd.AddCommand(&cobra.Command{ | ||
Use: "config", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
config, err := getConfig() | ||
if err != nil { | ||
return err | ||
} | ||
out, _ := json.Marshal(config) | ||
fmt.Println(string(out)) | ||
return nil | ||
}, | ||
}) | ||
rootCmd.AddCommand(&cobra.Command{ | ||
Use: "passphrase", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
config, err := getConfig() | ||
if err != nil { | ||
return err | ||
} | ||
// FIXME: parse index from CLI | ||
fmt.Println(config.Passphrases[0]) | ||
return nil | ||
}, | ||
}) | ||
args := os.Args[1:] | ||
if args[0] == "entrypoint" && len(args) > 1 && args[1] != "--" { | ||
args = append([]string{"entrypoint", "--"}, args[1:]...) | ||
} | ||
rootCmd.SetArgs(args) | ||
viper.AutomaticEnv() | ||
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) | ||
if err := rootCmd.Execute(); err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err) | ||
os.Exit(1) | ||
} | ||
} |