Files
nym/sdk/ffi/go/src/lib.rs
T
mfahampshire e454d71b78 Max/client pool (#5188)
* tcp conn tracker

* make default decay const

* first pass connpool

* err handling conpool start

* added notes for next features

* first version working

* first pass spin out client_pool

* cancel token

* logging change

* bump default decay time

* bugfix: make sure to apply gateway score filtering when choosing initial node

* add duplicate packets received to troubleshooting

* client_pool.rs mod

* client pool example

* clippy

* client pool example done

* added disconnect to client pool

* update mod file

* add cancel token disconnect fn

* comments

* comments

* add clone

* added disconnect thread

* update example files tcpproxy

* client pool docs

* remove comments for future ffi push + lower default pool size from 4 to 2

* comment on ffi

* update command help

* clone impl

* remove clone

* fix clippy

* fix clippy again

* fix test

* tweaked text grammar

* updated comment in example

* future is now

* cherry

* cherry

* fix borked rebase

* fix fmt

* wasm fix

---------

Co-authored-by: Jędrzej Stuczyński <jedrzej.stuczynski@gmail.com>
2025-01-14 16:11:47 +00:00

164 lines
4.8 KiB
Rust

// Copyright 2023-2024 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
// due to autogenerated code
#![allow(clippy::empty_line_after_doc_comments)]
use nym_sdk::mixnet::Recipient;
use nym_sphinx_anonymous_replies::requests::AnonymousSenderTag;
uniffi::include_scaffolding!("bindings");
#[allow(clippy::enum_variant_names)]
#[derive(Debug, thiserror::Error)]
enum GoWrapError {
#[error("Couldn't init client")]
ClientInitError {},
#[error("Error getting self address")]
SelfAddrError {},
#[error("Error sending message")]
SendMsgError {},
#[error("Error sending reply")]
ReplyError {},
#[error("Could not start listening")]
ListenError {},
#[error("Couldn't init proxy client")]
ProxyInitError {},
#[error("Couldn't run proxy client")]
ProxyRunError {},
#[error("Couldn't init proxy server")]
ServerInitError {},
#[error("Couldn't get proxy server address")]
AddressGetterError {},
#[error("Couldn't run proxy server")]
ServerRunError {},
}
#[no_mangle]
fn init_logging() {
nym_bin_common::logging::setup_logging();
}
#[no_mangle]
fn init_ephemeral() -> Result<(), GoWrapError> {
match nym_ffi_shared::init_ephemeral_internal() {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::ClientInitError {}),
}
}
#[no_mangle]
fn get_self_address() -> Result<String, GoWrapError> {
match nym_ffi_shared::get_self_address_internal() {
Ok(addr) => Ok(addr),
Err(..) => Err(GoWrapError::SelfAddrError {}),
}
}
#[no_mangle]
fn send_message(recipient: String, message: String) -> Result<(), GoWrapError> {
let nym_recipient_type =
Recipient::try_from_base58_string(recipient).expect("couldn't create Recipient");
match nym_ffi_shared::send_message_internal(nym_recipient_type, &message) {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::SendMsgError {}),
}
}
#[no_mangle]
fn reply(recipient: Vec<u8>, message: String) -> Result<(), GoWrapError> {
let mut sized_array: [u8; 16] = [0; 16];
sized_array.copy_from_slice(&recipient[..16]);
let anon_recipient_type: AnonymousSenderTag = AnonymousSenderTag::from_bytes(sized_array);
match nym_ffi_shared::reply_internal(anon_recipient_type, &message) {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::ReplyError {}),
}
}
pub struct IncomingMessage {
message: String,
sender: Vec<u8>,
}
#[no_mangle]
fn listen_for_incoming() -> Result<IncomingMessage, GoWrapError> {
match nym_ffi_shared::listen_for_incoming_internal() {
Ok(received) => {
let message = String::from_utf8_lossy(&received.message).to_string();
let sender = received.sender_tag.unwrap().to_bytes().to_vec();
let incoming = IncomingMessage { message, sender };
Ok(incoming)
}
Err(_) => Err(GoWrapError::ListenError {}),
}
}
#[no_mangle]
fn new_proxy_client(
server_address: String,
listen_address: String,
listen_port: String,
close_timeout: u64,
env: Option<String>,
pool_size: u8,
) -> Result<(), GoWrapError> {
let server_nym_addr =
Recipient::try_from_base58_string(server_address).expect("couldn't create Recipient");
match nym_ffi_shared::proxy_client_new_internal(
server_nym_addr,
&listen_address,
&listen_port,
close_timeout,
env,
pool_size as usize,
) {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::ProxyInitError {}),
}
}
#[no_mangle]
fn new_proxy_client_default(
server_address: String,
env: Option<String>,
) -> Result<(), GoWrapError> {
let server_nym_addr =
Recipient::try_from_base58_string(server_address).expect("couldn't create Recipient");
match nym_ffi_shared::proxy_client_new_defaults_internal(server_nym_addr, env) {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::ProxyInitError {}),
}
}
fn run_proxy_client() -> Result<(), GoWrapError> {
match nym_ffi_shared::proxy_client_run_internal() {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::ProxyRunError {}),
}
}
fn new_proxy_server(
upstream_address: String,
config_dir: String,
env: Option<String>,
) -> Result<(), GoWrapError> {
match nym_ffi_shared::proxy_server_new_internal(&upstream_address, &config_dir, env) {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::ServerInitError {}),
}
}
fn proxy_server_address() -> Result<String, GoWrapError> {
match nym_ffi_shared::proxy_server_address_internal() {
Ok(address) => Ok(address.to_string()),
Err(_) => Err(GoWrapError::AddressGetterError {}),
}
}
fn run_proxy_server() -> Result<(), GoWrapError> {
match nym_ffi_shared::proxy_server_run_internal() {
Ok(_) => Ok(()),
Err(_) => Err(GoWrapError::ServerRunError {}),
}
}