Multiple files over 1 connection
This commit is contained in:
parent
505944667f
commit
7235048705
65
rsaserver.go
65
rsaserver.go
@ -63,40 +63,63 @@ 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.Println("Client ended communication")
|
||||
fmt.Printf("Received file\n")
|
||||
break
|
||||
}
|
||||
ciphertext, _ = base64.StdEncoding.DecodeString(strings.TrimSpace(chunk))
|
||||
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 {
|
||||
@ -108,8 +131,20 @@ func handleConnection(conn net.Conn, privateKey *rsa.PrivateKey) {
|
||||
conn.Write([]byte("Received and decrypted chunk\n"))
|
||||
}
|
||||
|
||||
handleDecrypted(plaintext, uid)
|
||||
conn.Write([]byte("Ready for next operation\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 more files signal: %v", err)
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(moreFiles) == "END_OF_COMMUNICATION" {
|
||||
fmt.Println("Client ended communication")
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user