Multiple files over 1 connection

This commit is contained in:
Joren Schipman 2024-05-02 16:00:39 +02:00
parent 505944667f
commit 7235048705
Signed by untrusted user who does not match committer: Joren
GPG Key ID: 280E33DFBC0F1B55

View File

@ -63,53 +63,88 @@ func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) {
fmt.Println("Got conn") fmt.Println("Got conn")
// Receive key, IV, and UID
keyData, err := bufio.NewReader(conn).ReadString('\n') keyData, err := bufio.NewReader(conn).ReadString('\n')
if err != nil { if err != nil {
log.Printf("Error reading key: %v", err) log.Printf("Error reading key: %v", err)
return return
} }
key, err := decryptKeyIV(strings.TrimSpace(keyData), privateKey)
key, _ := decryptKeyIV(keyData, privateKey) if err != nil {
log.Printf("Error decrypting key: %v", err)
return
}
conn.Write([]byte("Received key\n")) conn.Write([]byte("Received key\n"))
ivData, _ := bufio.NewReader(conn).ReadString('\n') ivData, err := bufio.NewReader(conn).ReadString('\n')
iv, _ := decryptKeyIV(ivData,privateKey) if err != nil {
log.Printf("Error reading IV: %v", err)
return
}
iv, err := decryptKeyIV(strings.TrimSpace(ivData), privateKey)
if err != nil {
log.Printf("Error decrypting IV: %v", err)
return
}
conn.Write([]byte("Received IV\n")) conn.Write([]byte("Received IV\n"))
uidData, _ := bufio.NewReader(conn).ReadString('\n') uidData, err := bufio.NewReader(conn).ReadString('\n')
uid, _ := decryptKeyIV(uidData,privateKey) if err != nil {
log.Printf("Error reading UID: %v", err)
return
}
uid, err := decryptKeyIV(strings.TrimSpace(uidData), privateKey)
if err != nil {
log.Printf("Error decrypting UID: %v", err)
return
}
conn.Write([]byte("Received UID\n")) conn.Write([]byte("Received UID\n"))
fmt.Println("Exchange, OK!") fmt.Println("Exchange, OK!")
var ciphertext []byte // Receive and handle multiple files
var chunk string
var plaintext []byte
for { for {
chunk, err = bufio.NewReader(conn).ReadString('\n') var plaintext []byte
for {
chunk, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.Printf("Error reading chunk: %v", err)
return
}
if strings.TrimSpace(chunk) == "END_OF_DATA" {
fmt.Printf("Received file\n")
break
}
ciphertext, err := base64.StdEncoding.DecodeString(strings.TrimSpace(chunk))
if err != nil {
log.Printf("Error decoding chunk: %v", err)
return
}
plaintextChunk, err := decrypt(ciphertext, key, iv)
if err != nil {
log.Printf("Error decrypting chunk: %v", err)
return
}
plaintext = append(plaintext, plaintextChunk...)
conn.Write([]byte("Received and decrypted chunk\n"))
}
go handleDecrypted(plaintext, uid)
conn.Write([]byte("Received and decrypted\n"))
// Check if there are more files to receive
moreFiles, err := bufio.NewReader(conn).ReadString('\n')
if err != nil { if err != nil {
log.Printf("Error reading chunk: %v", err) log.Printf("Error reading more files signal: %v", err)
return return
} }
if strings.TrimSpace(chunk) == "END_OF_DATA" { if strings.TrimSpace(moreFiles) == "END_OF_COMMUNICATION" {
fmt.Println("Client ended communication") fmt.Println("Client ended communication")
break break
} }
ciphertext, _ = base64.StdEncoding.DecodeString(strings.TrimSpace(chunk))
plaintextChunk, err := decrypt(ciphertext, key, iv)
if err != nil {
log.Printf("Error decrypting chunk: %v", err)
return
}
plaintext = append(plaintext, plaintextChunk...)
conn.Write([]byte("Received and decrypted chunk\n"))
} }
handleDecrypted(plaintext, uid)
conn.Write([]byte("Ready for next operation\n"))
} }