2022-01-09 13:05:36 +10:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-01-13 22:20:35 +10:30
|
|
|
"bufio"
|
2022-01-14 23:30:14 +10:30
|
|
|
"bytes"
|
2022-01-13 14:26:16 +10:30
|
|
|
"encoding/gob"
|
2022-01-09 13:05:36 +10:30
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-01-13 22:20:35 +10:30
|
|
|
"io"
|
2022-01-13 14:26:16 +10:30
|
|
|
"log"
|
2022-01-09 13:05:36 +10:30
|
|
|
"net"
|
2022-01-13 22:20:35 +10:30
|
|
|
"os"
|
2022-01-09 13:05:36 +10:30
|
|
|
|
|
|
|
"github.com/tardisx/netgiv/secure"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2022-01-13 22:20:35 +10:30
|
|
|
address string
|
|
|
|
port int
|
2022-01-14 23:30:14 +10:30
|
|
|
list bool
|
2022-01-15 14:04:13 +10:30
|
|
|
receive bool
|
2022-01-09 13:05:36 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Connect() error {
|
2022-01-13 22:20:35 +10:30
|
|
|
address := fmt.Sprintf("%s:%d", c.address, c.port)
|
|
|
|
|
2022-01-09 13:05:36 +10:30
|
|
|
serverAddress, _ := net.ResolveTCPAddr("tcp", address)
|
|
|
|
|
|
|
|
conn, err := net.DialTCP("tcp", nil, serverAddress)
|
|
|
|
if err != nil {
|
2022-01-13 14:26:16 +10:30
|
|
|
return errors.New("problem connecting to server, is it running?\n")
|
2022-01-09 13:05:36 +10:30
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2022-01-15 14:04:13 +10:30
|
|
|
log.Printf("Connection on %s\n", address)
|
2022-01-09 13:05:36 +10:30
|
|
|
|
|
|
|
sharedKey := secure.Handshake(conn)
|
2022-01-14 23:30:14 +10:30
|
|
|
secureConnection := secure.SecureConnection{Conn: conn, SharedKey: sharedKey, Buffer: &bytes.Buffer{}}
|
2022-01-09 13:05:36 +10:30
|
|
|
|
2022-01-13 14:26:16 +10:30
|
|
|
enc := gob.NewEncoder(&secureConnection)
|
2022-01-14 23:30:14 +10:30
|
|
|
dec := gob.NewDecoder(&secureConnection)
|
2022-01-09 13:05:36 +10:30
|
|
|
|
2022-01-14 23:30:14 +10:30
|
|
|
if c.list {
|
|
|
|
log.Printf("requesting file list")
|
|
|
|
// list mode
|
|
|
|
msg := secure.PacketStart{
|
|
|
|
OperationType: secure.OperationTypeList,
|
|
|
|
ClientName: "Justin Hawkins",
|
|
|
|
ProtocolVersion: "v1.0",
|
|
|
|
AuthToken: "abc123",
|
|
|
|
}
|
|
|
|
err := enc.Encode(msg)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// now we expect to get stuff back until we don't
|
|
|
|
for {
|
|
|
|
listPacket := secure.PacketListData{}
|
|
|
|
err := dec.Decode(&listPacket)
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
log.Printf("%d: %s (%d bytes)", listPacket.Id, listPacket.Kind, listPacket.FileSize)
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
log.Printf("done listing")
|
|
|
|
|
2022-01-15 14:04:13 +10:30
|
|
|
} else if c.receive {
|
|
|
|
log.Printf("receiving a file")
|
|
|
|
// list mode
|
|
|
|
msg := secure.PacketStart{
|
|
|
|
OperationType: secure.OperationTypeReceive,
|
|
|
|
ClientName: "Justin Hawkins",
|
|
|
|
ProtocolVersion: "v1.0",
|
|
|
|
AuthToken: "abc123",
|
|
|
|
}
|
|
|
|
err := enc.Encode(msg)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := secure.PacketReceiveDataStartRequest{
|
|
|
|
Id: 234,
|
|
|
|
}
|
|
|
|
err = enc.Encode(req)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// expect a response telling us if we can go ahead
|
|
|
|
res := secure.PacketReceiveDataStartResponse{}
|
|
|
|
err = dec.Decode(&res)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("server said %v", res)
|
|
|
|
if res.Status == secure.ReceiveDataStartResponseOK {
|
|
|
|
for {
|
|
|
|
res := secure.PacketReceiveDataNext{}
|
|
|
|
err = dec.Decode(&res)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
// log.Printf("got %d bytes, last is %t", res.Size, res.Last)
|
|
|
|
// print(res.Data)
|
|
|
|
os.Stdout.Write(res.Data[:res.Size])
|
|
|
|
if res.Last {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("finished")
|
|
|
|
} else {
|
|
|
|
panic("don't handle this yet")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.Close()
|
2022-01-14 23:30:14 +10:30
|
|
|
} else {
|
|
|
|
// must be send mode
|
2022-01-09 13:05:36 +10:30
|
|
|
|
2022-01-13 14:26:16 +10:30
|
|
|
msg := secure.PacketStart{
|
|
|
|
OperationType: secure.OperationTypeSend,
|
|
|
|
ClientName: "Justin Hawkins",
|
|
|
|
ProtocolVersion: "v1.0",
|
|
|
|
AuthToken: "abc123",
|
|
|
|
}
|
2022-01-09 13:05:36 +10:30
|
|
|
|
2022-01-13 14:26:16 +10:30
|
|
|
// gob.Register(secure.PacketSendStart{})
|
|
|
|
err := enc.Encode(msg)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-01-09 13:05:36 +10:30
|
|
|
|
2022-01-14 13:07:56 +10:30
|
|
|
data := secure.PacketSendDataStart{
|
|
|
|
Filename: "",
|
|
|
|
TotalSize: 0,
|
|
|
|
}
|
|
|
|
err = enc.Encode(data)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-01-13 22:20:35 +10:30
|
|
|
nBytes, nChunks := int64(0), int64(0)
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
buf := make([]byte, 0, 1024)
|
|
|
|
|
|
|
|
for {
|
|
|
|
n, err := reader.Read(buf[:cap(buf)])
|
2022-01-14 10:06:55 +10:30
|
|
|
|
2022-01-13 22:20:35 +10:30
|
|
|
buf = buf[:n]
|
2022-01-14 10:06:55 +10:30
|
|
|
|
2022-01-13 22:20:35 +10:30
|
|
|
if n == 0 {
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
nChunks++
|
|
|
|
nBytes += int64(len(buf))
|
2022-01-14 10:06:55 +10:30
|
|
|
|
2022-01-13 22:20:35 +10:30
|
|
|
send := secure.PacketSendDataNext{
|
|
|
|
Size: 5000,
|
|
|
|
Data: buf,
|
|
|
|
}
|
|
|
|
enc.Encode(send)
|
|
|
|
// time.Sleep(time.Second)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Println("Bytes:", nBytes, "Chunks:", nChunks)
|
|
|
|
|
2022-01-13 14:26:16 +10:30
|
|
|
conn.Close()
|
|
|
|
|
2022-01-09 13:05:36 +10:30
|
|
|
}
|
|
|
|
return nil
|
2022-01-14 23:30:14 +10:30
|
|
|
|
2022-01-09 13:05:36 +10:30
|
|
|
}
|