This repository provides a collection of example codes in multiple programming languages (C, C++, Go, Rust) demonstrating how to interact with the libv2root library to manage V2Ray services. The examples cover key functionalities such as initializing V2Ray, parsing configurations, testing connection latency, pinging servers, starting/stopping the V2Ray service, and resetting network proxies. This project is ideal for developers looking to integrate V2Ray into their applications using a C-based API across different programming environments.
- Multi-language Support: Implementations in C, C++, Go, and Rust, showcasing cross-language compatibility with
libv2root. - Comprehensive V2Ray Operations: Initialize V2Ray, parse VLESS configurations, test connections, ping servers, and manage service lifecycle.
- Sample Configuration: Includes a
config.jsonfile with a VLESS setup for quick testing. - Cross-platform: Tested on Linux/WSL, with notes for Windows users (use
libv2root.dllinstead oflibv2root.so).
C/: C implementation with dynamic loading oflibv2root.so.Cpp/: C++ implementation with enhanced error handling and modern C++ features.Go/: Go implementation usingcgofor seamless integration with the C API.Rust/: Rust implementation leveraging thelibloadingcrate for safe FFI.config.json: Sample VLESS configuration file for testing..gitignore: Excludes binary files (libv2root.so,v2ray, etc.) from version control.README.md: This file, providing an overview and setup instructions.
Each language folder contains a README.md with specific build and run instructions.
To use these examples, follow these steps:
-
Clone the Repository:
git clone https://github.com/V2RayRoot/V2Root-API-Examples.git cd V2Root-API-Examples -
Prepare Dependencies:
- Linux/WSL:
Install required libraries:
sudo apt install libjansson4 libssl-dev
- Windows:
Use
libv2root.dllinstead oflibv2root.so. Ensurelibjanssonandlibsslare available (e.g., via MSYS2 or pre-built binaries).
- Linux/WSL:
Install required libraries:
-
Place Required Files:
- Copy
libv2root.so(Linux) orlibv2root.dll(Windows) andv2rayexecutable to the project root. - The provided
config.jsonis pre-configured for a VLESS setup. Modify it if needed.
- Copy
-
Build and Run: Navigate to the desired language folder (e.g.,
C/,Go/) and follow the instructions in the respectiveREADME.mdto compile and execute the example.
-
Linux/WSL:
- Ensure
libv2root.sois in the project root or setLD_LIBRARY_PATH:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
- Tested on Ubuntu-based WSL environments.
- Ensure
-
Windows:
- Replace
libv2root.sowithlibv2root.dllin the code and project directory. - Ensure
libv2root.dllis in the same directory as the executable or in the system PATH. - Use a compatible build environment (e.g., MSYS2, MinGW, or Visual Studio).
- Replace
Each example performs the following steps:
- Initializes V2Ray with
init_v2ray. - Parses a VLESS configuration using
parse_config_string. - Tests connection latency with
test_config_connection. - Pings the server with
ping_server. - Starts V2Ray service on ports 2300 (HTTP) and 2301 (SOCKS) using
start_v2ray. - Waits for 20 seconds.
- Stops the service with
stop_v2ray. - Resets network proxies with
reset_network_proxy.
See language-specific README.md files for detailed build/run commands.
For questions or feedback, open an issue on GitHub or contact the repository maintainers.