Skip to content

Commit

Permalink
refactor: Rename forward to connect
Browse files Browse the repository at this point in the history
  • Loading branch information
ersonp authored and ersonp committed Jun 17, 2024
1 parent 70378f3 commit 65a4123
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 49 deletions.
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

0 comments on commit 65a4123

Please sign in to comment.