I got it to start being smarter :D
Change-Id: I559f85813c869953e68d8ee1902f53bb4168574c
diff --git a/runtime/internal/flow/conn/grpc/conn.go b/runtime/internal/flow/conn/grpc/conn.go
index 67a1f6e..d72bbf6 100644
--- a/runtime/internal/flow/conn/grpc/conn.go
+++ b/runtime/internal/flow/conn/grpc/conn.go
@@ -20,6 +20,8 @@
"golang.org/x/crypto/nacl/box"
)
+const doEncrypt = true
+
// Implements net.Conn. Encrypts messages using keys negotioated via NaCl.
// TODO: blessings
// TODO: discharges and caveats
@@ -43,6 +45,28 @@
func (c *conn) Read(b []byte) (n int, err error) {
c.mu.Lock()
defer c.mu.Unlock()
+
+ if !doEncrypt {
+ log.Printf("Beginning to Read.\n")
+ frame := [3]byte{}
+ frameCopied, err := c.rawConn.Read(frame[:])
+ if frameCopied != 3 {
+ return -1, errors.New("Did not copy 3 frame bytes.")
+ }
+ msgSize := read3ByteUint(frame)
+
+ resBuf := make([]byte, msgSize)
+ bytesRead, err := c.rawConn.Read(resBuf)
+ if err != nil {
+ log.Fatal(err)
+ }
+ copy(b, resBuf)
+ log.Printf("Read %d bytes: %v\n", bytesRead, resBuf)
+ log.Printf("Succeeded in reading.\n\n")
+ return bytesRead, nil
+ // return len(b), nil
+ }
+
log.Printf("Beginning to Read.\n")
frame := [3]byte{}
@@ -64,7 +88,7 @@
// tmp := make([]byte, 0, bytesRead-box.Overhead) // TODO: is this enough? Also, why do we need both of tmp and out?
out, ok := box.OpenAfterPrecomputation(nil, resBuf, c.currentNonce(), c.sharedKey)
log.Printf("%d bytes after opening: %v\n", len(out), out)
- log.Printf("%d bytes after opening: %v\n", len(out), string(out))
+ // log.Printf("%d bytes after opening: %v\n", len(out), string(out))
c.advanceNonce() // TODO: defer? Is there harm in advancing often than necessary (i.e. on error)?
if !ok {
// log.Printf("Failed to decrypt.\n")
@@ -76,7 +100,8 @@
// log.Printf("cap(b): %d, len(b) %d, \ncap(out): %d, len(out): %d\n", cap(b), len(b), cap(out), len(out))
log.Printf("Succeeded in reading.\n\n")
// return bytesRead, nil
- return len(b), nil
+ // return len(b), nil
+ return len(out), nil
}
// TODO: all this casting is gross
@@ -84,6 +109,28 @@
// log.Printf("Write called:\n%v\n", string(debug.Stack()))
c.mu.Lock()
defer c.mu.Unlock()
+
+ if !doEncrypt {
+ log.Printf("Beginning to write.\n")
+ tmp := make([]byte, 3)
+ err = write3ByteUint(tmp, len(b))
+ if err != nil {
+ return -1, err
+ }
+ bytesCopied, err := io.Copy(c.rawConn, bytes.NewReader(tmp))
+ if err != nil {
+ log.Fatal(err)
+ }
+ bytesCopied, err = io.Copy(c.rawConn, bytes.NewReader(b))
+ if err != nil {
+ log.Fatal(err)
+ }
+ log.Printf("Wrote %d bytes: %v\n", bytesCopied, b)
+ log.Printf("Succeeded in writing!\n\n")
+ // // return int(bytesCopied), nil
+ return len(b), nil
+ }
+
log.Printf("Beginning to write.\n")
log.Printf("%d bytes before sealing: %v\n", len(b), b)
tmp := make([]byte, 3, 3+len(b)+box.Overhead) // TODO: is this enough? Also, why do we need both of tmp and out?