netgiv/client.go

104 lines
1.7 KiB
Go
Raw Normal View History

package main
import (
2022-01-13 22:20:35 +10:30
"bufio"
"encoding/gob"
"errors"
"fmt"
2022-01-13 22:20:35 +10:30
"io"
"log"
"net"
2022-01-13 22:20:35 +10:30
"os"
"github.com/tardisx/netgiv/secure"
)
type Client struct {
2022-01-13 22:20:35 +10:30
address string
port int
}
func (c *Client) Connect() error {
2022-01-13 22:20:35 +10:30
address := fmt.Sprintf("%s:%d", c.address, c.port)
serverAddress, _ := net.ResolveTCPAddr("tcp", address)
conn, err := net.DialTCP("tcp", nil, serverAddress)
if err != nil {
return errors.New("problem connecting to server, is it running?\n")
}
defer conn.Close()
fmt.Printf("Connection on %s\n", address)
sharedKey := secure.Handshake(conn)
secureConnection := secure.SecureConnection{Conn: conn, SharedKey: sharedKey}
// reader := bufio.NewReader(os.Stdin)
enc := gob.NewEncoder(&secureConnection)
for {
msg := secure.PacketStart{
OperationType: secure.OperationTypeSend,
ClientName: "Justin Hawkins",
ProtocolVersion: "v1.0",
AuthToken: "abc123",
}
// gob.Register(secure.PacketSendStart{})
err := enc.Encode(msg)
if err != nil {
panic(err)
}
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)
conn.Close()
break
}
return nil
}