-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add a WireGuard client to the project. - New utility functions have been added to parse the output of `wg show` command and improve error handling. - Add start, down, show, show_config, generate_private_key, and get_public_key method to interact with the wg binary. - Created a new example file to offer more clear usage. Co-authored-by: mariobassem12 <[email protected]>
- Loading branch information
1 parent
d803a49
commit 7bd997e
Showing
5 changed files
with
221 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env -S v -n -w -gc none -no-retry-compilation -d use_openssl -enable-globals run | ||
|
||
import freeflowuniverse.herolib.clients.wireguard | ||
import time | ||
|
||
println('HIIII') | ||
|
||
// Create Wireguard client | ||
mut wg := wireguard.get()! | ||
println('Hello') | ||
config_file_path := '~/wg1.conf' | ||
|
||
println('Before start') | ||
wg.start(config_file_path: config_file_path)! | ||
println('${config_file_path} is started') | ||
|
||
time.sleep(time.second * 2) | ||
|
||
info := wg.show()! | ||
println('info: ${info}') | ||
|
||
config := wg.show_config(interface_name: 'wg1')! | ||
println('config: ${config}') | ||
|
||
private_key := wg.generate_private_key()! | ||
println('private_key: ${private_key}') | ||
|
||
public_key := wg.get_public_key(private_key: private_key)! | ||
println('public_key: ${public_key}') | ||
|
||
time.sleep(time.second * 2) | ||
|
||
wg.down(config_file_path: config_file_path)! | ||
println('${config_file_path} is down') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,115 @@ | ||
module wireguard | ||
|
||
fn (wg WireGuard) show() { | ||
import os | ||
|
||
pub struct WGPeer { | ||
pub mut: | ||
endpoint string | ||
allowed_ips string | ||
latest_handshake string | ||
transfer string | ||
persistent_keepalive string | ||
} | ||
|
||
pub struct WGInterface { | ||
pub mut: | ||
name string | ||
public_key string | ||
listening_port int | ||
} | ||
|
||
pub struct WGInfo { | ||
pub mut: | ||
interface_ WGInterface | ||
peers map[string]WGPeer | ||
} | ||
|
||
pub struct WGShow { | ||
pub mut: | ||
configs map[string]WGInfo | ||
} | ||
|
||
pub fn (wg WireGuard) show() !WGShow { | ||
cmd := 'wg show' | ||
res := os.execute(cmd) | ||
if res.exit_code != 0 { | ||
return error('failed to execute show command due to: ${res.output}') | ||
} | ||
|
||
return wg.parse_show_command_output(res.output) | ||
} | ||
|
||
@[params] | ||
pub struct ShowConfigArgs { | ||
pub: | ||
interface_name string @[required] | ||
} | ||
|
||
pub fn (wg WireGuard) show_config(args ShowConfigArgs) !WGInfo { | ||
configs := wg.show()!.configs | ||
config := configs[args.interface_name] or { | ||
return error('key ${args.interface_name} does not exists.') | ||
} | ||
return config | ||
} | ||
|
||
fn (wg WireGuard) up() { | ||
@[params] | ||
pub struct StartArgs { | ||
pub: | ||
config_file_path string @[required] | ||
} | ||
|
||
pub fn (wg WireGuard) start(args StartArgs) ! { | ||
if os.exists(args.config_file_path) { | ||
return error('File ${args.config_file_path} does not exists.') | ||
} | ||
|
||
cmd := 'sudo wg-quick up ${args.config_file_path}' | ||
println('cmd: ${cmd}') | ||
res := os.execute(cmd) | ||
if res.exit_code != 0 { | ||
return error('failed to execute start command due to: ${res.output}') | ||
} | ||
} | ||
|
||
@[params] | ||
pub struct DownArgs { | ||
pub: | ||
config_file_path string @[required] | ||
} | ||
|
||
pub fn (wg WireGuard) down(args DownArgs) ! { | ||
if os.exists(args.config_file_path) { | ||
return error('File ${args.config_file_path} does not exists.') | ||
} | ||
|
||
cmd := 'sudo wg-quick down ${args.config_file_path}' | ||
res := os.execute(cmd) | ||
if res.exit_code != 0 { | ||
return error('failed to execute down command due to: ${res.output}') | ||
} | ||
} | ||
|
||
fn (wg WireGuard) down() { | ||
pub fn (wg WireGuard) generate_private_key() !string { | ||
cmd := 'wg genkey' | ||
res := os.execute(cmd) | ||
if res.exit_code != 0 { | ||
return error('failed to execute genkey command due to: ${res.output}') | ||
} | ||
return res.output.trim_space() | ||
} | ||
|
||
fn (wg WireGuard) generate_key() !string { | ||
@[params] | ||
pub struct GetPublicKeyArgs { | ||
pub: | ||
private_key string @[required] | ||
} | ||
|
||
fn (wg WireGuard) get_public_key() { | ||
pub fn (wg WireGuard) get_public_key(args GetPublicKeyArgs) !string { | ||
cmd := 'echo ${args.private_key} | wg pubkey' | ||
res := os.execute(cmd) | ||
if res.exit_code != 0 { | ||
return error('failed to execute pubkey command due to: ${res.output}') | ||
} | ||
return res.output.trim_space() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
module wireguard | ||
|
||
fn (wg WireGuard) parse_show_command_output(res string) !WGShow { | ||
mut configs := map[string]WGInfo{} | ||
mut lines := res.split('\n') | ||
mut current_interface := '' | ||
mut current_peers := map[string]WGPeer{} | ||
mut iface := WGInterface{} | ||
mut peer_key := '' | ||
|
||
for line in lines { | ||
mut parts := line.trim_space().split(': ') | ||
if parts.len < 2 { | ||
continue | ||
} | ||
|
||
key := parts[0] | ||
value := parts[1] | ||
|
||
if key.starts_with('interface') { | ||
if current_interface != '' { | ||
configs[current_interface] = WGInfo{ | ||
interface_: iface | ||
peers: current_peers.clone() | ||
} | ||
current_peers.clear() | ||
} | ||
|
||
current_interface = value | ||
iface = WGInterface{ | ||
name: current_interface | ||
public_key: '' | ||
listening_port: 0 | ||
} | ||
} else if key == 'public key' { | ||
iface.public_key = value | ||
} else if key == 'listening port' { | ||
iface.listening_port = value.int() | ||
} else if key.starts_with('peer') { | ||
peer_key = value | ||
mut peer := WGPeer{ | ||
endpoint: '' | ||
allowed_ips: '' | ||
latest_handshake: '' | ||
transfer: '' | ||
persistent_keepalive: '' | ||
} | ||
current_peers[peer_key] = peer | ||
} else if key == 'endpoint' { | ||
current_peers[peer_key].endpoint = value | ||
} else if key == 'allowed ips' { | ||
current_peers[peer_key].allowed_ips = value | ||
} else if key == 'latest handshake' { | ||
current_peers[peer_key].latest_handshake = value | ||
} else if key == 'transfer' { | ||
current_peers[peer_key].transfer = value | ||
} else if key == 'persistent keepalive' { | ||
current_peers[peer_key].persistent_keepalive = value | ||
} | ||
} | ||
|
||
if current_interface != '' { | ||
configs[current_interface] = WGInfo{ | ||
interface_: iface | ||
peers: current_peers.clone() | ||
} | ||
} | ||
|
||
return WGShow{ | ||
configs: configs | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters