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 1 commit
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
Prev Previous commit
Next Next commit
refactor: Rename forward to connect
  • Loading branch information
ersonp authored and ersonp committed Jun 17, 2024
commit 65a4123c559ea8d0f9f919520e0fd98a3db3d2d2
10 changes: 5 additions & 5 deletions cmd/skywire-cli/commands/net/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ var conCmd = &cobra.Command{
}

if lsPorts {
forwardConns, err := rpcClient.List()
connectConns, err := rpcClient.List()
internal.Catch(cmd.Flags(), err)

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

for _, forwardConn := range forwardConns {
_, err = fmt.Fprintf(w, "%s\t%s\t%s\n", forwardConn.ID, strconv.Itoa(int(forwardConn.LocalPort)),
strconv.Itoa(int(forwardConn.RemotePort)))
for _, connectConn := range connectConns {
_, err = fmt.Fprintf(w, "%s\t%s\t%s\n", connectConn.ID, strconv.Itoa(int(connectConn.LocalPort)),
strconv.Itoa(int(connectConn.RemotePort)))
internal.Catch(cmd.Flags(), err)
}
internal.Catch(cmd.Flags(), w.Flush())
internal.PrintOutput(cmd.Flags(), forwardConns, b.String())
internal.PrintOutput(cmd.Flags(), connectConns, b.String())
os.Exit(0)
}

Expand Down
62 changes: 31 additions & 31 deletions pkg/app/appnet/forwarding.go → pkg/app/appnet/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,42 @@ import (

// nolint: gochecknoglobals
var (
forwardConns = make(map[uuid.UUID]*ForwardConn)
forwardConnsMu sync.Mutex
connectConns = make(map[uuid.UUID]*ConnectConn)
connectConnsMu sync.Mutex
)

// AddForwarding adds ForwardConn to with it's ID
func AddForwarding(fwd *ForwardConn) {
forwardConnsMu.Lock()
defer forwardConnsMu.Unlock()
forwardConns[fwd.ID] = fwd
// AddConnect adds ConnectConn to with it's ID
func AddConnect(fwd *ConnectConn) {
connectConnsMu.Lock()
defer connectConnsMu.Unlock()
connectConns[fwd.ID] = fwd
}

// GetForwardConn get's a ForwardConn by ID
func GetForwardConn(id uuid.UUID) *ForwardConn {
forwardConnsMu.Lock()
defer forwardConnsMu.Unlock()
// GetConnectConn get's a ConnectConn by ID
func GetConnectConn(id uuid.UUID) *ConnectConn {
connectConnsMu.Lock()
defer connectConnsMu.Unlock()

return forwardConns[id]
return connectConns[id]
}

// GetAllForwardConns gets all ForwardConns
func GetAllForwardConns() map[uuid.UUID]*ForwardConn {
forwardConnsMu.Lock()
defer forwardConnsMu.Unlock()
// GetAllConnectConns gets all ConnectConns
func GetAllConnectConns() map[uuid.UUID]*ConnectConn {
connectConnsMu.Lock()
defer connectConnsMu.Unlock()

return forwardConns
return connectConns
}

// RemoveForwardConn removes a ForwardConn by ID
func RemoveForwardConn(id uuid.UUID) {
forwardConnsMu.Lock()
defer forwardConnsMu.Unlock()
delete(forwardConns, id)
// RemoveConnectConn removes a ConnectConn by ID
func RemoveConnectConn(id uuid.UUID) {
connectConnsMu.Lock()
defer connectConnsMu.Unlock()
delete(connectConns, id)
}

// ForwardConn ...
type ForwardConn struct {
// ConnectConn represents a connection that is published on the skywire network
type ConnectConn struct {
ID uuid.UUID
LocalPort int
RemotePort int
Expand All @@ -63,8 +63,8 @@ type ForwardConn struct {
log *logging.Logger
}

// NewForwardConn creates a new forwarding conn
func NewForwardConn(log *logging.Logger, remoteConn net.Conn, remotePort, localPort int) *ForwardConn {
// NewConnectConn creates a new ConnectConn
func NewConnectConn(log *logging.Logger, remoteConn net.Conn, remotePort, localPort int) *ConnectConn {
closeChan := make(chan struct{})
var once sync.Once
handler := http.NewServeMux()
Expand All @@ -78,7 +78,7 @@ func NewForwardConn(log *logging.Logger, remoteConn net.Conn, remotePort, localP
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
fwdConn := &ForwardConn{
fwdConn := &ConnectConn{
ID: uuid.New(),
remoteConn: remoteConn,
srv: srv,
Expand All @@ -87,12 +87,12 @@ func NewForwardConn(log *logging.Logger, remoteConn net.Conn, remotePort, localP
closeChan: closeChan,
log: log,
}
AddForwarding(fwdConn)
AddConnect(fwdConn)
return fwdConn
}

// Serve serves a HTTP forward conn that accepts all requests and forwards them directly to the remote server over the specified net.Conn.
func (f *ForwardConn) Serve() {
func (f *ConnectConn) Serve() {
go func() {
err := f.srv.ListenAndServe()
if err != nil {
Expand All @@ -113,11 +113,11 @@ func (f *ForwardConn) Serve() {
}

// Close closes the server and remote connection.
func (f *ForwardConn) Close() (err error) {
func (f *ConnectConn) Close() (err error) {
f.closeOnce.Do(func() {
err = f.srv.Close()
err = f.remoteConn.Close()
RemoveForwardConn(f.ID)
RemoveConnectConn(f.ID)
})
return err
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/visor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ type API interface {
ListHTTPPorts() ([]int, error)
Connect(remotePK cipher.PubKey, remotePort, localPort int) (uuid.UUID, error)
Disconnect(id uuid.UUID) error
List() (map[uuid.UUID]*appnet.ForwardConn, error)
List() (map[uuid.UUID]*appnet.ConnectConn, error)
DialPing(config PingConfig) error
Ping(config PingConfig) ([]time.Duration, error)
StopPing(pk cipher.PubKey) error
Expand Down Expand Up @@ -1631,20 +1631,20 @@ func (v *Visor) Connect(remotePK cipher.PubKey, remotePort, localPort int) (uuid
v.log.WithError(fmt.Errorf(*sErr)).Error("Server closed with error")
return uuid.UUID{}, fmt.Errorf(*sErr)
}
forwardConn := appnet.NewForwardConn(v.log, remoteConn, remotePort, localPort)
forwardConn.Serve()
return forwardConn.ID, nil
connectConn := appnet.NewConnectConn(v.log, remoteConn, remotePort, localPort)
connectConn.Serve()
return connectConn.ID, nil
}

// Disconnect implements API.
func (v *Visor) Disconnect(id uuid.UUID) error {
forwardConn := appnet.GetForwardConn(id)
return forwardConn.Close()
connectConn := appnet.GetConnectConn(id)
return connectConn.Close()
}

// List implements API.
func (v *Visor) List() (map[uuid.UUID]*appnet.ForwardConn, error) {
return appnet.GetAllForwardConns(), nil
func (v *Visor) List() (map[uuid.UUID]*appnet.ConnectConn, error) {
return appnet.GetAllConnectConns(), nil
}

func isPortAvailable(log *logging.Logger, port int) bool {
Expand Down
2 changes: 1 addition & 1 deletion pkg/visor/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ func (r *RPC) Disconnect(id *uuid.UUID, _ *struct{}) (err error) {
}

// List returns all the ongoing skyforwarding connections
func (r *RPC) List(_ *struct{}, out *map[uuid.UUID]*appnet.ForwardConn) (err error) {
func (r *RPC) List(_ *struct{}, out *map[uuid.UUID]*appnet.ConnectConn) (err error) {
defer rpcutil.LogCall(r.log, "List", nil)(out, &err)
proxies, err := r.visor.List()
*out = proxies
Expand Down
6 changes: 3 additions & 3 deletions pkg/visor/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,8 @@ func (rc *rpcClient) Disconnect(id uuid.UUID) error {
}

// List calls List.
func (rc *rpcClient) List() (map[uuid.UUID]*appnet.ForwardConn, error) {
var out map[uuid.UUID]*appnet.ForwardConn
func (rc *rpcClient) List() (map[uuid.UUID]*appnet.ConnectConn, error) {
var out map[uuid.UUID]*appnet.ConnectConn
err := rc.Call("List", &struct{}{}, &out)
return out, err
}
Expand Down Expand Up @@ -1321,7 +1321,7 @@ func (mc *mockRPCClient) Disconnect(id uuid.UUID) error { //nolint:all
}

// List implements API.
func (mc *mockRPCClient) List() (map[uuid.UUID]*appnet.ForwardConn, error) {
func (mc *mockRPCClient) List() (map[uuid.UUID]*appnet.ConnectConn, error) {
return nil, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/visor/visor.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func (v *Visor) Close() error {
log.Info("Begin shutdown.")

// Cleanly close ongoing forward conns
for _, forwardConn := range appnet.GetAllForwardConns() {
for _, forwardConn := range appnet.GetAllConnectConns() {
err := forwardConn.Close()
if err != nil {
log.WithError(err).Warn("Forward conn stopped with unexpected result.")
Expand Down