-
Notifications
You must be signed in to change notification settings - Fork 5
/
htlcInterceptor.go
297 lines (268 loc) · 9.58 KB
/
htlcInterceptor.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"sync"
"github.com/callebtc/electronwall/config"
"github.com/callebtc/electronwall/rules"
"github.com/callebtc/electronwall/types"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
log "github.com/sirupsen/logrus"
)
// getHtlcForwardEvent returns a struct containing relevant information about the current
// forward request that the decision engine can then use
func (app *App) getHtlcForwardEvent(ctx context.Context, event *routerrpc.ForwardHtlcInterceptRequest) (types.HtlcForwardEvent, error) {
channelEdge, err := app.lnd.getPubKeyFromChannel(ctx, event.IncomingCircuitKey.ChanId)
if err != nil {
log.Errorf("[forward] Error getting pubkey for channel %s", ParseChannelID(event.IncomingCircuitKey.ChanId))
}
var pubkeyFrom, aliasFrom, pubkeyTo, aliasTo string
if channelEdge.Node1Pub != app.myInfo.IdentityPubkey {
pubkeyFrom = channelEdge.Node1Pub
} else {
pubkeyFrom = channelEdge.Node2Pub
}
aliasFrom, err = app.lnd.getNodeAlias(ctx, pubkeyFrom)
if err != nil {
aliasFrom = trimPubKey([]byte(pubkeyFrom))
log.Errorf("[forward] Error getting alias for node %s", aliasFrom)
}
// we need to figure out which side of the channel is the other end
channelEdgeTo, err := app.lnd.getPubKeyFromChannel(ctx, event.OutgoingRequestedChanId)
if err != nil {
log.Errorf("[forward] Error getting pubkey for channel %s", ParseChannelID(event.OutgoingRequestedChanId))
return types.HtlcForwardEvent{}, err
}
if channelEdgeTo.Node1Pub != app.myInfo.IdentityPubkey {
pubkeyTo = channelEdgeTo.Node1Pub
} else {
pubkeyTo = channelEdgeTo.Node2Pub
}
aliasTo, err = app.lnd.getNodeAlias(ctx, pubkeyTo)
if err != nil {
aliasTo = trimPubKey([]byte(pubkeyTo))
log.Errorf("[forward] Error getting alias for node %s", aliasTo)
}
return types.HtlcForwardEvent{
PubkeyFrom: pubkeyFrom,
AliasFrom: aliasFrom,
PubkeyTo: pubkeyTo,
AliasTo: aliasTo,
Event: event,
}, nil
}
// DispatchHTLCAcceptor is the HTLC acceptor event loop
func (app *App) DispatchHTLCAcceptor(ctx context.Context) {
go func() {
err := app.logHtlcEvents(ctx)
if err != nil {
log.Error("htlc event logger error",
"err", err)
}
}()
go func() {
err := app.interceptHtlcEvents(ctx)
if err != nil {
log.Error("htlc interceptor error",
"err", err)
}
// release wait group for htlc interceptor
ctx.Value(ctxKeyWaitGroup).(*sync.WaitGroup).Done()
}()
log.Info("[forward] Listening for incoming HTLCs")
}
// interceptHtlcEvents intercepts incoming htlc events
func (app *App) interceptHtlcEvents(ctx context.Context) error {
// interceptor, decide whether to accept or reject
interceptor, err := app.lnd.htlcInterceptor(ctx)
if err != nil {
return err
}
for {
event, err := interceptor.Recv()
if err != nil {
return err
}
go func() {
log.Tracef("[forward] HTLC event (%d->%d)", event.IncomingCircuitKey.ChanId, event.OutgoingRequestedChanId)
htlcForwardEvent, err := app.getHtlcForwardEvent(ctx, event)
if err != nil {
return
}
forward_info_string := fmt.Sprintf(
"from %s to %s (%d sat, chan_id:%s->%s, htlc_id:%d)",
htlcForwardEvent.AliasFrom,
htlcForwardEvent.AliasTo,
event.IncomingAmountMsat/1000,
ParseChannelID(event.IncomingCircuitKey.ChanId),
ParseChannelID(event.OutgoingRequestedChanId),
event.IncomingCircuitKey.HtlcId,
)
contextLogger := log.WithFields(log.Fields{
"event": "forward_request",
"in_alias": htlcForwardEvent.AliasFrom,
"out_alias": htlcForwardEvent.AliasTo,
"amount": event.IncomingAmountMsat / 1000,
"in_chan_id": ParseChannelID(event.IncomingCircuitKey.ChanId),
"out_chan_id": ParseChannelID(event.OutgoingRequestedChanId),
"htlc_id": event.IncomingCircuitKey.HtlcId,
})
// decision for routing
decision_chan := make(chan bool, 1)
list_decision, err := app.htlcInterceptDecision(ctx, event, decision_chan)
if err != nil {
return
}
rules_decision, err := rules.Apply(htlcForwardEvent, decision_chan)
if err != nil {
fmt.Printf("script error: %v", err)
return
}
accept := true
if !list_decision || !rules_decision {
accept = false
}
response := &routerrpc.ForwardHtlcInterceptResponse{
IncomingCircuitKey: event.IncomingCircuitKey,
}
switch accept {
case true:
if config.Configuration.LogJson {
contextLogger.Infof("allow")
} else {
log.Infof("[forward] ✅ Allow HTLC %s", forward_info_string)
}
response.Action = routerrpc.ResolveHoldForwardAction_RESUME
case false:
if config.Configuration.LogJson {
contextLogger.Infof("deny")
} else {
log.Infof("[forward] ❌ Deny HTLC %s", forward_info_string)
}
response.Action = routerrpc.ResolveHoldForwardAction_FAIL
}
err = interceptor.Send(response)
if err != nil {
return
}
}()
}
}
// htlcInterceptDecision implements the rules upon which the
// decision is made whether or not to relay an HTLC to the next
// peer.
// The decision is made based on the following rules:
// 1. Either use a allowlist or a denylist.
// 2. If a single channel ID is used (12320768x65536x0), check the incoming ID of the HTLC against the list.
// 3. If two channel IDs are used (7929856x65537x0->7143424x65537x0), check the incoming ID and the outgoing ID of the HTLC against the list.
func (app *App) htlcInterceptDecision(ctx context.Context, event *routerrpc.ForwardHtlcInterceptRequest, decision_chan chan bool) (bool, error) {
var accept bool
var listToParse []string
// // sleep for 60 seconds
// time.Sleep(60 * time.Second)
// determine filtering mode and list to parse
switch config.Configuration.ForwardMode {
case "allowlist":
accept = false
listToParse = config.Configuration.ForwardAllowlist
case "denylist":
accept = true
listToParse = config.Configuration.ForwardDenylist
default:
return false, fmt.Errorf("unknown forward mode: %s", config.Configuration.ForwardMode)
}
// parse list and decide
for _, forward_list_entry := range listToParse {
if forward_list_entry == "*" {
accept = !accept
break
}
if len(strings.Split(forward_list_entry, "->")) == 2 {
// check if entry is a pair of from->to
split := strings.Split(forward_list_entry, "->")
from_channel_id, to_channel_id := split[0], split[1]
if (ParseChannelID(event.IncomingCircuitKey.ChanId) == from_channel_id || from_channel_id == "*") &&
(ParseChannelID(event.OutgoingRequestedChanId) == to_channel_id || to_channel_id == "*") {
accept = !accept
log.Tracef("[test] Incoming: %s <-> %s, Outgoing: %s <-> %s", ParseChannelID(event.IncomingCircuitKey.ChanId), from_channel_id, ParseChannelID(event.OutgoingRequestedChanId), to_channel_id)
break
}
} else {
// single entry
if ParseChannelID(event.IncomingCircuitKey.ChanId) == forward_list_entry {
accept = !accept
break
}
}
}
// decision_chan <- accept
log.Infof("[list] decision: %t", accept)
return accept, nil
}
// logHtlcEvents reports on incoming htlc events
func (app *App) logHtlcEvents(ctx context.Context) error {
// htlc event subscriber, reports on incoming htlc events
stream, err := app.lnd.subscribeHtlcEvents(ctx, &routerrpc.SubscribeHtlcEventsRequest{})
if err != nil {
return err
}
for {
event, err := stream.Recv()
if err != nil {
return err
}
// we only care about HTLC forward events
if event.EventType != routerrpc.HtlcEvent_FORWARD {
continue
}
contextLogger := func(event *routerrpc.HtlcEvent) *log.Entry {
b, err := json.Marshal(event)
if err != nil {
panic(err)
}
return log.WithFields(log.Fields{
"type": "forward",
"event": string(b),
})
}
switch event.Event.(type) {
case *routerrpc.HtlcEvent_SettleEvent:
if config.Configuration.LogJson {
contextLogger(event).Infof("SettleEvent")
// contextLogger.Debugf("[forward] Preimage: %s", hex.EncodeToString(event.GetSettleEvent().Preimage))
} else {
log.Infof("[forward] ⚡️ HTLC SettleEvent (chan_id:%s, htlc_id:%d)", ParseChannelID(event.IncomingChannelId), event.IncomingHtlcId)
if event.GetSettleEvent() != nil && event.GetSettleEvent().Preimage != nil {
log.Debugf("[forward] Preimage: %s", hex.EncodeToString(event.GetSettleEvent().Preimage))
}
}
case *routerrpc.HtlcEvent_ForwardFailEvent:
if config.Configuration.LogJson {
contextLogger(event).Infof("ForwardFailEvent")
// contextLogger.Debugf("[forward] Reason: %s", event.GetForwardFailEvent())
} else {
log.Infof("[forward] HTLC ForwardFailEvent (chan_id:%s, htlc_id:%d)", ParseChannelID(event.IncomingChannelId), event.IncomingHtlcId)
// log.Debugf("[forward] Reason: %s", event.GetForwardFailEvent().String())
}
case *routerrpc.HtlcEvent_ForwardEvent:
if config.Configuration.LogJson {
contextLogger(event).Infof("ForwardEvent")
} else {
log.Infof("[forward] HTLC ForwardEvent (chan_id:%s, htlc_id:%d)", ParseChannelID(event.IncomingChannelId), event.IncomingHtlcId)
}
// log.Infof("[forward] HTLC ForwardEvent (chan_id:%s, htlc_id:%d)", ParseChannelID(event.IncomingChannelId), event.IncomingHtlcId)
// log.Debugf("[forward] Details: %s", event.GetForwardEvent().String())
case *routerrpc.HtlcEvent_LinkFailEvent:
if config.Configuration.LogJson {
contextLogger(event).Infof("LinkFailEvent")
// contextLogger(event).Debugf("[forward] Reason: %s", event.GetLinkFailEvent().FailureString)
} else {
log.Infof("[forward] HTLC LinkFailEvent (chan_id:%s, htlc_id:%d)", ParseChannelID(event.IncomingChannelId), event.IncomingHtlcId)
log.Debugf("[forward] Reason: %s", event.GetLinkFailEvent().FailureString)
}
}
}
}