diff --git a/asink/client.go b/asink/client.go index fccbb81..8add0a9 100644 --- a/asink/client.go +++ b/asink/client.go @@ -31,6 +31,8 @@ type AsinkGlobals struct { port int username string password string + encrypted bool + key string } var globals AsinkGlobals @@ -99,6 +101,12 @@ func StartClient(args []string) { globals.username, err = config.GetString("server", "username") globals.password, err = config.GetString("server", "password") + //TODO check errors on encryption settings + globals.encrypted, err = config.GetBool("encryption", "enabled") + if globals.encrypted { + globals.key, err = config.GetString("encryption", "key") + } + globals.db, err = GetAndInitDB(config) if err != nil { panic(err) @@ -209,15 +217,28 @@ func ProcessLocalEvent(globals AsinkGlobals, event *asink.Event) { if err != nil { panic(err) } - defer uploadWriteCloser.Close() uploadFile, err := os.Open(cachedFilename) if err != nil { + uploadWriteCloser.Close() panic(err) } - defer uploadFile.Close() - _, err = io.Copy(uploadWriteCloser, uploadFile) + if globals.encrypted { + encrypter, err := NewEncrypter(uploadWriteCloser, globals.key) + if err != nil { + uploadWriteCloser.Close() + uploadFile.Close() + panic(err) + } + _, err = io.Copy(encrypter, uploadFile) + encrypter.Close() + } else { + _, err = io.Copy(uploadWriteCloser, uploadFile) + } + uploadFile.Close() + uploadWriteCloser.Close() + StatStopUpload() if err != nil { panic(err) @@ -289,7 +310,15 @@ func ProcessRemoteEvent(globals AsinkGlobals, event *asink.Event) { panic(err) } defer downloadReadCloser.Close() - _, err = io.Copy(outfile, downloadReadCloser) + if globals.encrypted { + decrypter, err := NewDecrypter(downloadReadCloser, globals.key) + if err != nil { + panic(err) + } + _, err = io.Copy(outfile, decrypter) + } else { + _, err = io.Copy(outfile, downloadReadCloser) + } outfile.Close() StatStopDownload() diff --git a/asink/encryption.go b/asink/encryption.go new file mode 100644 index 0000000..a700d20 --- /dev/null +++ b/asink/encryption.go @@ -0,0 +1,38 @@ +/* + Copyright (C) 2013 Aaron Lindsay +*/ + +package main + +import ( + "code.google.com/p/go.crypto/openpgp" + "io" +) + +func NewEncrypter(writer io.WriteCloser, key string) (plaintextWriter io.WriteCloser, err error) { + return openpgp.SymmetricallyEncrypt(writer, []byte(key), nil, nil) +} + +type Decrypter struct { + details *openpgp.MessageDetails +} + +func NewDecrypter(ciphertextReader io.ReadCloser, key string) (decrypter io.Reader, err error) { + prompt := func(keys []openpgp.Key, symmetric bool) ([]byte, error) { + return []byte(key), nil + } + + details, err := openpgp.ReadMessage(ciphertextReader, nil, prompt, nil) + if err != nil { + decrypter = nil + return + } + + decrypter = Decrypter{details} + + return +} + +func (d Decrypter) Read(p []byte) (n int, err error) { + return d.details.UnverifiedBody.Read(p) +}