mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 21:30:16 +02:00
add zip file
This commit is contained in:
parent
6e27bfebdd
commit
59fc697b4f
5 changed files with 252 additions and 46 deletions
|
@ -1,5 +1,9 @@
|
|||
package croc
|
||||
|
||||
func init() {
|
||||
SetLogLevel("debug")
|
||||
}
|
||||
|
||||
// Relay initiates a relay
|
||||
func (c *Croc) Relay() error {
|
||||
// start relay
|
||||
|
|
|
@ -1,49 +1,43 @@
|
|||
package croc
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
// func TestEncrypt(t *testing.T) {
|
||||
// key := getRandomName()
|
||||
// encrypted, salt, iv := encrypt([]byte("hello, world"), key)
|
||||
// decrypted, err := decrypt(encrypted, key, salt, iv)
|
||||
// if err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
// if string(decrypted) != "hello, world" {
|
||||
// t.Error("problem decrypting")
|
||||
// }
|
||||
// _, err = decrypt(encrypted, "wrong passphrase", salt, iv)
|
||||
// if err == nil {
|
||||
// t.Error("should not work!")
|
||||
// }
|
||||
// }
|
||||
|
||||
func TestEncrypt(t *testing.T) {
|
||||
key := getRandomName()
|
||||
encrypted, salt, iv := encrypt([]byte("hello, world"), key)
|
||||
decrypted, err := decrypt(encrypted, key, salt, iv)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if string(decrypted) != "hello, world" {
|
||||
t.Error("problem decrypting")
|
||||
}
|
||||
_, err = decrypt(encrypted, "wrong passphrase", salt, iv)
|
||||
if err == nil {
|
||||
t.Error("should not work!")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptFiles(t *testing.T) {
|
||||
key := getRandomName()
|
||||
if err := ioutil.WriteFile("temp", []byte("hello, world!"), 0644); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := encryptFile("temp", "temp.enc", key); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := decryptFile("temp.enc", "temp.dec", key); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
data, err := ioutil.ReadFile("temp.dec")
|
||||
if string(data) != "hello, world!" {
|
||||
t.Errorf("Got something weird: " + string(data))
|
||||
}
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := decryptFile("temp.enc", "temp.dec", key+"wrong password"); err == nil {
|
||||
t.Error("should throw error!")
|
||||
}
|
||||
os.Remove("temp.dec")
|
||||
os.Remove("temp.enc")
|
||||
os.Remove("temp")
|
||||
}
|
||||
// func TestEncryptFiles(t *testing.T) {
|
||||
// key := getRandomName()
|
||||
// if err := ioutil.WriteFile("temp", []byte("hello, world!"), 0644); err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
// if err := encryptFile("temp", "temp.enc", key); err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
// if err := decryptFile("temp.enc", "temp.dec", key); err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
// data, err := ioutil.ReadFile("temp.dec")
|
||||
// if string(data) != "hello, world!" {
|
||||
// t.Errorf("Got something weird: " + string(data))
|
||||
// }
|
||||
// if err != nil {
|
||||
// t.Error(err)
|
||||
// }
|
||||
// if err := decryptFile("temp.enc", "temp.dec", key+"wrong password"); err == nil {
|
||||
// t.Error("should throw error!")
|
||||
// }
|
||||
// os.Remove("temp.dec")
|
||||
// os.Remove("temp.enc")
|
||||
// os.Remove("temp")
|
||||
// }
|
||||
|
|
10
src/utils.go
10
src/utils.go
|
@ -192,3 +192,13 @@ func getLocalIP() string {
|
|||
}
|
||||
return bestIP
|
||||
}
|
||||
|
||||
// exists reports whether the named file or directory exists.
|
||||
func exists(name string) bool {
|
||||
if _, err := os.Stat(name); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
177
src/zip.go
Normal file
177
src/zip.go
Normal file
|
@ -0,0 +1,177 @@
|
|||
package croc
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"compress/flate"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
log "github.com/cihub/seelog"
|
||||
)
|
||||
|
||||
func unzipFile(src, dest string) (err error) {
|
||||
r, err := zip.OpenReader(src)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
for _, f := range r.File {
|
||||
var rc io.ReadCloser
|
||||
rc, err = f.Open()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
// Store filename/path for returning and using later on
|
||||
fpath := filepath.Join(dest, f.Name)
|
||||
log.Debugf("unzipping %s", fpath)
|
||||
|
||||
if f.FileInfo().IsDir() {
|
||||
|
||||
// Make Folder
|
||||
os.MkdirAll(fpath, os.ModePerm)
|
||||
|
||||
} else {
|
||||
|
||||
// Make File
|
||||
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var outFile *os.File
|
||||
outFile, err = os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = io.Copy(outFile, rc)
|
||||
|
||||
// Close the file without defer to close before next iteration of loop
|
||||
outFile.Close()
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
log.Debugf("unzipped %s to %s", src, dest)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func zipFile(fname string, compress bool) (writtenFilename string, err error) {
|
||||
pathtofile, filename := filepath.Split(fname)
|
||||
curdir, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
curdir, err = filepath.Abs(curdir)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
defer os.Chdir(curdir)
|
||||
os.Chdir(pathtofile)
|
||||
|
||||
newfile, err := ioutil.TempFile("/tmp/", "croc")
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
writtenFilename = newfile.Name()
|
||||
defer newfile.Close()
|
||||
|
||||
zipWriter := zip.NewWriter(newfile)
|
||||
zipWriter.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
|
||||
if compress {
|
||||
return flate.NewWriter(out, flate.BestCompression)
|
||||
} else {
|
||||
return flate.NewWriter(out, flate.NoCompression)
|
||||
}
|
||||
})
|
||||
defer zipWriter.Close()
|
||||
|
||||
zipfile, err := os.Open(filename)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return "", err
|
||||
}
|
||||
defer zipfile.Close()
|
||||
// Get the file information
|
||||
info, err := zipfile.Stat()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
// write header informaiton
|
||||
header, err := zip.FileInfoHeader(info)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
var writer io.Writer
|
||||
if info.IsDir() {
|
||||
baseDir := filename
|
||||
filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
header, err := zip.FileInfoHeader(info)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if baseDir != "" {
|
||||
header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, baseDir))
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
header.Name += "/"
|
||||
} else {
|
||||
header.Method = zip.Deflate
|
||||
}
|
||||
|
||||
writer, err = zipWriter.CreateHeader(header)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
_, err = io.Copy(writer, file)
|
||||
return err
|
||||
})
|
||||
} else {
|
||||
writer, err = zipWriter.CreateHeader(header)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
_, err = io.Copy(writer, newfile)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
log.Debugf("wrote zip file to %s", writtenFilename)
|
||||
return
|
||||
}
|
21
src/zip_test.go
Normal file
21
src/zip_test.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package croc
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
log "github.com/cihub/seelog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestZip(t *testing.T) {
|
||||
defer log.Flush()
|
||||
writtenFilename, err := zipFile("../testing_data", false)
|
||||
assert.Nil(t, err)
|
||||
defer os.Remove(writtenFilename)
|
||||
|
||||
err = unzipFile(writtenFilename, ".")
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, exists("testing_data"))
|
||||
os.RemoveAll("testing_data")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue