|
| 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 | +} |
0 commit comments