gitlab.com/SkynetLabs/skyd@v1.6.9/cmd/skyc/gatewaycmd.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "text/tabwriter" 7 "time" 8 9 "github.com/spf13/cobra" 10 11 "gitlab.com/NebulousLabs/errors" 12 "go.sia.tech/siad/modules" 13 ) 14 15 var ( 16 gatewayAddressCmd = &cobra.Command{ 17 Use: "address", 18 Short: "Print the gateway address", 19 Long: "Print the network address of the gateway.", 20 Run: wrap(gatewayaddresscmd), 21 } 22 23 gatewayBandwidthCmd = &cobra.Command{ 24 Use: "bandwidth", 25 Short: "returns the total upload and download bandwidth usage for the gateway", 26 Long: `returns the total upload and download bandwidth usage for the gateway 27 and the duration of the bandwidth tracking.`, 28 Run: wrap(gatewaybandwidthcmd), 29 } 30 31 gatewayCmd = &cobra.Command{ 32 Use: "gateway", 33 Short: "Perform gateway actions", 34 Long: "View and manage the gateway's connected peers.", 35 Run: wrap(gatewaycmd), 36 } 37 38 gatewayBlocklistCmd = &cobra.Command{ 39 Use: "blocklist", 40 Short: "View and manage the gateway's blocklisted peers", 41 Long: "Display and manage the peers currently on the gateway blocklist.", 42 Run: wrap(gatewayblocklistcmd), 43 } 44 45 gatewayBlocklistAppendCmd = &cobra.Command{ 46 Use: "append [ip] [ip] [ip] [ip]...", 47 Short: "Adds new ip address(es) to the gateway blocklist.", 48 Long: `Adds new ip address(es) to the gateway blocklist. 49 Accepts a list of ip addresses or domain names as individual inputs. 50 51 For example: siac gateway blocklist append 123.123.123.123 111.222.111.222 mysiahost.duckdns.org`, 52 Run: gatewayblocklistappendcmd, 53 } 54 55 gatewayBlocklistClearCmd = &cobra.Command{ 56 Use: "clear", 57 Short: "Clear the blocklisted peers list", 58 Long: `Clear the blocklisted peers list. 59 60 For example: siac gateway blocklist clear`, 61 Run: gatewayblocklistclearcmd, 62 } 63 64 gatewayBlocklistRemoveCmd = &cobra.Command{ 65 Use: "remove [ip] [ip] [ip] [ip]...", 66 Short: "Remove ip address(es) from the gateway blocklist.", 67 Long: `Remove ip address(es) from the gateway blocklist. 68 Accepts a list of ip addresses or domain names as individual inputs. 69 70 For example: siac gateway blocklist remove 123.123.123.123 111.222.111.222 mysiahost.duckdns.org`, 71 Run: gatewayblocklistremovecmd, 72 } 73 74 gatewayBlocklistSetCmd = &cobra.Command{ 75 Use: "set [ip] [ip] [ip] [ip]...", 76 Short: "Set the gateway's blocklist", 77 Long: `Set the gateway's blocklist. 78 Accepts a list of ip addresses or domain names as individual inputs. 79 80 For example: siac gateway blocklist set 123.123.123.123 111.222.111.222 mysiahost.duckdns.org`, 81 Run: gatewayblocklistsetcmd, 82 } 83 84 gatewayConnectCmd = &cobra.Command{ 85 Use: "connect [address]", 86 Short: "Connect to a peer", 87 Long: "Connect to a peer and add it to the node list.", 88 Run: wrap(gatewayconnectcmd), 89 } 90 91 gatewayDisconnectCmd = &cobra.Command{ 92 Use: "disconnect [address]", 93 Short: "Disconnect from a peer", 94 Long: "Disconnect from a peer. Does not remove the peer from the node list.", 95 Run: wrap(gatewaydisconnectcmd), 96 } 97 98 gatewayListCmd = &cobra.Command{ 99 Use: "list", 100 Short: "View a list of peers", 101 Long: "View the current peer list.", 102 Run: wrap(gatewaylistcmd), 103 } 104 105 gatewayRatelimitCmd = &cobra.Command{ 106 Use: "ratelimit [maxdownloadspeed] [maxuploadspeed]", 107 Short: "set maxdownloadspeed and maxuploadspeed", 108 Long: `Set the maxdownloadspeed and maxuploadspeed in 109 Bytes per second: B/s, KB/s, MB/s, GB/s, TB/s 110 or 111 Bits per second: Bps, Kbps, Mbps, Gbps, Tbps 112 Set them to 0 for no limit.`, 113 Run: wrap(gatewayratelimitcmd), 114 } 115 ) 116 117 // gatewayconnectcmd is the handler for the command `skyc gateway add [address]`. 118 // Adds a new peer to the peer list. 119 func gatewayconnectcmd(addr string) { 120 err := httpClient.GatewayConnectPost(modules.NetAddress(addr)) 121 if err != nil { 122 die("Could not add peer:", err) 123 } 124 fmt.Println("Added", addr, "to peer list.") 125 } 126 127 // gatewaydisconnectcmd is the handler for the command `skyc gateway remove [address]`. 128 // Removes a peer from the peer list. 129 func gatewaydisconnectcmd(addr string) { 130 err := httpClient.GatewayDisconnectPost(modules.NetAddress(addr)) 131 if err != nil { 132 die("Could not remove peer:", err) 133 } 134 fmt.Println("Removed", addr, "from peer list.") 135 } 136 137 // gatewayaddresscmd is the handler for the command `skyc gateway address`. 138 // Prints the gateway's network address. 139 func gatewayaddresscmd() { 140 info, err := httpClient.GatewayGet() 141 if err != nil { 142 die("Could not get gateway address:", err) 143 } 144 fmt.Println("Address:", info.NetAddress) 145 } 146 147 // gatewaybandwidthcmd is the handler for the command `skyc gateway bandwidth`. 148 // returns the total upload and download bandwidth usage for the gateway 149 func gatewaybandwidthcmd() { 150 bandwidth, err := httpClient.GatewayBandwidthGet() 151 if err != nil { 152 die("Could not get bandwidth monitor", err) 153 } 154 155 fmt.Printf(`Download: %v 156 Upload: %v 157 Duration: %v 158 `, modules.FilesizeUnits(bandwidth.Download), modules.FilesizeUnits(bandwidth.Upload), fmtDuration(time.Since(bandwidth.StartTime))) 159 } 160 161 // gatewaycmd is the handler for the command `skyc gateway`. 162 // Prints the gateway's network address and number of peers. 163 func gatewaycmd() { 164 info, err := httpClient.GatewayGet() 165 if err != nil { 166 die("Could not get gateway address:", err) 167 } 168 fmt.Println("Address:", info.NetAddress) 169 fmt.Println("Active peers:", len(info.Peers)) 170 fmt.Println("Max download speed:", info.MaxDownloadSpeed) 171 fmt.Println("Max upload speed:", info.MaxUploadSpeed) 172 } 173 174 // gatewayblocklistcmd is the handler for the command `skyc gateway blocklist` 175 // Prints the ip addresses on the gateway blocklist 176 func gatewayblocklistcmd() { 177 gbg, err := httpClient.GatewayBlocklistGet() 178 if err != nil { 179 die("Could not get gateway blocklist", err) 180 } 181 fmt.Println(len(gbg.Blocklist), "ip addresses currently on the gateway blocklist") 182 for _, ip := range gbg.Blocklist { 183 fmt.Println(ip) 184 } 185 } 186 187 // gatewayblocklistappendcmd is the handler for the command 188 // `skyc gateway blocklist append` 189 // Adds one or more new ip addresses to the gateway's blocklist 190 func gatewayblocklistappendcmd(cmd *cobra.Command, addresses []string) { 191 if len(addresses) == 0 { 192 fmt.Println("No IP addresses submitted to append") 193 _ = cmd.UsageFunc()(cmd) 194 os.Exit(exitCodeUsage) 195 } 196 err := httpClient.GatewayAppendBlocklistPost(addresses) 197 if err != nil { 198 die("Could not append the ip addresses(es) to the gateway blocklist", err) 199 } 200 fmt.Println(addresses, "successfully added to the gateway blocklist") 201 } 202 203 // gatewayblocklistclearcmd is the handler for the command 204 // `skyc gateway blocklist clear` 205 // Clears the gateway blocklist 206 func gatewayblocklistclearcmd(cmd *cobra.Command, addresses []string) { 207 err := httpClient.GatewaySetBlocklistPost(addresses) 208 if err != nil { 209 die("Could not clear the gateway blocklist", err) 210 } 211 fmt.Println("successfully cleared the gateway blocklist") 212 } 213 214 // gatewayblocklistremovecmd is the handler for the command 215 // `skyc gateway blocklist remove` 216 // Removes one or more ip addresses from the gateway's blocklist 217 func gatewayblocklistremovecmd(cmd *cobra.Command, addresses []string) { 218 if len(addresses) == 0 { 219 fmt.Println("No IP addresses submitted to remove") 220 _ = cmd.UsageFunc()(cmd) 221 os.Exit(exitCodeUsage) 222 } 223 err := httpClient.GatewayRemoveBlocklistPost(addresses) 224 if err != nil { 225 die("Could not remove the ip address(es) from the gateway blocklist", err) 226 } 227 fmt.Println(addresses, "was successfully removed from the gateway blocklist") 228 } 229 230 // gatewayblocklistsetcmd is the handler for the command 231 // `skyc gateway blocklist set` 232 // Sets the gateway blocklist to the ip addresses passed in 233 func gatewayblocklistsetcmd(cmd *cobra.Command, addresses []string) { 234 if len(addresses) == 0 { 235 fmt.Println("No IP addresses submitted") 236 _ = cmd.UsageFunc()(cmd) 237 os.Exit(exitCodeUsage) 238 } 239 err := httpClient.GatewaySetBlocklistPost(addresses) 240 if err != nil { 241 die("Could not set the gateway blocklist", err) 242 } 243 fmt.Println(addresses, "was successfully set as the gateway blocklist") 244 } 245 246 // gatewaylistcmd is the handler for the command `skyc gateway list`. 247 // Prints a list of all peers. 248 func gatewaylistcmd() { 249 info, err := httpClient.GatewayGet() 250 if err != nil { 251 die("Could not get peer list:", err) 252 } 253 if len(info.Peers) == 0 { 254 fmt.Println("No peers to show.") 255 return 256 } 257 fmt.Println(len(info.Peers), "active peers:") 258 w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) 259 fmt.Fprintln(w, "Version\tOutbound\tAddress") 260 for _, peer := range info.Peers { 261 fmt.Fprintf(w, "%v\t%v\t%v\n", peer.Version, yesNo(!peer.Inbound), peer.NetAddress) 262 } 263 if err := w.Flush(); err != nil { 264 die("failed to flush writer") 265 } 266 } 267 268 // gatewayratelimitcmd is the handler for the command `skyc gateway ratelimit`. 269 // sets the maximum upload & download bandwidth the gateway module is permitted 270 // to use. 271 func gatewayratelimitcmd(downloadSpeedStr, uploadSpeedStr string) { 272 downloadSpeedInt, err := parseRatelimit(downloadSpeedStr) 273 if err != nil { 274 die(errors.AddContext(err, "unable to parse download speed")) 275 } 276 uploadSpeedInt, err := parseRatelimit(uploadSpeedStr) 277 if err != nil { 278 die(errors.AddContext(err, "unable to parse upload speed")) 279 } 280 281 err = httpClient.GatewayRateLimitPost(downloadSpeedInt, uploadSpeedInt) 282 if err != nil { 283 die("Could not set gateway ratelimit speed") 284 } 285 fmt.Println("Set gateway maxdownloadspeed to ", downloadSpeedInt, " and maxuploadspeed to ", uploadSpeedInt) 286 }