network reliability and security hardening. auto-cleanup expired backoff
entries, atomic file writes for key pinning, forward secrecy via rekeying, peer eviction when at max connections, NAT-PMP source address verification, UPnP response timeout
This commit is contained in:
@@ -402,13 +402,24 @@ pub const PinnedKeyExport = struct {
|
||||
// File Persistence
|
||||
// ============================================================================
|
||||
|
||||
/// Save pinned keys to a file
|
||||
/// Save pinned keys to a file.
|
||||
/// Uses atomic write (temp file + rename) to prevent corruption on crash.
|
||||
pub fn saveToFile(store: *KeyPinStore, path: []const u8) !void {
|
||||
const exports = try store.exportKeys(store.allocator);
|
||||
defer if (exports.len > 0) store.allocator.free(exports);
|
||||
|
||||
var file = try std.fs.cwd().createFile(path, .{});
|
||||
defer file.close();
|
||||
// Build temp path: original path + ".tmp"
|
||||
const tmp_path = try store.allocator.alloc(u8, path.len + 4);
|
||||
defer store.allocator.free(tmp_path);
|
||||
@memcpy(tmp_path[0..path.len], path);
|
||||
@memcpy(tmp_path[path.len..], ".tmp");
|
||||
|
||||
// Write to temp file
|
||||
var file = try std.fs.cwd().createFile(tmp_path, .{});
|
||||
errdefer {
|
||||
file.close();
|
||||
std.fs.cwd().deleteFile(tmp_path) catch {};
|
||||
}
|
||||
|
||||
// Write header: magic + version
|
||||
_ = try file.write("KEYPIN01");
|
||||
@@ -440,6 +451,14 @@ pub fn saveToFile(store: *KeyPinStore, path: []const u8) !void {
|
||||
_ = try file.write(&[_]u8{ 0, 0 });
|
||||
}
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
// Atomic rename: replaces target in one syscall
|
||||
std.fs.cwd().rename(tmp_path, path) catch |err| {
|
||||
std.fs.cwd().deleteFile(tmp_path) catch {};
|
||||
return err;
|
||||
};
|
||||
}
|
||||
|
||||
/// Load pinned keys from a file
|
||||
|
||||
Reference in New Issue
Block a user