summaryrefslogtreecommitdiff
path: root/internal/clients/connectors
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2021-10-09 21:10:29 +0300
committerPaul Buetow <paul@buetow.org>2021-10-10 13:36:41 +0300
commit97747ea0f3178f7f5890512d483fdccaa82846b0 (patch)
tree9ff1335ca26afc90e55fd6de416457e252d75a35 /internal/clients/connectors
parent7a7169791a64190e1002e38bc9c04ad0d5c1ce1f (diff)
vetting and linting and some code restyling
Diffstat (limited to 'internal/clients/connectors')
-rw-r--r--internal/clients/connectors/serverconnection.go48
-rw-r--r--internal/clients/connectors/serverless.go16
2 files changed, 39 insertions, 25 deletions
diff --git a/internal/clients/connectors/serverconnection.go b/internal/clients/connectors/serverconnection.go
index 1666a79..2d7b45a 100644
--- a/internal/clients/connectors/serverconnection.go
+++ b/internal/clients/connectors/serverconnection.go
@@ -16,7 +16,8 @@ import (
"golang.org/x/crypto/ssh"
)
-// ServerConnection represents a connection to a single remote dtail server via SSH protocol.
+// ServerConnection represents a connection to a single remote dtail server via
+// SSH protocol.
type ServerConnection struct {
server string
port int
@@ -28,9 +29,11 @@ type ServerConnection struct {
}
// NewServerConnection returns a new DTail SSH server connection.
-func NewServerConnection(server string, userName string, authMethods []ssh.AuthMethod, hostKeyCallback client.HostKeyCallback, handler handlers.Handler, commands []string) *ServerConnection {
- dlog.Client.Debug(server, "Creating new connection", server, handler, commands)
+func NewServerConnection(server string, userName string,
+ authMethods []ssh.AuthMethod, hostKeyCallback client.HostKeyCallback,
+ handler handlers.Handler, commands []string) *ServerConnection {
+ dlog.Client.Debug(server, "Creating new connection", server, handler, commands)
c := ServerConnection{
hostKeyCallback: hostKeyCallback,
server: server,
@@ -48,10 +51,12 @@ func NewServerConnection(server string, userName string, authMethods []ssh.AuthM
return &c
}
+// Server returns the server hostname connected to.
func (c *ServerConnection) Server() string {
return c.server
}
+// Handler returns the handler used for the connection.
func (c *ServerConnection) Handler() handlers.Handler {
return c.handler
}
@@ -72,23 +77,29 @@ func (c *ServerConnection) initServerPort() {
}
}
-func (c *ServerConnection) Start(ctx context.Context, cancel context.CancelFunc, throttleCh, statsCh chan struct{}) {
+// Start the connection to the server.
+func (c *ServerConnection) Start(ctx context.Context, cancel context.CancelFunc,
+ throttleCh, statsCh chan struct{}) {
+
// Throttle how many connections can be established concurrently (based on ch length)
dlog.Client.Debug(c.server, "Throttling connection", len(throttleCh), cap(throttleCh))
select {
case throttleCh <- struct{}{}:
case <-ctx.Done():
- dlog.Client.Debug(c.server, "Not establishing connection as context is done", len(throttleCh), cap(throttleCh))
+ dlog.Client.Debug(c.server, "Not establishing connection as context is done",
+ len(throttleCh), cap(throttleCh))
return
}
- dlog.Client.Debug(c.server, "Throttling says that the connection can be established", len(throttleCh), cap(throttleCh))
+ dlog.Client.Debug(c.server, "Throttling says that the connection can be established",
+ len(throttleCh), cap(throttleCh))
go func() {
defer func() {
if !c.throttlingDone {
- dlog.Client.Debug(c.server, "Unthrottling connection (1)", len(throttleCh), cap(throttleCh))
+ dlog.Client.Debug(c.server, "Unthrottling connection (1)",
+ len(throttleCh), cap(throttleCh))
c.throttlingDone = true
<-throttleCh
}
@@ -107,7 +118,9 @@ func (c *ServerConnection) Start(ctx context.Context, cancel context.CancelFunc,
}
// Dail into a new SSH connection. Close connection in case of an error.
-func (c *ServerConnection) dial(ctx context.Context, cancel context.CancelFunc, throttleCh, statsCh chan struct{}) error {
+func (c *ServerConnection) dial(ctx context.Context, cancel context.CancelFunc,
+ throttleCh, statsCh chan struct{}) error {
+
dlog.Client.Debug(c.server, "Incrementing connection stats")
statsCh <- struct{}{}
defer func() {
@@ -128,31 +141,30 @@ func (c *ServerConnection) dial(ctx context.Context, cancel context.CancelFunc,
}
// Create the SSH session. Close the session in case of an error.
-func (c *ServerConnection) session(ctx context.Context, cancel context.CancelFunc, client *ssh.Client, throttleCh chan struct{}) error {
- dlog.Client.Debug(c.server, "Creating SSH session")
+func (c *ServerConnection) session(ctx context.Context, cancel context.CancelFunc,
+ client *ssh.Client, throttleCh chan struct{}) error {
+ dlog.Client.Debug(c.server, "Creating SSH session")
session, err := client.NewSession()
if err != nil {
return err
}
defer session.Close()
-
return c.handle(ctx, cancel, session, throttleCh)
}
-func (c *ServerConnection) handle(ctx context.Context, cancel context.CancelFunc, session *ssh.Session, throttleCh chan struct{}) error {
- dlog.Client.Debug(c.server, "Creating handler for SSH session")
+func (c *ServerConnection) handle(ctx context.Context, cancel context.CancelFunc,
+ session *ssh.Session, throttleCh chan struct{}) error {
+ dlog.Client.Debug(c.server, "Creating handler for SSH session")
stdinPipe, err := session.StdinPipe()
if err != nil {
return err
}
-
stdoutPipe, err := session.StdoutPipe()
if err != nil {
return err
}
-
if err := session.Shell(); err != nil {
return err
}
@@ -161,12 +173,10 @@ func (c *ServerConnection) handle(ctx context.Context, cancel context.CancelFunc
io.Copy(stdinPipe, c.handler)
cancel()
}()
-
go func() {
io.Copy(c.handler, stdoutPipe)
cancel()
}()
-
go func() {
select {
case <-c.handler.Done():
@@ -182,13 +192,13 @@ func (c *ServerConnection) handle(ctx context.Context, cancel context.CancelFunc
}
if !c.throttlingDone {
- dlog.Client.Debug(c.server, "Unthrottling connection (2)", len(throttleCh), cap(throttleCh))
+ dlog.Client.Debug(c.server, "Unthrottling connection (2)",
+ len(throttleCh), cap(throttleCh))
c.throttlingDone = true
<-throttleCh
}
<-ctx.Done()
c.handler.Shutdown()
-
return nil
}
diff --git a/internal/clients/connectors/serverless.go b/internal/clients/connectors/serverless.go
index 768a5ce..2ff490a 100644
--- a/internal/clients/connectors/serverless.go
+++ b/internal/clients/connectors/serverless.go
@@ -18,8 +18,10 @@ type Serverless struct {
userName string
}
-// NewServerConnection returns a new connection.
-func NewServerless(userName string, handler handlers.Handler, commands []string) *Serverless {
+// NewServerless starts a new serverless session.
+func NewServerless(userName string, handler handlers.Handler,
+ commands []string) *Serverless {
+
dlog.Client.Debug("Creating new serverless connector", handler, commands)
return &Serverless{
userName: userName,
@@ -28,15 +30,20 @@ func NewServerless(userName string, handler handlers.Handler, commands []string)
}
}
+// Server returns serverless server indicator.
func (s *Serverless) Server() string {
return "local(serverless)"
}
+// Handler returns the handler used for the serverless connection.
func (s *Serverless) Handler() handlers.Handler {
return s.handler
}
-func (s *Serverless) Start(ctx context.Context, cancel context.CancelFunc, throttleCh, statsCh chan struct{}) {
+// Start the serverless connection.
+func (s *Serverless) Start(ctx context.Context, cancel context.CancelFunc,
+ throttleCh, statsCh chan struct{}) {
+
dlog.Client.Debug("Starting serverless connector")
go func() {
defer cancel()
@@ -81,13 +88,11 @@ func (s *Serverless) handle(ctx context.Context, cancel context.CancelFunc) erro
dlog.Client.Trace("io.Copy(serverHandler, s.handler) => done")
terminate()
}()
-
go func() {
io.Copy(s.handler, serverHandler)
dlog.Client.Trace("io.Copy(s.handler, serverHandler) => done")
terminate()
}()
-
go func() {
select {
case <-s.handler.Done():
@@ -107,6 +112,5 @@ func (s *Serverless) handle(ctx context.Context, cancel context.CancelFunc) erro
<-ctx.Done()
dlog.Client.Trace("s.handler.Shutdown()")
s.handler.Shutdown()
-
return nil
}