Skip to content

Commit

Permalink
CLI for transport setup node (#65)
Browse files Browse the repository at this point in the history
* cli interface for transport setup node
* fix Makefile ; make format
  • Loading branch information
0pcom authored Apr 3, 2024
1 parent 58b8010 commit 50ccbbf
Show file tree
Hide file tree
Showing 16 changed files with 1,003 additions and 91 deletions.
74 changes: 9 additions & 65 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,88 +45,32 @@ export REGISTRY=${DOCKER_REGISTRY}
## : ## _ [Prepare code]

dep: ## Sorts dependencies
@command -v yarn >/dev/null 2>&1 && yarn --cwd ./pkg/node-visualizer/web install || true
# GO111MODULE=on GOPRIVATE=github.com/skycoin/* go get -v github.com/skycoin/skywire@master
GO111MODULE=on GOPRIVATE=github.com/skycoin/* go mod vendor -v
yarn --cwd ./pkg/node-visualizer/web install
# GO111MODULE=on GOPRIVATE=github.com/skycoin/* go mod vendor -v

format: dep ## Formats the code. Must have goimports and goimports-reviser installed (use make install-linters).
goimports -w -local github.com/skycoin/skywire-services ./pkg
goimports -w -local github.com/skycoin/skywire-services ./cmd
goimports -w -local github.com/skycoin/skywire-services ./internal
find . -type f -name '*.go' -not -path "./vendor/*" -exec goimports-reviser -project-name ${PROJECT_BASE} {} \;
goimports -w -local github.com/skycoin/skywire-services ./pkg ./cmd ./internal
find . -type f -name '*.go' -not -path "./.git/*" -not -path "./vendor/*" -exec goimports-reviser -project-name ${PROJECT_BASE} {} \;

## : ## _ [Build, install, clean]

build: dep ## Build binaries
${OPTS} go build ${BUILD_OPTS} -o ./bin/route-finder ./cmd/route-finder
${OPTS} go build ${BUILD_OPTS} -o ./bin/setup-node ./cmd/setup-node
${OPTS} go build ${BUILD_OPTS} -o ./bin/transport-discovery ./cmd/transport-discovery
${OPTS} go build ${BUILD_OPTS} -o ./bin/address-resolver ./cmd/address-resolver
${OPTS} go build ${BUILD_OPTS} -o ./bin/sw-env ./cmd/sw-env
${OPTS} go build ${BUILD_OPTS} -o ./bin/keys-gen ./cmd/keys-gen
${OPTS} go build ${BUILD_OPTS} -o ./bin/network-monitor ./cmd/network-monitor
${OPTS} go build ${BUILD_OPTS} -o ./apps/vpn-client ./cmd/vpn-lite-client
${OPTS} go build ${BUILD_OPTS} -o ./bin/transport-setup ./cmd/transport-setup
${OPTS} go build ${BUILD_OPTS} -o ./bin/config-bootstrapper ./cmd/config-bootstrapper
${OPTS} go build ${BUILD_OPTS} -o ./bin/liveness-checker ./cmd/liveness-checker
${OPTS} go build ${BUILD_OPTS} -o ./bin/dmsg-monitor ./cmd/dmsg-monitor
${OPTS} go build ${BUILD_OPTS} -o ./bin/tpd-monitor ./cmd/tpd-monitor
${OPTS} go build ${BUILD_OPTS} -o ./bin/vpn-monitor ./cmd/vpn-monitor
${OPTS} go build ${BUILD_OPTS} -o ./bin/skysocks-monitor ./cmd/skysocks-monitor
${OPTS} go build ${BUILD_OPTS} -o ./apps/skysocks-client ./cmd/skysocks-lite-client
${OPTS} go build ${BUILD_OPTS} -o ./bin/public-visor-monitor ./cmd/public-visor-monitor
${OPTS} go build ${BUILD_OPTS} -o ./bin/uptime-tracker ./cmd/uptime-tracker
${OPTS} go build ${BUILD_OPTS} -o ./bin/skywire-services ./cmd/skywire-services

# yarn --cwd ./pkg/node-visualizer/web build
# rm -rf ./pkg/node-visualizer/api/build/static
# mv ./pkg/node-visualizer/web/build/* ./pkg/node-visualizer/api/build
# ${OPTS} go build ${BUILD_OPTS} -o ./bin/node-visualizer ./cmd/node-visualizer

build-deploy: ## Build for deployment Docker images
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o ./release/address-resolver ./cmd/address-resolver
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o ./release/route-finder ./cmd/route-finder
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o ./release/setup-node ./cmd/setup-node
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o ./release/transport-discovery ./cmd/transport-discovery
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o ./release/network-monitor ./cmd/network-monitor
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o ./release/vpn-client ./cmd/vpn-lite-client
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o ./release/transport-setup ./cmd/transport-setup
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o ./release/node-visualizer ./cmd/node-visualizer
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o ./release/dmsg-monitor ./cmd/dmsg-monitor
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o ./release/tpd-monitor ./cmd/tpd-monitor
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o ./release/vpn-monitor ./cmd/vpn-monitor
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o /release/skysocks-monitor ./cmd/skysocks-monitor
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o /release/skysocks-client ./cmd/skysocks-lite-client
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o ./release/public-visor-monitor ./cmd/public-visor-monitor
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o /release/uptime-tracker ./cmd/uptime-tracker
${DOCKER_OPTS} go build ${BUILD_OPTS_DEPLOY} -mod=vendor -o ./release/skywire-services ./cmd/skywire-services

build-race: dep ## Build binaries
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/route-finder ./cmd/route-finder
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/setup-node ./cmd/setup-node
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/transport-discovery ./cmd/transport-discovery
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/address-resolver ./cmd/address-resolver
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/sw-env ./cmd/sw-env
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/keys-gen ./cmd/keys-gen
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/network-monitor ./cmd/network-monitor
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/vpn-client ./cmd/vpn-lite-client
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/transport-setup ./cmd/transport-setup
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/node-visualizer ./cmd/node-visualizer
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/dmsg-monitor ./cmd/dmsg-monitor
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/tpd-monitor ./cmd/tpd-monitor
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/vpn-monitor ./cmd/vpn-monitor
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/skysocks-monitor ./cmd/skysocks-monitor
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/skysocks-client ./cmd/skysocks-lite-client
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/public-visor-monitor ./cmd/public-visor-monitor
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/uptime-tracker ./cmd/uptime-tracker
${OPTS} go build ${BUILD_OPTS} -race -o ./bin/skywire-services ./cmd/skywire-services

install: ## Install route-finder, transport-discovery, address-resolver, sw-env, keys-gen, network-monitor, node-visualizer
${OPTS} go install ${BUILD_OPTS} \
./cmd/route-finder \
./cmd/transport-discovery \
./cmd/address-resolver \
./cmd/sw-env \
./cmd/keys-gen \
./cmd/network-monitor \
./cmd/uptime-tracker \
./cmd/node-visualizer
${OPTS} go install ${BUILD_OPTS} ./cmd/skywire-services

clean: ## Clean compiled binaries
rm -rf bin
Expand Down
147 changes: 145 additions & 2 deletions cmd/transport-setup/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package commands

import (
"encoding/json"
"fmt"
"log"
"net/http"
Expand All @@ -10,22 +11,51 @@ import (
"strings"
"time"

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

"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 +67,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 +114,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.Fatalf("-1 invalid public key: %v\n", err)
}
err = pk2.Set(toPK)
if err != nil {
log.Fatalf("-2 invalid public key: %v\n", 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 occurred: %v\n", err)
}
res, err := script.Echo(string(addtpJSON)).Post(tpsnAddr + "/add").String()
if err != nil {
log.Fatalf("Error occurred: %v\n", err)
}
if nice {
fmt.Printf("%v", string(pretty.Color(pretty.Pretty([]byte(res)), nil)))
} else {
fmt.Printf("%v", res)
}

},
}
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.Fatalf("invalid public key: %v\n", err)
}
tpid, err := uuid.Parse(tpID)
if err != nil {
log.Fatalf("invalid tp id: %v\n", err)
}
rmtp := api.UUIDRequest{
From: pk1,
ID: tpid,
}
rmtpJSON, err := json.Marshal(rmtp)
if err != nil {
log.Fatalf("Error occurred: %v\n", err)
}
res, err := script.Echo(string(rmtpJSON)).Post(tpsnAddr + "/remove").String()
if err != nil {
log.Fatalf("Error occurred: %v\n", err)
}
if nice {
fmt.Printf("%v", string(pretty.Color(pretty.Pretty([]byte(res)), nil)))
} else {
fmt.Printf("%v", 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("%v", string(pretty.Color(pretty.Pretty([]byte(res)), nil)))
} else {
fmt.Printf("%v", res)
}
},
}

// Execute executes root CLI command.
func Execute() {
if err := RootCmd.Execute(); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions cmd/uptime-tracker/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ import (
"github.com/skycoin/dmsg/pkg/direct"
"github.com/skycoin/dmsg/pkg/dmsg"
"github.com/skycoin/dmsg/pkg/dmsghttp"
"github.com/skycoin/skywire-services/internal/pg"
"github.com/skycoin/skywire-services/internal/utmetrics"
"github.com/skycoin/skywire-services/pkg/uptime-tracker/api"
"github.com/skycoin/skywire-services/pkg/uptime-tracker/store"
"github.com/spf13/cobra"
"gorm.io/gorm"

"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire-utilities/pkg/cmdutil"
Expand All @@ -28,6 +21,13 @@ import (
"github.com/skycoin/skywire-utilities/pkg/metricsutil"
"github.com/skycoin/skywire-utilities/pkg/storeconfig"
"github.com/skycoin/skywire-utilities/pkg/tcpproxy"
"github.com/spf13/cobra"
"gorm.io/gorm"

"github.com/skycoin/skywire-services/internal/pg"
"github.com/skycoin/skywire-services/internal/utmetrics"
"github.com/skycoin/skywire-services/pkg/uptime-tracker/api"
"github.com/skycoin/skywire-services/pkg/uptime-tracker/store"
)

const (
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
1 change: 0 additions & 1 deletion internal/skysocks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/hashicorp/yamux"
ipc "github.com/james-barrow/golang-ipc"

"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/router"
"github.com/skycoin/skywire/pkg/skyenv"
Expand Down
1 change: 0 additions & 1 deletion internal/skysocks/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"

ipc "github.com/james-barrow/golang-ipc"

"github.com/skycoin/skywire/pkg/skyenv"
)

Expand Down
6 changes: 3 additions & 3 deletions pkg/uptime-tracker/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import (
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/sirupsen/logrus"
"github.com/skycoin/skywire-services/internal/utmetrics"
"github.com/skycoin/skywire-services/pkg/uptime-tracker/store"

"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"github.com/skycoin/skywire-utilities/pkg/cipher"
"github.com/skycoin/skywire-utilities/pkg/geo"
Expand All @@ -32,6 +29,9 @@ import (
"github.com/skycoin/skywire-utilities/pkg/logging"
"github.com/skycoin/skywire-utilities/pkg/metricsutil"
"github.com/skycoin/skywire-utilities/pkg/netutil"

"github.com/skycoin/skywire-services/internal/utmetrics"
"github.com/skycoin/skywire-services/pkg/uptime-tracker/store"
)

const (
Expand Down
Loading

0 comments on commit 50ccbbf

Please sign in to comment.