You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.4 KiB
80 lines
1.4 KiB
package main
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/gob"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
type Request struct {
|
|
Operation string
|
|
Site string
|
|
Password string
|
|
Timestamp int64
|
|
Nonce string
|
|
}
|
|
|
|
type Response struct {
|
|
Message string
|
|
}
|
|
|
|
func generateNonce() (string, error) {
|
|
b := make([]byte, 12) // 96-bit nonce
|
|
_, err := rand.Read(b)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return hex.EncodeToString(b), nil
|
|
}
|
|
|
|
func main() {
|
|
conn, err := net.Dial("tcp", "localhost:9898")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
var op, site, pwd string
|
|
|
|
fmt.Print("Operation (store/get): ")
|
|
fmt.Scanln(&op)
|
|
fmt.Print("Site: ")
|
|
fmt.Scanln(&site)
|
|
|
|
if op == "store" {
|
|
fmt.Print("Password: ")
|
|
fmt.Scanln(&pwd)
|
|
}
|
|
|
|
nonce, err := generateNonce()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
req := Request{
|
|
Operation: op,
|
|
Site: site,
|
|
Password: pwd,
|
|
Timestamp: time.Now().Unix(),
|
|
Nonce: nonce,
|
|
}
|
|
|
|
enc := gob.NewEncoder(conn)
|
|
dec := gob.NewDecoder(conn)
|
|
|
|
if err := enc.Encode(req); err != nil {
|
|
fmt.Println("Failed to send request:", err)
|
|
return
|
|
}
|
|
|
|
var res Response
|
|
if err := dec.Decode(&res); err != nil {
|
|
fmt.Println("Failed to receive response:", err)
|
|
return
|
|
}
|
|
|
|
fmt.Println("Server response:", res.Message)
|
|
}
|
|
|