This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrpc.go
133 lines (113 loc) · 3.14 KB
/
rpc.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
package main
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"time"
lightning "github.com/fiatjaf/lightningd-gjson-rpc"
"github.com/fiatjaf/lightningd-gjson-rpc/plugin"
)
func handleRPC(w http.ResponseWriter, r *http.Request) {
p := r.Context().Value("plugin").(*plugin.Plugin)
var req lightning.JSONRPCMessage
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
p.Log("got invalid JSON on RPC call")
w.WriteHeader(400)
return
}
req.Version = "2.0"
// check permissions
if permissions, ok := r.Context().Value("permissions").(map[string]bool); ok {
if len(permissions) > 0 {
if _, allowed := permissions[req.Method]; !allowed {
p.Logf("insufficient permissions for '%s' call", req.Method)
w.WriteHeader(401)
return
}
}
}
// actually do the call
respbytes, err := p.Client.CallMessageRaw(time.Second*30, req)
if err != nil {
p.Logf("'%s' call returned an error", req.Method)
w.WriteHeader(500)
if cmderr, ok := err.(lightning.ErrorCommand); ok {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(LightningError{
Type: "lightning",
Name: "LightningError",
Message: cmderr.Message,
Code: cmderr.Code,
FullType: "lightning",
Request: req,
})
}
return
}
w.Header().Set("Content-Type", "application/json")
// if we have a "Range" header, try to filter the response
if rangeHeader := r.Header.Get("Range"); rangeHeader != "" {
spl := strings.Split(rangeHeader, "=")
if len(spl) != 2 {
goto sendEverythingWithoutRange
}
unit := spl[0]
spl = strings.Split(spl[1], "-")
if len(spl) != 2 {
goto sendEverythingWithoutRange
}
from, err1 := strconv.Atoi(spl[0])
to, err2 := strconv.Atoi(spl[1])
var transformEntries func([]json.RawMessage) []json.RawMessage
if spl[0] == "" {
// suffix-length, only "to" should be valid
if err2 != nil {
goto sendEverythingWithoutRange
}
// we go from `-to` to the end
transformEntries = func(entries []json.RawMessage) []json.RawMessage {
if len(entries) < to {
to = len(entries)
}
return entries[len(entries)-to:]
}
} else {
// range-start - range-end, both should be valid
if err1 != nil || err2 != nil {
goto sendEverythingWithoutRange
}
// go from `from` to `to`
transformEntries = func(entries []json.RawMessage) []json.RawMessage {
if from < 0 {
from = 0
}
if len(entries) < to {
to = len(entries)
}
return entries[from:to]
}
}
var response map[string][]json.RawMessage
err = json.Unmarshal(respbytes, &response)
if err != nil {
goto sendEverythingWithoutRange
}
if entries, ok := response[unit]; ok {
response[unit] = transformEntries(entries)
}
json.NewEncoder(w).Encode(response)
return
}
sendEverythingWithoutRange:
w.Write(respbytes)
}
type LightningError struct {
Type string `json:"type"`
Name string `json:"name"`
Message string `json:"message"`
Code int `json:"code"`
FullType string `json:"fullType"`
Request lightning.JSONRPCMessage `json:"request"`
}