From dfe99f4ddb6b5ac5f777da81db239cfd851d5ce0 Mon Sep 17 00:00:00 2001 From: Justin Hawkins Date: Fri, 14 Jan 2022 10:06:55 +1030 Subject: [PATCH] Add file type info detection --- client.go | 40 ++++++++++++++++++++-------------------- go.mod | 1 + go.sum | 2 ++ secure/secure.go | 1 + 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/client.go b/client.go index 659e206..58bdeb6 100644 --- a/client.go +++ b/client.go @@ -10,6 +10,7 @@ import ( "net" "os" + "github.com/h2non/filetype" "github.com/tardisx/netgiv/secure" ) @@ -52,23 +53,17 @@ func (c *Client) Connect() error { panic(err) } - data := secure.PacketSendDataStart{ - Filename: "foobar", - TotalSize: 3, - } - err = enc.Encode(data) - if err != nil { - panic(err) - } - log.Print("done that") - nBytes, nChunks := int64(0), int64(0) reader := bufio.NewReader(os.Stdin) buf := make([]byte, 0, 1024) + startSent := false + for { n, err := reader.Read(buf[:cap(buf)]) + buf = buf[:n] + if n == 0 { if err == nil { continue @@ -80,7 +75,21 @@ func (c *Client) Connect() error { } nChunks++ nBytes += int64(len(buf)) - // process buf + + if !startSent { + kind, _ := filetype.Match(buf) + data := secure.PacketSendDataStart{ + Filename: "foobar", + TotalSize: 3, + Kind: kind.MIME.Value, + } + err = enc.Encode(data) + if err != nil { + panic(err) + } + log.Print("done that") + startSent = true + } send := secure.PacketSendDataNext{ Size: 5000, @@ -97,15 +106,6 @@ func (c *Client) Connect() error { conn.Close() break - // response := make([]byte, 1024) - - // _, err = secureConnection.Read(response) - // if err != nil { - // fmt.Print("Connection to the server was closed.\n") - // break - // } - - // fmt.Printf("%s\n", response) } return nil diff --git a/go.mod b/go.mod index 5d51094..595f0ee 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/tardisx/netgiv go 1.17 require ( + github.com/h2non/filetype v1.1.3 // indirect golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect ) diff --git a/go.sum b/go.sum index e11fe49..09ac3c4 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= +github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M= golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= diff --git a/secure/secure.go b/secure/secure.go index 2209432..08cf613 100644 --- a/secure/secure.go +++ b/secure/secure.go @@ -188,6 +188,7 @@ type PacketStart struct { type PacketSendDataStart struct { Filename string TotalSize uint32 + Kind string } type PacketSendDataNext struct {