/
client.go
121 lines (105 loc) · 2.74 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package client
import (
"context"
"crypto/x509"
"fmt"
"github.com/SuzukiHonoka/spaceship/internal/transport"
"github.com/SuzukiHonoka/spaceship/internal/transport/rpc"
proxy "github.com/SuzukiHonoka/spaceship/internal/transport/rpc/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"io"
"log"
"net"
"os"
)
const TransportName = "proxy"
var (
uuid string
connQueue *ConnQueue
)
type Client struct {
proxy.ProxyClient
DoneFunc func() error
}
func SetUUID(uid string) {
uuid = uid
}
func Init(server, hostName string, tls bool, mux uint8, cas []string) error {
var credential credentials.TransportCredentials
if tls {
pool, err := x509.SystemCertPool()
if err != nil {
if len(cas) == 0 {
return fmt.Errorf("addtional ca not found while system cert pool can not be copied: %w", err)
}
log.Println("copy system cert pool failed, creating new empty pool")
pool = x509.NewCertPool()
}
// load custom cas if exist
for _, path := range cas {
var cert []byte
if cert, err = os.ReadFile(path); err != nil {
log.Printf("read CA file from path: %s failed: %v", path, err)
continue
}
if !pool.AppendCertsFromPEM(cert) {
log.Printf("CA: [%s] add to cert pool failed", path)
}
}
// error handling omitted
credential = credentials.NewClientTLSFromCert(pool, hostName)
} else {
credential = insecure.NewCredentials()
}
params := NewParams(server, append(rpc.DialOptions, grpc.WithTransportCredentials(credential))...)
connQueue = NewConnQueue(int(mux), params)
return connQueue.Init()
}
func Destroy() {
// double check since credential errors might occur
if connQueue != nil {
connQueue.Destroy()
}
}
func (c *Client) Dial(_, _ string) (net.Conn, error) {
return nil, fmt.Errorf("%s: dial not implemented", c.String())
}
func NewClient() (*Client, error) {
client, doneFunc, err := connQueue.GetClient()
if err != nil {
return nil, err
}
return &Client{ProxyClient: client, DoneFunc: doneFunc}, nil
}
func (c *Client) String() string {
return TransportName
}
func (c *Client) Close() error {
return c.DoneFunc()
}
func (c *Client) Proxy(ctx context.Context, req *transport.Request, localAddr chan<- string, w io.Writer, r io.Reader) error {
defer close(localAddr)
sessionCtx, cancel := context.WithCancel(ctx)
defer cancel()
// rcp client
stream, err := c.ProxyClient.Proxy(sessionCtx)
if err != nil {
return err
}
//log.Printf("sending proxy to rpc: %s", req.Host)
forwardError := make(chan error)
f := NewForwarder(ctx, stream, w, r)
go func() {
forwardError <- f.Start(req, localAddr)
}()
// block main
select {
case err = <-forwardError:
return err
case <-ctx.Done():
}
//log.Println("client done")
return nil
}