handle more failures

This commit is contained in:
Cyberes 2024-06-27 17:38:04 -06:00
parent 059c3514b4
commit 2fd486a6e4
2 changed files with 17 additions and 7 deletions

View File

@ -82,7 +82,7 @@ func main() {
forwardConn, err := net.DialUDP("udp", nil, forwardAddr) forwardConn, err := net.DialUDP("udp", nil, forwardAddr)
if err != nil { if err != nil {
logger.Errorln("Error dialing to forward address:", err) logger.Fatalln("Error dialing to forward address:", err)
return return
} }
defer forwardConn.Close() defer forwardConn.Close()
@ -93,27 +93,31 @@ func main() {
buf := make([]byte, 1500) // 1500 is the standard internet MTU buf := make([]byte, 1500) // 1500 is the standard internet MTU
n, addr, err := conn.ReadFromUDP(buf) n, addr, err := conn.ReadFromUDP(buf)
if err != nil { if err != nil {
logger.Errorln("Error reading from UDP:", err) logger.Fatalf(formatLogMsg(addr.IP.String(), dest.Address, dest.Port, fmt.Sprintf("Error reading from UDP: %s", err)))
return continue
} }
// Forward the decrypted message // Forward the decrypted message
go func(addr *net.UDPAddr, buf []byte, n int) { go func(addr *net.UDPAddr, buf []byte, n int) {
plaintext, err := encryption.Decrypt(key, buf[:n]) // Use only the part of the buffer that has data plaintext, err := encryption.Decrypt(key, buf[:n]) // Use only the part of the buffer that has data
if err != nil { if err != nil {
fmt.Println("Error decrypting message:", err) logger.Errorf(formatLogMsg(addr.IP.String(), dest.Address, dest.Port, fmt.Sprintf("Error decrypting message: %s", err)))
return return
} }
_, err = forwardConn.Write(plaintext) _, err = forwardConn.Write(plaintext)
if err != nil { if err != nil {
fmt.Println("Error forwarding message:", err) logger.Errorf(formatLogMsg(addr.IP.String(), dest.Address, dest.Port, fmt.Sprintf("Error forwarding message: %s", err)))
return return
} }
logger.Infof("%s -> %s:%d -- %s\n", addr.IP, dest.Address, dest.Port, string(plaintext)) logger.Infof(formatLogMsg(addr.IP.String(), dest.Address, dest.Port, string(plaintext)))
}(addr, buf, n) }(addr, buf, n)
} }
}(port, dest) }(port, dest)
} }
select {} select {}
} }
func formatLogMsg(srcIp string, destIp string, destPort int, msg string) string {
return fmt.Sprintf("%s -> %s:%d -- %s", srcIp, destIp, destPort, msg)
}

View File

@ -21,8 +21,14 @@ ciphertext, tag = cipher.encrypt_and_digest(message)
# Send the encrypted message to the server # Send the encrypted message to the server
sock.sendto(cipher.nonce + ciphertext + tag, server_address) sock.sendto(cipher.nonce + ciphertext + tag, server_address)
sock.close()
cipher = ChaCha20_Poly1305.new(key=key, nonce=cipher.nonce) cipher = ChaCha20_Poly1305.new(key=key, nonce=cipher.nonce)
plaintext = cipher.decrypt_and_verify(ciphertext, tag) plaintext = cipher.decrypt_and_verify(ciphertext, tag)
print(plaintext) print(plaintext)
time.sleep(1)
# Send a bad message to the server
sock.sendto(b"this should fail", server_address)
sock.close()