-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (99 loc) · 2.72 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
package main
import (
"context"
"log"
"os"
"sync"
"github.com/jessevdk/go-flags"
"github.com/lightninglabs/lndclient"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/ziggie1984/lncare/config"
)
type key int
const (
ctxKeyWaitGroup key = iota
)
type Channel struct {
*lnrpc.Channel
protectAgainstUnilateralClose bool
}
type lncare struct {
lnd LndClient
myInfo *lnrpc.GetInfoResponse
channels []*lnrpc.Channel
disabledChannels map[uint64]Channel
channelPairs map[string][2]*lnrpc.Channel
chanCache map[uint64]*lnrpc.ChannelEdge
excludeTo map[uint64]struct{}
excludeFrom map[uint64]struct{}
excludeBoth map[uint64]struct{}
}
func newLncareInstance(ctx context.Context, lnd *LndClient) *lncare {
myInfo, err := lnd.getMyInfo(ctx)
if err != nil {
log.Fatalf("Could not get my node info: %s", err)
}
return &lncare{
lnd: *lnd,
myInfo: myInfo,
}
}
func welcome() {
log.Println("---- ⚡️ Running lncare ----")
log.Println("--------------------------------------")
}
func newLndClient(ctx context.Context) (*LndClient, error) {
conn, err := lndclient.NewBasicConn(config.Configuration.Connect, config.Configuration.TLSCert, config.Configuration.MacaroonDir, config.Configuration.Network,
lndclient.MacFilename(config.Configuration.MacaroonFilename))
if err != nil {
log.Fatalf("Connection failed: %s", err)
return &LndClient{}, err
}
client := lnrpc.NewLightningClient(conn)
router := routerrpc.NewRouterClient(conn)
return &LndClient{
client: client,
conn: conn,
router: router,
}, nil
}
func main() {
parser := flags.NewParser(&config.Configuration, flags.Default)
_, err := parser.Parse()
if err != nil {
// This prevents and error message when using the help flags
switch t := err.(type) {
case *flags.Error:
if t.Type != 5 {
log.Fatalf("Error when parsing command line options: %s", err)
} else {
os.Exit(1)
}
default:
log.Fatalf("Unexpected error when parsing command line option with : %s", err)
}
}
welcome()
ctx := context.Background()
for {
lnd, err := newLndClient(ctx)
if err != nil {
log.Fatalf("Failed to create lnd client: %s", err)
return
}
lncare := newLncareInstance(ctx, lnd)
if len(lncare.myInfo.Alias) > 0 {
log.Printf("Connected to %s (%s)", lncare.myInfo.Alias, trimPubKey([]byte(lncare.myInfo.IdentityPubkey)))
} else {
log.Printf("Connected to %s", lncare.myInfo.IdentityPubkey)
}
var wg sync.WaitGroup
ctx = context.WithValue(ctx, ctxKeyWaitGroup, &wg)
wg.Add(2)
lncare.DispatchChannelManager(ctx)
lncare.DispatchPeerManager(ctx)
wg.Wait()
log.Println("All routines stopped. Waiting for new connection.")
}
}