Skip to content

Commit 6de34a8

Browse files
committed
litcli: add lit-autopilot commands
1 parent 3b67325 commit 6de34a8

File tree

3 files changed

+228
-28
lines changed

3 files changed

+228
-28
lines changed

cmd/litcli/autopilot.go

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/hex"
6+
"strconv"
7+
"strings"
8+
"time"
9+
10+
"github.com/lightninglabs/lightning-terminal/litrpc"
11+
"github.com/lightninglabs/lightning-terminal/rules"
12+
"github.com/urfave/cli"
13+
)
14+
15+
var autopilotCommands = cli.Command{
16+
Name: "autopilot",
17+
Usage: "manage autopilot sessions",
18+
Category: "Autopilot",
19+
Subcommands: []cli.Command{
20+
{
21+
Name: "features",
22+
ShortName: "f",
23+
Usage: "List available features",
24+
Action: listFeatures,
25+
},
26+
{
27+
Name: "add",
28+
ShortName: "a",
29+
Usage: "Initialize an autopilot session",
30+
Action: initAutopilotSession,
31+
Flags: []cli.Flag{
32+
labelFlag,
33+
expiryFlag,
34+
mailboxServerAddrFlag,
35+
devserver,
36+
cli.StringSliceFlag{
37+
Name: "feature",
38+
Required: true,
39+
},
40+
cli.StringFlag{
41+
Name: "channel-restrict-list",
42+
Usage: "list of channel IDs that the " +
43+
"autopilot server should not " +
44+
"perform actions on. In the " +
45+
"form of: chanID1,chanID2,...",
46+
},
47+
cli.StringFlag{
48+
Name: "peer-restrict-list",
49+
Usage: "list of peer IDs that the " +
50+
"autopilot server should not " +
51+
"perform actions on. In the " +
52+
"form of: peerID1,peerID2,...",
53+
},
54+
},
55+
},
56+
{
57+
Name: "revoke",
58+
ShortName: "r",
59+
Usage: "revoke an autopilot session",
60+
Description: "Revoke an active autopilot session",
61+
Action: revokeAutopilotSession,
62+
Flags: []cli.Flag{
63+
cli.StringFlag{
64+
Name: "localpubkey",
65+
Usage: "local pubkey of the " +
66+
"session to revoke",
67+
Required: true,
68+
},
69+
},
70+
},
71+
},
72+
}
73+
74+
func revokeAutopilotSession(ctx *cli.Context) error {
75+
ctxb := context.Background()
76+
clientConn, cleanup, err := connectClient(ctx)
77+
if err != nil {
78+
return err
79+
}
80+
defer cleanup()
81+
client := litrpc.NewAutopilotClient(clientConn)
82+
83+
pubkey, err := hex.DecodeString(ctx.String("localpubkey"))
84+
if err != nil {
85+
return err
86+
}
87+
88+
resp, err := client.RevokeAutopilotSession(
89+
ctxb, &litrpc.RevokeAutopilotSessionRequest{
90+
LocalPublicKey: pubkey,
91+
},
92+
)
93+
if err != nil {
94+
return err
95+
}
96+
97+
printRespJSON(resp)
98+
99+
return nil
100+
}
101+
102+
func listFeatures(ctx *cli.Context) error {
103+
ctxb := context.Background()
104+
clientConn, cleanup, err := connectClient(ctx)
105+
if err != nil {
106+
return err
107+
}
108+
defer cleanup()
109+
client := litrpc.NewAutopilotClient(clientConn)
110+
111+
resp, err := client.ListAutopilotFeatures(
112+
ctxb, &litrpc.ListAutopilotFeaturesRequest{},
113+
)
114+
if err != nil {
115+
return err
116+
}
117+
118+
printRespJSON(resp)
119+
120+
return nil
121+
}
122+
123+
func initAutopilotSession(ctx *cli.Context) error {
124+
sessionLength := time.Second * time.Duration(ctx.Uint64("expiry"))
125+
sessionExpiry := time.Now().Add(sessionLength).Unix()
126+
127+
ctxb := context.Background()
128+
clientConn, cleanup, err := connectClient(ctx)
129+
if err != nil {
130+
return err
131+
}
132+
defer cleanup()
133+
client := litrpc.NewAutopilotClient(clientConn)
134+
135+
ruleMap := &litrpc.RulesMap{
136+
Rules: make(map[string]*litrpc.RuleValue),
137+
}
138+
139+
chanRestrictList := ctx.String("channel-restrict-list")
140+
if chanRestrictList != "" {
141+
var chanIDs []uint64
142+
chans := strings.Split(chanRestrictList, ",")
143+
for _, c := range chans {
144+
i, err := strconv.ParseUint(c, 10, 64)
145+
if err != nil {
146+
return err
147+
}
148+
chanIDs = append(chanIDs, i)
149+
}
150+
151+
ruleMap.Rules[rules.ChannelRestrictName] = &litrpc.RuleValue{
152+
Value: &litrpc.RuleValue_ChannelRestrict{
153+
ChannelRestrict: &litrpc.ChannelRestrict{
154+
ChannelIds: chanIDs,
155+
},
156+
},
157+
}
158+
}
159+
160+
peerRestrictList := ctx.String("peer-restrict-list")
161+
if peerRestrictList != "" {
162+
peerIDs := strings.Split(peerRestrictList, ",")
163+
164+
ruleMap.Rules[rules.PeersRestrictName] = &litrpc.RuleValue{
165+
Value: &litrpc.RuleValue_PeerRestrict{
166+
PeerRestrict: &litrpc.PeerRestrict{
167+
PeerIds: peerIDs,
168+
},
169+
},
170+
}
171+
}
172+
173+
featureMap := make(map[string]*litrpc.FeatureConfig)
174+
for _, feature := range ctx.StringSlice("feature") {
175+
featureMap[feature] = &litrpc.FeatureConfig{
176+
Rules: ruleMap,
177+
Config: nil,
178+
}
179+
}
180+
181+
resp, err := client.AddAutopilotSession(
182+
ctxb, &litrpc.AddAutopilotSessionRequest{
183+
Label: ctx.String("label"),
184+
ExpiryTimestampSeconds: uint64(sessionExpiry),
185+
MailboxServerAddr: ctx.String("mailboxserveraddr"),
186+
DevServer: ctx.Bool("devserver"),
187+
Features: featureMap,
188+
},
189+
)
190+
if err != nil {
191+
return err
192+
}
193+
194+
printRespJSON(resp)
195+
196+
return nil
197+
}

cmd/litcli/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func main() {
8686
app.Commands = append(app.Commands, sessionCommands...)
8787
app.Commands = append(app.Commands, accountsCommands...)
8888
app.Commands = append(app.Commands, listActionsCommand)
89+
app.Commands = append(app.Commands, autopilotCommands)
8990

9091
err := app.Run(os.Args)
9192
if err != nil {

cmd/litcli/sessions.go

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,28 @@ var (
1515
// defaultSessionExpiry is the default time a session can be used for.
1616
// The current value evaluates to 90 days.
1717
defaultSessionExpiry = time.Hour * 24 * 90
18+
19+
labelFlag = cli.StringFlag{
20+
Name: "label",
21+
Usage: "session label",
22+
Required: true,
23+
}
24+
expiryFlag = cli.Uint64Flag{
25+
Name: "expiry",
26+
Usage: "number of seconds that the session should " +
27+
"remain active",
28+
Value: uint64(defaultSessionExpiry.Seconds()),
29+
}
30+
mailboxServerAddrFlag = cli.StringFlag{
31+
Name: "mailboxserveraddr",
32+
Usage: "the host:port of the mailbox server to be used",
33+
Value: "mailbox.terminal.lightning.today:443",
34+
}
35+
devserver = cli.BoolFlag{
36+
Name: "devserver",
37+
Usage: "set to true to skip verification of the " +
38+
"server's tls cert.",
39+
}
1840
)
1941

2042
var sessionCommands = []cli.Command{
@@ -38,26 +60,10 @@ var addSessionCommand = cli.Command{
3860
Description: "Add a new active session.",
3961
Action: addSession,
4062
Flags: []cli.Flag{
41-
cli.StringFlag{
42-
Name: "label",
43-
Usage: "session label",
44-
},
45-
cli.Uint64Flag{
46-
Name: "expiry",
47-
Usage: "number of seconds that the session should " +
48-
"remain active",
49-
Value: uint64(defaultSessionExpiry.Seconds()),
50-
},
51-
cli.StringFlag{
52-
Name: "mailboxserveraddr",
53-
Usage: "the host:port of the mailbox server to be used",
54-
Value: "mailbox.terminal.lightning.today:443",
55-
},
56-
cli.BoolFlag{
57-
Name: "devserver",
58-
Usage: "set to true to skip verification of the " +
59-
"server's tls cert.",
60-
},
63+
labelFlag,
64+
expiryFlag,
65+
mailboxServerAddrFlag,
66+
devserver,
6167
cli.StringFlag{
6268
Name: "type",
6369
Usage: "session type to be created which will " +
@@ -90,11 +96,6 @@ func addSession(ctx *cli.Context) error {
9096
defer cleanup()
9197
client := litrpc.NewSessionsClient(clientConn)
9298

93-
label := ctx.String("label")
94-
if label == "" {
95-
return fmt.Errorf("must set a label for the session")
96-
}
97-
9899
sessTypeStr := ctx.String("type")
99100
sessType, err := parseSessionType(sessTypeStr)
100101
if err != nil {
@@ -115,7 +116,7 @@ func addSession(ctx *cli.Context) error {
115116
ctxb := context.Background()
116117
resp, err := client.AddSession(
117118
ctxb, &litrpc.AddSessionRequest{
118-
Label: label,
119+
Label: ctx.String("label"),
119120
SessionType: sessType,
120121
ExpiryTimestampSeconds: uint64(sessionExpiry),
121122
MailboxServerAddr: ctx.String("mailboxserveraddr"),
@@ -260,8 +261,9 @@ var revokeSessionCommand = cli.Command{
260261
Action: revokeSession,
261262
Flags: []cli.Flag{
262263
cli.StringFlag{
263-
Name: "localpubkey",
264-
Usage: "local pubkey of the session to revoke",
264+
Name: "localpubkey",
265+
Usage: "local pubkey of the session to revoke",
266+
Required: true,
265267
},
266268
},
267269
}

0 commit comments

Comments
 (0)