-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzb.go
178 lines (152 loc) · 4.08 KB
/
zb.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
177
178
package zb
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"github.com/json-iterator/go"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"time"
)
const (
OK = 1000
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
type ZB struct {
httpClient *http.Client
accessKey string
secretKey string
debugMode bool
marketURL string // "http://api.zb.live/data/v1/"
tradeURL string // "https://trade.zb.live/api/"
}
func (z *ZB) HttpGet(reqUrl string, postData string, headers map[string]string, result interface{}) ([]byte, error) {
return z.NewHttpRequest("GET", reqUrl, postData, headers, result)
}
func (z *ZB) HttpPost(reqUrl string, postData string, headers map[string]string, result interface{}) ([]byte, error) {
return z.NewHttpRequest("POST", reqUrl, postData, headers, result)
}
func (z *ZB) NewHttpRequest(method string, reqUrl string, postData string, requestHeaders map[string]string, result interface{}) ([]byte, error) {
if z.debugMode {
log.Printf("reqUrl: %v", reqUrl)
}
req, _ := http.NewRequest(method, reqUrl, strings.NewReader(postData))
//req.Header.Set("User-Agent", defaultUserAgent)
if requestHeaders != nil {
for k, v := range requestHeaders {
req.Header.Add(k, v)
}
}
resp, err := z.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bodyData, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if z.debugMode {
log.Printf("%v", string(bodyData))
}
if resp.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("HttpStatusCode: %d, Desc: %s", resp.StatusCode, string(bodyData)))
}
if result != nil {
err = json.Unmarshal(bodyData, result)
}
return bodyData, err
}
func (z *ZB) buildSignParams(params *url.Values) error {
params.Set("accesskey", z.accessKey)
payload := params.Encode()
secretKeySha, _ := GetSHA(z.secretKey)
sign, err := GetParamHmacMD5Sign(secretKeySha, payload)
if err != nil {
return err
}
params.Set("sign", sign)
params.Set("reqTime", fmt.Sprintf("%d", time.Now().UnixNano()/int64(time.Millisecond)))
return nil
}
// SetProxy 设置代理
// proxyURL: "socks5://127.0.0.1:1080"
func (z *ZB) SetProxy(proxyURL string) (err error) {
transport := CloneDefaultTransport()
transport.Proxy, err = ParseProxy(proxyURL)
if err != nil {
return
}
z.httpClient.Transport = transport
return nil
}
// domain: zb.live/zb.com
func NewZB(httpClient *http.Client, host string, accessKey string, secretKey string, debugMode bool) *ZB {
marketURL := fmt.Sprintf("http://api.%v/data/v1/", host) // "http://api.zb.live/data/v1/"
tradeURL := fmt.Sprintf("https://trade.%v/api/", host) // "https://trade.zb.live/api/"
if httpClient == nil {
httpClient = &http.Client{
Timeout: 10 * time.Second,
}
}
return &ZB{
httpClient: httpClient,
accessKey: accessKey,
secretKey: secretKey,
debugMode: debugMode,
marketURL: marketURL,
tradeURL: tradeURL,
}
}
func GetSHA(text string) (string, error) {
sha := sha1.New()
_, err := sha.Write([]byte(text))
if err != nil {
return "", err
}
return hex.EncodeToString(sha.Sum(nil)), nil
}
func GetParamHmacSHA256Sign(secret, params string) (string, error) {
mac := hmac.New(sha256.New, []byte(secret))
_, err := mac.Write([]byte(params))
if err != nil {
return "", err
}
return hex.EncodeToString(mac.Sum(nil)), nil
}
func GetParamHmacMD5Sign(secret, params string) (string, error) {
mac := hmac.New(md5.New, []byte(secret))
_, err := mac.Write([]byte(params))
if err != nil {
return "", err
}
return hex.EncodeToString(mac.Sum(nil)), nil
}
/**
*md5签名
*/
func GetParamMD5Sign(secret, params string) (string, error) {
hash := md5.New()
_, err := hash.Write([]byte(params))
if err != nil {
return "", err
}
return hex.EncodeToString(hash.Sum(nil)), nil
}
func GetParamHmacSHA256Base64Sign(secret, params string) (string, error) {
mac := hmac.New(sha256.New, []byte(secret))
_, err := mac.Write([]byte(params))
if err != nil {
return "", err
}
signByte := mac.Sum(nil)
return base64.StdEncoding.EncodeToString(signByte), nil
}