Skip to content

Commit

Permalink
[WIP] App/Services showing ports subcommand skywire-cli visor ports (
Browse files Browse the repository at this point in the history
…#1412)

* initial commit

* fetch all ports from skyenv/config/running service

* add hypervisor port

* commit WIP - switching machines

* use rpc to get the hypervisor port and replace hardcoded references to the default port

* a little change on skysocks

* define method GetAppPort and SetAppPort for apps initialization

* add SetAppPort to apps

* improve command output by adding Type column/field

* Improve rpcClient.Ports response

* add type field to some of ports

* improve Ports() data

* remove useless services that read from skyenv

* improve output by add some notes

* add port type to dmsgpty

* add cli and proc port to list, read from config

Co-authored-by: Moses Narrow <[email protected]>
  • Loading branch information
mrpalide and 0pcom authored Dec 8, 2022
1 parent b68aa4d commit ae552ce
Show file tree
Hide file tree
Showing 21 changed files with 279 additions and 22 deletions.
8 changes: 8 additions & 0 deletions cmd/apps/skychat/skychat.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func listenLoop() {
return
}

setAppPort(appCl, port)

for {
fmt.Println("Accepting skychat conn...")
conn, err := l.Accept()
Expand Down Expand Up @@ -280,3 +282,9 @@ func setAppError(appCl *app.Client, appErr error) {
print(fmt.Sprintf("Failed to set error %v: %v\n", appErr, err))
}
}

func setAppPort(appCl *app.Client, port routing.Port) {
if err := appCl.SetAppPort(port); err != nil {
print(fmt.Sprintf("Failed to set port %v: %v\n", port, err))
}
}
7 changes: 7 additions & 0 deletions cmd/apps/skysocks-client/skysocks-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func main() {
os.Exit(1)
}
defer setAppStatus(appCl, appserver.AppDetailedStatusStopped)
setAppPort(appCl, socksPort)
for {
conn, err := dialServer(ctx, appCl, pk)
if err != nil {
Expand Down Expand Up @@ -123,3 +124,9 @@ func setAppStatus(appCl *app.Client, status appserver.AppDetailedStatus) {
print(fmt.Sprintf("Failed to set status %v: %v\n", status, err))
}
}

func setAppPort(appCl *app.Client, port routing.Port) {
if err := appCl.SetAppPort(port); err != nil {
print(fmt.Sprintf("Failed to set port %v: %v\n", port, err))
}
}
12 changes: 10 additions & 2 deletions cmd/apps/skysocks/skysocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
)

const (
netType = appnet.TypeSkynet
port routing.Port = 3
netType = appnet.TypeSkynet
port = routing.Port(3)
)

func main() {
Expand Down Expand Up @@ -52,6 +52,8 @@ func main() {
os.Exit(1)
}

setAppPort(appCl, port)

fmt.Println("Starting serving proxy server")

if runtime.GOOS == "windows" {
Expand Down Expand Up @@ -94,3 +96,9 @@ func setAppError(appCl *app.Client, appErr error) {
print(fmt.Sprintf("Failed to set error %v: %v\n", appErr, err))
}
}

func setAppPort(appCl *app.Client, port routing.Port) {
if err := appCl.SetAppPort(port); err != nil {
print(fmt.Sprintf("Failed to set port %v: %v\n", port, err))
}
}
8 changes: 8 additions & 0 deletions cmd/apps/vpn-client/vpn-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/skycoin/skywire/pkg/app"
"github.com/skycoin/skywire/pkg/app/appevent"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/skycoin/skywire/pkg/routing"
"github.com/skycoin/skywire/pkg/skyenv"
)

Expand Down Expand Up @@ -106,6 +107,7 @@ func main() {
}
}

setAppPort(appCl, appCl.Config().RoutingPort)
fmt.Printf("Connecting to VPN server %s\n", serverPK.String())

vpnClientCfg := vpn.ClientConfig{
Expand Down Expand Up @@ -190,3 +192,9 @@ func setAppStatus(appCl *app.Client, status appserver.AppDetailedStatus) {
print(fmt.Sprintf("Failed to set status %v: %v\n", status, err))
}
}

func setAppPort(appCl *app.Client, port routing.Port) {
if err := appCl.SetAppPort(port); err != nil {
print(fmt.Sprintf("Failed to set port %v: %v\n", port, err))
}
}
8 changes: 7 additions & 1 deletion cmd/apps/vpn-server/vpn-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func main() {
setAppErr(appCl, err)
os.Exit(1)
}

setAppPort(appCl, vpnPort)
fmt.Printf("Got app listener, bound to %d\n", vpnPort)

srvCfg := vpn.ServerConfig{
Expand Down Expand Up @@ -131,3 +131,9 @@ func setAppStatus(appCl *app.Client, status appserver.AppDetailedStatus) {
print(fmt.Sprintf("Failed to set status %v: %v\n", status, err))
}
}

func setAppPort(appCl *app.Client, port routing.Port) {
if err := appCl.SetAppPort(port); err != nil {
print(fmt.Sprintf("Failed to set port %v: %v\n", port, err))
}
}
29 changes: 20 additions & 9 deletions cmd/skywire-cli/commands/visor/hv.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/toqueteos/webbrowser"

"github.com/skycoin/skywire-utilities/pkg/cipher"
clirpc "github.com/skycoin/skywire/cmd/skywire-cli/commands/rpc"
"github.com/skycoin/skywire/cmd/skywire-cli/internal"
"github.com/skycoin/skywire/pkg/visor/hypervisorconfig"
"github.com/skycoin/skywire/pkg/visor/visorconfig"
)

Expand All @@ -22,7 +24,6 @@ func init() {
hvpkCmd.Flags().BoolVarP(&pkg, "pkg", "p", false, "read from /opt/skywire/skywire.json")
hvpkCmd.Flags().BoolVarP(&web, "http", "w", false, "serve public key via http")
hvCmd.AddCommand(chvpkCmd)

}

var hvCmd = &cobra.Command{
Expand All @@ -35,9 +36,8 @@ var hvuiCmd = &cobra.Command{
Use: "ui",
Short: "open Hypervisor UI in default browser",
Long: "\n open Hypervisor UI in default browser",
Run: func(_ *cobra.Command, _ []string) {
//TODO: get the actual port from config instead of using default value here
if err := webbrowser.Open("http://127.0.0.1:8000/"); err != nil {
Run: func(cmd *cobra.Command, _ []string) {
if err := webbrowser.Open(fmt.Sprintf("http://127.0.0.1%s/", HypervisorPort(cmd.Flags()))); err != nil {
logger.Fatal("Failed to open hypervisor UI in browser:", err)
}
},
Expand All @@ -49,11 +49,9 @@ var hvpkCmd = &cobra.Command{
Long: "\n Public key of remote hypervisor(s) set in config",
Run: func(cmd *cobra.Command, _ []string) {
var hypervisors []cipher.PubKey

if pkg {
path = visorconfig.Pkgpath
}

if path != "" {
conf, err := visorconfig.ReadFile(path)
if err != nil {
Expand All @@ -67,7 +65,7 @@ var hvpkCmd = &cobra.Command{
}
overview, err := rpcClient.Overview()
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
internal.PrintFatalRPCError(cmd.Flags(), err)
}
hypervisors = overview.Hypervisors
}
Expand All @@ -82,12 +80,25 @@ var chvpkCmd = &cobra.Command{
Run: func(cmd *cobra.Command, _ []string) {
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
internal.PrintFatalRPCError(cmd.Flags(), err)
}
overview, err := rpcClient.Overview()
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
internal.PrintFatalRPCError(cmd.Flags(), err)
}
internal.PrintOutput(cmd.Flags(), overview.ConnectedHypervisor, fmt.Sprintf("%v\n", overview.ConnectedHypervisor))
},
}

// HypervisorPort returns the port of the hypervisor; either from the running visor or the default value
func HypervisorPort(cmdFlags *pflag.FlagSet) string {
rpcClient, err := clirpc.Client(cmdFlags)
if err != nil {
return hypervisorconfig.HTTPAddr()
}
ports, err := rpcClient.Ports()
if err != nil {
return hypervisorconfig.HTTPAddr()
}
return fmt.Sprintf(":%s", ports["hypervisor"])
}
43 changes: 40 additions & 3 deletions cmd/skywire-cli/commands/visor/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"sort"
"strings"

"github.com/spf13/cobra"
Expand All @@ -28,6 +29,7 @@ func init() {
pkCmd.Flags().StringVarP(&webPort, "prt", "x", "7998", "serve public key via http")
RootCmd.AddCommand(summaryCmd)
RootCmd.AddCommand(buildInfoCmd)
RootCmd.AddCommand(portsCmd)
}

var pkCmd = &cobra.Command{
Expand All @@ -52,7 +54,7 @@ var pkCmd = &cobra.Command{
}
overview, err := rpcClient.Overview()
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
internal.PrintFatalRPCError(cmd.Flags(), err)
}
pk = overview.PubKey.String() + "\n"
if web {
Expand All @@ -77,7 +79,7 @@ var summaryCmd = &cobra.Command{
}
summary, err := rpcClient.Summary()
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
internal.PrintFatalRPCError(cmd.Flags(), err)
}
msg := fmt.Sprintf(".:: Visor Summary ::.\nPublic key: %q\nSymmetric NAT: %t\nIP: %s\nDMSG Server: %q\nPing: %q\nVisor Version: %s\nSkybian Version: %s\nUptime Tracker: %s\nTime Online: %f seconds\nBuild Tag: %s\n",
summary.Overview.PubKey, summary.Overview.IsSymmetricNAT, summary.Overview.LocalIP, summary.DmsgStats.ServerPK, summary.DmsgStats.RoundTrip, summary.Overview.BuildInfo.Version, summary.SkybianBuildVersion,
Expand Down Expand Up @@ -121,14 +123,49 @@ var buildInfoCmd = &cobra.Command{
}
overview, err := rpcClient.Overview()
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect: %v", err))
internal.PrintFatalRPCError(cmd.Flags(), err)
}
buildInfo := overview.BuildInfo
msg := fmt.Sprintf("Version %q built on %q against commit %q\n", buildInfo.Version, buildInfo.Date, buildInfo.Commit)
internal.PrintOutput(cmd.Flags(), buildInfo, msg)
},
}

var portsCmd = &cobra.Command{
Use: "ports",
Short: "List of Ports",
Long: "\n List of all ports used by visor services and apps",
Run: func(cmd *cobra.Command, _ []string) {
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
ports, err := rpcClient.Ports()
if err != nil {
internal.PrintFatalRPCError(cmd.Flags(), err)
}
msg := "+-------------------------------------------+\n"
msg += fmt.Sprintf("| %-21s | %-7s | %-7s |\n", "App/Service", "Type", "Port")
msg += "|-------------------------------------------|\n"

portsName := make([]string, 0, len(ports))
for portName := range ports {
portsName = append(portsName, portName)
}
sort.Strings(portsName)

for _, portName := range portsName {
msg += fmt.Sprintf("| %-21s | %-7s | %7s |\n", portName, ports[portName].Type, ports[portName].Port)
}

msg += "|===========================================|\n"
msg += "| SKYNET: connection between apps and visor |\n"
msg += "| DMSG: connection by dmsg service |\n"
msg += "+-------------------------------------------+\n"
internal.PrintOutput(cmd.Flags(), ports, msg)
},
}

func srvpk(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintf(w, pk) //nolint
}
15 changes: 8 additions & 7 deletions cmd/skywire-cli/commands/vpn/vvpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/skycoin/skywire-utilities/pkg/buildinfo"
"github.com/skycoin/skywire-utilities/pkg/cipher"
clirpc "github.com/skycoin/skywire/cmd/skywire-cli/commands/rpc"
clivisor "github.com/skycoin/skywire/cmd/skywire-cli/commands/visor"
"github.com/skycoin/skywire/cmd/skywire-cli/internal"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/skycoin/skywire/pkg/visor"
Expand Down Expand Up @@ -58,7 +59,7 @@ var vpnUICmd = &cobra.Command{
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to read in config: %v", err))
}
url = fmt.Sprintf("http://127.0.0.1:8000/#/vpn/%s/", conf.PK.Hex())
url = fmt.Sprintf("http://127.0.0.1%s/#/vpn/%s/", clivisor.HypervisorPort(cmd.Flags()), conf.PK.Hex())
} else {
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
Expand All @@ -68,7 +69,7 @@ var vpnUICmd = &cobra.Command{
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect; is skywire running?: %v", err))
}
url = fmt.Sprintf("http://127.0.0.1:8000/#/vpn/%s/", overview.PubKey.Hex())
url = fmt.Sprintf("http://127.0.0.1%s/#/vpn/%s/", clivisor.HypervisorPort(cmd.Flags()), overview.PubKey.Hex())
}
if err := webbrowser.Open(url); err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to open VPN UI in browser:: %v", err))
Expand All @@ -89,17 +90,17 @@ var vpnURLCmd = &cobra.Command{
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to read in config: %v", err))
}
url = fmt.Sprintf("http://127.0.0.1:8000/#/vpn/%s/", conf.PK.Hex())
url = fmt.Sprintf("http://127.0.0.1%s/#/vpn/%s/", clivisor.HypervisorPort(cmd.Flags()), conf.PK.Hex())
} else {
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}
overview, err := rpcClient.Overview()
if err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("Failed to connect; is skywire running?: %v", err))
internal.PrintFatalRPCError(cmd.Flags(), err)
}
url = fmt.Sprintf("http://127.0.0.1:8000/#/vpn/%s/", overview.PubKey.Hex())
url = fmt.Sprintf("http://127.0.0.1%s/#/vpn/%s/", clivisor.HypervisorPort(cmd.Flags()), overview.PubKey.Hex())
}

output := struct {
Expand All @@ -118,15 +119,15 @@ var vpnListCmd = &cobra.Command{
Run: func(cmd *cobra.Command, _ []string) {
rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
internal.PrintFatalRPCError(cmd.Flags(), err)
}
if isUnFiltered {
ver = ""
country = ""
}
servers, err := rpcClient.VPNServers(ver, country)
if err != nil {
internal.PrintFatalError(cmd.Flags(), err)
internal.PrintFatalRPCError(cmd.Flags(), err)
}
if len(servers) == 0 {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("No VPN Servers found"))
Expand Down
5 changes: 5 additions & 0 deletions cmd/skywire-cli/internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func PrintFatalError(cmdFlags *pflag.FlagSet, err error) {
log.Fatal(err)
}

// PrintFatalRPCError prints errors for skywire-cli commands packages
func PrintFatalRPCError(cmdFlags *pflag.FlagSet, err error) {
PrintFatalError(cmdFlags, fmt.Errorf("Failed to connect; is skywire running?: %v", err))
}

// PrintError prints errors for skywire-cli commands packages
func PrintError(cmdFlags *pflag.FlagSet, err error) {
isJSON, _ := cmdFlags.GetBool(JSONString) //nolint:errcheck
Expand Down
15 changes: 15 additions & 0 deletions pkg/app/appserver/mock_rpc_ingress_client.go

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

Loading

0 comments on commit ae552ce

Please sign in to comment.