ext.rs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (c)2021 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. use std::ffi::{CStr, CString};
  13. use std::os::raw::c_char;
  14. use url::Url;
  15. use crate::ZeroIDC;
  16. #[cfg(any(
  17. all(target_os = "linux", target_arch = "x86"),
  18. all(target_os = "linux", target_arch = "x86_64"),
  19. all(target_os = "linux", target_arch = "aarch64"),
  20. target_os = "windows",
  21. target_os = "macos",
  22. ))]
  23. #[no_mangle]
  24. pub extern "C" fn zeroidc_new(
  25. issuer: *const c_char,
  26. client_id: *const c_char,
  27. auth_endpoint: *const c_char,
  28. provider: *const c_char,
  29. web_listen_port: u16,
  30. ) -> *mut ZeroIDC {
  31. if issuer.is_null() {
  32. println!("issuer is null");
  33. return std::ptr::null_mut();
  34. }
  35. if client_id.is_null() {
  36. println!("client_id is null");
  37. return std::ptr::null_mut();
  38. }
  39. if provider.is_null() {
  40. println!("provider is null");
  41. return std::ptr::null_mut();
  42. }
  43. if auth_endpoint.is_null() {
  44. println!("auth_endpoint is null");
  45. return std::ptr::null_mut();
  46. }
  47. let issuer = unsafe { CStr::from_ptr(issuer) };
  48. let client_id = unsafe { CStr::from_ptr(client_id) };
  49. let provider = unsafe { CStr::from_ptr(provider) };
  50. let auth_endpoint = unsafe { CStr::from_ptr(auth_endpoint) };
  51. match ZeroIDC::new(
  52. issuer.to_str().unwrap(),
  53. client_id.to_str().unwrap(),
  54. provider.to_str().unwrap(),
  55. auth_endpoint.to_str().unwrap(),
  56. web_listen_port,
  57. ) {
  58. Ok(idc) => Box::into_raw(Box::new(idc)),
  59. Err(s) => {
  60. println!("Error creating ZeroIDC instance: {}", s);
  61. std::ptr::null_mut()
  62. }
  63. }
  64. }
  65. #[cfg(any(
  66. all(target_os = "linux", target_arch = "x86"),
  67. all(target_os = "linux", target_arch = "x86_64"),
  68. all(target_os = "linux", target_arch = "aarch64"),
  69. target_os = "windows",
  70. target_os = "macos",
  71. ))]
  72. #[no_mangle]
  73. pub extern "C" fn zeroidc_delete(ptr: *mut ZeroIDC) {
  74. if ptr.is_null() {
  75. return;
  76. }
  77. let idc = unsafe {
  78. assert!(!ptr.is_null());
  79. &mut *ptr
  80. };
  81. idc.stop();
  82. unsafe {
  83. let _ = Box::from_raw(ptr);
  84. }
  85. }
  86. #[cfg(any(
  87. all(target_os = "linux", target_arch = "x86"),
  88. all(target_os = "linux", target_arch = "x86_64"),
  89. all(target_os = "linux", target_arch = "aarch64"),
  90. target_os = "windows",
  91. target_os = "macos",
  92. ))]
  93. #[no_mangle]
  94. pub extern "C" fn zeroidc_start(ptr: *mut ZeroIDC) {
  95. let idc = unsafe {
  96. assert!(!ptr.is_null());
  97. &mut *ptr
  98. };
  99. idc.start();
  100. }
  101. #[cfg(any(
  102. all(target_os = "linux", target_arch = "x86"),
  103. all(target_os = "linux", target_arch = "x86_64"),
  104. all(target_os = "linux", target_arch = "aarch64"),
  105. target_os = "windows",
  106. target_os = "macos",
  107. ))]
  108. #[no_mangle]
  109. pub extern "C" fn zeroidc_stop(ptr: *mut ZeroIDC) {
  110. let idc = unsafe {
  111. assert!(!ptr.is_null());
  112. &mut *ptr
  113. };
  114. idc.stop();
  115. }
  116. #[cfg(any(
  117. all(target_os = "linux", target_arch = "x86"),
  118. all(target_os = "linux", target_arch = "x86_64"),
  119. all(target_os = "linux", target_arch = "aarch64"),
  120. target_os = "windows",
  121. target_os = "macos",
  122. ))]
  123. #[no_mangle]
  124. pub extern "C" fn zeroidc_is_running(ptr: *mut ZeroIDC) -> bool {
  125. let idc = unsafe {
  126. assert!(!ptr.is_null());
  127. &mut *ptr
  128. };
  129. idc.is_running()
  130. }
  131. #[no_mangle]
  132. pub extern "C" fn zeroidc_get_exp_time(ptr: *mut ZeroIDC) -> u64 {
  133. let id = unsafe {
  134. assert!(!ptr.is_null());
  135. &mut *ptr
  136. };
  137. id.get_exp_time()
  138. }
  139. #[cfg(any(
  140. all(target_os = "linux", target_arch = "x86"),
  141. all(target_os = "linux", target_arch = "x86_64"),
  142. all(target_os = "linux", target_arch = "aarch64"),
  143. target_os = "windows",
  144. target_os = "macos",
  145. ))]
  146. #[no_mangle]
  147. pub extern "C" fn zeroidc_set_nonce_and_csrf(ptr: *mut ZeroIDC, csrf_token: *const c_char, nonce: *const c_char) {
  148. let idc = unsafe {
  149. assert!(!ptr.is_null());
  150. &mut *ptr
  151. };
  152. if csrf_token.is_null() {
  153. println!("csrf_token is null");
  154. return;
  155. }
  156. if nonce.is_null() {
  157. println!("nonce is null");
  158. return;
  159. }
  160. let csrf_token = unsafe { CStr::from_ptr(csrf_token) }.to_str().unwrap().to_string();
  161. let nonce = unsafe { CStr::from_ptr(nonce) }.to_str().unwrap().to_string();
  162. idc.set_nonce_and_csrf(csrf_token, nonce);
  163. }
  164. #[cfg(any(
  165. all(target_os = "linux", target_arch = "x86"),
  166. all(target_os = "linux", target_arch = "x86_64"),
  167. all(target_os = "linux", target_arch = "aarch64"),
  168. target_os = "windows",
  169. target_os = "macos",
  170. ))]
  171. #[no_mangle]
  172. pub extern "C" fn free_cstr(s: *mut c_char) {
  173. if s.is_null() {
  174. println!("passed a null object");
  175. return;
  176. }
  177. unsafe {
  178. let _ = CString::from_raw(s);
  179. }
  180. }
  181. #[cfg(any(
  182. all(target_os = "linux", target_arch = "x86"),
  183. all(target_os = "linux", target_arch = "x86_64"),
  184. all(target_os = "linux", target_arch = "aarch64"),
  185. target_os = "windows",
  186. target_os = "macos",
  187. ))]
  188. #[no_mangle]
  189. pub extern "C" fn zeroidc_get_auth_url(ptr: *mut ZeroIDC) -> *mut c_char {
  190. if ptr.is_null() {
  191. println!("passed a null object");
  192. return std::ptr::null_mut();
  193. }
  194. let idc = unsafe { &mut *ptr };
  195. let s = CString::new(idc.auth_url()).unwrap();
  196. s.into_raw()
  197. }
  198. #[cfg(any(
  199. all(target_os = "linux", target_arch = "x86"),
  200. all(target_os = "linux", target_arch = "x86_64"),
  201. all(target_os = "linux", target_arch = "aarch64"),
  202. target_os = "windows",
  203. target_os = "macos",
  204. ))]
  205. #[no_mangle]
  206. pub extern "C" fn zeroidc_token_exchange(idc: *mut ZeroIDC, code: *const c_char) -> *mut c_char {
  207. if idc.is_null() {
  208. println!("idc is null");
  209. return std::ptr::null_mut();
  210. }
  211. if code.is_null() {
  212. println!("code is null");
  213. return std::ptr::null_mut();
  214. }
  215. let idc = unsafe { &mut *idc };
  216. let code = unsafe { CStr::from_ptr(code) }.to_str().unwrap();
  217. let ret = idc.do_token_exchange(code);
  218. match ret {
  219. Ok(ret) => {
  220. #[cfg(debug_assertions)]
  221. {
  222. println!("do_token_exchange ret: {}", ret);
  223. }
  224. let ret = CString::new(ret).unwrap();
  225. ret.into_raw()
  226. }
  227. Err(e) => {
  228. #[cfg(debug_assertions)]
  229. {
  230. println!("do_token_exchange err: {}", e);
  231. }
  232. let errstr = format!("{{\"errorMessage\": \"{}\"}}", e);
  233. let ret = CString::new(errstr).unwrap();
  234. ret.into_raw()
  235. }
  236. }
  237. }
  238. #[no_mangle]
  239. pub extern "C" fn zeroidc_get_url_param_value(param: *const c_char, path: *const c_char) -> *mut c_char {
  240. if param.is_null() {
  241. println!("param is null");
  242. return std::ptr::null_mut();
  243. }
  244. if path.is_null() {
  245. println!("path is null");
  246. return std::ptr::null_mut();
  247. }
  248. let param = unsafe { CStr::from_ptr(param) }.to_str().unwrap();
  249. let path = unsafe { CStr::from_ptr(path) }.to_str().unwrap();
  250. let url = "http://localhost:9993".to_string() + path;
  251. let url = Url::parse(&url).unwrap();
  252. let pairs = url.query_pairs();
  253. for p in pairs {
  254. if p.0 == param {
  255. let s = CString::new(p.1.into_owned()).unwrap();
  256. return s.into_raw();
  257. }
  258. }
  259. std::ptr::null_mut()
  260. }
  261. #[no_mangle]
  262. pub extern "C" fn zeroidc_network_id_from_state(state: *const c_char) -> *mut c_char {
  263. if state.is_null() {
  264. println!("state is null");
  265. return std::ptr::null_mut();
  266. }
  267. let state = unsafe { CStr::from_ptr(state) }.to_str().unwrap();
  268. let split = state.split('_');
  269. let split = split.collect::<Vec<&str>>();
  270. if split.len() != 2 {
  271. return std::ptr::null_mut();
  272. }
  273. let s = CString::new(split[1]).unwrap();
  274. s.into_raw()
  275. }
  276. #[cfg(any(
  277. all(target_os = "linux", target_arch = "x86"),
  278. all(target_os = "linux", target_arch = "x86_64"),
  279. all(target_os = "linux", target_arch = "aarch64"),
  280. target_os = "windows",
  281. target_os = "macos",
  282. ))]
  283. #[no_mangle]
  284. pub extern "C" fn zeroidc_kick_refresh_thread(idc: *mut ZeroIDC) {
  285. if idc.is_null() {
  286. println!("idc is null");
  287. return;
  288. }
  289. let idc = unsafe { &mut *idc };
  290. idc.kick_refresh_thread();
  291. }