Skip to content

Commit

Permalink
cli interface for transport setup node
Browse files Browse the repository at this point in the history
  • Loading branch information
0pcom committed Apr 2, 2024
1 parent 58b8010 commit 9f4e413
Show file tree
Hide file tree
Showing 7 changed files with 976 additions and 4 deletions.
148 changes: 145 additions & 3 deletions cmd/transport-setup/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,52 @@ import (
"path/filepath"
"strings"
"time"
"encoding/json"

"github.com/google/uuid"
"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"github.com/skycoin/skywire-utilities/pkg/logging"
"github.com/spf13/cobra"

"github.com/tidwall/pretty"
"github.com/bitfield/script"
"github.com/skycoin/skywire-services/pkg/transport-setup/api"
"github.com/skycoin/skywire-services/pkg/transport-setup/config"
)

var (
pk1 cipher.PubKey
pk2 cipher.PubKey
logLvl string
configFile string
tpsnAddr string
fromPK string
toPK string
tpID string
tpType string
nice bool
)

func init() {
RootCmd.Flags().SortFlags = false
addTPCmd.Flags().SortFlags = false
rmTPCmd.Flags().SortFlags = false
listTPCmd.Flags().SortFlags = false
RootCmd.Flags().StringVarP(&configFile, "config", "c", "", "path to config file\033[0m")
RootCmd.Flags().StringVarP(&logLvl, "loglvl", "l", "info", "set log level one of: info, error, warn, debug, trace, panic")
RootCmd.Flags().StringVarP(&logLvl, "loglvl", "l", "debug", "[info|error|warn|debug|trace|panic]")
RootCmd.AddCommand(addTPCmd, rmTPCmd, listTPCmd)
addTPCmd.Flags().StringVarP(&fromPK, "from", "1", "", "PK to request transport setup")
addTPCmd.Flags().StringVarP(&toPK, "to", "2", "", "other transport edge PK")
addTPCmd.Flags().StringVarP(&tpType, "type", "t", "", "transport type to request creation of [stcpr|sudph|dmsg]")
rmTPCmd.Flags().StringVarP(&fromPK, "from", "1", "", "PK to request transport takedown")
rmTPCmd.Flags().StringVarP(&tpID, "tpid", "i", "", "id of transport to remove")
listTPCmd.Flags().StringVarP(&fromPK, "from", "1", "", "PK to request transport list")
addTPCmd.Flags().BoolVarP(&nice, "pretty", "p", false, "pretty print result")
rmTPCmd.Flags().BoolVarP(&nice, "pretty", "p", false, "pretty print result")
listTPCmd.Flags().BoolVarP(&nice, "pretty", "p", false, "pretty print result")
addTPCmd.Flags().StringVarP(&tpsnAddr, "addr", "z", "http://127.0.0.1:8080", "address of the transport setup-node")
rmTPCmd.Flags().StringVarP(&tpsnAddr, "addr", "z", "http://127.0.0.1:8080", "address of the transport setup-node")
listTPCmd.Flags().StringVarP(&tpsnAddr, "addr", "z", "http://127.0.0.1:8080", "address of the transport setup-node")
}

// RootCmd contains the root command
Expand All @@ -37,7 +66,22 @@ var RootCmd = &cobra.Command{
Long: `
┌┬┐┬─┐┌─┐┌┐┌┌─┐┌─┐┌─┐┬─┐┌┬┐ ┌─┐┌─┐┌┬┐┬ ┬┌─┐
│ ├┬┘├─┤│││└─┐├─┘│ │├┬┘ │───└─┐├┤ │ │ │├─┘
┴ ┴└─┴ ┴┘└┘└─┘┴ └─┘┴└─ ┴ └─┘└─┘ ┴ └─┘┴ `,
┴ ┴└─┴ ┴┘└┘└─┘┴ └─┘┴└─ ┴ └─┘└─┘ ┴ └─┘┴
Transport setup server for skywire
Takes config in the following format:
{
"dmsg": {
"discovery": "http://dmsgd.skywire.skycoin.com",
"servers": [],
"sessions_count": 2
},
"log_level": "",
"port":8080,
"public_key": "",
"secret_key": "",
"transport_discovery": "http://tpd.skywire.skycoin.com"
}`,
SilenceErrors: true,
SilenceUsage: true,
DisableSuggestions: true,
Expand Down Expand Up @@ -69,6 +113,104 @@ var RootCmd = &cobra.Command{
},
}

var addTPCmd = &cobra.Command{
Use: "add",
Short: "add transport to remote visor",
Long: ``,
SilenceErrors: true,
SilenceUsage: true,
DisableSuggestions: true,
DisableFlagsInUseLine: true,
Run: func(_ *cobra.Command, args []string) {
err := pk1.Set(fromPK)
if err != nil {
log.Fatal("-1 invalid public key: ", err)
}
err = pk2.Set(toPK)
if err != nil {
log.Fatal("-2 invalid public key: ", err)
}
if tpType != "dmsg" && tpType != "stcpr" && tpType != "sudph" {
log.Fatal("invalid transport type specified: ", tpType)
}
addtp := api.TransportRequest{
From: pk1,
To: pk2,
Type: tpType,
}
addtpJSON, err := json.Marshal(addtp)
if err != nil {
log.Fatalf("Error marshalling json: %v", err)

Check failure on line 143 in cmd/transport-setup/commands/root.go

View workflow job for this annotation

GitHub Actions / linux

`marshalling` is a misspelling of `marshaling` (misspell)
}
res, err := script.Echo(string(addtpJSON)).Post(tpsnAddr+"/add").String()
if err != nil {
log.Fatalf("error: ",err)
}
if nice {
fmt.Printf(string(pretty.Color(pretty.Pretty([]byte(res)), nil)))

Check failure on line 150 in cmd/transport-setup/commands/root.go

View workflow job for this annotation

GitHub Actions / linux

SA1006: printf-style function with dynamic format string and no further arguments should use print-style function instead (staticcheck)
} else {
fmt.Printf(res)

Check failure on line 152 in cmd/transport-setup/commands/root.go

View workflow job for this annotation

GitHub Actions / linux

SA1006: printf-style function with dynamic format string and no further arguments should use print-style function instead (staticcheck)
}

},
}
var rmTPCmd = &cobra.Command{
Use: "rm",
Short: "remove transport from remote visor",
Long: ``,
SilenceErrors: true,
SilenceUsage: true,
DisableSuggestions: true,
DisableFlagsInUseLine: true,
Run: func(_ *cobra.Command, args []string) {
err := pk1.Set(fromPK)
if err != nil {
log.Fatal("invalid public key: ", err)
}
tpid, err := uuid.Parse(tpID)
if err != nil {
log.Fatal("invalid tp id: ", err)
}
rmtp := api.UUIDRequest{
From: pk1,
ID: tpid,
}
rmtpJSON, err := json.Marshal(rmtp)
if err != nil {
log.Fatalf("Error marshalling json: %v", err)

Check failure on line 180 in cmd/transport-setup/commands/root.go

View workflow job for this annotation

GitHub Actions / linux

`marshalling` is a misspelling of `marshaling` (misspell)
}
res, err := script.Echo(string(rmtpJSON)).Post(tpsnAddr+"/remove").String()
if err != nil {
log.Fatalf("error: ",err)
}
if nice {
fmt.Printf(string(pretty.Color(pretty.Pretty([]byte(res)), nil)))

Check failure on line 187 in cmd/transport-setup/commands/root.go

View workflow job for this annotation

GitHub Actions / linux

SA1006: printf-style function with dynamic format string and no further arguments should use print-style function instead (staticcheck)
} else {
fmt.Printf(res)
}
},
}
var listTPCmd = &cobra.Command{
Use: "list",
Short: "list transports of remote visor",
Long: ``,
SilenceErrors: true,
SilenceUsage: true,
DisableSuggestions: true,
DisableFlagsInUseLine: true,
Run: func(_ *cobra.Command, args []string) {
res, err := script.Get(tpsnAddr+"/"+fromPK+"/transports").String()
if err != nil {
log.Fatal("something unexpected happened: ", err, res)
}
if nice {
fmt.Printf(string(pretty.Color(pretty.Pretty([]byte(res)), nil)))
} else {
fmt.Printf(res)
}
},
}

// Execute executes root CLI command.
func Execute() {
if err := RootCmd.Execute(); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
toolchain go1.21.5

require (
github.com/bitfield/script v0.22.0
github.com/ccding/go-stun/stun v0.0.0-20200514191101-4dc67bcdb029
github.com/dgraph-io/badger/v3 v3.2103.2
github.com/docker/docker v1.13.1
Expand All @@ -29,6 +30,7 @@ require (
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
github.com/tidwall/pretty v1.2.1
github.com/xtaci/kcp-go v5.4.20+incompatible
golang.org/x/net v0.14.0
golang.zx2c4.com/wireguard v0.0.0-20230223181233-21636207a675
Expand All @@ -43,7 +45,6 @@ require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/VictoriaMetrics/metrics v1.24.0 // indirect
github.com/bitfield/script v0.22.0 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/bytedance/sonic v1.10.0 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@ github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 h1:89CEmDvlq/F7S
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161/go.mod h1:wM7WEvslTq+iOEAMDLSzhVuOt5BRZ05WirO+b09GHQU=
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b h1:fj5tQ8acgNUr6O8LEplsxDhUIe2573iLkJc+PqnzZTI=
github.com/templexxx/xor v0.0.0-20191217153810-f85b25db303b/go.mod h1:5XA7W9S6mni3h5uvOC75dA3m9CCCaS83lltmc0ukdi4=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tjfoc/gmsm v1.4.1 h1:aMe1GlZb+0bLjn+cKTPEvvn9oUEBlJitaZiiBwsbgho=
github.com/tjfoc/gmsm v1.4.1/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE=
github.com/tkrajina/gpxgo v1.1.2 h1:il6rjS6IGm3yqa/yr7+fKBlF3ufWDEPZrYi/kxI1Jv0=
Expand Down
20 changes: 20 additions & 0 deletions vendor/github.com/tidwall/pretty/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 122 additions & 0 deletions vendor/github.com/tidwall/pretty/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9f4e413

Please sign in to comment.