mirror of https://github.com/schollz/croc.git
initial implementation for reading input files from stdin
This commit is contained in:
parent
a35612abfe
commit
19bfbcb9c2
22
connect.go
22
connect.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
@ -46,6 +47,7 @@ type FileMetaData struct {
|
||||||
Path string
|
Path string
|
||||||
IsDir bool
|
IsDir bool
|
||||||
IsEncrypted bool
|
IsEncrypted bool
|
||||||
|
DeleteAfterSending bool
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -63,6 +65,22 @@ func NewConnection(config *AppConfig) (*Connection, error) {
|
||||||
c.NumberOfConnections = config.NumberOfConnections
|
c.NumberOfConnections = config.NumberOfConnections
|
||||||
c.rate = config.Rate
|
c.rate = config.Rate
|
||||||
if len(config.File) > 0 {
|
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
|
// check wether the file is a dir
|
||||||
info, err := os.Stat(config.File)
|
info, err := os.Stat(config.File)
|
||||||
if err != nil {
|
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."+strconv.Itoa(id)))
|
||||||
}
|
}
|
||||||
os.Remove(path.Join(c.Path, c.File.Name+".enc"))
|
os.Remove(path.Join(c.Path, c.File.Name+".enc"))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) Run() error {
|
func (c *Connection) Run() error {
|
||||||
|
@ -635,5 +652,8 @@ func (c *Connection) sendFile(id int, connection net.Conn) error {
|
||||||
file.Close()
|
file.Close()
|
||||||
err = os.Remove(c.File.Name + ".enc." + strconv.Itoa(id))
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
7
main.go
7
main.go
|
@ -3,10 +3,11 @@ package main
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/urfave/cli"
|
|
||||||
"github.com/yudai/gotty/pkg/homedir"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/urfave/cli"
|
||||||
|
"github.com/yudai/gotty/pkg/homedir"
|
||||||
)
|
)
|
||||||
|
|
||||||
const BUFFERSIZE = 1024
|
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"`
|
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"`
|
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"`
|
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:""`
|
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:""`
|
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"`
|
Rate int `yaml:"rate" flagName:"rate" flagSName:"R" flagDescribe:"Throttle down to speed in kbps" default:"1000000"`
|
||||||
|
|
Loading…
Reference in New Issue