Some more cleanup to the port forwarder and connection
This commit is contained in:
parent
7332aa428c
commit
fddcd876b0
2 changed files with 21 additions and 23 deletions
|
|
@ -7,27 +7,27 @@ import (
|
|||
)
|
||||
|
||||
type Connection struct {
|
||||
SessionID string `json:"sessionId"`
|
||||
SessionToken string `json:"sessionToken"`
|
||||
RelaySAS string `json:"relaySas"`
|
||||
RelayEndpoint string `json:"relayEndpoint"`
|
||||
SessionID string
|
||||
SessionToken string
|
||||
RelaySAS string
|
||||
RelayEndpoint string
|
||||
}
|
||||
|
||||
func (r Connection) validate() error {
|
||||
if r.SessionID == "" {
|
||||
return errors.New("connection sessionID is required")
|
||||
return errors.New("connection SessionID is required")
|
||||
}
|
||||
|
||||
if r.SessionToken == "" {
|
||||
return errors.New("connection sessionToken is required")
|
||||
return errors.New("connection SessionToken is required")
|
||||
}
|
||||
|
||||
if r.RelaySAS == "" {
|
||||
return errors.New("connection relaySas is required")
|
||||
return errors.New("connection RelaySAS is required")
|
||||
}
|
||||
|
||||
if r.RelayEndpoint == "" {
|
||||
return errors.New("connection relayEndpoint is required")
|
||||
return errors.New("connection RelayEndpoint is required")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -4,22 +4,24 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
type PortForwarder struct {
|
||||
client *Client
|
||||
server *Server
|
||||
port int
|
||||
channels []ssh.Channel
|
||||
client *Client
|
||||
server *Server
|
||||
port int
|
||||
errCh chan error
|
||||
}
|
||||
|
||||
func NewPortForwarder(client *Client, server *Server, port int) *PortForwarder {
|
||||
return &PortForwarder{client, server, port, []ssh.Channel{}}
|
||||
return &PortForwarder{
|
||||
client: client,
|
||||
server: server,
|
||||
port: port,
|
||||
errCh: make(chan error),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *PortForwarder) Start(ctx context.Context) error {
|
||||
|
|
@ -37,22 +39,18 @@ func (l *PortForwarder) Start(ctx context.Context) error {
|
|||
go l.handleConnection(ctx, conn)
|
||||
}
|
||||
|
||||
// clean up after ourselves
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *PortForwarder) handleConnection(ctx context.Context, conn net.Conn) {
|
||||
channel, err := l.client.openStreamingChannel(ctx, l.server.streamName, l.server.streamCondition)
|
||||
if err != nil {
|
||||
log.Println("errrr handle Connect")
|
||||
log.Println(err) // TODO(josebalius) handle this somehow
|
||||
l.errCh <- fmt.Errorf("error opening streaming channel for new connection: %v", err)
|
||||
return
|
||||
}
|
||||
l.channels = append(l.channels, channel)
|
||||
|
||||
copyConn := func(writer io.Writer, reader io.Reader) {
|
||||
_, err := io.Copy(writer, reader)
|
||||
if err != nil {
|
||||
if _, err := io.Copy(writer, reader); err != nil {
|
||||
channel.Close()
|
||||
conn.Close()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue