diff --git a/Cargo.toml b/Cargo.toml index 233bc72..61e326e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "netbird-embed" -version = "0.1.1" +version = "0.1.2" edition = "2021" description = "Rust bindings for NetBird's client/embed via Go C-shared FFI" license = "BSD-3-Clause" diff --git a/build.rs b/build.rs index df3d9a5..443ca76 100644 --- a/build.rs +++ b/build.rs @@ -57,7 +57,8 @@ fn main() { // Rerun if Go sources change println!("cargo:rerun-if-changed=go-lib/netbird_embed.go"); - println!("cargo:rerun-if-changed=go-lib/socketpair.go"); + println!("cargo:rerun-if-changed=go-lib/socketpair_unix.go"); + println!("cargo:rerun-if-changed=go-lib/socketpair_windows.go"); println!("cargo:rerun-if-changed=go-lib/go.mod"); println!("cargo:rerun-if-changed=go-lib/go.sum"); } @@ -115,6 +116,7 @@ unsafe extern "C" { management_url: *const c_char, device_name: *const c_char, token: *const c_char, + wireguard_port: c_int, ) -> c_int; pub fn nb_create_errmsg(buf: *mut c_char, buf_len: c_int); diff --git a/go-lib/netbird_embed.go b/go-lib/netbird_embed.go index 0b134b8..764f586 100644 --- a/go-lib/netbird_embed.go +++ b/go-lib/netbird_embed.go @@ -62,7 +62,7 @@ func writeJSON(data []byte, buf *C.char, bufLen C.int) C.int { // On failure, use nb_create_errmsg to retrieve the error. // //export nb_new -func nb_new(setup_key *C.char, management_url *C.char, device_name *C.char, token *C.char) C.int { +func nb_new(setup_key *C.char, management_url *C.char, device_name *C.char, token *C.char, wireguard_port C.int) C.int { opts := embed.Options{} if setup_key != nil { @@ -86,6 +86,11 @@ func nb_new(setup_key *C.char, management_url *C.char, device_name *C.char, toke } } + if wireguard_port >= 0 { + port := int(wireguard_port) + opts.WireguardPort = &port + } + client, err := embed.New(opts) if err != nil { handleMu.Lock() diff --git a/src/lib.rs b/src/lib.rs index f7621d5..ed3cfbd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,6 +23,9 @@ pub struct ClientOptions { pub device_name: Option, /// JWT token for OIDC-based registration. pub token: Option, + /// WireGuard listen port. Use `Some(0)` for a random port (avoids conflicts + /// with other WireGuard instances). `None` uses the NetBird default (51820). + pub wireguard_port: Option, } /// A NetBird embedded client. @@ -48,12 +51,15 @@ impl Client { let device_name = make_cstring(opts.device_name.as_deref())?; let token = make_cstring(opts.token.as_deref())?; + let wg_port = opts.wireguard_port.unwrap_or(-1); + let handle = unsafe { ffi::nb_new( cstr_ptr(&setup_key), cstr_ptr(&management_url), cstr_ptr(&device_name), cstr_ptr(&token), + wg_port as c_int, ) };