-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.go
132 lines (115 loc) · 2.75 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
package lightning
import (
"bytes"
"crypto/tls"
"encoding/json"
"io"
"io/ioutil"
"net"
"net/http"
"strings"
"time"
"github.com/tidwall/gjson"
)
var DefaultTimeout = time.Second * 5
type Client struct {
PaymentHandler func(gjson.Result)
LastInvoiceIndex int
CallTimeout time.Duration
// lightning-rpc socket
Path string
LightningDir string
// spark/sparko server
SparkURL string
SparkToken string
DontCheckCertificates bool
}
// the lowest-level method for a socket client
func (ln *Client) callMessageBytes(
timeout time.Duration,
retrySequence int,
message []byte,
) (res []byte, err error) {
conn, err := net.Dial("unix", ln.Path)
if err != nil {
if retrySequence < 6 {
time.Sleep(time.Second * 2 * (time.Duration(retrySequence) + 1))
return ln.callMessageBytes(timeout, retrySequence+1, message)
} else {
err = ErrorConnect{ln.Path, err.Error()}
return
}
}
defer conn.Close()
respchan := make(chan []byte)
errchan := make(chan error)
go func() {
decoder := json.NewDecoder(conn)
for {
var response JSONRPCResponse
err := decoder.Decode(&response)
if err == io.EOF {
errchan <- ErrorConnectionBroken{}
break
} else if err != nil {
errchan <- ErrorJSONDecode{err.Error()}
break
} else if response.Error != nil && response.Error.Code != 0 {
errchan <- ErrorCommand{response.Error.Message, response.Error.Code, response.Error.Data}
break
}
respchan <- response.Result
}
}()
conn.Write(message)
select {
case v := <-respchan:
return v, nil
case err = <-errchan:
return
case <-time.After(timeout):
err = ErrorTimeout{int(timeout.Seconds())}
return
}
}
// the lowest-level method for a spark client
func (ln *Client) callSpark(timeout time.Duration, body []byte) (res []byte, err error) {
client := &http.Client{
Timeout: timeout,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: ln.DontCheckCertificates},
},
}
url := ln.SparkURL
if !strings.HasSuffix(url, "/rpc") {
url = strings.TrimSuffix(url, "/")
url += "/rpc"
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
if err != nil {
err = ErrorConnect{url, err.Error()}
return
}
if ln.SparkToken != "" {
req.Header.Add("X-Access", ln.SparkToken)
}
resp, err := client.Do(req)
if err != nil {
if strings.Index(err.Error(), "imeout") != -1 {
err = ErrorTimeout{int(timeout.Seconds())}
return
}
err = ErrorConnect{ln.SparkURL, err.Error()}
return
}
if resp.StatusCode >= 300 {
var sparkerr JSONRPCError
err = json.NewDecoder(resp.Body).Decode(&sparkerr)
if err != nil {
return
}
return nil, ErrorCommand{sparkerr.Message, sparkerr.Code, sparkerr.Data}
}
res, err = ioutil.ReadAll(resp.Body)
return
}