Multiple files over 1 connection
This commit is contained in:
parent
505944667f
commit
7235048705
87
rsaserver.go
87
rsaserver.go
@ -63,53 +63,88 @@ func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) {
|
||||
|
||||
fmt.Println("Got conn")
|
||||
|
||||
// Receive key, IV, and UID
|
||||
keyData, err := bufio.NewReader(conn).ReadString('\n')
|
||||
if err != nil {
|
||||
log.Printf("Error reading key: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
key, _ := decryptKeyIV(keyData, privateKey)
|
||||
key, err := decryptKeyIV(strings.TrimSpace(keyData), privateKey)
|
||||
if err != nil {
|
||||
log.Printf("Error decrypting key: %v", err)
|
||||
return
|
||||
}
|
||||
conn.Write([]byte("Received key\n"))
|
||||
|
||||
ivData, _ := bufio.NewReader(conn).ReadString('\n')
|
||||
iv, _ := decryptKeyIV(ivData,privateKey)
|
||||
ivData, err := bufio.NewReader(conn).ReadString('\n')
|
||||
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"))
|
||||
|
||||
uidData, _ := bufio.NewReader(conn).ReadString('\n')
|
||||
uid, _ := decryptKeyIV(uidData,privateKey)
|
||||
uidData, err := bufio.NewReader(conn).ReadString('\n')
|
||||
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"))
|
||||
|
||||
fmt.Println("Exchange, OK!")
|
||||
|
||||
var ciphertext []byte
|
||||
var chunk string
|
||||
var plaintext []byte
|
||||
|
||||
// Receive and handle multiple files
|
||||
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 {
|
||||
log.Printf("Error reading chunk: %v", err)
|
||||
log.Printf("Error reading more files signal: %v", err)
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(chunk) == "END_OF_DATA" {
|
||||
if strings.TrimSpace(moreFiles) == "END_OF_COMMUNICATION" {
|
||||
fmt.Println("Client ended communication")
|
||||
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"))
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user