Skip to content

Commit

Permalink
feat(nnr): update support
Browse files Browse the repository at this point in the history
update support

Signed-off-by: ysicing <i@ysicing.me>
  • Loading branch information
ysicing committed Oct 10, 2023
1 parent da13ace commit 4bab98e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 6 deletions.
54 changes: 48 additions & 6 deletions cmd/nnr/nnr.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ func NewCmdNNR(f factory.Factory) *cobra.Command {
}
cmd.AddCommand(listServers(f))
cmd.AddCommand(listRules(f))
cmd.AddCommand(addRule(f))
cmd.AddCommand(delRule(f))
cmd.AddCommand(updateRule(f))
cmd.AddCommand(listRemoteNode(f))
// cmd.AddCommand(addRule(f))
// cmd.AddCommand(delRule(f))
// cmd.AddCommand(updateRule(f))
cmd.PersistentFlags().StringVarP(&token, "token", "t", os.Getenv("NNR_TOKEN"), "token")
return cmd
}

func listServers(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "nodes",
Aliases: []string{"servers"},
Short: "list servers",
Use: "servers",
Short: "list servers",
RunE: func(cmd *cobra.Command, args []string) error {
api := nnr.New(token)
s, err := api.ListServers()
Expand Down Expand Up @@ -74,6 +74,12 @@ func listRules(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "rules",
Short: "list rules",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(token) == 0 {
return fmt.Errorf("token is empty")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
api := nnr.New(token)
servers := api.ServersMap()
Expand Down Expand Up @@ -114,6 +120,42 @@ func listRules(f factory.Factory) *cobra.Command {
return cmd
}

func listRemoteNode(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "nodes",
Aliases: []string{"remotes"},
Short: "list remote nodes",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(token) == 0 {
return fmt.Errorf("token is empty")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
api := nnr.New(token)
s, err := api.SortRemoteNode()
if err != nil {
return err
}
if len(s) == 0 {
f.GetLog().Infof("no nodes found")
return nil
}
f.GetLog().Infof("found %d node", len(s))
sort.Slice(s, func(i, j int) bool {
return s[i].Traffic >= s[j].Traffic
})
table := uitable.New()
table.AddRow("远程地址", "流量")
for _, index := range s {
table.AddRow(index.Remote, util.Traffic(index.Traffic))
}
return output.EncodeTable(os.Stdout, table)
},
}
return cmd
}

func addRule(f factory.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "add",
Expand Down
39 changes: 39 additions & 0 deletions internal/pkg/nnr/nnr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package nnr

import (
"fmt"

"github.com/cockroachdb/errors"
"github.com/imroc/req/v3"
"github.com/ysicing/tiga/common"
Expand Down Expand Up @@ -63,3 +65,40 @@ func (o *Option) ListRules() ([]Rule, error) {
}
return rulesResp.Data, nil
}

func (o *Option) ListRemoteNode() (map[string]Node, error) {
var rulesResp RulesResp
_, err := o.SetSuccessResult(&rulesResp).Post("https://nnr.moe/api/rules")
if err != nil {
return nil, err
}
if rulesResp.Status != 1 {
return nil, errors.New("list rules failed")
}
mapNodes := make(map[string]Node, 60)
for _, rule := range rulesResp.Data {
key := fmt.Sprintf("%s:%v", rule.Remote, rule.RPort)
if _, ok := mapNodes[key]; ok {
node := mapNodes[key]
node.Traffic = node.Traffic + rule.Traffic
mapNodes[key] = node
} else {
mapNodes[key] = Node{
Remote: key,
}
}
}
return mapNodes, nil
}

func (o *Option) SortRemoteNode() ([]Node, error) {
rNodes, err := o.ListRemoteNode()
if err != nil {
return nil, err
}
var nodes []Node
for _, node := range rNodes {
nodes = append(nodes, node)
}
return nodes, nil
}
25 changes: 25 additions & 0 deletions internal/pkg/nnr/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,28 @@ type RulesResp struct {
Status int `json:"status,omitempty"`
Data []Rule `json:"data,omitempty"`
}

type Node struct {
Traffic int64 `json:"traffic,omitempty"`
Remote string `json:"remote,omitempty"`
}

type AddRule struct {
Sid string `json:"sid"`
Remote string `json:"remote"`
RPort int `json:"rport"`
Type string `json:"type"`
Name string `json:"name,omitempty"`
}

type UpdateRule struct {
Rid string `json:"rid"`
Remote string `json:"remote"`
RPort int `json:"rport"`
Type string `json:"type"`
Name string `json:"name,omitempty"`
}

type GetOrDeleteRule struct {
Rid string `json:"rid"`
}

0 comments on commit 4bab98e

Please sign in to comment.