Add file type info detection

This commit is contained in:
Justin Hawkins 2022-01-14 10:06:55 +10:30
parent 1779291586
commit dfe99f4ddb
4 changed files with 24 additions and 20 deletions

View File

@ -10,6 +10,7 @@ import (
"net" "net"
"os" "os"
"github.com/h2non/filetype"
"github.com/tardisx/netgiv/secure" "github.com/tardisx/netgiv/secure"
) )
@ -52,23 +53,17 @@ func (c *Client) Connect() error {
panic(err) 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) nBytes, nChunks := int64(0), int64(0)
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
buf := make([]byte, 0, 1024) buf := make([]byte, 0, 1024)
startSent := false
for { for {
n, err := reader.Read(buf[:cap(buf)]) n, err := reader.Read(buf[:cap(buf)])
buf = buf[:n] buf = buf[:n]
if n == 0 { if n == 0 {
if err == nil { if err == nil {
continue continue
@ -80,7 +75,21 @@ func (c *Client) Connect() error {
} }
nChunks++ nChunks++
nBytes += int64(len(buf)) 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{ send := secure.PacketSendDataNext{
Size: 5000, Size: 5000,
@ -97,15 +106,6 @@ func (c *Client) Connect() error {
conn.Close() conn.Close()
break 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 return nil

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/tardisx/netgiv
go 1.17 go 1.17
require ( require (
github.com/h2non/filetype v1.1.3 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
) )

2
go.sum
View File

@ -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 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= 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= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=

View File

@ -188,6 +188,7 @@ type PacketStart struct {
type PacketSendDataStart struct { type PacketSendDataStart struct {
Filename string Filename string
TotalSize uint32 TotalSize uint32
Kind string
} }
type PacketSendDataNext struct { type PacketSendDataNext struct {