feat: Add WireGuard client support

- Add a new WireGuard client to the project.
- Includes a factory, model, and basic client functionality.

Co-authored-by: mariobassem12 <mariobassem12@gmail.com>
This commit is contained in:
Mahmoud Emad
2025-02-02 12:52:42 +02:00
parent 5a8ad0a47b
commit d803a49a85
5 changed files with 193 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
!!hero_code.generate_client
name:'wireguard'
classname:'WireGuard'
singleton:0
default:1
hasconfig:1
reset:0

View File

@@ -0,0 +1,16 @@
module wireguard
fn (wg WireGuard) show() {
}
fn (wg WireGuard) up() {
}
fn (wg WireGuard) down() {
}
fn (wg WireGuard) generate_key() !string {
}
fn (wg WireGuard) get_public_key() {
}

View File

@@ -0,0 +1,30 @@
# wireguard
To get started
```vlang
import freeflowuniverse.herolib.clients. wireguard
mut client:= wireguard.get()!
client...
```
## example heroscript
```hero
!!wireguard.configure
secret: '...'
host: 'localhost'
port: 8888
```

View File

@@ -0,0 +1,101 @@
module wireguard
import freeflowuniverse.herolib.core.base
import freeflowuniverse.herolib.core.playbook
__global (
wireguard_global map[string]&WireGuard
wireguard_default string
)
/////////FACTORY
@[params]
pub struct ArgsGet {
pub mut:
name string
}
fn args_get(args_ ArgsGet) ArgsGet {
mut args := args_
if args.name == '' {
args.name = wireguard_default
}
if args.name == '' {
args.name = 'default'
}
return args
}
pub fn get(args_ ArgsGet) !&WireGuard {
mut args := args_get(args_)
if args.name !in wireguard_global {
if args.name == 'default' {
if !config_exists(args) {
if default {
config_save(args)!
}
}
config_load(args)!
}
}
return wireguard_global[args.name] or {
println(wireguard_global)
panic('could not get config for wireguard with name:${args.name}')
}
}
fn config_exists(args_ ArgsGet) bool {
mut args := args_get(args_)
mut context := base.context() or { panic('bug') }
return context.hero_config_exists('wireguard', args.name)
}
fn config_load(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
mut heroscript := context.hero_config_get('wireguard', args.name)!
play(heroscript: heroscript)!
}
fn config_save(args_ ArgsGet) ! {
mut args := args_get(args_)
mut context := base.context()!
context.hero_config_set('wireguard', args.name, heroscript_default()!)!
}
fn set(o WireGuard) ! {
mut o2 := obj_init(o)!
wireguard_global[o.name] = &o2
wireguard_default = o.name
}
@[params]
pub struct PlayArgs {
pub mut:
heroscript string // if filled in then plbook will be made out of it
plbook ?playbook.PlayBook
reset bool
}
pub fn play(args_ PlayArgs) ! {
mut args := args_
if args.heroscript == '' {
args.heroscript = heroscript_default()!
}
mut plbook := args.plbook or { playbook.new(text: args.heroscript)! }
mut install_actions := plbook.find(filter: 'wireguard.configure')!
if install_actions.len > 0 {
for install_action in install_actions {
mut p := install_action.params
cfg_play(p)!
}
}
}
// switch instance to be used for wireguard
pub fn switch(name string) {
wireguard_default = name
}

View File

@@ -0,0 +1,38 @@
module wireguard
import freeflowuniverse.herolib.data.paramsparser
pub const version = '1.14.3'
const singleton = false
const default = true
// TODO: THIS IS EXAMPLE CODE AND NEEDS TO BE CHANGED IN LINE TO STRUCT BELOW, IS STRUCTURED AS HEROSCRIPT
pub fn heroscript_default() !string {
heroscript := "
!!wireguard.configure
name:'wireguard'
"
return heroscript
}
// THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED
@[heap]
pub struct WireGuard {
pub mut:
name string = 'default'
}
fn cfg_play(p paramsparser.Params) ! {
// THIS IS EXAMPLE CODE AND NEEDS TO BE CHANGED IN LINE WITH struct above
mut mycfg := WireGuard{
name: p.get_default('name', 'default')!
}
set(mycfg)!
}
fn obj_init(obj_ WireGuard) !WireGuard {
// never call get here, only thing we can do here is work on object itself
mut obj := obj_
return obj
}