mirror of https://github.com/wg-easy/wg-easy
Browse Source
* refactor code * refactor code * add some todos * update pnpm, start migrating to database * add missing i18n key * add todo * basic setup stylingpull/1648/head
committed by
Bernd Storath
28 changed files with 332 additions and 5584 deletions
File diff suppressed because it is too large
@ -1,4 +1,5 @@ |
|||||
export default defineEventHandler(async (event) => { |
export default defineEventHandler(async (event) => { |
||||
setHeader(event, 'Content-Type', 'application/json'); |
setHeader(event, 'Content-Type', 'application/json'); |
||||
|
// TODO: enable by default
|
||||
return MAX_AGE > 0; |
return MAX_AGE > 0; |
||||
}); |
}); |
||||
|
@ -1,5 +1,11 @@ |
|||||
export default defineEventHandler((event) => { |
export default defineEventHandler(async (event) => { |
||||
setHeader(event, 'Content-Type', 'application/json'); |
setHeader(event, 'Content-Type', 'application/json'); |
||||
const sort = UI_ENABLE_SORT_CLIENTS; |
const system = await Database.getSystem(); |
||||
return sort === 'true' ? true : false; |
if (!system) |
||||
|
throw createError({ |
||||
|
statusCode: 500, |
||||
|
statusMessage: 'Invalid', |
||||
|
}); |
||||
|
|
||||
|
return system.sortClients.enabled; |
||||
}); |
}); |
||||
|
@ -0,0 +1,146 @@ |
|||||
|
import packageJson from '@/package.json'; |
||||
|
|
||||
|
import type { SessionConfig } from 'h3'; |
||||
|
import type { Lang } from './types'; |
||||
|
|
||||
|
export type IpTables = { |
||||
|
PreUp: string; |
||||
|
PostUp: string; |
||||
|
PreDown: string; |
||||
|
PostDown: string; |
||||
|
}; |
||||
|
|
||||
|
export type WGInterface = { |
||||
|
privateKey: string; |
||||
|
publicKey: string; |
||||
|
address: string; |
||||
|
}; |
||||
|
|
||||
|
export type WGConfig = { |
||||
|
mtu: number; |
||||
|
persistentKeepalive: number; |
||||
|
rangeAddress: string; |
||||
|
defaultDns: string[]; |
||||
|
allowedIps: string[]; |
||||
|
}; |
||||
|
|
||||
|
export enum ChartType { |
||||
|
None = 0, |
||||
|
Line = 1, |
||||
|
Area = 2, |
||||
|
Bar = 3, |
||||
|
} |
||||
|
|
||||
|
export type TrafficStats = { |
||||
|
enabled: boolean; |
||||
|
type: ChartType; |
||||
|
}; |
||||
|
|
||||
|
export type Prometheus = { |
||||
|
enabled: boolean; |
||||
|
password: string | null; |
||||
|
}; |
||||
|
|
||||
|
export type Feature = { |
||||
|
enabled: boolean; |
||||
|
}; |
||||
|
|
||||
|
/** |
||||
|
* Representing the WireGuard network configuration data structure of a computer interface system. |
||||
|
*/ |
||||
|
export type System = { |
||||
|
interface: WGInterface; |
||||
|
|
||||
|
release: string; |
||||
|
// maxAge
|
||||
|
sessionTimeout: number; |
||||
|
lang: Lang; |
||||
|
|
||||
|
userConfig: WGConfig; |
||||
|
|
||||
|
wgPath: string; |
||||
|
wgDevice: string; |
||||
|
wgHost: string; |
||||
|
wgPort: number; |
||||
|
wgConfigPort: number; |
||||
|
|
||||
|
iptables: IpTables; |
||||
|
trafficStats: TrafficStats; |
||||
|
|
||||
|
clientExpiration: Feature; |
||||
|
oneTimeLinks: Feature; |
||||
|
sortClients: Feature; |
||||
|
|
||||
|
prometheus: Prometheus; |
||||
|
sessionConfig: SessionConfig; |
||||
|
}; |
||||
|
|
||||
|
/** |
||||
|
* Interface for system-related database operations. |
||||
|
* This interface provides methods for retrieving system configuration data |
||||
|
* and specific system properties, such as the language setting, from the database. |
||||
|
*/ |
||||
|
export interface SystemRepository { |
||||
|
/** |
||||
|
* Retrieves the system configuration data from the database. |
||||
|
*/ |
||||
|
getSystem(): Promise<System | null>; |
||||
|
|
||||
|
/** |
||||
|
* Retrieves the system's language setting. |
||||
|
*/ |
||||
|
getLang(): Promise<Lang>; |
||||
|
} |
||||
|
|
||||
|
// TODO: move to migration
|
||||
|
export const DEFAULT_SYSTEM: System = { |
||||
|
release: packageJson.release.version, |
||||
|
interface: { |
||||
|
privateKey: '', |
||||
|
publicKey: '', |
||||
|
address: '10.8.0.1', |
||||
|
}, |
||||
|
sessionTimeout: 3600, // 1 hour
|
||||
|
lang: 'en', |
||||
|
userConfig: { |
||||
|
mtu: 1420, |
||||
|
persistentKeepalive: 0, |
||||
|
// TODO: assume handle CIDR to compute next ip in WireGuard
|
||||
|
rangeAddress: '10.8.0.0/24', |
||||
|
defaultDns: ['1.1.1.1'], |
||||
|
allowedIps: ['0.0.0.0/0', '::/0'], |
||||
|
}, |
||||
|
wgPath: WG_PATH, |
||||
|
wgDevice: 'wg0', |
||||
|
wgHost: WG_HOST || '', |
||||
|
wgPort: 51820, |
||||
|
wgConfigPort: 51820, |
||||
|
iptables: { |
||||
|
PreUp: '', |
||||
|
PostUp: '', |
||||
|
PreDown: '', |
||||
|
PostDown: '', |
||||
|
}, |
||||
|
trafficStats: { |
||||
|
enabled: false, |
||||
|
type: ChartType.None, |
||||
|
}, |
||||
|
clientExpiration: { |
||||
|
enabled: false, |
||||
|
}, |
||||
|
oneTimeLinks: { |
||||
|
enabled: false, |
||||
|
}, |
||||
|
sortClients: { |
||||
|
enabled: false, |
||||
|
}, |
||||
|
prometheus: { |
||||
|
enabled: false, |
||||
|
password: null, |
||||
|
}, |
||||
|
sessionConfig: { |
||||
|
password: getRandomHex(256), |
||||
|
name: 'wg-easy', |
||||
|
cookie: undefined, |
||||
|
}, |
||||
|
}; |
@ -1,55 +0,0 @@ |
|||||
import packageJson from '@/package.json'; |
|
||||
|
|
||||
import { ChartType, Lang } from '../types'; |
|
||||
|
|
||||
import type { System } from './model'; |
|
||||
|
|
||||
const DEFAULT_SYSTEM_MODEL: System = { |
|
||||
release: packageJson.release.version, |
|
||||
interface: { |
|
||||
privateKey: '', |
|
||||
publicKey: '', |
|
||||
address: '10.8.0.1', |
|
||||
}, |
|
||||
port: PORT ? Number(PORT) : 51821, |
|
||||
webuiHost: '0.0.0.0', |
|
||||
sessionTimeout: 3600, // 1 hour
|
|
||||
lang: Lang.EN, |
|
||||
userConfig: { |
|
||||
mtu: 1420, |
|
||||
persistentKeepalive: 0, |
|
||||
// TODO: assume handle CIDR to compute next ip in WireGuard
|
|
||||
rangeAddress: '10.8.0.0/24', |
|
||||
defaultDns: ['1.1.1.1'], |
|
||||
allowedIps: ['0.0.0.0/0', '::/0'], |
|
||||
}, |
|
||||
wgPath: WG_PATH, |
|
||||
wgDevice: 'wg0', |
|
||||
wgHost: WG_HOST || '', |
|
||||
wgPort: 51820, |
|
||||
wgConfigPort: 51820, |
|
||||
iptables: { |
|
||||
wgPreUp: '', |
|
||||
wgPostUp: '', |
|
||||
wgPreDown: '', |
|
||||
wgPostDown: '', |
|
||||
}, |
|
||||
trafficStats: { |
|
||||
enabled: false, |
|
||||
type: ChartType.None, |
|
||||
}, |
|
||||
wgEnableExpiresTime: false, |
|
||||
wgEnableOneTimeLinks: false, |
|
||||
wgEnableSortClients: false, |
|
||||
prometheus: { |
|
||||
enabled: false, |
|
||||
password: null, |
|
||||
}, |
|
||||
sessionConfig: { |
|
||||
password: getRandomHex(256), |
|
||||
name: 'wg-easy', |
|
||||
cookie: undefined, |
|
||||
}, |
|
||||
}; |
|
||||
|
|
||||
export default DEFAULT_SYSTEM_MODEL; |
|
@ -1,45 +0,0 @@ |
|||||
import type { SessionConfig } from 'h3'; |
|
||||
import type { |
|
||||
Url, |
|
||||
IpTables, |
|
||||
Lang, |
|
||||
Port, |
|
||||
Prometheus, |
|
||||
SessionTimeOut, |
|
||||
TrafficStats, |
|
||||
Version, |
|
||||
WGConfig, |
|
||||
WGInterface, |
|
||||
} from '../types'; |
|
||||
|
|
||||
/** |
|
||||
* Representing the WireGuard network configuration data structure of a computer interface system. |
|
||||
*/ |
|
||||
export type System = { |
|
||||
interface: WGInterface; |
|
||||
|
|
||||
release: Version; |
|
||||
port: number; |
|
||||
webuiHost: string; |
|
||||
// maxAge
|
|
||||
sessionTimeout: SessionTimeOut; |
|
||||
lang: Lang; |
|
||||
|
|
||||
userConfig: WGConfig; |
|
||||
|
|
||||
wgPath: string; |
|
||||
wgDevice: string; |
|
||||
wgHost: Url; |
|
||||
wgPort: Port; |
|
||||
wgConfigPort: Port; |
|
||||
|
|
||||
iptables: IpTables; |
|
||||
trafficStats: TrafficStats; |
|
||||
|
|
||||
wgEnableExpiresTime: boolean; |
|
||||
wgEnableOneTimeLinks: boolean; |
|
||||
wgEnableSortClients: boolean; |
|
||||
|
|
||||
prometheus: Prometheus; |
|
||||
sessionConfig: SessionConfig; |
|
||||
}; |
|
@ -1,22 +0,0 @@ |
|||||
import type { Lang } from '../types'; |
|
||||
import type { System } from './model'; |
|
||||
|
|
||||
/** |
|
||||
* Interface for system-related database operations. |
|
||||
* This interface provides methods for retrieving system configuration data |
|
||||
* and specific system properties, such as the language setting, from the database. |
|
||||
*/ |
|
||||
export default interface SystemRepository { |
|
||||
/** |
|
||||
* Retrieves the system configuration data from the database. |
|
||||
* @returns {Promise<System | null>} A promise that resolves to the system data |
|
||||
* if found, or `undefined` if the system data is not available. |
|
||||
*/ |
|
||||
getSystem(): Promise<System | null>; |
|
||||
|
|
||||
/** |
|
||||
* Retrieves the system's language setting. |
|
||||
* @returns {Promise<Lang>} The current language setting of the system. |
|
||||
*/ |
|
||||
getLang(): Promise<Lang>; |
|
||||
} |
|
@ -1,61 +1 @@ |
|||||
import type * as crypto from 'node:crypto'; |
export type Lang = 'en' | 'fr'; |
||||
|
|
||||
export enum Lang { |
|
||||
/* english */ |
|
||||
EN = 'en', |
|
||||
/* french */ |
|
||||
FR = 'fr', |
|
||||
} |
|
||||
|
|
||||
export type Ipv4 = `${number}.${number}.${number}.${number}`; |
|
||||
export type Ipv4CIDR = `${number}.${number}.${number}.${number}/${number}`; |
|
||||
export type Ipv6 = |
|
||||
`${string}:${string}:${string}:${string}:${string}:${string}:${string}:${string}`; |
|
||||
export type Ipv6CIDR = |
|
||||
`${string}:${string}:${string}:${string}:${string}:${string}:${string}:${string}/${number}`; |
|
||||
|
|
||||
export type Address = Ipv4 | Ipv4CIDR | Ipv6 | Ipv6CIDR | '::/0'; |
|
||||
|
|
||||
export type UrlHttp = `http://${string}`; |
|
||||
export type UrlHttps = `https://${string}`; |
|
||||
export type Url = string | UrlHttp | UrlHttps | Address; |
|
||||
|
|
||||
export type ID = crypto.UUID; |
|
||||
export type Version = string; |
|
||||
export type SessionTimeOut = number; |
|
||||
export type Port = number; |
|
||||
export type HashPassword = string; |
|
||||
export type Command = string; |
|
||||
export type Key = string; |
|
||||
export type IpTables = { |
|
||||
wgPreUp: Command; |
|
||||
wgPostUp: Command; |
|
||||
wgPreDown: Command; |
|
||||
wgPostDown: Command; |
|
||||
}; |
|
||||
export type WGInterface = { |
|
||||
privateKey: Key; |
|
||||
publicKey: Key; |
|
||||
address: Address; |
|
||||
}; |
|
||||
export type WGConfig = { |
|
||||
mtu: number; |
|
||||
persistentKeepalive: number; |
|
||||
rangeAddress: Address; |
|
||||
defaultDns: Array<Address>; |
|
||||
allowedIps: Array<Address>; |
|
||||
}; |
|
||||
export enum ChartType { |
|
||||
None = 0, |
|
||||
Line = 1, |
|
||||
Area = 2, |
|
||||
Bar = 3, |
|
||||
} |
|
||||
export type TrafficStats = { |
|
||||
enabled: boolean; |
|
||||
type: ChartType; |
|
||||
}; |
|
||||
export type Prometheus = { |
|
||||
enabled: boolean; |
|
||||
password: HashPassword | null; |
|
||||
}; |
|
||||
|
@ -0,0 +1,55 @@ |
|||||
|
/** |
||||
|
* Represents user roles within the application, each with specific permissions : |
||||
|
* |
||||
|
* - `ADMIN`: Full permissions to all resources, including the app, database, etc |
||||
|
* - `EDITOR`: Granted write and read permissions on their own resources as well as |
||||
|
* `CLIENT` resources, but without `ADMIN` privileges |
||||
|
* - `CLIENT`: Granted write and read permissions only on their own resources. |
||||
|
*/ |
||||
|
export type ROLE = 'ADMIN' | 'EDITOR' | 'CLIENT'; |
||||
|
|
||||
|
/** |
||||
|
* Representing a user data structure. |
||||
|
*/ |
||||
|
export type User = { |
||||
|
id: string; |
||||
|
role: ROLE; |
||||
|
username: string; |
||||
|
password: string; |
||||
|
name?: string; |
||||
|
address?: string; |
||||
|
privateKey?: string; |
||||
|
publicKey?: string; |
||||
|
preSharedKey?: string; |
||||
|
createdAt: Date; |
||||
|
updatedAt: Date; |
||||
|
enabled: boolean; |
||||
|
}; |
||||
|
|
||||
|
/** |
||||
|
* Interface for user-related database operations. |
||||
|
* This interface provides methods for managing user data. |
||||
|
*/ |
||||
|
export interface UserRepository { |
||||
|
/** |
||||
|
* Retrieves all users from the database. |
||||
|
*/ |
||||
|
getUsers(): Promise<User[]>; |
||||
|
|
||||
|
/** |
||||
|
* Retrieves a user by their ID or User object from the database. |
||||
|
*/ |
||||
|
getUser(id: string): Promise<User | undefined>; |
||||
|
|
||||
|
newUserWithPassword(username: string, password: string): Promise<void>; |
||||
|
|
||||
|
/** |
||||
|
* Updates a user in the database. |
||||
|
*/ |
||||
|
updateUser(user: User): Promise<void>; |
||||
|
|
||||
|
/** |
||||
|
* Deletes a user from the database. |
||||
|
*/ |
||||
|
deleteUser(id: string): Promise<void>; |
||||
|
} |
@ -1,29 +0,0 @@ |
|||||
import type { Address, ID, Key, HashPassword } from '../types'; |
|
||||
|
|
||||
/** |
|
||||
* Represents user roles within the application, each with specific permissions : |
|
||||
* |
|
||||
* - `ADMIN`: Full permissions to all resources, including the app, database, etc |
|
||||
* - `EDITOR`: Granted write and read permissions on their own resources as well as |
|
||||
* `CLIENT` resources, but without `ADMIN` privileges |
|
||||
* - `CLIENT`: Granted write and read permissions only on their own resources. |
|
||||
*/ |
|
||||
export type ROLE = 'ADMIN' | 'EDITOR' | 'CLIENT'; |
|
||||
|
|
||||
/** |
|
||||
* Representing a user data structure. |
|
||||
*/ |
|
||||
export type User = { |
|
||||
id: ID; |
|
||||
role: ROLE; |
|
||||
username: string; |
|
||||
password: HashPassword; |
|
||||
name?: string; |
|
||||
address?: Address; |
|
||||
privateKey?: Key; |
|
||||
publicKey?: Key; |
|
||||
preSharedKey?: string; |
|
||||
createdAt: Date; |
|
||||
updatedAt: Date; |
|
||||
enabled: boolean; |
|
||||
}; |
|
@ -1,39 +0,0 @@ |
|||||
import type { ID } from '../types'; |
|
||||
import type { User } from './model'; |
|
||||
|
|
||||
/** |
|
||||
* Interface for user-related database operations. |
|
||||
* This interface provides methods for managing user data. |
|
||||
*/ |
|
||||
export default interface UserRepository { |
|
||||
/** |
|
||||
* Retrieves all users from the database. |
|
||||
* @returns {Promise<Array<User>>} A array of users data. |
|
||||
*/ |
|
||||
getUsers(): Promise<Array<User>>; |
|
||||
|
|
||||
/** |
|
||||
* Retrieves a user by their ID or User object from the database. |
|
||||
* @param {ID} id - The ID of the user or a User object. |
|
||||
* @returns {Promise<User | undefined>} A promise that resolves to the user data |
|
||||
* if found, or `undefined` if the user is not available. |
|
||||
*/ |
|
||||
getUser(id: ID): Promise<User | undefined>; |
|
||||
|
|
||||
newUserWithPassword(username: string, password: string): Promise<void>; |
|
||||
|
|
||||
/** |
|
||||
* Updates a user in the database. |
|
||||
* @param {User} user - The user to be saved. |
|
||||
* |
|
||||
* @returns {Promise<void>} A promise that resolves when the operation is complete. |
|
||||
*/ |
|
||||
updateUser(user: User): Promise<void>; |
|
||||
|
|
||||
/** |
|
||||
* Deletes a user from the database. |
|
||||
* @param {ID} id - The ID of the user or a User object to be deleted. |
|
||||
* @returns {Promise<void>} A promise that resolves when the user has been deleted. |
|
||||
*/ |
|
||||
deleteUser(id: ID): Promise<void>; |
|
||||
} |
|
Loading…
Reference in new issue