mirror of https://github.com/wg-easy/wg-easy
Browse Source
* Add per-client firewall filtering Implement server-side firewall rules to restrict client network access, allowing administrators to enforce security policies that cannot be bypassed by clients modifying their local configuration. This feature addresses the limitation where "Allowed IPs" only controls client-side routing but doesn't prevent clients from accessing networks they shouldn't reach. The firewall rules are enforced on the server using iptables/ip6tables and provide true access control. Features: - Opt-in via "Enable Per-Client Firewall" toggle in admin interface - Per-client "Firewall Allowed IPs" field for granular control - Support for IPs, CIDRs, and port-based filtering - Protocol specification: TCP, UDP, or both (default) - IPv4 and IPv6 dual-stack support - Falls back to client's allowedIps when firewallIps is empty - Clean separation of routing (allowedIps) from security (firewallIps) Supported formats: - 10.10.0.3 (single IP) - 10.10.0.0/24 (CIDR range) - 192.168.1.5:443 (IP with port, both TCP+UDP) - 192.168.1.5:443/tcp (IP with specific protocol) - [2001:db8::1]:443 (IPv6 with port) Implementation: - New database columns: firewall_enabled (interfaces), firewall_ips (clients) - Migration 0003_add_firewall_filtering for schema updates - firewall.ts utility for iptables chain management (WG_CLIENTS chain) - Integration into WireGuard.ts for automatic rule application - UI components with conditional rendering based on firewall toggle Technical details: - Uses custom WG_CLIENTS iptables chain for isolation - Rebuild strategy: flush and recreate all rules on config save - Mutex protection via rebuildInProgress/rebuildQueued flags - Graceful cleanup when firewall is disabled - No new dependencies (uses existing is-ip, is-cidr packages) * added Comprehensive documentation in README and docs/ for firewall filtering * validate firewall IPs * check for iptables before enabling the firewall and inform the user if it is missing * updated firewall docs * fix imports * remove extra import * Document all allowed IP/cidr/port/proto combinations that are allowed and check on save * add note on firewall being experimental and how to opt a single client out of the firewall. * cleanup more imports * add tests * Fix firewall IPv6 validation and test expectations Updated validation to correctly handle plain and bracketed IPv6 addresses, and fixed test to expect string from schema instead of object. * added comments to firewall rules and updated tests * fix auto-import * fix typescript errors * recreate sql migrations and rebase * improve tests, typechecking, documentation * fix formatting, fix types * improve type * added note for including host's IP in client firewall * updated language to include cidr and protocol options * another language update * refer to docs for firewall allowed IPs --------- Co-authored-by: Bernd Storath <[email protected]>pull/2518/head
committed by
GitHub
22 changed files with 1940 additions and 9 deletions
@ -0,0 +1,2 @@ |
|||
ALTER TABLE `clients_table` ADD `firewall_ips` text;--> statement-breakpoint |
|||
ALTER TABLE `interfaces_table` ADD `firewall_enabled` integer DEFAULT false NOT NULL; |
|||
@ -0,0 +1,987 @@ |
|||
{ |
|||
"version": "6", |
|||
"dialect": "sqlite", |
|||
"id": "0f072f91-cd10-4702-ae7b-245255d69d1e", |
|||
"prevId": "68c43b7a-772d-4c34-8278-e9fce5b53df1", |
|||
"tables": { |
|||
"clients_table": { |
|||
"name": "clients_table", |
|||
"columns": { |
|||
"id": { |
|||
"name": "id", |
|||
"type": "integer", |
|||
"primaryKey": true, |
|||
"notNull": true, |
|||
"autoincrement": true |
|||
}, |
|||
"user_id": { |
|||
"name": "user_id", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"interface_id": { |
|||
"name": "interface_id", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"name": { |
|||
"name": "name", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"ipv4_address": { |
|||
"name": "ipv4_address", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"ipv6_address": { |
|||
"name": "ipv6_address", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"pre_up": { |
|||
"name": "pre_up", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "''" |
|||
}, |
|||
"post_up": { |
|||
"name": "post_up", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "''" |
|||
}, |
|||
"pre_down": { |
|||
"name": "pre_down", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "''" |
|||
}, |
|||
"post_down": { |
|||
"name": "post_down", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "''" |
|||
}, |
|||
"private_key": { |
|||
"name": "private_key", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"public_key": { |
|||
"name": "public_key", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"pre_shared_key": { |
|||
"name": "pre_shared_key", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"expires_at": { |
|||
"name": "expires_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"allowed_ips": { |
|||
"name": "allowed_ips", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"server_allowed_ips": { |
|||
"name": "server_allowed_ips", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"firewall_ips": { |
|||
"name": "firewall_ips", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"persistent_keepalive": { |
|||
"name": "persistent_keepalive", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"mtu": { |
|||
"name": "mtu", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"j_c": { |
|||
"name": "j_c", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"j_min": { |
|||
"name": "j_min", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"j_max": { |
|||
"name": "j_max", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"i1": { |
|||
"name": "i1", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"i2": { |
|||
"name": "i2", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"i3": { |
|||
"name": "i3", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"i4": { |
|||
"name": "i4", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"i5": { |
|||
"name": "i5", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"dns": { |
|||
"name": "dns", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"server_endpoint": { |
|||
"name": "server_endpoint", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"enabled": { |
|||
"name": "enabled", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"created_at": { |
|||
"name": "created_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "(CURRENT_TIMESTAMP)" |
|||
}, |
|||
"updated_at": { |
|||
"name": "updated_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "(CURRENT_TIMESTAMP)" |
|||
} |
|||
}, |
|||
"indexes": { |
|||
"clients_table_ipv4_address_unique": { |
|||
"name": "clients_table_ipv4_address_unique", |
|||
"columns": [ |
|||
"ipv4_address" |
|||
], |
|||
"isUnique": true |
|||
}, |
|||
"clients_table_ipv6_address_unique": { |
|||
"name": "clients_table_ipv6_address_unique", |
|||
"columns": [ |
|||
"ipv6_address" |
|||
], |
|||
"isUnique": true |
|||
} |
|||
}, |
|||
"foreignKeys": { |
|||
"clients_table_user_id_users_table_id_fk": { |
|||
"name": "clients_table_user_id_users_table_id_fk", |
|||
"tableFrom": "clients_table", |
|||
"tableTo": "users_table", |
|||
"columnsFrom": [ |
|||
"user_id" |
|||
], |
|||
"columnsTo": [ |
|||
"id" |
|||
], |
|||
"onDelete": "restrict", |
|||
"onUpdate": "cascade" |
|||
}, |
|||
"clients_table_interface_id_interfaces_table_name_fk": { |
|||
"name": "clients_table_interface_id_interfaces_table_name_fk", |
|||
"tableFrom": "clients_table", |
|||
"tableTo": "interfaces_table", |
|||
"columnsFrom": [ |
|||
"interface_id" |
|||
], |
|||
"columnsTo": [ |
|||
"name" |
|||
], |
|||
"onDelete": "cascade", |
|||
"onUpdate": "cascade" |
|||
} |
|||
}, |
|||
"compositePrimaryKeys": {}, |
|||
"uniqueConstraints": {}, |
|||
"checkConstraints": {} |
|||
}, |
|||
"general_table": { |
|||
"name": "general_table", |
|||
"columns": { |
|||
"id": { |
|||
"name": "id", |
|||
"type": "integer", |
|||
"primaryKey": true, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": 1 |
|||
}, |
|||
"setup_step": { |
|||
"name": "setup_step", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"session_password": { |
|||
"name": "session_password", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"session_timeout": { |
|||
"name": "session_timeout", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"metrics_prometheus": { |
|||
"name": "metrics_prometheus", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"metrics_json": { |
|||
"name": "metrics_json", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"metrics_password": { |
|||
"name": "metrics_password", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"created_at": { |
|||
"name": "created_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "(CURRENT_TIMESTAMP)" |
|||
}, |
|||
"updated_at": { |
|||
"name": "updated_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "(CURRENT_TIMESTAMP)" |
|||
} |
|||
}, |
|||
"indexes": {}, |
|||
"foreignKeys": {}, |
|||
"compositePrimaryKeys": {}, |
|||
"uniqueConstraints": {}, |
|||
"checkConstraints": {} |
|||
}, |
|||
"hooks_table": { |
|||
"name": "hooks_table", |
|||
"columns": { |
|||
"id": { |
|||
"name": "id", |
|||
"type": "text", |
|||
"primaryKey": true, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"pre_up": { |
|||
"name": "pre_up", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"post_up": { |
|||
"name": "post_up", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"pre_down": { |
|||
"name": "pre_down", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"post_down": { |
|||
"name": "post_down", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"created_at": { |
|||
"name": "created_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "(CURRENT_TIMESTAMP)" |
|||
}, |
|||
"updated_at": { |
|||
"name": "updated_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "(CURRENT_TIMESTAMP)" |
|||
} |
|||
}, |
|||
"indexes": {}, |
|||
"foreignKeys": { |
|||
"hooks_table_id_interfaces_table_name_fk": { |
|||
"name": "hooks_table_id_interfaces_table_name_fk", |
|||
"tableFrom": "hooks_table", |
|||
"tableTo": "interfaces_table", |
|||
"columnsFrom": [ |
|||
"id" |
|||
], |
|||
"columnsTo": [ |
|||
"name" |
|||
], |
|||
"onDelete": "cascade", |
|||
"onUpdate": "cascade" |
|||
} |
|||
}, |
|||
"compositePrimaryKeys": {}, |
|||
"uniqueConstraints": {}, |
|||
"checkConstraints": {} |
|||
}, |
|||
"interfaces_table": { |
|||
"name": "interfaces_table", |
|||
"columns": { |
|||
"name": { |
|||
"name": "name", |
|||
"type": "text", |
|||
"primaryKey": true, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"device": { |
|||
"name": "device", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"port": { |
|||
"name": "port", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"private_key": { |
|||
"name": "private_key", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"public_key": { |
|||
"name": "public_key", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"ipv4_cidr": { |
|||
"name": "ipv4_cidr", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"ipv6_cidr": { |
|||
"name": "ipv6_cidr", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"mtu": { |
|||
"name": "mtu", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"j_c": { |
|||
"name": "j_c", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false, |
|||
"default": 7 |
|||
}, |
|||
"j_min": { |
|||
"name": "j_min", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false, |
|||
"default": 10 |
|||
}, |
|||
"j_max": { |
|||
"name": "j_max", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false, |
|||
"default": 1000 |
|||
}, |
|||
"s1": { |
|||
"name": "s1", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false, |
|||
"default": 128 |
|||
}, |
|||
"s2": { |
|||
"name": "s2", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false, |
|||
"default": 56 |
|||
}, |
|||
"s3": { |
|||
"name": "s3", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"s4": { |
|||
"name": "s4", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"h1": { |
|||
"name": "h1", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"h2": { |
|||
"name": "h2", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"h3": { |
|||
"name": "h3", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"h4": { |
|||
"name": "h4", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"i1": { |
|||
"name": "i1", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"i2": { |
|||
"name": "i2", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"i3": { |
|||
"name": "i3", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"i4": { |
|||
"name": "i4", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"i5": { |
|||
"name": "i5", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"enabled": { |
|||
"name": "enabled", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"firewall_enabled": { |
|||
"name": "firewall_enabled", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": false |
|||
}, |
|||
"created_at": { |
|||
"name": "created_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "(CURRENT_TIMESTAMP)" |
|||
}, |
|||
"updated_at": { |
|||
"name": "updated_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "(CURRENT_TIMESTAMP)" |
|||
} |
|||
}, |
|||
"indexes": { |
|||
"interfaces_table_port_unique": { |
|||
"name": "interfaces_table_port_unique", |
|||
"columns": [ |
|||
"port" |
|||
], |
|||
"isUnique": true |
|||
} |
|||
}, |
|||
"foreignKeys": {}, |
|||
"compositePrimaryKeys": {}, |
|||
"uniqueConstraints": {}, |
|||
"checkConstraints": {} |
|||
}, |
|||
"one_time_links_table": { |
|||
"name": "one_time_links_table", |
|||
"columns": { |
|||
"id": { |
|||
"name": "id", |
|||
"type": "integer", |
|||
"primaryKey": true, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"one_time_link": { |
|||
"name": "one_time_link", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"expires_at": { |
|||
"name": "expires_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"created_at": { |
|||
"name": "created_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "(CURRENT_TIMESTAMP)" |
|||
}, |
|||
"updated_at": { |
|||
"name": "updated_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "(CURRENT_TIMESTAMP)" |
|||
} |
|||
}, |
|||
"indexes": { |
|||
"one_time_links_table_one_time_link_unique": { |
|||
"name": "one_time_links_table_one_time_link_unique", |
|||
"columns": [ |
|||
"one_time_link" |
|||
], |
|||
"isUnique": true |
|||
} |
|||
}, |
|||
"foreignKeys": { |
|||
"one_time_links_table_id_clients_table_id_fk": { |
|||
"name": "one_time_links_table_id_clients_table_id_fk", |
|||
"tableFrom": "one_time_links_table", |
|||
"tableTo": "clients_table", |
|||
"columnsFrom": [ |
|||
"id" |
|||
], |
|||
"columnsTo": [ |
|||
"id" |
|||
], |
|||
"onDelete": "cascade", |
|||
"onUpdate": "cascade" |
|||
} |
|||
}, |
|||
"compositePrimaryKeys": {}, |
|||
"uniqueConstraints": {}, |
|||
"checkConstraints": {} |
|||
}, |
|||
"users_table": { |
|||
"name": "users_table", |
|||
"columns": { |
|||
"id": { |
|||
"name": "id", |
|||
"type": "integer", |
|||
"primaryKey": true, |
|||
"notNull": true, |
|||
"autoincrement": true |
|||
}, |
|||
"username": { |
|||
"name": "username", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"password": { |
|||
"name": "password", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"email": { |
|||
"name": "email", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"name": { |
|||
"name": "name", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"role": { |
|||
"name": "role", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"totp_key": { |
|||
"name": "totp_key", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"totp_verified": { |
|||
"name": "totp_verified", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"enabled": { |
|||
"name": "enabled", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"created_at": { |
|||
"name": "created_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "(CURRENT_TIMESTAMP)" |
|||
}, |
|||
"updated_at": { |
|||
"name": "updated_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "(CURRENT_TIMESTAMP)" |
|||
} |
|||
}, |
|||
"indexes": { |
|||
"users_table_username_unique": { |
|||
"name": "users_table_username_unique", |
|||
"columns": [ |
|||
"username" |
|||
], |
|||
"isUnique": true |
|||
} |
|||
}, |
|||
"foreignKeys": {}, |
|||
"compositePrimaryKeys": {}, |
|||
"uniqueConstraints": {}, |
|||
"checkConstraints": {} |
|||
}, |
|||
"user_configs_table": { |
|||
"name": "user_configs_table", |
|||
"columns": { |
|||
"id": { |
|||
"name": "id", |
|||
"type": "text", |
|||
"primaryKey": true, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"default_mtu": { |
|||
"name": "default_mtu", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"default_persistent_keepalive": { |
|||
"name": "default_persistent_keepalive", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"default_dns": { |
|||
"name": "default_dns", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"default_allowed_ips": { |
|||
"name": "default_allowed_ips", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"default_j_c": { |
|||
"name": "default_j_c", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false, |
|||
"default": 7 |
|||
}, |
|||
"default_j_min": { |
|||
"name": "default_j_min", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false, |
|||
"default": 10 |
|||
}, |
|||
"default_j_max": { |
|||
"name": "default_j_max", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false, |
|||
"default": 1000 |
|||
}, |
|||
"default_i1": { |
|||
"name": "default_i1", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"default_i2": { |
|||
"name": "default_i2", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"default_i3": { |
|||
"name": "default_i3", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"default_i4": { |
|||
"name": "default_i4", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"default_i5": { |
|||
"name": "default_i5", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": false, |
|||
"autoincrement": false |
|||
}, |
|||
"host": { |
|||
"name": "host", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"port": { |
|||
"name": "port", |
|||
"type": "integer", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false |
|||
}, |
|||
"created_at": { |
|||
"name": "created_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "(CURRENT_TIMESTAMP)" |
|||
}, |
|||
"updated_at": { |
|||
"name": "updated_at", |
|||
"type": "text", |
|||
"primaryKey": false, |
|||
"notNull": true, |
|||
"autoincrement": false, |
|||
"default": "(CURRENT_TIMESTAMP)" |
|||
} |
|||
}, |
|||
"indexes": {}, |
|||
"foreignKeys": { |
|||
"user_configs_table_id_interfaces_table_name_fk": { |
|||
"name": "user_configs_table_id_interfaces_table_name_fk", |
|||
"tableFrom": "user_configs_table", |
|||
"tableTo": "interfaces_table", |
|||
"columnsFrom": [ |
|||
"id" |
|||
], |
|||
"columnsTo": [ |
|||
"name" |
|||
], |
|||
"onDelete": "cascade", |
|||
"onUpdate": "cascade" |
|||
} |
|||
}, |
|||
"compositePrimaryKeys": {}, |
|||
"uniqueConstraints": {}, |
|||
"checkConstraints": {} |
|||
} |
|||
}, |
|||
"views": {}, |
|||
"enums": {}, |
|||
"_meta": { |
|||
"schemas": {}, |
|||
"tables": {}, |
|||
"columns": {} |
|||
}, |
|||
"internal": { |
|||
"indexes": {} |
|||
} |
|||
} |
|||
@ -0,0 +1,363 @@ |
|||
import debug from 'debug'; |
|||
import { isIPv6 } from 'is-ip'; |
|||
|
|||
import type { ClientType } from '#db/repositories/client/types'; |
|||
import type { InterfaceType } from '#db/repositories/interface/types'; |
|||
import type { UserConfigType } from '#db/repositories/userConfig/types'; |
|||
|
|||
const FW_DEBUG = debug('Firewall'); |
|||
const CHAIN_NAME = 'WG_CLIENTS'; |
|||
|
|||
// Mutex to prevent concurrent rule rebuilds
|
|||
let rebuildInProgress = false; |
|||
let rebuildQueued = false; |
|||
|
|||
// Cache iptables availability check result
|
|||
let iptablesAvailable: boolean | null = null; |
|||
|
|||
type ParsedEntry = { |
|||
ip: string; |
|||
port?: number; |
|||
proto?: 'tcp' | 'udp' | 'both'; |
|||
}; |
|||
|
|||
type FirewallClient = Pick< |
|||
ClientType, |
|||
| 'id' |
|||
| 'name' |
|||
| 'ipv4Address' |
|||
| 'ipv6Address' |
|||
| 'allowedIps' |
|||
| 'firewallIps' |
|||
| 'enabled' |
|||
>; |
|||
|
|||
/** |
|||
* Sanitize a client identifier for use in an iptables comment. |
|||
* Strips all characters except ASCII alphanumeric, space, underscore, hyphen, and dot. |
|||
* Combines with client ID for a safe, identifiable comment. |
|||
* Truncates to 256 bytes (iptables comment module limit). |
|||
*/ |
|||
function sanitizeComment(clientId: number, clientName: string): string { |
|||
const safe = clientName.replace(/[^a-zA-Z0-9 _.-]/g, ''); |
|||
const comment = `client ${clientId}: ${safe}`; |
|||
return comment.slice(0, 256); |
|||
} |
|||
|
|||
/** |
|||
* Parse a firewall entry string into its components. |
|||
* Supports formats: |
|||
* - IP: "10.0.0.1" or "2001:db8::1" |
|||
* - CIDR: "10.0.0.0/24" or "2001:db8::/32" |
|||
* - IP:port: "10.0.0.1:443" or "[2001:db8::1]:443" |
|||
* - IP:port/proto: "10.0.0.1:443/tcp" or "10.0.0.1:53/udp" |
|||
* - CIDR:port: "10.0.0.0/24:443" |
|||
* - CIDR:port/proto: "10.0.0.0/24:443/tcp" or "10.0.0.0/24:53/udp" |
|||
* |
|||
* Note: Protocol (/tcp or /udp) requires a port. "IP/tcp" or "CIDR/tcp" without |
|||
* a port is invalid and will throw an error. |
|||
* |
|||
* @throws {Error} If protocol is specified without a port |
|||
*/ |
|||
function parseFirewallEntry(entry: string): ParsedEntry { |
|||
// Extract protocol suffix first: /tcp or /udp
|
|||
let proto: 'tcp' | 'udp' | 'both' | undefined; |
|||
let remaining = entry; |
|||
|
|||
if (entry.endsWith('/tcp')) { |
|||
proto = 'tcp'; |
|||
remaining = entry.slice(0, -4); |
|||
} else if (entry.endsWith('/udp')) { |
|||
proto = 'udp'; |
|||
remaining = entry.slice(0, -4); |
|||
} |
|||
|
|||
// Handle IPv6 with port: [2001:db8::1]:443
|
|||
if (remaining.startsWith('[')) { |
|||
const match = remaining.match(/^\[(.+)\]:(\d+)$/); |
|||
if (match && match[1] && match[2]) { |
|||
return { |
|||
ip: match[1], |
|||
port: parseInt(match[2], 10), |
|||
proto: proto ?? 'both', |
|||
}; |
|||
} |
|||
// Just bracketed IPv6 without port
|
|||
const ipMatch = remaining.match(/^\[(.+)\]$/); |
|||
if (ipMatch && ipMatch[1]) { |
|||
if (proto) { |
|||
throw new Error( |
|||
`Invalid firewall entry "${entry}": Protocol (/${proto}) requires a port. Use format like "[${ipMatch[1]}]:443/${proto}"` |
|||
); |
|||
} |
|||
return { ip: ipMatch[1] }; |
|||
} |
|||
if (proto) { |
|||
throw new Error( |
|||
`Invalid firewall entry "${entry}": Protocol (/${proto}) requires a port` |
|||
); |
|||
} |
|||
return { ip: remaining }; |
|||
} |
|||
|
|||
// Handle IPv4 with port or CIDR with port
|
|||
// Count colons to distinguish IPv6 from IPv4:port
|
|||
const colonCount = (remaining.match(/:/g) || []).length; |
|||
|
|||
if (colonCount === 1) { |
|||
// Could be IPv4:port or CIDR:port
|
|||
const lastColon = remaining.lastIndexOf(':'); |
|||
const possiblePort = remaining.slice(lastColon + 1); |
|||
if (/^\d+$/.test(possiblePort)) { |
|||
return { |
|||
ip: remaining.slice(0, lastColon), |
|||
port: parseInt(possiblePort, 10), |
|||
proto: proto ?? 'both', |
|||
}; |
|||
} |
|||
} |
|||
|
|||
// Plain IP or CIDR (IPv4 or IPv6)
|
|||
if (proto) { |
|||
throw new Error( |
|||
`Invalid firewall entry "${entry}": Protocol (/${proto}) requires a port. Use format like "${remaining}:443/${proto}"` |
|||
); |
|||
} |
|||
return { ip: remaining }; |
|||
} |
|||
|
|||
/** |
|||
* Generate iptables rule arguments for a single firewall entry |
|||
*/ |
|||
function generateRuleArgs( |
|||
clientIp: string, |
|||
entry: ParsedEntry, |
|||
comment?: string, |
|||
action: 'A' | 'D' = 'A' |
|||
): string[] { |
|||
const rules: string[] = []; |
|||
const commentArg = comment ? ` -m comment --comment "${comment}"` : ''; |
|||
const baseArgs = `-${action} ${CHAIN_NAME} -s ${clientIp} -d ${entry.ip}`; |
|||
|
|||
if (entry.port) { |
|||
// Port-specific rules
|
|||
if (entry.proto === 'tcp' || entry.proto === 'both') { |
|||
rules.push( |
|||
`${baseArgs} -p tcp --dport ${entry.port}${commentArg} -j ACCEPT` |
|||
); |
|||
} |
|||
if (entry.proto === 'udp' || entry.proto === 'both') { |
|||
rules.push( |
|||
`${baseArgs} -p udp --dport ${entry.port}${commentArg} -j ACCEPT` |
|||
); |
|||
} |
|||
} else { |
|||
// No port - allow all traffic to destination
|
|||
rules.push(`${baseArgs}${commentArg} -j ACCEPT`); |
|||
} |
|||
|
|||
return rules; |
|||
} |
|||
|
|||
export const firewall = { |
|||
/** |
|||
* Initialize the custom chain if it doesn't exist |
|||
*/ |
|||
async initChain(interfaceName: string): Promise<void> { |
|||
FW_DEBUG( |
|||
`Initializing firewall chain ${CHAIN_NAME} for interface ${interfaceName}` |
|||
); |
|||
|
|||
// Create chain if not exists (iptables returns error if exists, so we ignore)
|
|||
await exec(`iptables -N ${CHAIN_NAME} 2>/dev/null || true`); |
|||
await exec(`ip6tables -N ${CHAIN_NAME} 2>/dev/null || true`); |
|||
|
|||
// Ensure chain is referenced from FORWARD (if not already)
|
|||
// Insert at position 1 to process before generic ACCEPT rules
|
|||
await exec( |
|||
`iptables -C FORWARD -i ${interfaceName} -j ${CHAIN_NAME} 2>/dev/null || iptables -I FORWARD 1 -i ${interfaceName} -j ${CHAIN_NAME}` |
|||
); |
|||
await exec( |
|||
`ip6tables -C FORWARD -i ${interfaceName} -j ${CHAIN_NAME} 2>/dev/null || ip6tables -I FORWARD 1 -i ${interfaceName} -j ${CHAIN_NAME}` |
|||
); |
|||
}, |
|||
|
|||
/** |
|||
* Flush all rules in the custom chain |
|||
*/ |
|||
async flushChain(): Promise<void> { |
|||
FW_DEBUG(`Flushing firewall chain ${CHAIN_NAME}`); |
|||
await exec(`iptables -F ${CHAIN_NAME} 2>/dev/null || true`); |
|||
await exec(`ip6tables -F ${CHAIN_NAME} 2>/dev/null || true`); |
|||
}, |
|||
|
|||
/** |
|||
* Apply firewall rules for a single client |
|||
*/ |
|||
async applyClientRules( |
|||
client: FirewallClient, |
|||
defaultAllowedIps: string[], |
|||
enableIpv6: boolean |
|||
): Promise<void> { |
|||
// Determine which IPs to use for firewall rules
|
|||
// Priority: firewallIps > allowedIps > defaultAllowedIps
|
|||
const effectiveIps = |
|||
client.firewallIps && client.firewallIps.length > 0 |
|||
? client.firewallIps |
|||
: (client.allowedIps ?? defaultAllowedIps); |
|||
|
|||
FW_DEBUG( |
|||
`Applying firewall rules for client ${client.name} (${client.id}): ${effectiveIps.join(', ')}` |
|||
); |
|||
|
|||
const comment = sanitizeComment(client.id, client.name); |
|||
|
|||
for (const ipEntry of effectiveIps) { |
|||
const parsed = parseFirewallEntry(ipEntry); |
|||
const baseIp = parsed.ip.split('/')[0] ?? parsed.ip; // Handle CIDR by checking base IP
|
|||
const destIsIpv6 = isIPv6(baseIp); |
|||
|
|||
if (destIsIpv6) { |
|||
if (enableIpv6) { |
|||
const rules = generateRuleArgs(client.ipv6Address, parsed, comment); |
|||
for (const rule of rules) { |
|||
await exec(`ip6tables ${rule}`); |
|||
} |
|||
} |
|||
} else { |
|||
const rules = generateRuleArgs(client.ipv4Address, parsed, comment); |
|||
for (const rule of rules) { |
|||
await exec(`iptables ${rule}`); |
|||
} |
|||
} |
|||
} |
|||
}, |
|||
|
|||
/** |
|||
* Full rebuild of firewall rules from database state |
|||
*/ |
|||
async rebuildRules( |
|||
wgInterface: InterfaceType, |
|||
clients: FirewallClient[], |
|||
userConfig: UserConfigType, |
|||
enableIpv6: boolean |
|||
): Promise<void> { |
|||
if (!wgInterface.firewallEnabled) { |
|||
FW_DEBUG('Firewall filtering disabled, removing any existing rules'); |
|||
await this.removeFiltering(wgInterface.name); |
|||
return; |
|||
} |
|||
|
|||
// Handle concurrent rebuilds with queue
|
|||
if (rebuildInProgress) { |
|||
FW_DEBUG('Rebuild already in progress, queuing'); |
|||
rebuildQueued = true; |
|||
return; |
|||
} |
|||
|
|||
rebuildInProgress = true; |
|||
|
|||
try { |
|||
FW_DEBUG('Rebuilding firewall rules...'); |
|||
|
|||
// Initialize chain structure
|
|||
await this.initChain(wgInterface.name); |
|||
|
|||
// Flush existing rules
|
|||
await this.flushChain(); |
|||
|
|||
// Apply rules for each enabled client
|
|||
for (const client of clients) { |
|||
if (!client.enabled) continue; |
|||
await this.applyClientRules( |
|||
client, |
|||
userConfig.defaultAllowedIps, |
|||
enableIpv6 |
|||
); |
|||
} |
|||
|
|||
// Add final DROP for any traffic not explicitly allowed
|
|||
await exec(`iptables -A ${CHAIN_NAME} -j DROP`); |
|||
if (enableIpv6) { |
|||
await exec(`ip6tables -A ${CHAIN_NAME} -j DROP`); |
|||
} |
|||
|
|||
FW_DEBUG('Firewall rules rebuilt successfully'); |
|||
} finally { |
|||
rebuildInProgress = false; |
|||
|
|||
// If another rebuild was queued, run it now
|
|||
if (rebuildQueued) { |
|||
rebuildQueued = false; |
|||
FW_DEBUG('Processing queued rebuild'); |
|||
await this.rebuildRules(wgInterface, clients, userConfig, enableIpv6); |
|||
} |
|||
} |
|||
}, |
|||
|
|||
/** |
|||
* Remove all firewall filtering (when feature is disabled) |
|||
*/ |
|||
async removeFiltering(interfaceName: string): Promise<void> { |
|||
FW_DEBUG(`Removing firewall filtering for interface ${interfaceName}`); |
|||
|
|||
// Remove jump rules from FORWARD chain
|
|||
await exec( |
|||
`iptables -D FORWARD -i ${interfaceName} -j ${CHAIN_NAME} 2>/dev/null || true` |
|||
); |
|||
await exec( |
|||
`ip6tables -D FORWARD -i ${interfaceName} -j ${CHAIN_NAME} 2>/dev/null || true` |
|||
); |
|||
|
|||
// Flush and delete the chain
|
|||
await exec(`iptables -F ${CHAIN_NAME} 2>/dev/null || true`); |
|||
await exec(`ip6tables -F ${CHAIN_NAME} 2>/dev/null || true`); |
|||
await exec(`iptables -X ${CHAIN_NAME} 2>/dev/null || true`); |
|||
await exec(`ip6tables -X ${CHAIN_NAME} 2>/dev/null || true`); |
|||
}, |
|||
|
|||
/** |
|||
* Check if iptables (and optionally ip6tables) are available on the system. |
|||
* @param enableIpv6 - If true, also check for ip6tables. Defaults to true. |
|||
*/ |
|||
async isAvailable(enableIpv6: boolean = true): Promise<boolean> { |
|||
// Return cached result if we've already checked
|
|||
if (iptablesAvailable !== null) { |
|||
return iptablesAvailable; |
|||
} |
|||
|
|||
try { |
|||
// Check for iptables (always required)
|
|||
await exec('iptables --version'); |
|||
FW_DEBUG('iptables is available'); |
|||
|
|||
// Check for ip6tables (only if IPv6 is enabled)
|
|||
if (enableIpv6) { |
|||
await exec('ip6tables --version'); |
|||
FW_DEBUG('ip6tables is available'); |
|||
} else { |
|||
FW_DEBUG('IPv6 disabled, skipping ip6tables check'); |
|||
} |
|||
|
|||
iptablesAvailable = true; |
|||
return true; |
|||
} catch (error) { |
|||
iptablesAvailable = false; |
|||
FW_DEBUG('iptables/ip6tables is not available:', error); |
|||
return false; |
|||
} |
|||
}, |
|||
|
|||
/** |
|||
* Clear the availability cache to force a re-check |
|||
*/ |
|||
clearAvailabilityCache(): void { |
|||
iptablesAvailable = null; |
|||
}, |
|||
}; |
|||
|
|||
export const firewallTestExports = { |
|||
parseFirewallEntry, |
|||
generateRuleArgs, |
|||
sanitizeComment, |
|||
}; |
|||
@ -0,0 +1,294 @@ |
|||
import { describe, expect, test } from 'vitest'; |
|||
import { firewallTestExports } from '../../server/utils/firewall'; |
|||
import { typesTestExports } from '../../server/utils/types'; |
|||
|
|||
describe('firewall', () => { |
|||
describe('isValidFirewallEntry', () => { |
|||
test('invalid ips', () => { |
|||
expect(() => typesTestExports.FirewallIpEntrySchema.parse('')).toThrow(); |
|||
expect(() => |
|||
typesTestExports.FirewallIpEntrySchema.parse('255.255.255.256') |
|||
).toThrow(); |
|||
expect(() => |
|||
typesTestExports.FirewallIpEntrySchema.parse('1.1.1.256') |
|||
).toThrow(); |
|||
expect(() => |
|||
typesTestExports.FirewallIpEntrySchema.parse('1.1.1.1.1') |
|||
).toThrow(); |
|||
|
|||
expect(() => |
|||
typesTestExports.FirewallIpEntrySchema.parse('[]:443/udp') |
|||
).toThrow(); |
|||
expect(() => |
|||
typesTestExports.FirewallIpEntrySchema.parse('[]:443') |
|||
).toThrow(); |
|||
expect(() => |
|||
typesTestExports.FirewallIpEntrySchema.parse('[::1]/32') |
|||
).toThrow(); |
|||
expect(() => |
|||
typesTestExports.FirewallIpEntrySchema.parse('[1.1.1.1]/32') |
|||
).toThrow(); |
|||
expect(() => |
|||
typesTestExports.FirewallIpEntrySchema.parse('[::g]/32') |
|||
).toThrow(); |
|||
expect(() => |
|||
typesTestExports.FirewallIpEntrySchema.parse('2001:dbx::1') |
|||
).toThrow(); |
|||
}); |
|||
test('invalid port, protocol or cidr', () => { |
|||
expect(() => |
|||
typesTestExports.FirewallIpEntrySchema.parse('1.1.1.1:80/tcpp') |
|||
).toThrow(); |
|||
expect(() => |
|||
typesTestExports.FirewallIpEntrySchema.parse('1.1.1.1:65536') |
|||
).toThrow(); |
|||
expect(() => |
|||
typesTestExports.FirewallIpEntrySchema.parse('1.1.1.1:0') |
|||
).toThrow(); |
|||
expect(() => |
|||
typesTestExports.FirewallIpEntrySchema.parse('1.1.1.1/33') |
|||
).toThrow(); |
|||
expect(() => |
|||
typesTestExports.FirewallIpEntrySchema.parse('1.1.1.1/32:0') |
|||
).toThrow(); |
|||
}); |
|||
test('protocol without port', () => { |
|||
expect(() => |
|||
typesTestExports.FirewallIpEntrySchema.parse('1.1.1.1/tcp') |
|||
).toThrow(); |
|||
}); |
|||
test('valid entries', () => { |
|||
expect(typesTestExports.FirewallIpEntrySchema.parse('1.1.1.1')).toBe( |
|||
'1.1.1.1' |
|||
); |
|||
expect(typesTestExports.FirewallIpEntrySchema.parse('::/0')).toBe('::/0'); |
|||
expect(typesTestExports.FirewallIpEntrySchema.parse('::0/0')).toBe( |
|||
'::0/0' |
|||
); |
|||
expect(typesTestExports.FirewallIpEntrySchema.parse('2001:db8::1')).toBe( |
|||
'2001:db8::1' |
|||
); |
|||
expect(typesTestExports.FirewallIpEntrySchema.parse('::1')).toBe('::1'); |
|||
expect( |
|||
typesTestExports.FirewallIpEntrySchema.parse('2001:db8::1/32') |
|||
).toBe('2001:db8::1/32'); |
|||
expect(typesTestExports.FirewallIpEntrySchema.parse('[::1]')).toBe( |
|||
'[::1]' |
|||
); |
|||
expect(typesTestExports.FirewallIpEntrySchema.parse('[::1/32]')).toBe( |
|||
'[::1/32]' |
|||
); |
|||
}); |
|||
}); |
|||
describe('parseFirewallEntry', () => { |
|||
test('IPv4', () => { |
|||
expect(firewallTestExports.parseFirewallEntry('1.1.1.1')).toEqual({ |
|||
ip: '1.1.1.1', |
|||
}); |
|||
}); |
|||
test('IPv4 with Protocol', () => { |
|||
expect(() => |
|||
firewallTestExports.parseFirewallEntry('1.1.1.1/tcp') |
|||
).toThrow(); |
|||
expect(() => |
|||
firewallTestExports.parseFirewallEntry('1.1.1.1/udp') |
|||
).toThrow(); |
|||
}); |
|||
test('IPv4 with CIDR', () => { |
|||
expect(firewallTestExports.parseFirewallEntry('1.1.1.1/32')).toEqual({ |
|||
ip: '1.1.1.1/32', |
|||
}); |
|||
}); |
|||
test('IPv4 with CIDR and Protocol', () => { |
|||
expect(() => |
|||
firewallTestExports.parseFirewallEntry('1.1.1.1/32/tcp') |
|||
).toThrow(); |
|||
}); |
|||
test('IPv4 with Port', () => { |
|||
expect(firewallTestExports.parseFirewallEntry('1.1.1.1:80')).toEqual({ |
|||
ip: '1.1.1.1', |
|||
port: 80, |
|||
proto: 'both', |
|||
}); |
|||
}); |
|||
test('IPv4 with Port and Protocol', () => { |
|||
expect(firewallTestExports.parseFirewallEntry('1.1.1.1:80/tcp')).toEqual({ |
|||
ip: '1.1.1.1', |
|||
port: 80, |
|||
proto: 'tcp', |
|||
}); |
|||
expect(firewallTestExports.parseFirewallEntry('1.1.1.1:80/udp')).toEqual({ |
|||
ip: '1.1.1.1', |
|||
port: 80, |
|||
proto: 'udp', |
|||
}); |
|||
}); |
|||
test('IPv4 with CIDR and Port', () => { |
|||
expect( |
|||
firewallTestExports.parseFirewallEntry('10.10.0.0/24:443') |
|||
).toEqual({ |
|||
ip: '10.10.0.0/24', |
|||
port: 443, |
|||
proto: 'both', |
|||
}); |
|||
}); |
|||
test('IPv4 with CIDR, Port and Protocol', () => { |
|||
expect( |
|||
firewallTestExports.parseFirewallEntry('10.10.0.0/24:443/tcp') |
|||
).toEqual({ |
|||
ip: '10.10.0.0/24', |
|||
port: 443, |
|||
proto: 'tcp', |
|||
}); |
|||
expect( |
|||
firewallTestExports.parseFirewallEntry('10.10.0.0/24:443/udp') |
|||
).toEqual({ |
|||
ip: '10.10.0.0/24', |
|||
port: 443, |
|||
proto: 'udp', |
|||
}); |
|||
}); |
|||
test('IPv6', () => { |
|||
expect(firewallTestExports.parseFirewallEntry('[2001:db8::1]')).toEqual({ |
|||
ip: '2001:db8::1', |
|||
}); |
|||
expect(firewallTestExports.parseFirewallEntry('2001:db8::1')).toEqual({ |
|||
ip: '2001:db8::1', |
|||
}); |
|||
}); |
|||
test('IPv6 with Protocol', () => { |
|||
expect(() => |
|||
firewallTestExports.parseFirewallEntry('[2001:db8::1]/tcp') |
|||
).toThrow(); |
|||
expect(() => |
|||
firewallTestExports.parseFirewallEntry('2001:db8::1/udp') |
|||
).toThrow(); |
|||
}); |
|||
test('IPv6 with CIDR', () => { |
|||
expect(firewallTestExports.parseFirewallEntry('::0/0')).toEqual({ |
|||
ip: '::0/0', |
|||
}); |
|||
expect(firewallTestExports.parseFirewallEntry('[::0/0]')).toEqual({ |
|||
ip: '::0/0', |
|||
}); |
|||
}); |
|||
test('IPv6 with CIDR and Protocol', () => { |
|||
expect(() => |
|||
firewallTestExports.parseFirewallEntry('::0/0/tcp') |
|||
).toThrow(); |
|||
}); |
|||
test('IPv6 with Port', () => { |
|||
expect( |
|||
firewallTestExports.parseFirewallEntry('[2001:db8::1]:443') |
|||
).toEqual({ |
|||
ip: '2001:db8::1', |
|||
port: 443, |
|||
proto: 'both', |
|||
}); |
|||
}); |
|||
test('IPv6 with Port and Protocol', () => { |
|||
expect( |
|||
firewallTestExports.parseFirewallEntry('[2001:db8::1]:443/tcp') |
|||
).toEqual({ |
|||
ip: '2001:db8::1', |
|||
port: 443, |
|||
proto: 'tcp', |
|||
}); |
|||
expect( |
|||
firewallTestExports.parseFirewallEntry('[2001:db8::1]:443/udp') |
|||
).toEqual({ |
|||
ip: '2001:db8::1', |
|||
port: 443, |
|||
proto: 'udp', |
|||
}); |
|||
}); |
|||
test('IPv6 with CIDR and Port', () => { |
|||
expect( |
|||
firewallTestExports.parseFirewallEntry('[2001:db8::/32]:443') |
|||
).toEqual({ |
|||
ip: '2001:db8::/32', |
|||
port: 443, |
|||
proto: 'both', |
|||
}); |
|||
}); |
|||
test('IPv6 with CIDR, Port and Protocol', () => { |
|||
expect( |
|||
firewallTestExports.parseFirewallEntry('[2001:db8::/32]:443/tcp') |
|||
).toEqual({ |
|||
ip: '2001:db8::/32', |
|||
port: 443, |
|||
proto: 'tcp', |
|||
}); |
|||
}); |
|||
}); |
|||
describe('sanitizeComment', () => { |
|||
test('basic ASCII name', () => { |
|||
expect(firewallTestExports.sanitizeComment(1, 'My Laptop')).toBe( |
|||
'client 1: My Laptop' |
|||
); |
|||
}); |
|||
test('strips non-ASCII and shell metacharacters', () => { |
|||
expect(firewallTestExports.sanitizeComment(42, 'café')).toBe( |
|||
'client 42: caf' |
|||
); |
|||
expect(firewallTestExports.sanitizeComment(5, 'a"; rm -rf /')).toBe( |
|||
'client 5: a rm -rf ' |
|||
); |
|||
expect(firewallTestExports.sanitizeComment(7, 'test$(cmd)`id`')).toBe( |
|||
'client 7: testcmdid' |
|||
); |
|||
}); |
|||
test('preserves allowed punctuation', () => { |
|||
expect(firewallTestExports.sanitizeComment(3, 'phone-2.lan_home')).toBe( |
|||
'client 3: phone-2.lan_home' |
|||
); |
|||
}); |
|||
test('truncates to 256 bytes', () => { |
|||
const longName = 'a'.repeat(300); |
|||
const result = firewallTestExports.sanitizeComment(1, longName); |
|||
expect(result.length).toBeLessThanOrEqual(256); |
|||
expect(result).toBe('client 1: ' + 'a'.repeat(246)); |
|||
}); |
|||
}); |
|||
describe('generateRuleArgs', () => { |
|||
test('includes comment when provided', () => { |
|||
const rules = firewallTestExports.generateRuleArgs( |
|||
'10.8.0.2', |
|||
{ ip: '10.0.0.1' }, |
|||
'client 1: test' |
|||
); |
|||
expect(rules).toEqual([ |
|||
'-A WG_CLIENTS -s 10.8.0.2 -d 10.0.0.1 -m comment --comment "client 1: test" -j ACCEPT', |
|||
]); |
|||
}); |
|||
test('omits comment when not provided', () => { |
|||
const rulesTcp = firewallTestExports.generateRuleArgs('10.8.0.2', { |
|||
ip: '10.0.0.1', |
|||
port: 80, |
|||
proto: 'tcp', |
|||
}); |
|||
expect(rulesTcp).toEqual([ |
|||
'-A WG_CLIENTS -s 10.8.0.2 -d 10.0.0.1 -p tcp --dport 80 -j ACCEPT', |
|||
]); |
|||
const rulesUdp = firewallTestExports.generateRuleArgs('10.8.0.2', { |
|||
ip: '10.0.0.1', |
|||
port: 80, |
|||
proto: 'udp', |
|||
}); |
|||
expect(rulesUdp).toEqual([ |
|||
'-A WG_CLIENTS -s 10.8.0.2 -d 10.0.0.1 -p udp --dport 80 -j ACCEPT', |
|||
]); |
|||
}); |
|||
test('comment with port generates two rules for both proto', () => { |
|||
const rules = firewallTestExports.generateRuleArgs( |
|||
'10.8.0.2', |
|||
{ ip: '10.0.0.1', port: 443, proto: 'both' }, |
|||
'client 2: phone' |
|||
); |
|||
expect(rules).toEqual([ |
|||
'-A WG_CLIENTS -s 10.8.0.2 -d 10.0.0.1 -p tcp --dport 443 -m comment --comment "client 2: phone" -j ACCEPT', |
|||
'-A WG_CLIENTS -s 10.8.0.2 -d 10.0.0.1 -p udp --dport 443 -m comment --comment "client 2: phone" -j ACCEPT', |
|||
]); |
|||
}); |
|||
}); |
|||
}); |
|||
Loading…
Reference in new issue