Skip to content

Commit

Permalink
litcli: per feature channel and peer restrictions
Browse files Browse the repository at this point in the history
Different features interpret the restrictions differently, which is why
we add the possibility of specifying restrictions per feature. Here we
change to a json format to be able to specify empty lists with [].
  • Loading branch information
bitromortac committed Oct 16, 2023
1 parent 112c9c8 commit 8a4eeb6
Showing 1 changed file with 77 additions and 44 deletions.
121 changes: 77 additions & 44 deletions cmd/litcli/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"

"github.com/lightninglabs/lightning-terminal/litrpc"
Expand Down Expand Up @@ -43,8 +41,9 @@ var addAutopilotSessionCmd = cli.Command{
Description: `
Initialize an Autopilot session.
If set for any feature, configuration flags need to be repeated for each
feature that is registered, corresponding to the order of features.`,
If set for any feature, configuration flags and channel and peer
restrict lists need to be repeated (empty if necessary) for each feature
that is registered, corresponding to the order of features.`,
Action: initAutopilotSession,
Flags: []cli.Flag{
labelFlag,
Expand All @@ -55,19 +54,21 @@ var addAutopilotSessionCmd = cli.Command{
Name: "feature",
Required: true,
},
cli.StringFlag{
Name: "channel-restrict-list",
Usage: "list of channel IDs that the " +
"Autopilot server should not " +
"perform actions on. In the " +
"form of: chanID1,chanID2,...",
cli.StringSliceFlag{
Name: "feature-channel-restrict-list",
Usage: `List of channel IDs that the ` +
`Autopilot server should not ` +
`perform actions on. In the ` +
`form of: [chanID1,chanID2,...]. ` +
`An empty list is set by using [].`,
},
cli.StringFlag{
Name: "peer-restrict-list",
Usage: "list of peer IDs that the " +
"Autopilot server should not " +
"perform actions on. In the " +
"form of: peerID1,peerID2,...",
cli.StringSliceFlag{
Name: "feature-peer-restrict-list",
Usage: `List of peer IDs that the ` +
`Autopilot server should not ` +
`perform actions on. In the ` +
`form of: ["peerID1","peerID2",...]. ` +
`An empty list is set by using [].`,
},
cli.StringFlag{
Name: "group_id",
Expand Down Expand Up @@ -196,45 +197,77 @@ func initAutopilotSession(ctx *cli.Context) error {
defer cleanup()
client := litrpc.NewAutopilotClient(clientConn)

ruleMap := &litrpc.RulesMap{
Rules: make(map[string]*litrpc.RuleValue),
features := ctx.StringSlice("feature")

perFeatureRules := make([]*litrpc.RulesMap, len(features))
for i := range features {
perFeatureRules[i] = &litrpc.RulesMap{
Rules: make(map[string]*litrpc.RuleValue),
}
}

chanRestrictList := ctx.String("channel-restrict-list")
if chanRestrictList != "" {
chanRestrictLists := ctx.StringSlice("feature-channel-restrict-list")
if len(chanRestrictLists) > 0 &&
len(features) != len(chanRestrictLists) {

return fmt.Errorf("number of features (%v) and channel "+
"restrict list (%v) must match", len(features),
len(chanRestrictLists))
}

for i, chanRestrictList := range chanRestrictLists {
var chanIDs []uint64
chans := strings.Split(chanRestrictList, ",")
for _, c := range chans {
i, err := strconv.ParseUint(c, 10, 64)
if err != nil {
return err
}
chanIDs = append(chanIDs, i)
err := json.Unmarshal([]byte(chanRestrictList), &chanIDs)
if err != nil {
return fmt.Errorf("could not parse "+
"channel restrict list for feature %v: %v",
features[i], err)
}

ruleMap.Rules[rules.ChannelRestrictName] = &litrpc.RuleValue{
Value: &litrpc.RuleValue_ChannelRestrict{
ChannelRestrict: &litrpc.ChannelRestrict{
ChannelIds: chanIDs,
},
},
if len(chanIDs) == 0 {
continue
}

perFeatureRules[i].Rules[rules.ChannelRestrictName] =
&litrpc.RuleValue{
Value: &litrpc.RuleValue_ChannelRestrict{
ChannelRestrict: &litrpc.ChannelRestrict{
ChannelIds: chanIDs,
},
},
}
}

peerRestrictList := ctx.String("peer-restrict-list")
if peerRestrictList != "" {
peerIDs := strings.Split(peerRestrictList, ",")
peerRestrictLists := ctx.StringSlice("feature-peer-restrict-list")
if len(peerRestrictLists) > 0 && len(features) != len(peerRestrictLists) {
return fmt.Errorf("number of features (%v) and peer "+
"restrict list (%v) must match", len(features),
len(peerRestrictLists))
}

ruleMap.Rules[rules.PeersRestrictName] = &litrpc.RuleValue{
Value: &litrpc.RuleValue_PeerRestrict{
PeerRestrict: &litrpc.PeerRestrict{
PeerIds: peerIDs,
},
},
for i, peerRestrictList := range peerRestrictLists {
var peerIDs []string
err := json.Unmarshal([]byte(peerRestrictList), &peerIDs)
if err != nil {
return fmt.Errorf("could not parse "+
"peer restrict list for feature %v: %v",
features[i], err)
}

if len(peerIDs) == 0 {
continue
}

perFeatureRules[i].Rules[rules.PeersRestrictName] =
&litrpc.RuleValue{
Value: &litrpc.RuleValue_PeerRestrict{
PeerRestrict: &litrpc.PeerRestrict{
PeerIds: peerIDs,
},
},
}
}

features := ctx.StringSlice("feature")
configs := ctx.StringSlice("feature-config")
if len(configs) > 0 && len(features) != len(configs) {
return fmt.Errorf("number of features (%v) and configurations "+
Expand Down Expand Up @@ -262,7 +295,7 @@ func initAutopilotSession(ctx *cli.Context) error {
}

featureMap[feature] = &litrpc.FeatureConfig{
Rules: ruleMap,
Rules: perFeatureRules[i],
Config: config,
}
}
Expand Down

0 comments on commit 8a4eeb6

Please sign in to comment.