-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialer.go
48 lines (40 loc) · 1.08 KB
/
dialer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package awsdial
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/mmmorris1975/ssm-session-client/datachannel"
"io"
"net"
"strconv"
)
type Dialer struct {
Client *ssm.Client
}
func (d *Dialer) Dial(ctx context.Context, target string, port int) (net.Conn, error) {
in := &ssm.StartSessionInput{
DocumentName: aws.String("AWS-StartSSHSession"),
Target: aws.String(target),
Parameters: map[string][]string{
"portNumber": {strconv.Itoa(port)},
},
}
start, err := d.Client.StartSession(ctx, in)
if err != nil {
return nil, fmt.Errorf("calling StartSession API: %w", err)
}
c := &datachannel.SsmDataChannel{}
err = c.StartSessionFromDataChannelURL(*start.StreamUrl, *start.TokenValue)
if err != nil {
return nil, fmt.Errorf("opening ssm datachannel: %w", err)
}
err = c.WaitForHandshakeComplete()
if err != nil {
return nil, fmt.Errorf("waiting for ssm handshake: %w", err)
}
pr, pw := io.Pipe()
go c.WriteTo(pw)
conn := ssmconn{SsmDataChannel: c, pr: pr, target: target}
return conn, nil
}