Browse Source

fix formatting, fix types

pull/2418/head
Bernd Storath 5 months ago
parent
commit
839d88fa0a
  1. 9
      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

9
src/app/stores/global.ts

@ -1,7 +1,10 @@
export const useGlobalStore = defineStore('Global', () => {
const { data: information, refresh: refreshInformation } = useFetch('/api/information', {
method: 'get',
});
const { data: information, refresh: refreshInformation } = useFetch(
'/api/information',
{
method: 'get',
}
);
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
firewall.clearAvailabilityCache();
const iptablesAvailable = await firewall.isAvailable(!WG_ENV.DISABLE_IPV6);
const iptablesAvailable = await firewall.isAvailable(
!WG_ENV.DISABLE_IPV6
);
if (!iptablesAvailable) {
const requiredTools = WG_ENV.DISABLE_IPV6
? 'iptables'

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

@ -18,6 +18,13 @@ function createPreparedStatement(db: DBType) {
})
.where(eq(wgInterface.name, sql.placeholder('interface')))
.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();
}
setFirewallEnabled(firewallEnabled: boolean) {
return this.#statements.setFirewallEnabled.execute({
interface: 'wg0',
firewallEnabled,
});
}
updateCidr(data: InterfaceCidrUpdateType) {
return this.#db.transaction(async (tx) => {
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 iptablesAvailable = await firewall.isAvailable(enableIpv6);
if (!iptablesAvailable) {
const requiredTools = enableIpv6
? 'iptables/ip6tables'
: 'iptables';
const requiredTools = enableIpv6 ? 'iptables/ip6tables' : 'iptables';
console.warn(
`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
}
}

15
src/server/utils/firewall.ts

@ -1,7 +1,6 @@
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';
@ -21,6 +20,16 @@ type ParsedEntry = {
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.
* 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
*/
async applyClientRules(
client: ClientType,
client: FirewallClient,
defaultAllowedIps: string[],
enableIpv6: boolean
): Promise<void> {
@ -227,7 +236,7 @@ export const firewall = {
*/
async rebuildRules(
wgInterface: InterfaceType,
clients: ClientType[],
clients: FirewallClient[],
userConfig: UserConfigType,
enableIpv6: boolean
): Promise<void> {

Loading…
Cancel
Save