diff --git a/cmd/debora/commands.go b/cmd/debora/commands.go index 7e6199d3..af0819ee 100644 --- a/cmd/debora/commands.go +++ b/cmd/debora/commands.go @@ -2,11 +2,16 @@ package main import ( "fmt" + "io" + "net/url" + "os" + acm "github.com/tendermint/tendermint/account" "github.com/tendermint/tendermint/binary" btypes "github.com/tendermint/tendermint/cmd/barak/types" . "github.com/tendermint/tendermint/common" "github.com/tendermint/tendermint/rpc" + "net/http" ) // These are convenience functions for a single developer. @@ -44,6 +49,36 @@ func ListProcesses(privKey acm.PrivKey, remote string, command btypes.CommandLis return response, err } +func DownloadFile(privKey acm.PrivKey, remote string, command btypes.CommandServeFile, outPath string) (n int64, err error) { + // Create authCommandJSONBytes + nonce, err := GetNonce(remote) + if err != nil { + return 0, err + } + commandBytes, signature := SignCommand(privKey, nonce+1, command) + authCommand := btypes.AuthCommand{ + CommandJSONStr: string(commandBytes), + Signatures: []acm.Signature{signature}, + } + authCommandJSONBytes := binary.JSONBytes(authCommand) + // Make request and write to outPath. + httpResponse, err := http.PostForm(remote+"/download", url.Values{"auth_command": {string(authCommandJSONBytes)}}) + if err != nil { + return 0, err + } + defer httpResponse.Body.Close() + outFile, err := os.OpenFile(outPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) + if err != nil { + return 0, err + } + defer outFile.Close() + n, err = io.Copy(outFile, httpResponse.Body) + if err != nil { + return 0, err + } + return n, nil +} + //----------------------------------------------------------------------------- // Utility method to get nonce from the remote. diff --git a/cmd/debora/main.go b/cmd/debora/main.go index 714f8091..77469eed 100644 --- a/cmd/debora/main.go +++ b/cmd/debora/main.go @@ -79,6 +79,11 @@ func main() { Usage: "list processes", Action: cliListProcesses, }, + cli.Command{ + Name: "download", + Usage: "download file ", + Action: cliDownloadFile, + }, } app.Run(os.Args) } @@ -189,3 +194,24 @@ func cliListProcesses(c *cli.Context) { } } } + +func cliDownloadFile(c *cli.Context) { + args := c.Args() + if len(args) != 2 { + Exit("Must specify ") + } + remotePath := args[0] + localPathPrefix := args[1] + command := btypes.CommandServeFile{ + Path: remotePath, + } + for i, remote := range Config.Remotes { + localPath := Fmt("%v_%v", localPathPrefix, i) + n, err := DownloadFile(Config.PrivKey, remote, command, localPath) + if err != nil { + fmt.Printf("%v failure. %v\n", remote, err) + } else { + fmt.Printf("%v success. Wrote %v bytes to %v\n", remote, n, localPath) + } + } +} diff --git a/process/process.go b/process/process.go index 3f18378e..d0b12f10 100644 --- a/process/process.go +++ b/process/process.go @@ -31,7 +31,7 @@ const ( // execPath: command name // args: args to command. (should not include name) func Create(mode int, label string, execPath string, args []string, input string, outPath string) (*Process, error) { - outFile, err := os.OpenFile(outPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) + outFile, err := os.OpenFile(outPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) if err != nil { return nil, err }