-
Notifications
You must be signed in to change notification settings - Fork 0
/
glinet.go
143 lines (110 loc) · 2.97 KB
/
glinet.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
package glinet
import (
"bytes"
"context"
"log"
"net/http"
"net/url"
"strings"
"github.com/gorilla/rpc/v2/json2"
)
const (
Version = "v0.0.4"
defaultBaseUrl = "http://192.168.8.1"
defaultUserAgent = "glinet-client-go" + "/" + Version
)
type ClientInterface interface {
GetSid() string
CallWithStringSlice(method string, params []string, result interface{}) error
CallWithInterface(method string, params, result interface{}) error
CallWithInterfaceSlice(method string, params []interface{}, result interface{}) error
}
type Client struct {
BaseURL *url.URL
UserAgent string
common service // Reuse a single struct instead of allocating one for each service on the heap.
sid string
// services
AdGuard *AdGuardService
Digest *DigestService
System *SystemService
}
type service struct {
client ClientInterface
context context.Context
}
func NewClient(username string, password []byte) *Client {
return NewClientWithHost(defaultBaseUrl, username, password)
}
func NewClientWithHost(host string, username string, password []byte) *Client {
if !strings.HasPrefix(host, "http") {
host = "http://" + host
}
baseUrl, err := url.Parse(host + "/rpc")
if err != nil {
log.Fatal("Error parsing host", err)
}
c := &Client{BaseURL: baseUrl, UserAgent: defaultUserAgent}
c.common.client = c
c.common.context = context.TODO()
// services
c.AdGuard = (*AdGuardService)(&c.common)
c.Digest = (*DigestService)(&c.common)
c.System = (*SystemService)(&c.common)
login, err := c.Digest.Login(username, password)
if err != nil {
log.Fatal("Error logging in: ", err)
}
c.sid = login.Sid
return c
}
func NewClientUnauthenticated() *Client {
baseUrl, _ := url.Parse(defaultBaseUrl)
c := &Client{BaseURL: baseUrl, UserAgent: defaultUserAgent}
c.common.client = c
c.common.context = context.TODO()
// services
c.AdGuard = (*AdGuardService)(&c.common)
c.Digest = (*DigestService)(&c.common)
c.System = (*SystemService)(&c.common)
return c
}
func (c *Client) GetSid() string {
return c.sid
}
func (c *Client) CallWithStringSlice(method string, params []string, result interface{}) error {
buf, _ := json2.EncodeClientRequest(method, params)
err := c.call(buf, result)
if err != nil {
return err
}
return nil
}
func (c *Client) CallWithInterface(method string, params, result interface{}) error {
buf, _ := json2.EncodeClientRequest(method, params)
err := c.call(buf, result)
if err != nil {
return err
}
return nil
}
func (c *Client) CallWithInterfaceSlice(method string, params []interface{}, result interface{}) error {
buf, _ := json2.EncodeClientRequest(method, params)
err := c.call(buf, result)
if err != nil {
return err
}
return nil
}
func (c *Client) call(buf []byte, result interface{}) error {
body := bytes.NewBuffer(buf)
res, err := http.Post(c.BaseURL.String(), "application/json", body)
if err != nil {
return err
}
err = json2.DecodeClientResponse(res.Body, result)
if err != nil {
return err
}
return nil
}