Skip to content

Commit

Permalink
cli: display info for socks5 proxy, add commands me list_proxies an…
Browse files Browse the repository at this point in the history
…d `me set_proxy`
  • Loading branch information
pymq committed Jan 12, 2025
1 parent 909c91f commit 91bfd0e
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 22 deletions.
66 changes: 51 additions & 15 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
"strings"

"github.com/GrigoryKrasnochub/updaterini"
"github.com/anywherelan/awl/api/apiclient"
"github.com/anywherelan/awl/config"
"github.com/anywherelan/awl/update"
"github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
"github.com/urfave/cli/v2"

"github.com/anywherelan/awl/api/apiclient"
"github.com/anywherelan/awl/config"
"github.com/anywherelan/awl/update"
)

const (
Expand Down Expand Up @@ -86,10 +87,9 @@ func (a *Application) init() {
Usage: "Group of commands to work with your status and settings",
Subcommands: []*cli.Command{
{
Name: "status",
Aliases: []string{"stats"},
Usage: "Print your server status, network stats",
Before: a.initApiConnection,
Name: "status",
Usage: "Print your server status, network stats",
Before: a.initApiConnection,
Action: func(c *cli.Context) error {
return printStatus(a.api)
},
Expand Down Expand Up @@ -117,6 +117,36 @@ func (a *Application) init() {
return renameMe(a.api, c.String("name"))
},
},
{
Name: "list_proxies",
Usage: "Prints list of available SOCKS5 proxies",
Before: a.initApiConnection,
Action: func(c *cli.Context) error {
return listProxies(a.api)
},
},
{
Name: "set_proxy",
Usage: "Sets SOCKS5 proxy for your peer, empty pid/name means disable proxy",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pid",
Usage: "peer id",
Required: false,
},
&cli.StringFlag{
Name: "name",
Usage: "peer name",
Required: false,
},
},
Before: func(c *cli.Context) error {
return a.initApiAndPeerId(c, false)
},
Action: func(c *cli.Context) error {
return setProxy(a.api, c.String("pid"))
},
},
},
},
{
Expand All @@ -131,10 +161,10 @@ func (a *Application) init() {
Name: "format",
Aliases: []string{"f"},
Required: false,
Value: "npslucv",
Value: "npslucev",
Usage: "control table columns list and order.Each char add column, write column chars together without gap. Use these chars to add specific columns:\n " +
"n - peers number\n p - peers name, domain and ip address\n i - peers id\n s - peers status\n l - peers last seen datetime\n v - peers awl version" +
"\n u - network usage by peer (in/out)\n c - list of peers connections (IP address + protocol)\n ",
"\n u - network usage by peer (in/out)\n c - list of peers connections (IP address + protocol)\n e - exit node status\n ",
},
},
Before: a.initApiConnection,
Expand Down Expand Up @@ -185,7 +215,7 @@ func (a *Application) init() {
Required: false,
},
},
Before: a.initApiAndPeerId,
Before: a.initApiAndPeerIdRequired,
Action: func(c *cli.Context) error {
return removePeer(a.api, c.String("pid"))
},
Expand All @@ -210,7 +240,7 @@ func (a *Application) init() {
Required: true,
},
},
Before: a.initApiAndPeerId,
Before: a.initApiAndPeerIdRequired,
Action: func(c *cli.Context) error {
return changePeerAlias(a.api, c.String("pid"), c.String("new_name"))
},
Expand All @@ -235,7 +265,7 @@ func (a *Application) init() {
Required: true,
},
},
Before: a.initApiAndPeerId,
Before: a.initApiAndPeerIdRequired,
Action: func(c *cli.Context) error {
return changePeerDomain(a.api, c.String("pid"), c.String("domain"))
},
Expand All @@ -260,7 +290,7 @@ func (a *Application) init() {
Required: false,
},
},
Before: a.initApiAndPeerId,
Before: a.initApiAndPeerIdRequired,
Action: func(c *cli.Context) error {
return setAllowUsingAsExitNode(a.api, c.String("pid"), c.Bool("allow"))
},
Expand Down Expand Up @@ -408,7 +438,11 @@ func (a *Application) initApiConnection(c *cli.Context) (err error) {
return nil
}

func (a *Application) initApiAndPeerId(c *cli.Context) error {
func (a *Application) initApiAndPeerIdRequired(c *cli.Context) error {
return a.initApiAndPeerId(c, true)
}

func (a *Application) initApiAndPeerId(c *cli.Context, isRequired bool) error {
err := a.initApiConnection(c)
if err != nil {
return err
Expand All @@ -419,8 +453,10 @@ func (a *Application) initApiAndPeerId(c *cli.Context) error {
return nil
}
alias := c.String("name")
if alias == "" {
if alias == "" && isRequired {
return fmt.Errorf("peerID or name should be defined")
} else if alias == "" && !isRequired {
return nil
}

pid, err = getPeerIdByAlias(a.api, alias)
Expand Down
50 changes: 43 additions & 7 deletions cli/me.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"strings"
"time"

"github.com/anywherelan/awl/api/apiclient"
"github.com/mdp/qrterminal/v3"
"github.com/olekukonko/tablewriter"

"github.com/anywherelan/awl/api/apiclient"
)

func printStatus(api *apiclient.Client) error {
Expand All @@ -17,17 +18,15 @@ func printStatus(api *apiclient.Client) error {
return err
}

dnsStatus := "working"
if !stats.IsAwlDNSSetAsSystem {
dnsStatus = "not working"
}

table := tablewriter.NewWriter(os.Stdout)
table.AppendBulk([][]string{
{"Download rate", fmt.Sprintf("%s (%s)", stats.NetworkStatsInIECUnits.RateIn, stats.NetworkStatsInIECUnits.TotalIn)},
{"Upload rate", fmt.Sprintf("%s (%s)", stats.NetworkStatsInIECUnits.RateOut, stats.NetworkStatsInIECUnits.TotalOut)},
{"Bootstrap peers", fmt.Sprintf("%d/%d", stats.TotalBootstrapPeers, stats.ConnectedBootstrapPeers)},
{"DNS", dnsStatus},
{"DNS", formatWorkingStatus(stats.IsAwlDNSSetAsSystem)},
{"SOCKS5 Proxy", formatWorkingStatus(stats.SOCKS5.ListenerEnabled)},
{"SOCKS5 Proxy address", stats.SOCKS5.ListenAddress},
{"SOCKS5 Proxy exit node", stats.SOCKS5.UsingPeerName},
{"Reachability", strings.ToLower(stats.Reachability)},
{"Uptime", stats.Uptime.Round(time.Second).String()},
{"Server version", stats.ServerVersion},
Expand All @@ -38,6 +37,13 @@ func printStatus(api *apiclient.Client) error {
return nil
}

func formatWorkingStatus(working bool) string {
if working {
return "working"
}
return "not working"
}

func printPeerId(api *apiclient.Client) error {
info, err := api.PeerInfo()
if err != nil {
Expand All @@ -60,3 +66,33 @@ func renameMe(api *apiclient.Client, newName string) error {

return nil
}

func listProxies(api *apiclient.Client) error {
proxies, err := api.ListAvailableProxies()
if err != nil {
return err
}

if len(proxies) == 0 {
fmt.Println("no available proxies")
return nil
}

fmt.Println("Proxies:")
for _, proxy := range proxies {
fmt.Printf("- peer name: %s | peer id: %s\n", proxy.PeerName, proxy.PeerID)
}

return nil
}

func setProxy(api *apiclient.Client, peerID string) error {
err := api.UpdateProxySettings(peerID)
if err != nil {
return err
}

fmt.Println("proxy settings updated successfully")

return nil
}
4 changes: 4 additions & 0 deletions cli/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func printPeersStatus(api *apiclient.Client, format string) error {
TableFormatNetworkUsage = "u"
TableFormatConnection = "c"
TableFormatVersion = "v"
TableFormatExitNode = "e"
)

fHeaderMap := map[string]string{
Expand All @@ -35,6 +36,7 @@ func printPeersStatus(api *apiclient.Client, format string) error {
TableFormatNetworkUsage: "network usage\n(↓in/↑out)",
TableFormatConnection: "connections\naddress | protocol",
TableFormatVersion: "version",
TableFormatExitNode: "exit node",
}

if len(format) < 1 {
Expand Down Expand Up @@ -122,6 +124,8 @@ func printPeersStatus(api *apiclient.Client, format string) error {
row = append(row, strings.Join(consStr, "\n"))
case TableFormatVersion:
row = append(row, peer.Version)
case TableFormatExitNode:
row = append(row, fmt.Sprintf("we allow: %v\npeer allowed: %v", peer.WeAllowUsingAsExitNode, peer.AllowedUsingAsExitNode))
}
}
table.Append(row)
Expand Down

0 comments on commit 91bfd0e

Please sign in to comment.