From 65a4123c559ea8d0f9f919520e0fd98a3db3d2d2 Mon Sep 17 00:00:00 2001 From: ersonp Date: Sat, 15 Jun 2024 15:54:49 +0530 Subject: [PATCH] refactor: Rename `forward` to `connect` --- cmd/skywire-cli/commands/net/connect.go | 10 ++-- pkg/app/appnet/{forwarding.go => connect.go} | 62 ++++++++++---------- pkg/visor/api.go | 16 ++--- pkg/visor/rpc.go | 2 +- pkg/visor/rpc_client.go | 6 +- pkg/visor/visor.go | 2 +- 6 files changed, 49 insertions(+), 49 deletions(-) rename pkg/app/appnet/{forwarding.go => connect.go} (72%) diff --git a/cmd/skywire-cli/commands/net/connect.go b/cmd/skywire-cli/commands/net/connect.go index 58a74e6670..6031496f57 100644 --- a/cmd/skywire-cli/commands/net/connect.go +++ b/cmd/skywire-cli/commands/net/connect.go @@ -47,7 +47,7 @@ var conCmd = &cobra.Command{ } if lsPorts { - forwardConns, err := rpcClient.List() + connectConns, err := rpcClient.List() internal.Catch(cmd.Flags(), err) var b bytes.Buffer @@ -55,13 +55,13 @@ var conCmd = &cobra.Command{ _, 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) } diff --git a/pkg/app/appnet/forwarding.go b/pkg/app/appnet/connect.go similarity index 72% rename from pkg/app/appnet/forwarding.go rename to pkg/app/appnet/connect.go index fdc9d48d83..da28e4f8b4 100644 --- a/pkg/app/appnet/forwarding.go +++ b/pkg/app/appnet/connect.go @@ -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 @@ -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() @@ -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, @@ -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 { @@ -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 } diff --git a/pkg/visor/api.go b/pkg/visor/api.go index 8e63206da8..b57dfeaa02 100644 --- a/pkg/visor/api.go +++ b/pkg/visor/api.go @@ -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 @@ -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 { diff --git a/pkg/visor/rpc.go b/pkg/visor/rpc.go index 2b713bf8a6..8d03a9f899 100644 --- a/pkg/visor/rpc.go +++ b/pkg/visor/rpc.go @@ -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 diff --git a/pkg/visor/rpc_client.go b/pkg/visor/rpc_client.go index ab8381a6b2..4b9dffda8b 100644 --- a/pkg/visor/rpc_client.go +++ b/pkg/visor/rpc_client.go @@ -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 } @@ -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 } diff --git a/pkg/visor/visor.go b/pkg/visor/visor.go index 4fb66884a6..63c21fc6a7 100644 --- a/pkg/visor/visor.go +++ b/pkg/visor/visor.go @@ -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.")