forked from moul/quicssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
57 lines (50 loc) · 1.05 KB
/
client.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
49
50
51
52
53
54
55
56
57
package main
import (
"crypto/tls"
"log"
"os"
"sync"
quic "github.com/quic-go/quic-go"
cli "github.com/urfave/cli/v2"
"golang.org/x/net/context"
)
func client(c *cli.Context) error {
ctx, cancel := context.WithCancel(context.Background())
config := &tls.Config{
InsecureSkipVerify: true,
NextProtos: []string{"quicssh"},
}
log.Printf("Dialing %q...", c.String("addr"))
session, err := quic.DialAddr(ctx, c.String("addr"), config, nil)
if err != nil {
return err
}
defer func() {
if err := session.CloseWithError(0, "close"); err != nil {
log.Printf("session close error: %v", err)
}
}()
log.Printf("Opening stream sync...")
stream, err := session.OpenStreamSync(ctx)
if err != nil {
return err
}
log.Printf("Piping stream with QUIC...")
var wg sync.WaitGroup
wg.Add(3)
c1 := readAndWrite(ctx, stream, os.Stdout, &wg)
c2 := readAndWrite(ctx, os.Stdin, stream, &wg)
select {
case err = <-c1:
if err != nil {
return err
}
case err = <-c2:
if err != nil {
return err
}
}
cancel()
wg.Wait()
return nil
}