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

Refactor/sky rev fwd #1830

Draft
wants to merge 25 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
70378f3
refactor: Rename `skyrev` and `skyfwd` to `net`
Jun 15, 2024
65a4123
refactor: Rename `forward` to `connect`
Jun 15, 2024
8ee38fa
feat: Add `publish` and update `connect`
Jun 17, 2024
102802f
fix(publish): Remove ReadTimeout
ersonp Jun 22, 2024
47672e0
fix(connect): Add sync.Mutex to handleConnectFunc
ersonp Jun 22, 2024
c5681da
refactor: Remove `RegisterHTTPPort` and `DeregisterHTTPPort` from API…
ersonp Jun 22, 2024
09b1532
chore(visor/api): Fix go-staticcheck warnings
ersonp Jun 22, 2024
a575224
chore: Update .gitignore to ignore skywire-config.json and local dire…
ersonp Jun 22, 2024
5eecb1a
refactor: Convert remaning skyfwd code to `net`
ersonp Jun 22, 2024
27b864e
refactor: Rename and change `ListHTTPPorts` and `ListConnected`
ersonp Jun 22, 2024
2a8cee5
refactor: Update ConnectConn to use http.Server for serving
ersonp Jun 22, 2024
87e6748
chore: Update comment
ersonp Jun 22, 2024
d73b400
refactor: Update `connect` command to print connection details in JSO…
ersonp Jun 22, 2024
dcd7b58
feat: add app type support
ersonp Jun 24, 2024
870c6ee
feat: ensure port availability
ersonp Jun 25, 2024
cbbc403
refactor: update APIs
ersonp Jun 25, 2024
4e69512
refactor: add subcommands to con and pub
ersonp Jun 25, 2024
b9b97c5
fix(publish): remove http.Server headers
ersonp Jun 25, 2024
7205554
chore: fix linting issues
ersonp Jun 26, 2024
b2f97a9
chore: fix linting
ersonp Jun 26, 2024
1daefdd
fix(publish): impliment proper usage of LocalPort
ersonp Jun 26, 2024
02e630c
refactor: Update `connect` and `publish` commands
ersonp Jun 26, 2024
42e44ad
refactor: Update `publish.go` to address Slowloris attack vector
ersonp Jun 26, 2024
18b96db
chore: fix linting
ersonp Jun 26, 2024
3ab96d9
chore: fix linting
ersonp Jun 28, 2024
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
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
*.out
.DS_Store
*.pem
*.db

.idea/
.vscode/

./skywire-config.json
./skywire.json
./build/
build/
./apps/
./skywire/
./local/*
./local*/*
local*/
./transport_logs
./dmsgpty
./rewards
Expand All @@ -26,16 +27,16 @@ rewards
./pkg/visor/apps/
./pkg/visor/bar/
./pkg/visor/foo/
./skywire-config.json

./bin
./node
./users.db
./hypervisor
./*-node
./*-visor
./*-cli
./*-server
./*.json
*.json
!/dmsghttp-config.json
!/services-config.json
./*.sh
Expand Down
157 changes: 157 additions & 0 deletions cmd/skywire-cli/commands/net/connect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Package net cmd/skywire-cli/commands/net/connect.go
package net

import (
"bytes"
"fmt"
"os"
"strconv"
"strings"
"text/tabwriter"

"github.com/google/uuid"
"github.com/spf13/cobra"

"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/app/appnet"
)

var (
localPort int
)

func init() {
conCmd.Flags().IntVarP(&localPort, "port", "p", 0, "local port to serve the remote app on")
conCmd.Flags().StringVarP(&netType, "type", "t", "http", "type of the remote app connection (http, tcp, udp)")

conCmd.AddCommand(lsCmd)
conCmd.AddCommand(stopCmd)
RootCmd.AddCommand(conCmd)
}

// conCmd contains commands to connect to a published app on the skywire network
var conCmd = &cobra.Command{
Use: "con <remote_pk:remote_port> [flags]",
Short: "Connect to a published app on the skywire network",
Long: "Connect to a published app on the skywire network.\n This will allow you to access the remote app via the skywire network.",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {

rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}

if len(args) == 0 {
cmd.Help() //nolint
os.Exit(0)
}

var remotePK cipher.PubKey
var remotePort int

parts := strings.Split(args[0], ":")

if len(parts) != 2 {
cmd.Help() //nolint
os.Exit(0)
}

internal.Catch(cmd.Flags(), remotePK.Set(parts[0]))

if remotePort, err = strconv.Atoi(parts[1]); err != nil {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("invalid port: %s", parts[1]))
}

if remotePort == 0 || localPort == 0 {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("port cannot be 0"))
}
//65535 is the highest TCP port number
if 65536 < remotePort || 65536 < localPort {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("port cannot be greater than 65535"))
}

var appType appnet.AppType

switch netType {
case "http":
appType = appnet.HTTP
case "tcp":
appType = appnet.TCP
case "udp":
appType = appnet.UDP
default:
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("invalid type"))
}

connInfo, err := rpcClient.Connect(remotePK, remotePort, localPort, appType)
internal.Catch(cmd.Flags(), err)

internal.PrintOutput(cmd.Flags(), connInfo, fmt.Sprintf("Connected to %s with ID: %s\n", connInfo.RemoteAddr.String(), connInfo.ID.String()))
internal.PrintOutput(cmd.Flags(), connInfo, fmt.Sprintf("%v available on localhost:%d\n", connInfo.AppType, connInfo.WebPort))
},
}

// lsCmd contains commands to list connected apps on the skywire network
var lsCmd = &cobra.Command{
Use: "ls",
Short: "List connected apps on the skywire network",
Long: "List connected apps on the skywire network.\nThis will show you the ID, address, and web port of the connected apps.",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {

if len(args) != 0 {
cmd.Help() //nolint
os.Exit(0)
}

rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}

connectConns, err := rpcClient.ListConnected()
internal.Catch(cmd.Flags(), err)

var b bytes.Buffer
w := tabwriter.NewWriter(&b, 0, 0, 3, ' ', tabwriter.TabIndent)
_, err = fmt.Fprintln(w, "id\taddr\tweb_port\tapp_type")
internal.Catch(cmd.Flags(), err)

for _, connectConn := range connectConns {
_, err = fmt.Fprintf(w, "%v\t%v\t%v\t%v\n", connectConn.ID, connectConn.RemoteAddr,
connectConn.WebPort, connectConn.AppType)
internal.Catch(cmd.Flags(), err)
}
internal.Catch(cmd.Flags(), w.Flush())
internal.PrintOutput(cmd.Flags(), connectConns, b.String())
},
}

// stopCmd contains commands to stop a connection to a published app on the skywire network
var stopCmd = &cobra.Command{
Use: "stop <id>",
Short: "Stop a connection to a published app on the skywire network",
Long: "Stop a connection to a published app on the skywire network.\nThis will disconnect you from the remote app.",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {

if len(args) == 0 {
cmd.Help() //nolint
os.Exit(0)
}

rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}

id, err := uuid.Parse(args[0])
internal.Catch(cmd.Flags(), err)
err = rpcClient.Disconnect(id)
internal.Catch(cmd.Flags(), err)
internal.PrintOutput(cmd.Flags(), "OK", "OK\n")
},
}
149 changes: 149 additions & 0 deletions cmd/skywire-cli/commands/net/publish.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Package net cmd/skywire-cli/commands/net/publish.go
package net

import (
"bytes"
"fmt"
"os"
"text/tabwriter"

"github.com/google/uuid"
"github.com/spf13/cobra"

clirpc "github.com/skycoin/skywire/cmd/skywire-cli/commands/rpc"
"github.com/skycoin/skywire/cmd/skywire-cli/internal"
"github.com/skycoin/skywire/pkg/app/appnet"
)

var (
netType string
skyPort int
)

func init() {
pubCmd.PersistentFlags().IntVarP(&localPort, "port", "p", 0, "local port of the external (http, tcp, udp) app")
pubCmd.PersistentFlags().IntVarP(&skyPort, "skyport", "s", localPort, "skywire port for the external (http, tcp, udp) app")
pubCmd.PersistentFlags().StringVarP(&netType, "type", "t", "http", "type of the external app connection (http, tcp, udp)")

pubCmd.AddCommand(lsPubCmd)
pubCmd.AddCommand(stopPubCmd)
RootCmd.AddCommand(pubCmd)
}

// pubCmd contains commands to publish over the skywire network
var pubCmd = &cobra.Command{
Use: "pub [flags]",
Short: "Publish over skywire network",
Long: "Publish over skywire network\nPublish a local port over the skywire network. This will allow other nodes to access the local port via the skywire network.",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, _ []string) {

rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}

if skyPort == 0 {
skyPort = localPort
}

if localPort == 0 && skyPort == 0 {
cmd.Help() //nolint
os.Exit(0)
}

//port 0 is reserved / not usable
if localPort == 0 {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("port cannot be 0"))
}

//skyPort 0 is reserved / not usable
if skyPort == 0 {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("skyPort cannot be 0"))
}

//65535 is the highest TCP port number
if 65536 < localPort {
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("port cannot be greater than 65535"))
}

var appType appnet.AppType

switch netType {
case "http":
appType = appnet.HTTP
case "tcp":
appType = appnet.TCP
case "udp":
appType = appnet.UDP
default:
internal.PrintFatalError(cmd.Flags(), fmt.Errorf("invalid type"))
}

internal.Catch(cmd.Flags(), err)
pubInfo, err := rpcClient.Publish(localPort, skyPort, appType)
internal.Catch(cmd.Flags(), err)

internal.PrintOutput(cmd.Flags(), pubInfo, fmt.Sprintf("Published on %s with ID: %s\n", pubInfo.SkyAddr.String(), pubInfo.ID.String()))

},
}

// lsPubCmd lists all the publised apps on the skywire network by the visor
var lsPubCmd = &cobra.Command{
Use: "ls",
Short: "List published apps on the skywire network by the visor",
Long: "List published apps on the skywire network by the visor\nThe list contains the id and the local port of the published app.",
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {

if len(args) != 0 {
cmd.Help() //nolint
os.Exit(0)
}

rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}

liss, err := rpcClient.ListPublished()
internal.Catch(cmd.Flags(), err)
var b bytes.Buffer
w := tabwriter.NewWriter(&b, 0, 0, 2, ' ', tabwriter.TabIndent)
_, err = fmt.Fprintln(w, "id\tsky_port\tlocal_port\tapp_type")
internal.Catch(cmd.Flags(), err)
for _, lis := range liss {
_, err = fmt.Fprintf(w, "%v\t%v\t%v\t%v\n", lis.ID, lis.SkyAddr.GetPort(), lis.LocalPort, lis.AppType)
internal.Catch(cmd.Flags(), err)
}
internal.Catch(cmd.Flags(), w.Flush())
internal.PrintOutput(cmd.Flags(), liss, b.String())
},
}

// stopPubCmd stops a published app on the skywire network published by the visor
var stopPubCmd = &cobra.Command{
Use: "stop <id>",
Short: "Stop a published app on the skywire network by the visor",
Long: "Stop a published app on the skywire network by the visor.\nThis will stop the published app and remove it from the skywire network.",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {

if len(args) == 0 {
cmd.Help() //nolint
os.Exit(0)
}

rpcClient, err := clirpc.Client(cmd.Flags())
if err != nil {
os.Exit(1)
}

id, err := uuid.Parse(args[0])
internal.Catch(cmd.Flags(), err)
err = rpcClient.Depublish(id)
internal.Catch(cmd.Flags(), err)
internal.PrintOutput(cmd.Flags(), "OK", "OK\n")
},
}
18 changes: 18 additions & 0 deletions cmd/skywire-cli/commands/net/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Package net cmd/skywire-cli/commands/net/root.go
package net

import (
"github.com/spf13/cobra"

clirpc "github.com/skycoin/skywire/cmd/skywire-cli/commands/rpc"
)

func init() {
RootCmd.PersistentFlags().StringVar(&clirpc.Addr, "rpc", "localhost:3435", "RPC server address")
}

// RootCmd contains commands that interact with the skywire network
var RootCmd = &cobra.Command{
Use: "net",
Short: "Publish and connect to skywire network",
}
7 changes: 3 additions & 4 deletions cmd/skywire-cli/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
clireward "github.com/skycoin/skywire/cmd/skywire-cli/commands/reward"
clirewards "github.com/skycoin/skywire/cmd/skywire-cli/commands/rewards"
cliroute "github.com/skycoin/skywire/cmd/skywire-cli/commands/route"
cliskyfwd "github.com/skycoin/skywire/cmd/skywire-cli/commands/skyfwd"
cliskyrev "github.com/skycoin/skywire/cmd/skywire-cli/commands/skyrev"

clinet "github.com/skycoin/skywire/cmd/skywire-cli/commands/net"
clisurvey "github.com/skycoin/skywire/cmd/skywire-cli/commands/survey"
clitp "github.com/skycoin/skywire/cmd/skywire-cli/commands/tp"
cliut "github.com/skycoin/skywire/cmd/skywire-cli/commands/ut"
Expand All @@ -40,8 +40,7 @@ func init() {
clivisor.RootCmd,
clivpn.RootCmd,
cliut.RootCmd,
cliskyfwd.RootCmd,
cliskyrev.RootCmd,
clinet.RootCmd,
clireward.RootCmd,
clirewards.RootCmd,
clisurvey.RootCmd,
Expand Down
Loading
Loading