From 19bfbcb9c253806077ec013d9a26843c3b5d7517 Mon Sep 17 00:00:00 2001 From: Travis Date: Fri, 13 Apr 2018 09:31:43 -0700 Subject: [PATCH] initial implementation for reading input files from stdin --- connect.go | 34 +++++++++++++++++++++++++++------- main.go | 7 ++++--- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/connect.go b/connect.go index 43196d3..17b1985 100644 --- a/connect.go +++ b/connect.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io" + "io/ioutil" "net" "os" "os/signal" @@ -40,12 +41,13 @@ type Connection struct { } type FileMetaData struct { - Name string - Size int - Hash string - Path string - IsDir bool - IsEncrypted bool + Name string + Size int + Hash string + Path string + IsDir bool + IsEncrypted bool + DeleteAfterSending bool } const ( @@ -63,6 +65,22 @@ func NewConnection(config *AppConfig) (*Connection, error) { c.NumberOfConnections = config.NumberOfConnections c.rate = config.Rate if len(config.File) > 0 { + if config.File == "stdin" { + f, err := ioutil.TempFile(os.TempDir(), "croc-stdin-") + if err != nil { + return c, err + } + _, err = io.Copy(f, os.Stdin) + if err != nil { + return c, err + } + config.File = f.Name() + err = f.Close() + if err != nil { + return c, err + } + c.File.DeleteAfterSending = true + } // check wether the file is a dir info, err := os.Stat(config.File) if err != nil { @@ -108,7 +126,6 @@ func (c *Connection) cleanup() { os.Remove(path.Join(c.Path, c.File.Name+".enc."+strconv.Itoa(id))) } os.Remove(path.Join(c.Path, c.File.Name+".enc")) - } func (c *Connection) Run() error { @@ -635,5 +652,8 @@ func (c *Connection) sendFile(id int, connection net.Conn) error { file.Close() err = os.Remove(c.File.Name + ".enc." + strconv.Itoa(id)) } + if err != nil && c.File.DeleteAfterSending { + err = os.Remove(path.Join(c.File.Path, c.File.Name)) + } return err } diff --git a/main.go b/main.go index 86f5058..f36241e 100644 --- a/main.go +++ b/main.go @@ -3,10 +3,11 @@ package main import ( "bufio" "fmt" - "github.com/urfave/cli" - "github.com/yudai/gotty/pkg/homedir" "os" "strings" + + "github.com/urfave/cli" + "github.com/yudai/gotty/pkg/homedir" ) const BUFFERSIZE = 1024 @@ -19,7 +20,7 @@ type AppConfig struct { PathSpec bool `yaml:"ask-save" flagName:"ask-save" flagSName:"q" flagDescribe:"Ask for path to save to" default:"false"` DontEncrypt bool `yaml:"no-encrypt" flagName:"no-encrypt" flagSName:"g" flagDescribe:"Turn off encryption" default:"false"` Server string `yaml:"server" flagName:"server" flagSName:"l" flagDescribe:"Address of relay server" default:"cowyo.com"` - File string `yaml:"send" flagName:"send" flagSName:"s" flagDescribe:"File to send" default:""` + File string `yaml:"send" flagName:"send" flagSName:"s" flagDescribe:"File to send (\"stdin\" to read from stdin)" default:""` Path string `yaml:"save" flagName:"save" flagSName:"p" flagDescribe:"Path to save to" default:""` Code string `yaml:"code" flagName:"code" flagSName:"c" flagDescribe:"Use your own code phrase" default:""` Rate int `yaml:"rate" flagName:"rate" flagSName:"R" flagDescribe:"Throttle down to speed in kbps" default:"1000000"`