Skip to content

Commit 73a83f6

Browse files
committed
refactor(pkg,api,agent): use param as reverse connection path
1 parent 9c21a02 commit 73a83f6

File tree

9 files changed

+28
-28
lines changed

9 files changed

+28
-28
lines changed

pkg/agent/agent.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ func (a *Agent) Listen(ctx context.Context) error {
492492
"{sshEndpoint}", strings.Split(sshEndpoint, ":")[0],
493493
).Replace("{namespace}.{tenantName}@{sshEndpoint}")
494494

495-
listener, err := a.cli.NewReverseListener(ctx, a.authData.Token)
495+
listener, err := a.cli.NewReverseListener(ctx, a.authData.Token, "/ssh/connection")
496496
if err != nil {
497497
log.WithError(err).WithFields(log.Fields{
498498
"version": AgentVersion,

pkg/api/client/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type publicAPI interface {
2525
Endpoints() (*models.Endpoints, error)
2626
AuthDevice(req *models.DeviceAuthRequest) (*models.DeviceAuthResponse, error)
2727
AuthPublicKey(req *models.PublicKeyAuthRequest, token string) (*models.PublicKeyAuthResponse, error)
28-
NewReverseListener(ctx context.Context, token string) (*revdial.Listener, error)
28+
NewReverseListener(ctx context.Context, token string, connPath string) (*revdial.Listener, error)
2929
}
3030

3131
//go:generate mockery --name=Client --filename=client.go

pkg/api/client/client_public.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ func (c *client) AuthPublicKey(req *models.PublicKeyAuthRequest, token string) (
104104

105105
// NewReverseListener creates a new reverse listener connection to ShellHub's server. This listener receives the SSH
106106
// requests coming from the ShellHub server. Only authenticated devices can obtain a listener connection.
107-
func (c *client) NewReverseListener(ctx context.Context, token string) (*revdial.Listener, error) {
107+
func (c *client) NewReverseListener(ctx context.Context, token string, connPath string) (*revdial.Listener, error) {
108108
if token == "" {
109109
return nil, errors.New("token is empty")
110110
}
111111

112-
if err := c.reverser.Auth(ctx, token); err != nil {
112+
if err := c.reverser.Auth(ctx, token, connPath); err != nil {
113113
return nil, err
114114
}
115115

pkg/api/client/client_public_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -433,15 +433,15 @@ func TestReverseListener(t *testing.T) {
433433
description: "fail when connot auth the agent on the SSH server",
434434
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
435435
requiredMocks: func() {
436-
mock.On("Auth", context.Background(), "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c").Return(errors.New("")).Once()
436+
mock.On("Auth", context.Background(), "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", "").Return(errors.New("")).Once()
437437
},
438438
expected: errors.New(""),
439439
},
440440
{
441441
description: "fail when connot create a new reverse listener",
442442
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
443443
requiredMocks: func() {
444-
mock.On("Auth", context.Background(), "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c").Return(nil).Once()
444+
mock.On("Auth", context.Background(), "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", "").Return(nil).Once()
445445

446446
mock.On("NewListener").Return(nil, errors.New("")).Once()
447447
},
@@ -451,7 +451,7 @@ func TestReverseListener(t *testing.T) {
451451
description: "success to create a new reverse listener",
452452
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
453453
requiredMocks: func() {
454-
mock.On("Auth", context.Background(), "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c").Return(nil).Once()
454+
mock.On("Auth", context.Background(), "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c", "").Return(nil).Once()
455455

456456
mock.On("NewListener").Return(new(revdial.Listener), nil).Once()
457457
},
@@ -468,7 +468,7 @@ func TestReverseListener(t *testing.T) {
468468

469469
test.requiredMocks()
470470

471-
_, err = cli.NewReverseListener(ctx, test.token)
471+
_, err = cli.NewReverseListener(ctx, test.token, "")
472472
assert.Equal(t, err, test.expected)
473473
})
474474
}

pkg/api/client/mocks/client.go

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/client/mocks/reverser.go

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/api/client/reverser.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
//go:generate mockery --name=IReverser --filename=reverser.go
1616
type IReverser interface {
17-
Auth(ctx context.Context, token string) error
17+
Auth(ctx context.Context, token string, connPath string) error
1818
NewListener() (*revdial.Listener, error)
1919
}
2020

@@ -35,8 +35,8 @@ func NewReverser(host string) *Reverser {
3535
}
3636

3737
// Auth creates a initial connection to the ShellHub SSH's server and authenticate it with the token received.
38-
func (r *Reverser) Auth(ctx context.Context, token string) error {
39-
uri, err := url.JoinPath(r.host, "/ssh/connection")
38+
func (r *Reverser) Auth(ctx context.Context, token string, connPath string) error {
39+
uri, err := url.JoinPath(r.host, connPath)
4040
if err != nil {
4141
return err
4242
}

pkg/connman/connman.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ func New() *ConnectionManager {
2626
}
2727
}
2828

29-
func (m *ConnectionManager) Set(key string, conn *wsconnadapter.Adapter) {
30-
dialer := revdial.NewDialer(conn, "/ssh/revdial")
29+
func (m *ConnectionManager) Set(key string, conn *wsconnadapter.Adapter, connPath string) {
30+
dialer := revdial.NewDialer(conn, connPath)
3131

3232
m.dialers.Store(key, dialer)
3333

pkg/httptunnel/httptunnel.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (t *Tunnel) Router() http.Handler {
8282
return c.String(http.StatusBadRequest, err.Error())
8383
}
8484

85-
t.connman.Set(id, wsconnadapter.New(conn))
85+
t.connman.Set(id, wsconnadapter.New(conn), t.DialerPath)
8686

8787
return nil
8888
})

0 commit comments

Comments
 (0)