Browse Source

fix formatting, fix types

pull/2418/head
Bernd Storath 5 months ago
parent
commit
839d88fa0a
  1. 7
      src/app/stores/global.ts
  2. 4
      src/server/api/admin/interface/index.post.ts
  3. 14
      src/server/database/repositories/interface/service.ts
  4. 6
      src/server/utils/WireGuard.ts
  5. 15
      src/server/utils/firewall.ts

7
src/app/stores/global.ts

@ -1,7 +1,10 @@
export const useGlobalStore = defineStore('Global', () => { export const useGlobalStore = defineStore('Global', () => {
const { data: information, refresh: refreshInformation } = useFetch('/api/information', { const { data: information, refresh: refreshInformation } = useFetch(
'/api/information',
{
method: 'get', method: 'get',
}); }
);
const sortClient = ref(true); // Sort clients by name, true = asc, false = desc const sortClient = ref(true); // Sort clients by name, true = asc, false = desc

4
src/server/api/admin/interface/index.post.ts

@ -14,7 +14,9 @@ export default definePermissionEventHandler(
// Clear cache to force fresh check // Clear cache to force fresh check
firewall.clearAvailabilityCache(); firewall.clearAvailabilityCache();
const iptablesAvailable = await firewall.isAvailable(!WG_ENV.DISABLE_IPV6); const iptablesAvailable = await firewall.isAvailable(
!WG_ENV.DISABLE_IPV6
);
if (!iptablesAvailable) { if (!iptablesAvailable) {
const requiredTools = WG_ENV.DISABLE_IPV6 const requiredTools = WG_ENV.DISABLE_IPV6
? 'iptables' ? 'iptables'

14
src/server/database/repositories/interface/service.ts

@ -18,6 +18,13 @@ function createPreparedStatement(db: DBType) {
}) })
.where(eq(wgInterface.name, sql.placeholder('interface'))) .where(eq(wgInterface.name, sql.placeholder('interface')))
.prepare(), .prepare(),
setFirewallEnabled: db
.update(wgInterface)
.set({
firewallEnabled: sql.placeholder('firewallEnabled') as never as boolean,
})
.where(eq(wgInterface.name, sql.placeholder('interface')))
.prepare(),
}; };
} }
@ -56,6 +63,13 @@ export class InterfaceService {
.execute(); .execute();
} }
setFirewallEnabled(firewallEnabled: boolean) {
return this.#statements.setFirewallEnabled.execute({
interface: 'wg0',
firewallEnabled,
});
}
updateCidr(data: InterfaceCidrUpdateType) { updateCidr(data: InterfaceCidrUpdateType) {
return this.#db.transaction(async (tx) => { return this.#db.transaction(async (tx) => {
const oldCidr = await tx.query.wgInterface const oldCidr = await tx.query.wgInterface

6
src/server/utils/WireGuard.ts

@ -270,13 +270,11 @@ class WireGuard {
const enableIpv6 = !WG_ENV.DISABLE_IPV6; const enableIpv6 = !WG_ENV.DISABLE_IPV6;
const iptablesAvailable = await firewall.isAvailable(enableIpv6); const iptablesAvailable = await firewall.isAvailable(enableIpv6);
if (!iptablesAvailable) { if (!iptablesAvailable) {
const requiredTools = enableIpv6 const requiredTools = enableIpv6 ? 'iptables/ip6tables' : 'iptables';
? 'iptables/ip6tables'
: 'iptables';
console.warn( console.warn(
`WARNING: Per-Client Firewall is enabled but ${requiredTools} is not available. Disabling firewall feature. Please install ${requiredTools} to use this feature.` `WARNING: Per-Client Firewall is enabled but ${requiredTools} is not available. Disabling firewall feature. Please install ${requiredTools} to use this feature.`
); );
await Database.interfaces.update({ firewallEnabled: false }); await Database.interfaces.setFirewallEnabled(false);
wgInterface.firewallEnabled = false; // Update local copy wgInterface.firewallEnabled = false; // Update local copy
} }
} }

15
src/server/utils/firewall.ts

@ -1,7 +1,6 @@
import debug from 'debug'; import debug from 'debug';
import { isIPv6 } from 'is-ip'; import { isIPv6 } from 'is-ip';
import type { ClientType } from '#db/repositories/client/types';
import type { InterfaceType } from '#db/repositories/interface/types'; import type { InterfaceType } from '#db/repositories/interface/types';
import type { UserConfigType } from '#db/repositories/userConfig/types'; import type { UserConfigType } from '#db/repositories/userConfig/types';
@ -21,6 +20,16 @@ type ParsedEntry = {
proto?: 'tcp' | 'udp' | 'both'; proto?: 'tcp' | 'udp' | 'both';
}; };
type FirewallClient = {
id: number;
name: string;
ipv4Address: string;
ipv6Address: string;
firewallIps: string[] | null;
allowedIps: string[] | null;
enabled: boolean;
};
/** /**
* Sanitize a client identifier for use in an iptables comment. * Sanitize a client identifier for use in an iptables comment.
* Strips all characters except ASCII alphanumeric, space, underscore, hyphen, and dot. * Strips all characters except ASCII alphanumeric, space, underscore, hyphen, and dot.
@ -184,7 +193,7 @@ export const firewall = {
* Apply firewall rules for a single client * Apply firewall rules for a single client
*/ */
async applyClientRules( async applyClientRules(
client: ClientType, client: FirewallClient,
defaultAllowedIps: string[], defaultAllowedIps: string[],
enableIpv6: boolean enableIpv6: boolean
): Promise<void> { ): Promise<void> {
@ -227,7 +236,7 @@ export const firewall = {
*/ */
async rebuildRules( async rebuildRules(
wgInterface: InterfaceType, wgInterface: InterfaceType,
clients: ClientType[], clients: FirewallClient[],
userConfig: UserConfigType, userConfig: UserConfigType,
enableIpv6: boolean enableIpv6: boolean
): Promise<void> { ): Promise<void> {

Loading…
Cancel
Save