0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

Merge pull request #37 from threefjord/master

Adding -save and -ask-save flags, joining paths when necessary
This commit is contained in:
Zack 2017-10-22 09:28:05 -06:00 committed by GitHub
commit 7fbfe7cecc
2 changed files with 24 additions and 10 deletions

View file

@ -25,7 +25,9 @@ type Connection struct {
NumberOfConnections int NumberOfConnections int
Code string Code string
HashedCode string HashedCode string
Path string
IsSender bool IsSender bool
AskPath bool
Debug bool Debug bool
DontEncrypt bool DontEncrypt bool
Wait bool Wait bool
@ -88,6 +90,8 @@ func NewConnection(flags *Flags) *Connection {
c.IsSender = true c.IsSender = true
} else { } else {
c.IsSender = false c.IsSender = false
c.AskPath = flags.PathSpec
c.Path = flags.Path
} }
log.SetFormatter(&log.TextFormatter{}) log.SetFormatter(&log.TextFormatter{})
@ -305,9 +309,15 @@ func (c *Connection) runClient() error {
log.Debugf("meta data received: %v", c.File) log.Debugf("meta data received: %v", c.File)
// have the main thread ask for the okay // have the main thread ask for the okay
if id == 0 { if id == 0 {
fmt.Printf("Receiving file (%d bytes) into: %s\n", c.File.Size, c.File.Name) fmt.Printf("Receiving file (%d bytes) into: %s\n", c.File.Size, path.Join(c.Path, c.File.Name))
var sentFileNames []string var sentFileNames []string
if c.AskPath {
getPath := getInput("path: ")
if len(getPath) > 0 {
c.Path = path.Clean(getPath)
}
}
if fileAlreadyExists(sentFileNames, c.File.Name) { if fileAlreadyExists(sentFileNames, c.File.Name) {
fmt.Printf("Will not overwrite file!") fmt.Printf("Will not overwrite file!")
os.Exit(1) os.Exit(1)
@ -367,19 +377,19 @@ func (c *Connection) runClient() error {
} }
log.Debugf("Code: [%s]", c.Code) log.Debugf("Code: [%s]", c.Code)
if c.DontEncrypt { if c.DontEncrypt {
if err := CopyFile(c.File.Name+".enc", c.File.Name); err != nil { if err := CopyFile(path.Join(c.Path, c.File.Name+".enc"), path.Join(c.Path, c.File.Name)); err != nil {
return err return err
} }
} else { } else {
if err := DecryptFile(c.File.Name+".enc", c.File.Name, c.Code); err != nil { if err := DecryptFile(path.Join(c.Path, c.File.Name+".enc"), path.Join(c.Path, c.File.Name), c.Code); err != nil {
return errors.Wrap(err, "Problem decrypting file") return errors.Wrap(err, "Problem decrypting file")
} }
} }
if !c.Debug { if !c.Debug {
os.Remove(c.File.Name + ".enc") os.Remove(path.Join(c.Path, c.File.Name+".enc"))
} }
fileHash, err := HashFile(c.File.Name) fileHash, err := HashFile(path.Join(c.Path, c.File.Name))
if err != nil { if err != nil {
log.Error(err) log.Error(err)
} }
@ -389,7 +399,7 @@ func (c *Connection) runClient() error {
if c.File.Hash != fileHash { if c.File.Hash != fileHash {
return fmt.Errorf("\nUh oh! %s is corrupted! Sorry, try again.\n", c.File.Name) return fmt.Errorf("\nUh oh! %s is corrupted! Sorry, try again.\n", c.File.Name)
} else { } else {
fmt.Printf("\nReceived file written to %s\n", c.File.Name) fmt.Printf("\nReceived file written to %s\n", path.Join(c.Path, c.File.Name))
} }
if c.File.IsDir { // if the file was originally a dir if c.File.IsDir { // if the file was originally a dir
@ -421,13 +431,13 @@ func (c *Connection) catFile() error {
// cat the file // cat the file
files := make([]string, c.NumberOfConnections) files := make([]string, c.NumberOfConnections)
for id := range files { for id := range files {
files[id] = c.File.Name + ".enc." + strconv.Itoa(id) files[id] = path.Join(c.Path, c.File.Name+".enc."+strconv.Itoa(id))
} }
toRemove := true toRemove := true
if c.Debug { if c.Debug {
toRemove = false toRemove = false
} }
return CatFiles(files, c.File.Name+".enc", toRemove) return CatFiles(files, path.Join(c.Path, c.File.Name+".enc"), toRemove)
} }
func (c *Connection) receiveFile(id int, connection net.Conn) error { func (c *Connection) receiveFile(id int, connection net.Conn) error {
@ -447,9 +457,9 @@ func (c *Connection) receiveFile(id int, connection net.Conn) error {
return errors.New("chunk size is empty!") return errors.New("chunk size is empty!")
} }
os.Remove(c.File.Name + ".enc." + strconv.Itoa(id)) os.Remove(path.Join(c.Path, c.File.Name+".enc."+strconv.Itoa(id)))
log.Debug("Making " + c.File.Name + ".enc." + strconv.Itoa(id)) log.Debug("Making " + c.File.Name + ".enc." + strconv.Itoa(id))
newFile, err := os.Create(c.File.Name + ".enc." + strconv.Itoa(id)) newFile, err := os.Create(path.Join(c.Path, c.File.Name+".enc."+strconv.Itoa(id)))
if err != nil { if err != nil {
panic(err) panic(err)
} }

View file

@ -16,9 +16,11 @@ type Flags struct {
Relay bool Relay bool
Debug bool Debug bool
Wait bool Wait bool
PathSpec bool
DontEncrypt bool DontEncrypt bool
Server string Server string
File string File string
Path string
Code string Code string
Rate int Rate int
NumberOfConnections int NumberOfConnections int
@ -43,8 +45,10 @@ func main() {
flag.BoolVar(&flags.Relay, "relay", false, "run as relay") flag.BoolVar(&flags.Relay, "relay", false, "run as relay")
flag.BoolVar(&flags.Debug, "debug", false, "debug mode") flag.BoolVar(&flags.Debug, "debug", false, "debug mode")
flag.BoolVar(&flags.Wait, "wait", false, "wait for code to be sent") flag.BoolVar(&flags.Wait, "wait", false, "wait for code to be sent")
flag.BoolVar(&flags.PathSpec, "ask-save", false, "ask for path to save to")
flag.StringVar(&flags.Server, "server", "cowyo.com", "address of relay server") flag.StringVar(&flags.Server, "server", "cowyo.com", "address of relay server")
flag.StringVar(&flags.File, "send", "", "file to send") flag.StringVar(&flags.File, "send", "", "file to send")
flag.StringVar(&flags.Path, "save", "", "path to save to")
flag.StringVar(&flags.Code, "code", "", "use your own code phrase") flag.StringVar(&flags.Code, "code", "", "use your own code phrase")
flag.IntVar(&flags.Rate, "rate", oneGigabytePerSecond, "throttle down to speed in kbps") flag.IntVar(&flags.Rate, "rate", oneGigabytePerSecond, "throttle down to speed in kbps")
flag.BoolVar(&flags.DontEncrypt, "no-encrypt", false, "turn off encryption") flag.BoolVar(&flags.DontEncrypt, "no-encrypt", false, "turn off encryption")