~apreiml/go-wormhole

17e3a4fe4e1ec6b0373797fe734350480bfab90f — Armin Preiml 2 years ago 46e8dd0 master
check ack hash on send file
1 files changed, 20 insertions(+), 5 deletions(-)

M apps/transfer.go
M apps/transfer.go => apps/transfer.go +20 -5
@@ 61,6 61,11 @@ type answer struct {
	FileAck    string `json:"file_ack"`
}

type ack struct {
	Ack    string `json:"ack"`
	Sha256 string `json:"sha256"`
}

const (
	TransferTypeMessage   = 0
	TransferTypeFile      = 1


@@ 157,15 162,16 @@ MessageLoop:
	}

	t.trans.Connect()
	t.hashSum = sha256.New()

	log.Println("start write file")
	_, err = io.Copy(t.trans, f)
	_, err = io.Copy(t, f)
	if err != nil {
		return err
	}
	t.trans.Flush()

	log.Println("end write file")
	sum := t.hashSum.Sum(nil)
	hexSum := hex.EncodeToString(sum[:])

	b := make([]byte, 4096)
	n, err := t.trans.Read(b)


@@ 173,9 179,17 @@ MessageLoop:
		return err
	}

	log.Println(string(b[:n]))
	var a ack
	err = json.Unmarshal(b[:n], &a)
	if err != nil {
		log.Println(err)
		return fmt.Errorf("ack invalid json: %s", string(b[:n]))
	}

	if a.Ack != "ok" || a.Sha256 != hexSum {
		return fmt.Errorf("invalid ack: expected %s, got: %s", hexSum, a.Sha256)
	}

	// TODO CHECK ack
	return nil
}



@@ 278,6 292,7 @@ func (t *Transfer) Read(p []byte) (n int, err error) {

func (t *Transfer) Write(p []byte) (n int, err error) {
	n, err = t.trans.Write(p)
	t.hashSum.Write(p[:n])
	return
}