2022-01-09 13:05:36 +10:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-01-13 14:26:16 +10:30
|
|
|
"encoding/gob"
|
2022-01-09 13:05:36 +10:30
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-01-13 14:26:16 +10:30
|
|
|
"log"
|
2022-01-09 13:05:36 +10:30
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/tardisx/netgiv/secure"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
port int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Connect() error {
|
|
|
|
address := fmt.Sprintf("127.0.0.1:%d", c.port)
|
|
|
|
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()
|
|
|
|
|
|
|
|
fmt.Printf("Connection on %s\n", address)
|
|
|
|
|
|
|
|
sharedKey := secure.Handshake(conn)
|
|
|
|
secureConnection := secure.SecureConnection{Conn: conn, SharedKey: sharedKey}
|
|
|
|
|
2022-01-13 14:26:16 +10:30
|
|
|
// reader := bufio.NewReader(os.Stdin)
|
|
|
|
enc := gob.NewEncoder(&secureConnection)
|
2022-01-09 13:05:36 +10:30
|
|
|
|
|
|
|
for {
|
|
|
|
|
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-13 14:26:16 +10:30
|
|
|
data := secure.PacketSendDataStart{
|
|
|
|
Filename: "foobar",
|
|
|
|
TotalSize: 3,
|
|
|
|
Data: []byte{0x20, 0x21, 0x22},
|
|
|
|
}
|
|
|
|
err = enc.Encode(data)
|
2022-01-09 13:05:36 +10:30
|
|
|
if err != nil {
|
2022-01-13 14:26:16 +10:30
|
|
|
panic(err)
|
2022-01-09 13:05:36 +10:30
|
|
|
}
|
2022-01-13 14:26:16 +10:30
|
|
|
log.Print("done that")
|
|
|
|
conn.Close()
|
|
|
|
|
|
|
|
break
|
|
|
|
// response := make([]byte, 1024)
|
|
|
|
|
|
|
|
// _, err = secureConnection.Read(response)
|
|
|
|
// if err != nil {
|
|
|
|
// fmt.Print("Connection to the server was closed.\n")
|
|
|
|
// break
|
|
|
|
// }
|
2022-01-09 13:05:36 +10:30
|
|
|
|
2022-01-13 14:26:16 +10:30
|
|
|
// fmt.Printf("%s\n", response)
|
2022-01-09 13:05:36 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|