Started moving from WireGuard for Windows to embeddable-dll-service
This commit is contained in:
8
src/main/cpp/.idea/.gitignore
generated
vendored
Normal file
8
src/main/cpp/.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
1
src/main/cpp/.idea/.name
generated
Normal file
1
src/main/cpp/.idea/.name
generated
Normal file
@@ -0,0 +1 @@
|
||||
WireGuardJNI
|
||||
2
src/main/cpp/.idea/cpp.iml
generated
Normal file
2
src/main/cpp/.idea/cpp.iml
generated
Normal file
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
||||
4
src/main/cpp/.idea/misc.xml
generated
Normal file
4
src/main/cpp/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||
</project>
|
||||
8
src/main/cpp/.idea/modules.xml
generated
Normal file
8
src/main/cpp/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/cpp.iml" filepath="$PROJECT_DIR$/.idea/cpp.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
src/main/cpp/.idea/vcs.xml
generated
Normal file
6
src/main/cpp/.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
24
src/main/cpp/CMakeLists.txt
Normal file
24
src/main/cpp/CMakeLists.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(WireGuardJNI)
|
||||
|
||||
# Set the C standard and C++ standard (optional if needed)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
|
||||
# Specify where the JDK is located
|
||||
set(JAVA_HOME "C:/Program Files/Java/openjdk-21")
|
||||
set(JNI_INCLUDE_DIR "${JAVA_HOME}/include")
|
||||
set(JNI_INCLUDE_DIR2 "${JAVA_HOME}/include/win32")
|
||||
|
||||
|
||||
# Include the JNI header files
|
||||
include_directories(${JNI_INCLUDE_DIR} ${JNI_INCLUDE_DIR2})
|
||||
|
||||
# Add the source file that includes your JNI C wrapper code
|
||||
add_library(wireguard_wrapper SHARED wireguard_wrapper.c)
|
||||
|
||||
# Optionally, you can specify output directory for the compiled library
|
||||
set_target_properties(wireguard_wrapper PROPERTIES
|
||||
LIBRARY_OUTPUT_DIRECTORY build/lib
|
||||
OUTPUT_NAME "wireguard_wrapper"
|
||||
)
|
||||
117
src/main/cpp/tech_nevets_tvpn_wg_WireGuardJNI.h
Normal file
117
src/main/cpp/tech_nevets_tvpn_wg_WireGuardJNI.h
Normal file
@@ -0,0 +1,117 @@
|
||||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||
#include <jni.h>
|
||||
/* Header for class tech_nevets_tvpn_wg_WireGuardJNI */
|
||||
|
||||
#ifndef _Included_tech_nevets_tvpn_wg_WireGuardJNI
|
||||
#define _Included_tech_nevets_tvpn_wg_WireGuardJNI
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/*
|
||||
* Class: tech_nevets_tvpn_wg_WireGuardJNI
|
||||
* Method: installAdapter
|
||||
* Signature: ()I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_installAdapter
|
||||
(JNIEnv *, jobject);
|
||||
|
||||
/*
|
||||
* Class: tech_nevets_tvpn_wg_WireGuardJNI
|
||||
* Method: removeAdapter
|
||||
* Signature: ()I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_removeAdapter
|
||||
(JNIEnv *, jobject);
|
||||
|
||||
/*
|
||||
* Class: tech_nevets_tvpn_wg_WireGuardJNI
|
||||
* Method: startTunnel
|
||||
* Signature: (Ljava/lang/String;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_startTunnel
|
||||
(JNIEnv *, jobject, jstring);
|
||||
|
||||
/*
|
||||
* Class: tech_nevets_tvpn_wg_WireGuardJNI
|
||||
* Method: stopTunnel
|
||||
* Signature: (Ljava/lang/String;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_stopTunnel
|
||||
(JNIEnv *, jobject, jstring);
|
||||
|
||||
/*
|
||||
* Class: tech_nevets_tvpn_wg_WireGuardJNI
|
||||
* Method: createTunnel
|
||||
* Signature: (Ljava/lang/String;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_createTunnel
|
||||
(JNIEnv *, jobject, jstring);
|
||||
|
||||
/*
|
||||
* Class: tech_nevets_tvpn_wg_WireGuardJNI
|
||||
* Method: deleteTunnel
|
||||
* Signature: (Ljava/lang/String;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_deleteTunnel
|
||||
(JNIEnv *, jobject, jstring);
|
||||
|
||||
/*
|
||||
* Class: tech_nevets_tvpn_wg_WireGuardJNI
|
||||
* Method: getConfig
|
||||
* Signature: (Ljava/lang/String;)Ljava/lang/String;
|
||||
*/
|
||||
JNIEXPORT jstring JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_getConfig
|
||||
(JNIEnv *, jobject, jstring);
|
||||
|
||||
/*
|
||||
* Class: tech_nevets_tvpn_wg_WireGuardJNI
|
||||
* Method: updateConfig
|
||||
* Signature: (Ljava/lang/String;Ljava/lang/String;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_updateConfig
|
||||
(JNIEnv *, jobject, jstring, jstring);
|
||||
|
||||
/*
|
||||
* Class: tech_nevets_tvpn_wg_WireGuardJNI
|
||||
* Method: addConfig
|
||||
* Signature: (Ljava/lang/String;Ljava/lang/String;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_addConfig
|
||||
(JNIEnv *, jobject, jstring, jstring);
|
||||
|
||||
/*
|
||||
* Class: tech_nevets_tvpn_wg_WireGuardJNI
|
||||
* Method: getActiveTunnel
|
||||
* Signature: ()Ljava/lang/String;
|
||||
*/
|
||||
JNIEXPORT jstring JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_getActiveTunnel
|
||||
(JNIEnv *, jobject);
|
||||
|
||||
/*
|
||||
* Class: tech_nevets_tvpn_wg_WireGuardJNI
|
||||
* Method: isActive
|
||||
* Signature: ()Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_isActive
|
||||
(JNIEnv *, jobject);
|
||||
|
||||
/*
|
||||
* Class: tech_nevets_tvpn_wg_WireGuardJNI
|
||||
* Method: initializeWireGuard
|
||||
* Signature: (Ljava/lang/String;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_initializeWireGuard
|
||||
(JNIEnv *, jobject, jstring);
|
||||
|
||||
/*
|
||||
* Class: tech_nevets_tvpn_wg_WireGuardJNI
|
||||
* Method: cleanup
|
||||
* Signature: ()V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_cleanup
|
||||
(JNIEnv *, jobject);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
308
src/main/cpp/wireguard.h
Normal file
308
src/main/cpp/wireguard.h
Normal file
@@ -0,0 +1,308 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 OR MIT
|
||||
*
|
||||
* Copyright (C) 2018-2021 WireGuard LLC. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#include <ipexport.h>
|
||||
#include <ifdef.h>
|
||||
#include <ws2ipdef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef ALIGNED
|
||||
# if defined(_MSC_VER)
|
||||
# define ALIGNED(n) __declspec(align(n))
|
||||
# elif defined(__GNUC__)
|
||||
# define ALIGNED(n) __attribute__((aligned(n)))
|
||||
# else
|
||||
# error "Unable to define ALIGNED"
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/* MinGW is missing this one, unfortunately. */
|
||||
#ifndef _Post_maybenull_
|
||||
# define _Post_maybenull_
|
||||
#endif
|
||||
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4324) /* structure was padded due to alignment specifier */
|
||||
|
||||
/**
|
||||
* A handle representing WireGuard adapter
|
||||
*/
|
||||
typedef struct _WIREGUARD_ADAPTER *WIREGUARD_ADAPTER_HANDLE;
|
||||
|
||||
/**
|
||||
* Creates a new WireGuard adapter.
|
||||
*
|
||||
* @param Name The requested name of the adapter. Zero-terminated string of up to MAX_ADAPTER_NAME-1
|
||||
* characters.
|
||||
*
|
||||
* @param TunnelType Name of the adapter tunnel type. Zero-terminated string of up to MAX_ADAPTER_NAME-1
|
||||
* characters.
|
||||
*
|
||||
* @param RequestedGUID The GUID of the created network adapter, which then influences NLA generation deterministically.
|
||||
* If it is set to NULL, the GUID is chosen by the system at random, and hence a new NLA entry is
|
||||
* created for each new adapter. It is called "requested" GUID because the API it uses is
|
||||
* completely undocumented, and so there could be minor interesting complications with its usage.
|
||||
*
|
||||
* @return If the function succeeds, the return value is the adapter handle. Must be released with
|
||||
* WireGuardCloseAdapter. If the function fails, the return value is NULL. To get extended error information, call
|
||||
* GetLastError.
|
||||
*/
|
||||
typedef _Must_inspect_result_
|
||||
_Return_type_success_(return != NULL)
|
||||
_Post_maybenull_
|
||||
WIREGUARD_ADAPTER_HANDLE(WINAPI WIREGUARD_CREATE_ADAPTER_FUNC)
|
||||
(_In_z_ LPCWSTR Name, _In_z_ LPCWSTR TunnelType, _In_opt_ const GUID *RequestedGUID);
|
||||
|
||||
/**
|
||||
* Opens an existing WireGuard adapter.
|
||||
*
|
||||
* @param Name The requested name of the adapter. Zero-terminated string of up to MAX_ADAPTER_NAME-1
|
||||
* characters.
|
||||
*
|
||||
* @return If the function succeeds, the return value is the adapter handle. Must be released with
|
||||
* WireGuardCloseAdapter. If the function fails, the return value is NULL. To get extended error information, call
|
||||
* GetLastError.
|
||||
*/
|
||||
typedef _Must_inspect_result_
|
||||
_Return_type_success_(return != NULL)
|
||||
_Post_maybenull_
|
||||
WIREGUARD_ADAPTER_HANDLE(WINAPI WIREGUARD_OPEN_ADAPTER_FUNC)(_In_z_ LPCWSTR Name);
|
||||
|
||||
/**
|
||||
* Releases WireGuard adapter resources and, if adapter was created with WireGuardCreateAdapter, removes adapter.
|
||||
*
|
||||
* @param Adapter Adapter handle obtained with WireGuardCreateAdapter or WireGuardOpenAdapter.
|
||||
*/
|
||||
typedef VOID(WINAPI WIREGUARD_CLOSE_ADAPTER_FUNC)(_In_opt_ WIREGUARD_ADAPTER_HANDLE Adapter);
|
||||
|
||||
/**
|
||||
* Deletes the WireGuard driver if there are no more adapters in use.
|
||||
*
|
||||
* @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To
|
||||
* get extended error information, call GetLastError.
|
||||
*/
|
||||
typedef _Return_type_success_(return != FALSE)
|
||||
BOOL(WINAPI WIREGUARD_DELETE_DRIVER_FUNC)(VOID);
|
||||
|
||||
/**
|
||||
* Returns the LUID of the adapter.
|
||||
*
|
||||
* @param Adapter Adapter handle obtained with WireGuardCreateAdapter or WireGuardOpenAdapter
|
||||
*
|
||||
* @param Luid Pointer to LUID to receive adapter LUID.
|
||||
*/
|
||||
typedef VOID(WINAPI WIREGUARD_GET_ADAPTER_LUID_FUNC)(_In_ WIREGUARD_ADAPTER_HANDLE Adapter, _Out_ NET_LUID *Luid);
|
||||
|
||||
/**
|
||||
* Determines the version of the WireGuard driver currently loaded.
|
||||
*
|
||||
* @return If the function succeeds, the return value is the version number. If the function fails, the return value is
|
||||
* zero. To get extended error information, call GetLastError. Possible errors include the following:
|
||||
* ERROR_FILE_NOT_FOUND WireGuard not loaded
|
||||
*/
|
||||
typedef _Return_type_success_(return != 0)
|
||||
DWORD(WINAPI WIREGUARD_GET_RUNNING_DRIVER_VERSION_FUNC)(VOID);
|
||||
|
||||
/**
|
||||
* Determines the level of logging, passed to WIREGUARD_LOGGER_CALLBACK.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
WIREGUARD_LOG_INFO, /**< Informational */
|
||||
WIREGUARD_LOG_WARN, /**< Warning */
|
||||
WIREGUARD_LOG_ERR /**< Error */
|
||||
} WIREGUARD_LOGGER_LEVEL;
|
||||
|
||||
/**
|
||||
* Called by internal logger to report diagnostic messages
|
||||
*
|
||||
* @param Level Message level.
|
||||
*
|
||||
* @param Timestamp Message timestamp in in 100ns intervals since 1601-01-01 UTC.
|
||||
*
|
||||
* @param Message Message text.
|
||||
*/
|
||||
typedef VOID(CALLBACK *WIREGUARD_LOGGER_CALLBACK)(
|
||||
_In_ WIREGUARD_LOGGER_LEVEL Level,
|
||||
_In_ DWORD64 Timestamp,
|
||||
_In_z_ LPCWSTR Message);
|
||||
|
||||
/**
|
||||
* Sets logger callback function.
|
||||
*
|
||||
* @param NewLogger Pointer to callback function to use as a new global logger. NewLogger may be called from various
|
||||
* threads concurrently. Should the logging require serialization, you must handle serialization in
|
||||
* NewLogger. Set to NULL to disable.
|
||||
*/
|
||||
typedef VOID(WINAPI WIREGUARD_SET_LOGGER_FUNC)(_In_ WIREGUARD_LOGGER_CALLBACK NewLogger);
|
||||
|
||||
/**
|
||||
* Whether and how logs from the driver are collected for the callback function.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
WIREGUARD_ADAPTER_LOG_OFF, /**< No logs are generated from the driver. */
|
||||
WIREGUARD_ADAPTER_LOG_ON, /**< Logs are generated from the driver. */
|
||||
WIREGUARD_ADAPTER_LOG_ON_WITH_PREFIX /**< Logs are generated from the driver, index-prefixed. */
|
||||
} WIREGUARD_ADAPTER_LOG_STATE;
|
||||
|
||||
/**
|
||||
* Sets whether and how the adapter logs to the logger previously set up with WireGuardSetLogger.
|
||||
*
|
||||
* @param Adapter Adapter handle obtained with WireGuardCreateAdapter or WireGuardOpenAdapter
|
||||
*
|
||||
* @param LogState Adapter logging state.
|
||||
*
|
||||
* @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To
|
||||
* get extended error information, call GetLastError.
|
||||
*/
|
||||
typedef _Return_type_success_(return != FALSE)
|
||||
BOOL(WINAPI WIREGUARD_SET_ADAPTER_LOGGING_FUNC)
|
||||
(_In_ WIREGUARD_ADAPTER_HANDLE Adapter, _In_ WIREGUARD_ADAPTER_LOG_STATE LogState);
|
||||
|
||||
/**
|
||||
* Determines the state of the adapter.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
WIREGUARD_ADAPTER_STATE_DOWN, /**< Down */
|
||||
WIREGUARD_ADAPTER_STATE_UP, /**< Up */
|
||||
} WIREGUARD_ADAPTER_STATE;
|
||||
|
||||
/**
|
||||
* Sets the adapter state of the WireGuard adapter. Note: sockets are owned by the process that sets the state to up.
|
||||
*
|
||||
* @param Adapter Adapter handle obtained with WireGuardCreateAdapter or WireGuardOpenAdapter
|
||||
*
|
||||
* @param State Adapter state.
|
||||
*
|
||||
* @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To
|
||||
* get extended error information, call GetLastError.
|
||||
*/
|
||||
typedef _Return_type_success_(return != FALSE)
|
||||
BOOL(WINAPI WIREGUARD_SET_ADAPTER_STATE_FUNC)
|
||||
(_In_ WIREGUARD_ADAPTER_HANDLE Adapter, _In_ WIREGUARD_ADAPTER_STATE State);
|
||||
|
||||
/**
|
||||
* Gets the adapter state of the WireGuard adapter.
|
||||
*
|
||||
* @param Adapter Adapter handle obtained with WireGuardCreateAdapter or WireGuardOpenAdapter
|
||||
*
|
||||
* @param State Pointer to adapter state.
|
||||
*
|
||||
* @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To
|
||||
* get extended error information, call GetLastError.
|
||||
*/
|
||||
typedef _Must_inspect_result_
|
||||
_Return_type_success_(return != FALSE)
|
||||
BOOL(WINAPI WIREGUARD_GET_ADAPTER_STATE_FUNC)
|
||||
(_In_ WIREGUARD_ADAPTER_HANDLE Adapter, _Out_ WIREGUARD_ADAPTER_STATE *State);
|
||||
|
||||
#define WIREGUARD_KEY_LENGTH 32
|
||||
|
||||
typedef struct _WIREGUARD_ALLOWED_IP WIREGUARD_ALLOWED_IP;
|
||||
struct ALIGNED(8) _WIREGUARD_ALLOWED_IP
|
||||
{
|
||||
union
|
||||
{
|
||||
IN_ADDR V4;
|
||||
IN6_ADDR V6;
|
||||
} Address; /**< IP address */
|
||||
ADDRESS_FAMILY AddressFamily; /**< Address family, either AF_INET or AF_INET6 */
|
||||
BYTE Cidr; /**< CIDR of allowed IPs */
|
||||
};
|
||||
|
||||
typedef enum
|
||||
{
|
||||
WIREGUARD_PEER_HAS_PUBLIC_KEY = 1 << 0, /**< The PublicKey field is set */
|
||||
WIREGUARD_PEER_HAS_PRESHARED_KEY = 1 << 1, /**< The PresharedKey field is set */
|
||||
WIREGUARD_PEER_HAS_PERSISTENT_KEEPALIVE = 1 << 2, /**< The PersistentKeepAlive field is set */
|
||||
WIREGUARD_PEER_HAS_ENDPOINT = 1 << 3, /**< The Endpoint field is set */
|
||||
WIREGUARD_PEER_REPLACE_ALLOWED_IPS = 1 << 5, /**< Remove all allowed IPs before adding new ones */
|
||||
WIREGUARD_PEER_REMOVE = 1 << 6, /**< Remove specified peer */
|
||||
WIREGUARD_PEER_UPDATE = 1 << 7 /**< Do not add a new peer */
|
||||
} WIREGUARD_PEER_FLAG;
|
||||
|
||||
typedef struct _WIREGUARD_PEER WIREGUARD_PEER;
|
||||
struct ALIGNED(8) _WIREGUARD_PEER
|
||||
{
|
||||
WIREGUARD_PEER_FLAG Flags; /**< Bitwise combination of flags */
|
||||
DWORD Reserved; /**< Reserved; must be zero */
|
||||
BYTE PublicKey[WIREGUARD_KEY_LENGTH]; /**< Public key, the peer's primary identifier */
|
||||
BYTE PresharedKey[WIREGUARD_KEY_LENGTH]; /**< Preshared key for additional layer of post-quantum resistance */
|
||||
WORD PersistentKeepalive; /**< Seconds interval, or 0 to disable */
|
||||
SOCKADDR_INET Endpoint; /**< Endpoint, with IP address and UDP port number*/
|
||||
DWORD64 TxBytes; /**< Number of bytes transmitted */
|
||||
DWORD64 RxBytes; /**< Number of bytes received */
|
||||
DWORD64 LastHandshake; /**< Time of the last handshake, in 100ns intervals since 1601-01-01 UTC */
|
||||
DWORD AllowedIPsCount; /**< Number of allowed IP structs following this struct */
|
||||
};
|
||||
|
||||
typedef enum
|
||||
{
|
||||
WIREGUARD_INTERFACE_HAS_PUBLIC_KEY = (1 << 0), /**< The PublicKey field is set */
|
||||
WIREGUARD_INTERFACE_HAS_PRIVATE_KEY = (1 << 1), /**< The PrivateKey field is set */
|
||||
WIREGUARD_INTERFACE_HAS_LISTEN_PORT = (1 << 2), /**< The ListenPort field is set */
|
||||
WIREGUARD_INTERFACE_REPLACE_PEERS = (1 << 3) /**< Remove all peers before adding new ones */
|
||||
} WIREGUARD_INTERFACE_FLAG;
|
||||
|
||||
typedef struct _WIREGUARD_INTERFACE WIREGUARD_INTERFACE;
|
||||
struct ALIGNED(8) _WIREGUARD_INTERFACE
|
||||
{
|
||||
WIREGUARD_INTERFACE_FLAG Flags; /**< Bitwise combination of flags */
|
||||
WORD ListenPort; /**< Port for UDP listen socket, or 0 to choose randomly */
|
||||
BYTE PrivateKey[WIREGUARD_KEY_LENGTH]; /**< Private key of interface */
|
||||
BYTE PublicKey[WIREGUARD_KEY_LENGTH]; /**< Corresponding public key of private key */
|
||||
DWORD PeersCount; /**< Number of peer structs following this struct */
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the configuration of the WireGuard adapter.
|
||||
*
|
||||
* @param Adapter Adapter handle obtained with WireGuardCreateAdapter or WireGuardOpenAdapter
|
||||
*
|
||||
* @param Config Configuration for the adapter.
|
||||
*
|
||||
* @param Bytes Number of bytes in Config allocation.
|
||||
*
|
||||
* @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To
|
||||
* get extended error information, call GetLastError.
|
||||
*/
|
||||
typedef _Return_type_success_(return != FALSE)
|
||||
BOOL(WINAPI WIREGUARD_SET_CONFIGURATION_FUNC)
|
||||
(_In_ WIREGUARD_ADAPTER_HANDLE Adapter, _In_reads_bytes_(Bytes) const WIREGUARD_INTERFACE *Config, _In_ DWORD Bytes);
|
||||
|
||||
/**
|
||||
* Gets the configuration of the WireGuard adapter.
|
||||
*
|
||||
* @param Adapter Adapter handle obtained with WireGuardCreateAdapter or WireGuardOpenAdapter
|
||||
*
|
||||
* @param Config Configuration for the adapter.
|
||||
*
|
||||
* @param Bytes Pointer to number of bytes in Config allocation.
|
||||
*
|
||||
* @return If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To
|
||||
* get extended error information, call GetLastError, which if ERROR_MORE_DATA, Bytes is updated with the
|
||||
* required size.
|
||||
*/
|
||||
typedef _Must_inspect_result_
|
||||
_Return_type_success_(return != FALSE)
|
||||
BOOL(WINAPI WIREGUARD_GET_CONFIGURATION_FUNC)
|
||||
(_In_ WIREGUARD_ADAPTER_HANDLE Adapter,
|
||||
_Out_writes_bytes_all_(*Bytes) WIREGUARD_INTERFACE *Config,
|
||||
_Inout_ DWORD *Bytes);
|
||||
|
||||
#pragma warning(pop)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
161
src/main/cpp/wireguard_wrapper.c
Normal file
161
src/main/cpp/wireguard_wrapper.c
Normal file
@@ -0,0 +1,161 @@
|
||||
#include <jni.h>
|
||||
#include <stdio.h>
|
||||
#include <winsock2.h>
|
||||
#include <windows.h>
|
||||
#include "wireguard.h"
|
||||
#include <string.h>
|
||||
#include "tech_nevets_tvpn_wg_WireGuardJNI.h"
|
||||
|
||||
const char *APP_DIR = "C:\\Program Files\\TVPN\\";
|
||||
|
||||
JNIEXPORT jint JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_createTunnel(JNIEnv *env, jobject obj, jstring tunnelName) {
|
||||
const char *tunnelCharArr = (*env)->GetStringUTFChars(env, tunnelName, NULL);
|
||||
if (tunnelCharArr == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *initialServiceName = "WireGuardTunnel$";
|
||||
char *tnlName = malloc(strlen(initialServiceName) + strlen(tunnelCharArr) + 1);
|
||||
strcpy(tnlName, initialServiceName);
|
||||
strcat(tnlName, tunnelCharArr);
|
||||
|
||||
char *initialDisplayName = "WireGuard Tunnel: ";
|
||||
char *displayName = malloc(strlen(initialDisplayName) + strlen(tunnelCharArr) + 1);
|
||||
strcpy(displayName, initialDisplayName);
|
||||
strcat(displayName, tunnelCharArr);
|
||||
|
||||
SC_HANDLE hSCManager = OpenSCManager(
|
||||
NULL,
|
||||
NULL,
|
||||
SC_MANAGER_ALL_ACCESS
|
||||
);
|
||||
if (hSCManager == NULL) {
|
||||
printf("OpenSCManager failed (%lu)\n", GetLastError());
|
||||
return 2;
|
||||
}
|
||||
|
||||
char *params = "TVPN.exe /installtunnelservice ";
|
||||
char *conf = ".conf";
|
||||
char *exePath = malloc(strlen(APP_DIR) + strlen(params) + strlen(tunnelCharArr) + strlen(conf) + 1);
|
||||
strcpy(exePath, APP_DIR);
|
||||
strcat(exePath, params);
|
||||
strcat(exePath, tunnelCharArr);
|
||||
strcat(exePath, conf);
|
||||
|
||||
SC_HANDLE hService = CreateServiceA(
|
||||
hSCManager,
|
||||
tnlName,
|
||||
displayName,
|
||||
SERVICE_ALL_ACCESS,
|
||||
SERVICE_WIN32_OWN_PROCESS,
|
||||
SERVICE_AUTO_START,
|
||||
SERVICE_ERROR_NORMAL,
|
||||
exePath,
|
||||
NULL,
|
||||
NULL,
|
||||
"Nsi\0TcpIp\0\0",
|
||||
NULL,
|
||||
NULL
|
||||
);
|
||||
|
||||
DWORD sid_info = SERVICE_SID_TYPE_UNRESTRICTED;
|
||||
|
||||
if (!ChangeServiceConfig2A(hService, SERVICE_CONFIG_SERVICE_SID_INFO, &sid_info)) {
|
||||
printf("ChangeServiceConfig2W failed (%lu)\n", GetLastError());
|
||||
(*env)->ReleaseStringUTFChars(env, tunnelName, tunnelCharArr);
|
||||
free(tnlName);
|
||||
free(displayName);
|
||||
free(exePath);
|
||||
CloseServiceHandle(hSCManager);
|
||||
CloseServiceHandle(hService);
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
||||
(*env)->ReleaseStringUTFChars(env, tunnelName, tunnelCharArr);
|
||||
free(tnlName);
|
||||
free(displayName);
|
||||
free(exePath);
|
||||
CloseServiceHandle(hSCManager);
|
||||
CloseServiceHandle(hService);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_deleteTunnel(JNIEnv *env, jobject obj, jstring tunnelName) {
|
||||
const char *nativeString = (*env)->GetStringUTFChars(env, tunnelName, NULL);
|
||||
|
||||
char *initialServiceName = "WireGuardTunnel$";
|
||||
char *tnlName = malloc(strlen(initialServiceName) + strlen(nativeString) + 1);
|
||||
strcpy(tnlName, initialServiceName);
|
||||
strcat(tnlName, nativeString);
|
||||
|
||||
SC_HANDLE hSCManager = OpenSCManager(
|
||||
NULL,
|
||||
NULL,
|
||||
SC_MANAGER_ALL_ACCESS
|
||||
);
|
||||
if (hSCManager == NULL) {
|
||||
// Handle error
|
||||
return 1;
|
||||
}
|
||||
|
||||
SC_HANDLE hService = OpenServiceA(hSCManager, tnlName, DELETE);
|
||||
if (hService != NULL) {
|
||||
if (!DeleteService(hService)) {
|
||||
// Handle error
|
||||
return 2;
|
||||
}
|
||||
CloseServiceHandle(hService);
|
||||
} else {
|
||||
// Handle error
|
||||
return 3;
|
||||
}
|
||||
|
||||
CloseServiceHandle(hSCManager);
|
||||
(*env)->ReleaseStringUTFChars(env, tunnelName, nativeString);
|
||||
free(tnlName);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
JNIEXPORT jint JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_initializeWireGuard(JNIEnv *env, jobject obj, jstring configFilePath) {
|
||||
const char *nativeString = (*env)->GetStringUTFChars(env, configFilePath, 0);
|
||||
|
||||
int confLength = strlen(nativeString);
|
||||
|
||||
(*env)->ReleaseStringUTFChars(env, configFilePath, nativeString);
|
||||
|
||||
return confLength;
|
||||
|
||||
// const char *nativeConfigFilePath = (*env)->GetStringUTFChars(env, configFilePath, 0);
|
||||
|
||||
// Load the WireGuard DLL dynamically
|
||||
// hInstLibrary = LoadLibrary("tunnel.dll"); // Adjust with the actual DLL name
|
||||
|
||||
// if (hInstLibrary) {
|
||||
// Example: Load the WireGuard function from DLL
|
||||
// wg_func_t wg_init_func = (wg_func_t) GetProcAddress(hInstLibrary, "WireGuardInitFunction"); // Adjust based on the WireGuard API
|
||||
|
||||
|
||||
// if (wg_init_func) {
|
||||
// wg_init_func(nativeConfigFilePath);
|
||||
// (*env)->ReleaseStringUTFChars(env, configFilePath, nativeConfigFilePath);
|
||||
// return 0; // Success
|
||||
// } else {
|
||||
// printf("Failed to get function from WireGuard DLL\n");
|
||||
// return -2; // Failed to get function
|
||||
// }
|
||||
// } else {
|
||||
// printf("Failed to load WireGuard DLL\n");
|
||||
// return -1; // Failed to load DLL
|
||||
// }
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL Java_tech_nevets_tvpn_wg_WireGuardJNI_cleanup(JNIEnv *env, jobject obj) {
|
||||
printf("Silly goose, this isn't implemented yet!\n");
|
||||
|
||||
// if (hInstLibrary) {
|
||||
// FreeLibrary(hInstLibrary);
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user