-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.go
132 lines (117 loc) · 2.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
// Copyright 2015 Constantin Karataev. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package provides Webmoney xml interfaces
// References
// https://wiki.wmtransfer.com/projects/webmoney/wiki/XML-interfaces
// https://wiki.webmoney.ru/projects/webmoney/wiki/XML-%D0%B8%D0%BD%D1%82%D0%B5%D1%80%D1%84%D0%B5%D0%B9%D1%81%D1%8B
package webmoney
import (
"crypto/tls"
"crypto/x509"
"io"
"log"
"net/http"
"os"
"strings"
_ "github.com/paulrosania/go-charset/data"
)
type WmClient struct {
Wmid string
Key string
Pass string
Cert string
SecretKey string
}
// IsClassic function return true if current settings indicate
// that request is signed classic key
func (w *WmClient) IsClassic() bool {
if w.Key != "" && w.Pass != "" {
return true
} else {
return false
}
}
// IsLight function return true if current settings indicate
// that requests is signed light keeper
func (w *WmClient) IsLight() bool {
if w.Key != "" && w.Cert != "" {
return true
} else {
return false
}
}
// noInit check settings for connetion and sign not set
// before start use you must set Wmid, Key and Pass for Keeper Classic(WinPro)
// or wmid, key and cert for Keepr Light(WebPro)
func (w *WmClient) noInit() bool {
if w.Wmid == "" || w.Key == "" || w.Pass == "" {
return true
} else {
return false
}
}
// Function send requst to server and return response how string
func (w *WmClient) sendRequest(url string, body string) (string, error) {
var client *http.Client
client = http.DefaultClient
if os.Getenv("USE_W3S_CERT") != "" {
tr, err := w.getTransport()
if err != nil {
return "", err
}
client.Transport = tr
}
resp, err := client.Post(url, "text/xml", strings.NewReader(body))
if err != nil {
return "", err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Println(err)
}
}(resp.Body)
result, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
} else {
return string(result), nil
}
}
func (w *WmClient) getTransport() (*http.Transport, error) {
var tr *http.Transport
// load root ca
r := strings.NewReader(ROOT_CA)
caCert, err := io.ReadAll(r)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
if w.IsClassic() {
tr = &http.Transport{
TLSClientConfig: &tls.Config{
//RootCAs: caCertPool,
Renegotiation: tls.RenegotiateFreelyAsClient,
},
DisableCompression: true,
}
} else {
cert, err := tls.LoadX509KeyPair(w.Cert, w.Key)
if err != nil {
return nil, err
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
//RootCAs: caCertPool,
Renegotiation: tls.RenegotiateFreelyAsClient,
}
tlsConfig.BuildNameToCertificate()
tr = &http.Transport{
TLSClientConfig: tlsConfig,
DisableCompression: true,
}
}
return tr, nil
}