-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
162 lines (135 loc) · 3.48 KB
/
main.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
package main
import (
"context"
"encoding/hex"
"flag"
"fmt"
"log/slog"
"os"
"runtime/debug"
"github.com/aftermath2/acceptlnd/config"
"github.com/aftermath2/acceptlnd/lightning"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/pkg/errors"
)
func main() {
configPath := flag.String("config", "acceptlnd.yml", "Path to the configuration file")
debug := flag.Bool("debug", false, "Enable debug level logging")
version := flag.Bool("version", false, "Show version")
flag.Parse()
if *version {
printVersion()
os.Exit(0)
}
level := &slog.LevelVar{}
if *debug {
level.Set(slog.LevelDebug)
}
loggerOpts := &slog.HandlerOptions{
AddSource: *debug,
Level: level,
}
logger := slog.New(slog.NewTextHandler(os.Stdout, loggerOpts))
slog.SetDefault(logger)
config, err := config.Load(*configPath)
if err != nil {
fatal(err)
}
client, err := lightning.NewClient(config)
if err != nil {
fatal(err)
}
if err := handleChannelRequests(config, client); err != nil {
fatal(err)
}
}
func fatal(err error) {
slog.Error(err.Error())
os.Exit(1)
}
// handleChannelRequests listens to the ChannnelAcceptor RPC stream and accepts/rejects requests.
func handleChannelRequests(config config.Config, client lightning.Client) error {
ctx := context.Background()
stream, err := client.ChannelAcceptor(ctx)
if err != nil {
return errors.Wrap(err, "subscribing to the channel acceptor stream")
}
slog.Info("Listening for channel requests")
for {
req, err := stream.Recv()
if err != nil {
return errors.Wrap(err, "receiving channel request")
}
slog.Debug("Channel opening request", slog.Any("request", req))
resp, err := handleRequest(config, client, req)
if err != nil {
resp.Error = err.Error()
} else {
resp.Accept = true
}
if err := stream.Send(resp); err != nil {
return errors.Wrap(err, "sending channel response")
}
logResponse(response{
accepted: resp.Accept,
id: hex.EncodeToString(req.PendingChanId),
publicKey: hex.EncodeToString(req.NodePubkey),
err: resp.Error,
})
}
}
func handleRequest(
config config.Config,
client lightning.Client,
req *lnrpc.ChannelAcceptRequest,
) (*lnrpc.ChannelAcceptResponse, error) {
ctx := context.Background()
resp := &lnrpc.ChannelAcceptResponse{Accept: false, PendingChanId: req.PendingChanId}
node, err := client.GetInfo(ctx, &lnrpc.GetInfoRequest{})
if err != nil {
return resp, errors.New("Internal server error")
}
getPeerInfoReq := &lnrpc.NodeInfoRequest{
PubKey: hex.EncodeToString(req.NodePubkey),
IncludeChannels: true,
}
peer, err := client.GetNodeInfo(ctx, getPeerInfoReq)
if err != nil {
return resp, errors.New("Internal server error")
}
slog.Debug("Peer node information", slog.Any("node", peer))
for _, policy := range config.Policies {
if err := policy.Evaluate(req, resp, node, peer); err != nil {
return resp, err
}
}
return resp, nil
}
type response struct {
id string
publicKey string
err string
accepted bool
}
func logResponse(res response) {
args := []any{
slog.Bool("accepted", res.accepted),
slog.String("id", res.id),
slog.String("public_key", res.publicKey),
}
if !res.accepted {
args = append(args, slog.String("error", res.err))
}
slog.Info("New request received", args...)
}
func printVersion() {
bi, _ := debug.ReadBuildInfo()
var commit string
for _, s := range bi.Settings {
if s.Key == "vcs.revision" {
commit = s.Value
break
}
}
fmt.Println("AcceptLND", bi.Main.Version, commit)
}