-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
160 lines (140 loc) · 3.49 KB
/
client.go
File metadata and controls
160 lines (140 loc) · 3.49 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package delugerpc
import (
"bytes"
"compress/zlib"
"crypto/tls"
"errors"
"fmt"
"net"
"net/rpc"
"reflect"
"strings"
"github.com/rogaps/delugerpc/rencode"
)
type rpcResponseTypeID int
const (
rpcResponse rpcResponseTypeID = 1
rpcError rpcResponseTypeID = 2
rpcEvent rpcResponseTypeID = 3
)
type clientCodec struct {
conn *tls.Conn
respBody interface{}
}
func (c *clientCodec) WriteRequest(r *rpc.Request, body interface{}) error {
var b bytes.Buffer
var msg []interface{}
var req []interface{}
zw := zlib.NewWriter(&b)
e := rencode.NewEncoder(zw)
args, kwargs := getArgs(body)
msg = append(msg, r.Seq, r.ServiceMethod, args, kwargs)
req = append(req, msg)
if err := e.Encode(req); err != nil {
return err
}
if err := zw.Close(); err != nil {
return err
}
if _, err := c.conn.Write(b.Bytes()); err != nil {
return err
}
return nil
}
func (c *clientCodec) ReadResponseHeader(r *rpc.Response) (err error) {
zr, err := zlib.NewReader(c.conn)
if err != nil {
return
}
d := rencode.NewDecoder(zr)
var resp []interface{}
if err = d.Decode(&resp); err != nil {
return
}
messageType := resp[0].(int64)
r.Seq = uint64(resp[1].(int64))
switch rpcResponseTypeID(messageType) {
case rpcResponse:
c.respBody = resp[2]
return
case rpcError:
errMsg := resp[2].([]interface{})
exceptionType := errMsg[0]
exceptionMsg := errMsg[1]
return fmt.Errorf("%v: %v", exceptionType, exceptionMsg)
case rpcEvent:
return errors.New("event is not supported")
default:
return errors.New("unknown message type")
}
}
func (c *clientCodec) ReadResponseBody(body interface{}) (err error) {
bv := reflect.ValueOf(body)
if bv.Kind() != reflect.Ptr || bv.IsNil() {
return errors.New("Unwritable type passed into decode")
}
bv = bv.Elem()
if c.respBody != nil {
bv.Set(reflect.ValueOf(c.respBody))
}
return nil
}
func (c *clientCodec) Close() error {
return c.conn.Close()
}
func newDelugeCodec(conn *tls.Conn) rpc.ClientCodec {
return &clientCodec{
conn: conn,
}
}
// Dial creates RPC client with rencode codec
func Dial(network, address string) (*rpc.Client, error) {
conn, err := net.Dial(network, address)
if err != nil {
return nil, err
}
tlsConn := tls.Client(conn, &tls.Config{
ServerName: address,
InsecureSkipVerify: true,
})
return rpc.NewClientWithCodec(newDelugeCodec(tlsConn)), err
}
func getArgs(body interface{}) (args []interface{}, kwargs map[string]interface{}) {
bodyValue := reflect.ValueOf(body)
switch bodyValue.Kind() {
case reflect.Slice, reflect.Array:
for i := 0; i < bodyValue.Len(); i++ {
args = append(args, bodyValue.Index(i).Interface())
}
return
case reflect.Map:
for _, key := range bodyValue.MapKeys() {
if strings.EqualFold("args", key.String()) {
argsValue := bodyValue.MapIndex(key)
if argsValue.Kind() == reflect.Interface {
argsValue = argsValue.Elem()
}
if argsValue.Kind() == reflect.Slice ||
argsValue.Kind() == reflect.Array {
for i := 0; i < argsValue.Len(); i++ {
args = append(args, argsValue.Index(i).Interface())
}
}
} else if strings.EqualFold("kwargs", key.String()) {
kwargsValue := bodyValue.MapIndex(key)
if kwargsValue.Kind() == reflect.Interface {
kwargsValue = kwargsValue.Elem()
}
if kwargsValue.Kind() == reflect.Map {
kwargs = make(map[string]interface{})
for _, key := range kwargsValue.MapKeys() {
kwargs[key.String()] = kwargsValue.MapIndex(key).Interface()
}
}
}
}
return
default:
return
}
}