-
Notifications
You must be signed in to change notification settings - Fork 286
/
client.go
176 lines (151 loc) · 3.9 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package jsonrpc2
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"time"
"github.com/inverse-inc/go-utils/log"
"github.com/inverse-inc/packetfence/go/pfconfigdriver"
)
var httpClient *http.Client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSHandshakeTimeout: 1 * time.Second,
Dial: dialTimeout,
MaxIdleConns: 100,
IdleConnTimeout: 2 * time.Minute,
DisableCompression: true,
},
}
func dialTimeout(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, 10*time.Second)
}
type Client struct {
Id uint
Username string
Password string
Proto string
Host string
Port string
Method string
}
type JsonRPC2Request struct {
Method string `json:"method"`
JsonRPC string `json:"jsonrpc"`
Params interface{} `json:"params"`
Id uint `json:"id,omitempty"`
}
type JsonRPC2Error struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
func (e *JsonRPC2Error) Error() string {
return e.Message
}
type JsonRPC2Response struct {
JsonRPC string `json:"jsonrpc"`
Result interface{} `json:"result,omitempty"`
Error *JsonRPC2Error `json:"error,omitempty"`
Id uint `json:"id"`
}
func NewClientFromConfig(ctx context.Context) *Client {
var webservices pfconfigdriver.PfConfWebservices
pfconfigdriver.FetchDecodeSocket(ctx, &webservices)
return &Client{
Username: webservices.User,
Password: webservices.Pass,
Proto: webservices.Proto,
Host: webservices.Host,
Port: webservices.Port,
}
}
func NewAAAClientFromConfig(ctx context.Context) *Client {
var webservices pfconfigdriver.PfConfWebservices
var ports pfconfigdriver.PfConfPorts
pfconfigdriver.FetchDecodeSocket(ctx, &webservices)
pfconfigdriver.FetchDecodeSocket(ctx, &ports)
return &Client{
Username: webservices.User,
Password: webservices.Pass,
Proto: webservices.AAAProto,
Host: webservices.Host,
Port: ports.AAA,
}
}
func (c *Client) Call(ctx context.Context, method string, args interface{} ) (interface{}, error) {
c.Id++
request := JsonRPC2Request{
Method: method,
JsonRPC: "2.0",
Params: args,
Id: c.Id,
}
r, err := c.buildRequest(&request)
if err != nil {
return nil, err
}
start := time.Now()
resp, err := httpClient.Do(r)
elapsed := time.Now().Sub(start)
log.LoggerWContext(ctx).Debug(fmt.Sprintf("jsonrpc2.Call took %v for %s", elapsed, method))
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
var response JsonRPC2Response
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
if response.Error != nil {
return nil, response.Error
}
return response.Result, nil
}
func (c *Client) Notify(ctx context.Context, method string, args interface{} ) error {
request := JsonRPC2Request{
Method: method,
JsonRPC: "2.0",
Params: args,
Id: 0,
}
r, err := c.buildRequest(&request)
if err != nil {
return err
}
start := time.Now()
resp, err := httpClient.Do(r)
elapsed := time.Now().Sub(start)
log.LoggerWContext(ctx).Debug(fmt.Sprintf("jsonrpc2.Notify took %v for %s", elapsed, method))
if err != nil {
return err
}
_, err = ioutil.ReadAll(resp.Body)
resp.Body.Close()
return nil
}
func (c *Client) buildRequest(jr *JsonRPC2Request) (*http.Request, error) {
uri := fmt.Sprintf("%s://%s:%s", c.Proto, c.Host, c.Port)
var data []byte
var err error
if data, err = json.Marshal(jr); err != nil {
return nil, err
}
r, err := http.NewRequest("POST", uri, bytes.NewReader(data))
if err != nil {
return nil, err
}
r.Header.Set("Content-Type", "application/json-rpc")
r.Header.Set("Request", jr.Method)
if c.Proto == "https" {
r.SetBasicAuth(c.Username, c.Password)
}
return r, nil
}