parent
cb01738edd
commit
098d9c2c4e
|
@ -0,0 +1,220 @@
|
|||
# ClangFormatConfigureSource: 'clang-format-file://D:/development/c/daemon_agent/.clang-format'
|
||||
Language: Cpp
|
||||
AccessModifierOffset: -4
|
||||
InsertBraces: true
|
||||
AlignArrayOfStructures: Left
|
||||
AlignAfterOpenBracket: Align
|
||||
AlignConsecutiveMacros:
|
||||
Enabled: true
|
||||
AcrossEmptyLines: true
|
||||
AcrossComments: true
|
||||
AlignConsecutiveAssignments:
|
||||
Enabled: true
|
||||
AcrossEmptyLines: false
|
||||
AcrossComments: true
|
||||
PadOperators: true
|
||||
AlignCompound: true
|
||||
AlignConsecutiveBitFields: None
|
||||
AlignConsecutiveDeclarations:
|
||||
Enabled: true
|
||||
AcrossEmptyLines: false
|
||||
AcrossComments: true
|
||||
PadOperators: true
|
||||
AlignCompound: true
|
||||
AlignEscapedNewlines: Left
|
||||
AlignOperands: DontAlign
|
||||
AlignTrailingComments: true
|
||||
AllowAllArgumentsOnNextLine: false
|
||||
AllowAllConstructorInitializersOnNextLine: false
|
||||
AllowAllParametersOfDeclarationOnNextLine: false
|
||||
AllowShortEnumsOnASingleLine: false
|
||||
AllowShortBlocksOnASingleLine: Always
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: None
|
||||
AllowShortLambdasOnASingleLine: All
|
||||
AllowShortIfStatementsOnASingleLine: Never
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AlwaysBreakAfterDefinitionReturnType: None
|
||||
AlwaysBreakAfterReturnType: None
|
||||
AlwaysBreakBeforeMultilineStrings: true
|
||||
AlwaysBreakTemplateDeclarations: MultiLine
|
||||
AttributeMacros:
|
||||
- __capability
|
||||
- __unused
|
||||
BinPackArguments: false
|
||||
BinPackParameters: false
|
||||
BraceWrapping:
|
||||
AfterCaseLabel: false
|
||||
AfterClass: false
|
||||
AfterControlStatement: Never
|
||||
AfterEnum: false
|
||||
AfterFunction: false
|
||||
AfterNamespace: false
|
||||
AfterObjCDeclaration: false
|
||||
AfterStruct: false
|
||||
AfterUnion: false
|
||||
AfterExternBlock: false
|
||||
BeforeCatch: true
|
||||
BeforeElse: false
|
||||
BeforeLambdaBody: false
|
||||
BeforeWhile: false
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: true
|
||||
SplitEmptyRecord: true
|
||||
SplitEmptyNamespace: true
|
||||
BreakBeforeBinaryOperators: None
|
||||
BreakBeforeConceptDeclarations: true
|
||||
BreakBeforeBraces: Custom
|
||||
BreakBeforeInheritanceComma: false
|
||||
BreakInheritanceList: BeforeColon
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakConstructorInitializersBeforeComma: false
|
||||
BreakConstructorInitializers: BeforeColon
|
||||
BreakAfterJavaFieldAnnotations: false
|
||||
BreakStringLiterals: true
|
||||
ColumnLimit: 120
|
||||
CommentPragmas: '^ IWYU pragma:'
|
||||
CompactNamespaces: true
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||
ConstructorInitializerIndentWidth: 4
|
||||
ContinuationIndentWidth: 4
|
||||
Cpp11BracedListStyle: true
|
||||
DeriveLineEnding: true
|
||||
DerivePointerAlignment: false
|
||||
DisableFormat: false
|
||||
EmptyLineBeforeAccessModifier: LogicalBlock
|
||||
ExperimentalAutoDetectBinPacking: false
|
||||
FixNamespaceComments: true
|
||||
ForEachMacros:
|
||||
- foreach
|
||||
- Q_FOREACH
|
||||
- BOOST_FOREACH
|
||||
StatementAttributeLikeMacros:
|
||||
- Q_EMIT
|
||||
IncludeBlocks: Regroup
|
||||
IncludeCategories:
|
||||
- Regex: '^<ext/.*\.h>'
|
||||
Priority: 2
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
- Regex: '^<.*\.h>'
|
||||
Priority: 1
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
- Regex: '^<.*'
|
||||
Priority: 2
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
- Regex: '.*'
|
||||
Priority: 3
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
IncludeIsMainRegex: '([-_](test|unittest))?$'
|
||||
IncludeIsMainSourceRegex: ''
|
||||
IndentCaseLabels: true
|
||||
IndentCaseBlocks: false
|
||||
IndentGotoLabels: true
|
||||
IndentPPDirectives: None
|
||||
IndentExternBlock: AfterExternBlock
|
||||
IndentRequiresClause: false
|
||||
IndentWidth: 4
|
||||
IndentWrappedFunctionNames: false
|
||||
InsertTrailingCommas: None
|
||||
JavaScriptQuotes: Leave
|
||||
JavaScriptWrapImports: true
|
||||
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||
MacroBlockBegin: ''
|
||||
MacroBlockEnd: ''
|
||||
MaxEmptyLinesToKeep: 1
|
||||
NamespaceIndentation: None
|
||||
ObjCBinPackProtocolList: Never
|
||||
ObjCBlockIndentWidth: 2
|
||||
ObjCBreakBeforeNestedBlockParam: true
|
||||
ObjCSpaceAfterProperty: false
|
||||
ObjCSpaceBeforeProtocolList: true
|
||||
PenaltyBreakAssignment: 1000
|
||||
PenaltyBreakBeforeFirstCallParameter: 1
|
||||
PenaltyBreakComment: 300
|
||||
PenaltyBreakFirstLessLess: 140
|
||||
PenaltyBreakString: 1000
|
||||
PenaltyBreakTemplateDeclaration: 10
|
||||
PenaltyExcessCharacter: 1000000
|
||||
PenaltyReturnTypeOnItsOwnLine: 200
|
||||
PenaltyIndentedWhitespace: 0
|
||||
PointerAlignment: Right
|
||||
RawStringFormats:
|
||||
- Language: Cpp
|
||||
Delimiters:
|
||||
- cc
|
||||
- CC
|
||||
- cpp
|
||||
- Cpp
|
||||
- CPP
|
||||
- 'c++'
|
||||
- 'C++'
|
||||
CanonicalDelimiter: ''
|
||||
BasedOnStyle: google
|
||||
- Language: TextProto
|
||||
Delimiters:
|
||||
- pb
|
||||
- PB
|
||||
- proto
|
||||
- PROTO
|
||||
EnclosingFunctions:
|
||||
- EqualsProto
|
||||
- EquivToProto
|
||||
- PARSE_PARTIAL_TEXT_PROTO
|
||||
- PARSE_TEST_PROTO
|
||||
- PARSE_TEXT_PROTO
|
||||
- ParseTextOrDie
|
||||
- ParseTextProtoOrDie
|
||||
- ParseTestProto
|
||||
- ParsePartialTestProto
|
||||
CanonicalDelimiter: ''
|
||||
BasedOnStyle: google
|
||||
ReflowComments: false
|
||||
SortIncludes: Never
|
||||
SortJavaStaticImport: Before
|
||||
SortUsingDeclarations: true
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceAfterLogicalNot: false
|
||||
SpaceAfterTemplateKeyword: false
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeCaseColon: false
|
||||
SpaceBeforeCpp11BracedList: true
|
||||
SpaceBeforeCtorInitializerColon: true
|
||||
SpaceBeforeInheritanceColon: true
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceAroundPointerQualifiers: Default
|
||||
SpaceBeforeRangeBasedForLoopColon: true
|
||||
SpaceInEmptyBlock: false
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesBeforeTrailingComments: 4
|
||||
SpacesInAngles: false
|
||||
SpacesInConditionalStatement: false
|
||||
SpacesInContainerLiterals: false
|
||||
SpacesInCStyleCastParentheses: false
|
||||
SpacesInLineCommentPrefix:
|
||||
Minimum: 1
|
||||
Maximum: -1
|
||||
SpacesInParentheses: false
|
||||
SpacesInSquareBrackets: false
|
||||
SpaceBeforeSquareBrackets: false
|
||||
BitFieldColonSpacing: Both
|
||||
Standard: Auto
|
||||
StatementMacros:
|
||||
- Q_UNUSED
|
||||
- QT_REQUIRE_VERSION
|
||||
TabWidth: 4
|
||||
UseCRLF: false
|
||||
UseTab: Never
|
||||
SeparateDefinitionBlocks: Always
|
||||
WhitespaceSensitiveMacros:
|
||||
- STRINGIZE
|
||||
- PP_STRINGIZE
|
||||
- BOOST_PP_STRINGIZE
|
||||
- NS_SWIFT_NAME
|
||||
- CF_SWIFT_NAME
|
||||
TypenameMacros:
|
||||
- CONFIG_ITEM
|
||||
- PCONFIG_ITEM
|
|
@ -25,28 +25,12 @@ public partial class MainForm : Form
|
|||
NetTunnelLib.TunnelSDKInitEnv(Environment.CurrentDirectory);
|
||||
}
|
||||
|
||||
private void button1_Click(object sender, EventArgs e)
|
||||
private void test_get_network_info()
|
||||
{
|
||||
var buffer = new StringBuilder(256 + 1);
|
||||
//NetTunnelLib.TunnelSDKInitEnv();
|
||||
var v = NetTunnelLib.FindWireguardExe(buffer, 256);
|
||||
MessageBox.Show(buffer.ToString() + "----" + v.ToString());
|
||||
}
|
||||
|
||||
private void button2_Click(object sender, EventArgs e)
|
||||
{
|
||||
var buffer = new StringBuilder(64 + 1);
|
||||
var buffer2 = new StringBuilder(64 + 1);
|
||||
//var v = NetTunnelLib.RunPipeCmd("cmd.exe /C dir E:\\", buffer, 4096);
|
||||
//MessageBox.Show(buffer.ToString() + "\nResult: " + v.ToString());
|
||||
|
||||
var v = NetTunnelLib.GenerateWireguardKeyPairs(buffer, 64, buffer2, 64);
|
||||
lstLogs.Items.Add(v.ToString() + ": [" + buffer.ToString() + "] / [" + buffer2.ToString() + "]");
|
||||
|
||||
IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(NetCard)) * 32);
|
||||
int size = 0;
|
||||
|
||||
v = NetTunnelLib.GetAllNICInfo(pt, ref size);
|
||||
var v = NetTunnelLib.GetAllNICInfo(pt, ref size);
|
||||
|
||||
Console.WriteLine("Result {0}, total {1}", v, size);
|
||||
|
||||
|
@ -64,4 +48,59 @@ public partial class MainForm : Form
|
|||
|
||||
Marshal.FreeHGlobal(pt);
|
||||
}
|
||||
|
||||
private void test_create_wg_key()
|
||||
{
|
||||
var buffer = new StringBuilder(64 + 1);
|
||||
var buffer2 = new StringBuilder(64 + 1);
|
||||
var v = NetTunnelLib.GenerateWireguardKeyPairs(buffer, 64, buffer2, 64);
|
||||
lstLogs.Items.Add(v.ToString() + ": [" + buffer.ToString() + "] / [" + buffer2.ToString() + "]");
|
||||
}
|
||||
|
||||
private void test_create_wg_svr_cfg()
|
||||
{
|
||||
WgSvrConfig wsg = new WgSvrConfig();
|
||||
|
||||
wsg.Name = "wg_svr";
|
||||
wsg.Address = "10.10.10.250/24";
|
||||
wsg.ListenPort = 10000;
|
||||
wsg.PrivateKey = "EOHYEXSK4xw0LlB4I3ZlF5UqSaoma3Ea2KPhLfIdA14=";
|
||||
wsg.CliPubKey = "6BWnmkCJqJC5iNoCEZWTxwGNG7qwkxFoVgAk4DoIKCk=";
|
||||
wsg.AllowNet = "10.10.10.5/32";
|
||||
|
||||
var v = NetTunnelLib.WireGuardCreateServerConfig(wsg);
|
||||
|
||||
Console.WriteLine("Create WireGuard Server Configure File: {0}", v);
|
||||
}
|
||||
|
||||
public void test_create_wg_cli_cfg()
|
||||
{
|
||||
WgCliConfig cli = new WgCliConfig();
|
||||
|
||||
cli.Name = "wg_cli";
|
||||
cli.Address = "10.10.10.1/32";
|
||||
cli.PrivateKey = "WGAlqvys3O83VmWug6Z8NzUrxGr/PNHSeOVFnKLSe2k=";
|
||||
|
||||
cli.SvrPubKey = "8KEaqtWM5U35SR8S3QJriGHPr0VIqvk8A7BEuOjjp1M=";
|
||||
cli.AllowNet = "10.10.10.0/24, 10.0.4.16/30";
|
||||
cli.ServerURL = "101.35.234.160:10010";
|
||||
|
||||
var v = NetTunnelLib.WireGuardCreateClientConfig(cli);
|
||||
|
||||
Console.WriteLine("Create WireGuard Client Configure File: {0}", v);
|
||||
}
|
||||
|
||||
private void button1_Click(object sender, EventArgs e)
|
||||
{
|
||||
var buffer = new StringBuilder(256 + 1);
|
||||
//NetTunnelLib.TunnelSDKInitEnv();
|
||||
var v = NetTunnelLib.FindWireguardExe(buffer, 256);
|
||||
MessageBox.Show(buffer.ToString() + "----" + v.ToString());
|
||||
}
|
||||
|
||||
private void button2_Click(object sender, EventArgs e)
|
||||
{
|
||||
test_create_wg_svr_cfg();
|
||||
test_create_wg_cli_cfg();
|
||||
}
|
||||
}
|
|
@ -20,28 +20,73 @@ public enum LogLevel
|
|||
LOG_INFO,
|
||||
LOG_WARN,
|
||||
LOG_ERROR,
|
||||
LOG_CRITICAL
|
||||
LOG_CRITICAL,
|
||||
LOG_OFF
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
||||
public struct NetCard
|
||||
{
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
|
||||
public string Name;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 132)]
|
||||
public string Description;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
|
||||
public string IpAddr;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
|
||||
public string NetMask;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
|
||||
public string MacAddr;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
||||
public struct WgSvrConfig
|
||||
{
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
|
||||
public string Name;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
|
||||
public string Address;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
|
||||
public string PrivateKey;
|
||||
|
||||
public int ListenPort;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
|
||||
public string CliPubKey;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
||||
public string AllowNet;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
||||
public struct WgCliConfig
|
||||
{
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
|
||||
public string Name;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
|
||||
public string PrivateKey;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
|
||||
public string Address;
|
||||
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
|
||||
public string SvrPubKey;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
||||
public string AllowNet;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
|
||||
public string ServerURL;
|
||||
}
|
||||
|
||||
public class NetTunnelLib
|
||||
{
|
||||
[DllImport("NetTunnelSDK.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
|
@ -78,6 +123,12 @@ public class NetTunnelLib
|
|||
[DllImport("NetTunnelSDK.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
public static extern int GetAllNICInfo(IntPtr netCard, ref int size);
|
||||
|
||||
[DllImport("NetTunnelSDK.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
public static extern int WireGuardCreateServerConfig(WgSvrConfig cfg);
|
||||
|
||||
[DllImport("NetTunnelSDK.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
public static extern int WireGuardCreateClientConfig(WgCliConfig cfg);
|
||||
|
||||
//[DllImport("NetTunnelSDK.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
//public static extern int RunPipeCmd(String pszCmd, StringBuilder pszResultBuffer, int dwResultBufferSize);
|
||||
}
|
|
@ -167,9 +167,6 @@
|
|||
<ClCompile Include="tunnel.cpp">
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
|
||||
</ClCompile>
|
||||
<ClCompile Include="usrerr.cpp">
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
|
||||
</ClCompile>
|
||||
<ClCompile Include="wireguard.cpp">
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Use</PrecompiledHeader>
|
||||
</ClCompile>
|
||||
|
|
|
@ -44,9 +44,6 @@
|
|||
<ClCompile Include="tunnel.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="usrerr.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="wireguard.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
|
|
|
@ -2,19 +2,16 @@
|
|||
#include "pch.h"
|
||||
#include "tunnel.h"
|
||||
|
||||
BOOL APIENTRY DllMain(HMODULE hModule,
|
||||
DWORD ul_reason_for_call,
|
||||
LPVOID lpReserved
|
||||
)
|
||||
{
|
||||
switch (ul_reason_for_call)
|
||||
{
|
||||
case DLL_PROCESS_ATTACH:
|
||||
case DLL_THREAD_ATTACH:
|
||||
case DLL_THREAD_DETACH:
|
||||
case DLL_PROCESS_DETACH:
|
||||
TunnelSDKUnInit();
|
||||
break;
|
||||
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
|
||||
switch (ul_reason_for_call) {
|
||||
case DLL_PROCESS_ATTACH:
|
||||
case DLL_THREAD_ATTACH:
|
||||
case DLL_THREAD_DETACH:
|
||||
case DLL_PROCESS_DETACH:
|
||||
TunnelSDKUnInit();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
|
@ -1,21 +1,36 @@
|
|||
#pragma once
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
TCHAR wireguardPath[MAX_PATH];
|
||||
BOOL wireguardExists;
|
||||
TCHAR wgPath[MAX_PATH];
|
||||
BOOL wgExists;
|
||||
/**
|
||||
* @brief WireGuard 配置项
|
||||
*/
|
||||
typedef struct {
|
||||
TCHAR wireguardPath[MAX_PATH]; ///< wireguard.exe 路径
|
||||
BOOL wireguardExists; ///< wireguard.exe 是否存在
|
||||
TCHAR wgPath[MAX_PATH]; ///< wg.exe 路径
|
||||
BOOL wgExists; ///< wg.exe 是否存在
|
||||
} WIREGUARD_CFG, *PWIREGUARD_CFG;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
TCHAR workDirectory[MAX_PATH];
|
||||
PROTO_CRYPTO_TYPE proCryptoType;
|
||||
TCHAR proKeyBuf[CRYPTO_MAX][256];
|
||||
spdlog::level::level_enum logLevel;
|
||||
WIREGUARD_CFG wireguardCfg;
|
||||
/**
|
||||
* @brief SDK 全局配置项
|
||||
*/
|
||||
typedef struct {
|
||||
TCHAR workDirectory[MAX_PATH]; ///< SDK 当前工作目录
|
||||
PROTO_CRYPTO_TYPE proCryptoType; ///< 协议加密类型
|
||||
TCHAR proKeyBuf[256]; ///< 协议加密秘钥
|
||||
BOOL enableLog; ///< 是否启用日志
|
||||
spdlog::level::level_enum logLevel; ///< 日志等级
|
||||
TCHAR cfgPath[MAX_PATH]; ///< 配置文件路径
|
||||
WIREGUARD_CFG wireguardCfg; ///< wireguard 配置项 @see WIREGUARD_CFG
|
||||
} SDK_CONFIG, *PSDK_CONFIG;
|
||||
|
||||
#ifdef __cplusplus // If used by C++ code,
|
||||
extern "C" {
|
||||
// we need to export the C interface
|
||||
#endif
|
||||
|
||||
PSDK_CONFIG GetGlobalCfgInfo();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
|
@ -5,63 +5,52 @@
|
|||
#include <strsafe.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
|
||||
void RemoveTailLineBreak(TCHAR* pInputStr, int strSize)
|
||||
{
|
||||
void RemoveTailLineBreak(TCHAR *pInputStr, int strSize) {
|
||||
size_t length;
|
||||
if (pInputStr)
|
||||
{
|
||||
if (StringCbLength(pInputStr, strSize, &length) == S_OK && length > 0)
|
||||
{
|
||||
if (pInputStr[length - 2] == '\r' && pInputStr[length - 1] == '\n')
|
||||
{
|
||||
if (pInputStr) {
|
||||
if (StringCbLength(pInputStr, strSize, &length) == S_OK && length > 0) {
|
||||
if (pInputStr[length - 2] == '\r' && pInputStr[length - 1] == '\n') {
|
||||
pInputStr[length - 2] = pInputStr[length - 1] = 0;
|
||||
}
|
||||
else if (pInputStr[length - 1] == '\n')
|
||||
{
|
||||
} else if (pInputStr[length - 1] == '\n') {
|
||||
pInputStr[length - 1] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int RunPipeCmd(TCHAR* pszCmd, TCHAR* pszResultBuffer, int dwResultBufferSize)
|
||||
{
|
||||
BOOL bRet;
|
||||
HANDLE hReadPipe = nullptr;
|
||||
HANDLE hWritePipe = nullptr;
|
||||
STARTUPINFO si;
|
||||
int RunPipeCmd(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize) {
|
||||
BOOL bRet;
|
||||
HANDLE hReadPipe = nullptr;
|
||||
HANDLE hWritePipe = nullptr;
|
||||
STARTUPINFO si;
|
||||
PROCESS_INFORMATION pi;
|
||||
SECURITY_ATTRIBUTES securityAttributes;
|
||||
SECURITY_ATTRIBUTES securityAttributes = {};
|
||||
|
||||
memset(&securityAttributes, 0, sizeof(SECURITY_ATTRIBUTES));
|
||||
memset(&si, 0, sizeof(STARTUPINFO));
|
||||
memset(&pi, 0, sizeof(PROCESS_INFORMATION));
|
||||
|
||||
// 设定管道的安全属性
|
||||
securityAttributes.bInheritHandle = TRUE;
|
||||
securityAttributes.nLength = sizeof(securityAttributes);
|
||||
securityAttributes.bInheritHandle = TRUE;
|
||||
securityAttributes.nLength = sizeof(securityAttributes);
|
||||
securityAttributes.lpSecurityDescriptor = nullptr;
|
||||
|
||||
// 创建匿名管道
|
||||
bRet = ::CreatePipe(&hReadPipe, &hWritePipe, &securityAttributes, 0);
|
||||
if (FALSE == bRet)
|
||||
{
|
||||
if (FALSE == bRet) {
|
||||
SPDLOG_ERROR(TEXT("CreatePipe Error"));
|
||||
return -ERR_SYS_CALL;
|
||||
}
|
||||
|
||||
// 设置新进程参数
|
||||
si.cb = sizeof(si);
|
||||
si.hStdError = hWritePipe;
|
||||
si.hStdOutput = hWritePipe;
|
||||
si.cb = sizeof(si);
|
||||
si.hStdError = hWritePipe;
|
||||
si.hStdOutput = hWritePipe;
|
||||
si.wShowWindow = SW_HIDE;
|
||||
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
|
||||
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
|
||||
|
||||
// 创建新进程执行命令, 将执行结果写入匿名管道中
|
||||
bRet = ::CreateProcess(nullptr, (pszCmd), nullptr, nullptr, TRUE, 0, nullptr, nullptr, &si, &pi);
|
||||
if (FALSE == bRet)
|
||||
{
|
||||
if (FALSE == bRet) {
|
||||
SPDLOG_ERROR(TEXT("CreateProcess Error"));
|
||||
}
|
||||
|
||||
|
|
|
@ -2,10 +2,6 @@
|
|||
|
||||
#include <windows.h>
|
||||
|
||||
#ifndef ARRAY_SIZE
|
||||
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus // If used by C++ code,
|
||||
extern "C" {
|
||||
// we need to export the C interface
|
||||
|
@ -13,6 +9,7 @@ extern "C" {
|
|||
|
||||
void RemoveTailLineBreak(TCHAR* pInputStr, int strSize);
|
||||
int RunPipeCmd(TCHAR* pszCmd, TCHAR* pszResultBuffer, int dwResultBufferSize);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -8,62 +8,83 @@
|
|||
|
||||
#include "globalcfg.h"
|
||||
|
||||
#pragma comment(lib,"Iphlpapi.lib")
|
||||
#pragma comment(lib, "Iphlpapi.lib")
|
||||
|
||||
TUNNEL_API int GetAllNICInfo(PNIC_CONTENT pInfo, int* pItemCounts)
|
||||
{
|
||||
int nRel, id = 0;
|
||||
IP_ADDR_STRING* pIpAddrString;
|
||||
int NetmaskToCIDR(const TCHAR *pNetMask) {
|
||||
const TCHAR *tabCIDR[] = {
|
||||
TEXT("128.0.0.0"), TEXT("192.0.0.0"), TEXT("224.0.0.0"), TEXT("240.0.0.0"),
|
||||
TEXT("248.0.0.0"), TEXT("252.0.0.0"), TEXT("254.0.0.0"), TEXT("255.0.0.0"),
|
||||
TEXT("255.128.0.0"), TEXT("255.192.0.0"), TEXT("255.224.0.0"), TEXT("255.240.0.0"),
|
||||
TEXT("255.248.0.0"), TEXT("255.252.0.0"), TEXT("255.254.0.0"), TEXT("255.255.0.0"),
|
||||
TEXT("255.255.128.0"), TEXT("255.255.192.0"), TEXT("255.255.224.0"), TEXT("255.255.240.0"),
|
||||
TEXT("255.255.248.0"), TEXT("255.255.252.0"), TEXT("255.255.254.0"), TEXT("255.255.255.0"),
|
||||
TEXT("255.255.255.128"), TEXT("255.255.255.192"), TEXT("255.255.255.224"), TEXT("255.255.255.240"),
|
||||
TEXT("255.255.255.248"), TEXT("255.255.255.252"), TEXT("255.255.255.254"), TEXT("255.255.255.255")};
|
||||
|
||||
for (int i = 0; i < static_cast<int>(std::size(tabCIDR)); i++) {
|
||||
if (lstrcmp(tabCIDR[i], pNetMask) == 0) {
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0xFF;
|
||||
}
|
||||
|
||||
int GetAllNICInfo(PNIC_CONTENT pInfo, int *pItemCounts) {
|
||||
int nRel, id = 0;
|
||||
IP_ADDR_STRING *pIpAddrString;
|
||||
PIP_ADAPTER_INFO pIpAdapterInfo;
|
||||
unsigned long stSize;
|
||||
unsigned long stSize;
|
||||
|
||||
if (pItemCounts == nullptr || pInfo == nullptr)
|
||||
{
|
||||
if (pItemCounts == nullptr || pInfo == nullptr) {
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
|
||||
pIpAdapterInfo = new IP_ADAPTER_INFO[NET_CARD_MAX];
|
||||
stSize = sizeof(IP_ADAPTER_INFO) * NET_CARD_MAX;
|
||||
stSize = sizeof(IP_ADAPTER_INFO) * NET_CARD_MAX;
|
||||
|
||||
// WIN32 API get net card information
|
||||
nRel = GetAdaptersInfo(pIpAdapterInfo, &stSize);
|
||||
|
||||
if (ERROR_BUFFER_OVERFLOW == nRel)
|
||||
{
|
||||
if (ERROR_BUFFER_OVERFLOW == nRel) {
|
||||
delete[] pIpAdapterInfo;
|
||||
return -ERR_MALLOC_MEMORY;
|
||||
}
|
||||
|
||||
PIP_ADAPTER_INFO cur = pIpAdapterInfo;
|
||||
|
||||
while (cur)
|
||||
{
|
||||
StringCbCopy(pInfo[id].NetCardName, MAX_PATH, cur->AdapterName);
|
||||
StringCbCopy(pInfo[id].NetCardDescription, MAX_PATH, cur->Description);
|
||||
while (cur) {
|
||||
StringCbCopy(pInfo[id].NetCardName, MAX_ADAPTER_NAME_LENGTH, cur->AdapterName);
|
||||
StringCbCopy(pInfo[id].NetCardDescription, MAX_ADAPTER_DESCRIPTION_LENGTH, cur->Description);
|
||||
|
||||
switch (cur->Type)
|
||||
{
|
||||
case MIB_IF_TYPE_ETHERNET:
|
||||
pIpAddrString = &(cur->IpAddressList);
|
||||
StringCbCopy(pInfo[id].NetCardIpaddr, MAX_PATH, pIpAddrString->IpAddress.String);
|
||||
StringCbCopy(pInfo[id].NetCardNetmask, MAX_PATH, pIpAddrString->IpMask.String);
|
||||
break;
|
||||
case MIB_IF_TYPE_OTHER:
|
||||
case MIB_IF_TYPE_TOKENRING:
|
||||
case MIB_IF_TYPE_FDDI:
|
||||
case MIB_IF_TYPE_PPP:
|
||||
case MIB_IF_TYPE_LOOPBACK:
|
||||
case MIB_IF_TYPE_SLIP:
|
||||
break;
|
||||
default: // WIFI ,Unknown type
|
||||
pIpAddrString = &(cur->IpAddressList);
|
||||
StringCbCopy(pInfo[id].NetCardIpaddr, MAX_PATH, pIpAddrString->IpAddress.String);
|
||||
StringCbCopy(pInfo[id].NetCardNetmask, MAX_PATH, pIpAddrString->IpMask.String);
|
||||
break;
|
||||
switch (cur->Type) {
|
||||
case MIB_IF_TYPE_ETHERNET:
|
||||
pIpAddrString = &(cur->IpAddressList);
|
||||
StringCbCopy(pInfo[id].NetCardIpaddr, 20 - 1, pIpAddrString->IpAddress.String);
|
||||
StringCbCopy(pInfo[id].NetCardNetmask, 20 - 1, pIpAddrString->IpMask.String);
|
||||
break;
|
||||
case MIB_IF_TYPE_OTHER:
|
||||
case MIB_IF_TYPE_TOKENRING:
|
||||
case MIB_IF_TYPE_FDDI:
|
||||
case MIB_IF_TYPE_PPP:
|
||||
case MIB_IF_TYPE_LOOPBACK:
|
||||
case MIB_IF_TYPE_SLIP:
|
||||
break;
|
||||
default: // WIFI ,Unknown type
|
||||
pIpAddrString = &(cur->IpAddressList);
|
||||
StringCbCopy(pInfo[id].NetCardIpaddr, 20 - 1, pIpAddrString->IpAddress.String);
|
||||
StringCbCopy(pInfo[id].NetCardNetmask, 20 - 1, pIpAddrString->IpMask.String);
|
||||
break;
|
||||
}
|
||||
|
||||
StringCbPrintf(pInfo[id].NetCardMacAddr, MAX_PATH, "%02X:%02X:%02X:%02X:%02X:%02X",
|
||||
cur->Address[0], cur->Address[1], cur->Address[2], cur->Address[3], cur->Address[4],
|
||||
StringCbPrintf(pInfo[id].NetCardMacAddr,
|
||||
20 - 1,
|
||||
"%02X:%02X:%02X:%02X:%02X:%02X",
|
||||
cur->Address[0],
|
||||
cur->Address[1],
|
||||
cur->Address[2],
|
||||
cur->Address[3],
|
||||
cur->Address[4],
|
||||
cur->Address[5]);
|
||||
|
||||
id++;
|
||||
|
|
|
@ -5,47 +5,45 @@
|
|||
#include <spdlog/sinks/wincolor_sink.h>
|
||||
#include <spdlog/sinks/rotating_file_sink.h>
|
||||
#include <spdlog/sinks/dup_filter_sink.h>
|
||||
#include <spdlog/sinks/msvc_sink.h>
|
||||
|
||||
#include "usrerr.h"
|
||||
#include "globalcfg.h"
|
||||
#include "misc.h"
|
||||
|
||||
#define CONFIG_FILE_NAME TEXT("tunnelsdk.ini")
|
||||
|
||||
static SDK_CONFIG g_globalConfig;
|
||||
|
||||
PSDK_CONFIG GetGlobalCfgInfo()
|
||||
{
|
||||
PSDK_CONFIG GetGlobalCfgInfo() {
|
||||
return &g_globalConfig;
|
||||
}
|
||||
|
||||
TUNNEL_API int TunnelSDKInitEnv(const TCHAR* pWorkDir)
|
||||
{
|
||||
size_t length;
|
||||
spdlog::level::level_enum lv = g_globalConfig.logLevel;
|
||||
int TunnelSDKInitEnv(const TCHAR *pWorkDir) {
|
||||
size_t length;
|
||||
BOOL logEnable = g_globalConfig.enableLog;
|
||||
spdlog::level::level_enum lv = g_globalConfig.logLevel;
|
||||
|
||||
memset(&g_globalConfig, 0, sizeof(SDK_CONFIG));
|
||||
g_globalConfig.logLevel = lv;
|
||||
g_globalConfig.logLevel = lv;
|
||||
g_globalConfig.enableLog = logEnable;
|
||||
|
||||
if (!logEnable) {
|
||||
spdlog::set_level(spdlog::level::off);
|
||||
}
|
||||
|
||||
if (pWorkDir == nullptr)
|
||||
{
|
||||
if (pWorkDir == nullptr) {
|
||||
// 获取当前文件默认路径
|
||||
GetCurrentDirectory(MAX_PATH, g_globalConfig.workDirectory);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (StringCbLengthA(pWorkDir, MAX_PATH, &length) == S_OK && length == 0)
|
||||
{
|
||||
} else {
|
||||
if (StringCbLengthA(pWorkDir, MAX_PATH, &length) == S_OK && length == 0) {
|
||||
GetCurrentDirectory(MAX_PATH, g_globalConfig.workDirectory);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
StringCbCopy(g_globalConfig.workDirectory, MAX_PATH, pWorkDir);
|
||||
}
|
||||
}
|
||||
|
||||
if (FindWireguardExe(NULL, 0) != ERR_SUCCESS)
|
||||
{
|
||||
StringCbPrintf(g_globalConfig.cfgPath, MAX_PATH, TEXT("%s\\%s"), g_globalConfig.workDirectory, CONFIG_FILE_NAME);
|
||||
|
||||
if (FindWireguardExe(nullptr, 0) != ERR_SUCCESS) {
|
||||
SPDLOG_ERROR(TEXT("WireGuard not found, Please install WireGuard first or set the WireGuard Path."));
|
||||
return -ERR_ITEM_EXISTS;
|
||||
}
|
||||
|
@ -53,54 +51,49 @@ TUNNEL_API int TunnelSDKInitEnv(const TCHAR* pWorkDir)
|
|||
return ERR_SUCCESS;
|
||||
}
|
||||
|
||||
TUNNEL_API void TunnelSDKUnInit()
|
||||
{
|
||||
void TunnelSDKUnInit() {
|
||||
}
|
||||
|
||||
static spdlog::level::level_enum logLevelToSpdlogLevel(LOG_LEVEL level)
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
case LOG_TRACE:
|
||||
return spdlog::level::level_enum::trace;
|
||||
case LOG_DEBUG:
|
||||
return spdlog::level::level_enum::debug;
|
||||
case LOG_INFO:
|
||||
return spdlog::level::level_enum::info;
|
||||
case LOG_WARN:
|
||||
return spdlog::level::level_enum::warn;
|
||||
case LOG_ERROR:
|
||||
return spdlog::level::level_enum::err;
|
||||
case LOG_CRITICAL:
|
||||
return spdlog::level::level_enum::critical;
|
||||
default:
|
||||
return spdlog::level::level_enum::info;
|
||||
static spdlog::level::level_enum logLevelToSpdlogLevel(LOG_LEVEL level) {
|
||||
switch (level) {
|
||||
case LOG_TRACE:
|
||||
return spdlog::level::level_enum::trace;
|
||||
case LOG_DEBUG:
|
||||
return spdlog::level::level_enum::debug;
|
||||
case LOG_INFO:
|
||||
return spdlog::level::level_enum::info;
|
||||
case LOG_WARN:
|
||||
return spdlog::level::level_enum::warn;
|
||||
case LOG_ERROR:
|
||||
return spdlog::level::level_enum::err;
|
||||
case LOG_CRITICAL:
|
||||
return spdlog::level::level_enum::critical;
|
||||
case LOG_OFF:
|
||||
return spdlog::level::level_enum::off;
|
||||
default:
|
||||
return spdlog::level::level_enum::info;
|
||||
}
|
||||
}
|
||||
|
||||
TUNNEL_API void InitTunnelSDKLog(const TCHAR* pLogFile, LOG_LEVEL level)
|
||||
{
|
||||
void InitTunnelSDKLog(const TCHAR *pLogFile, LOG_LEVEL level) {
|
||||
TCHAR buf[MAX_PATH] = {0};
|
||||
|
||||
//::MessageBoxA(NULL, pLogFile, NULL, MB_OK);
|
||||
if (pLogFile && strlen(pLogFile) > 0)
|
||||
{
|
||||
if (pLogFile && strlen(pLogFile) > 0) {
|
||||
StringCbCopy(buf, MAX_PATH, pLogFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
StringCbCopy(buf, MAX_PATH, TEXT("tunnelsdk.log"));
|
||||
}
|
||||
|
||||
g_globalConfig.logLevel = logLevelToSpdlogLevel(level);
|
||||
g_globalConfig.enableLog = TRUE;
|
||||
g_globalConfig.logLevel = logLevelToSpdlogLevel(level);
|
||||
|
||||
auto dupFileFilter = std::make_shared<spdlog::sinks::dup_filter_sink_st>(std::chrono::seconds(5));
|
||||
auto dupStdFilter = std::make_shared<spdlog::sinks::dup_filter_sink_st>(std::chrono::seconds(5));
|
||||
auto dupStdFilter = std::make_shared<spdlog::sinks::dup_filter_sink_st>(std::chrono::seconds(5));
|
||||
|
||||
dupFileFilter->add_sink(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(buf, 1024 * 1024 * 5, 10));
|
||||
dupStdFilter->add_sink(std::make_shared<spdlog::sinks::wincolor_stdout_sink_mt>());
|
||||
|
||||
std::vector<spdlog::sink_ptr> sinks{dupStdFilter, dupFileFilter};
|
||||
std::vector<spdlog::sink_ptr> sinks {dupStdFilter, dupFileFilter};
|
||||
auto logger = std::make_shared<spdlog::logger>(TEXT("tunnelSDK"), sinks.begin(), sinks.end());
|
||||
spdlog::set_default_logger(logger);
|
||||
|
||||
|
@ -120,19 +113,15 @@ TUNNEL_API void InitTunnelSDKLog(const TCHAR* pLogFile, LOG_LEVEL level)
|
|||
SPDLOG_INFO(TEXT("Log({1}): {0}"), buf, static_cast<int>(level));
|
||||
}
|
||||
|
||||
TUNNEL_API void TunnelLogEnable(bool enLog)
|
||||
{
|
||||
if (enLog)
|
||||
{
|
||||
void TunnelLogEnable(bool enLog) {
|
||||
if (enLog) {
|
||||
spdlog::set_level(g_globalConfig.logLevel);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
spdlog::set_level(spdlog::level::level_enum::off);
|
||||
}
|
||||
}
|
||||
|
||||
TUNNEL_API int SetProtocolEncryptType(PROTO_CRYPTO_TYPE type, const TCHAR* pProKey)
|
||||
int SetProtocolEncryptType(const PROTO_CRYPTO_TYPE type, const TCHAR* pProKey)
|
||||
{
|
||||
if (type > CRYPTO_BASE64 && type < CRYPTO_MAX)
|
||||
{
|
||||
|
@ -143,26 +132,10 @@ TUNNEL_API int SetProtocolEncryptType(PROTO_CRYPTO_TYPE type, const TCHAR* pProK
|
|||
}
|
||||
|
||||
g_globalConfig.proCryptoType = type;
|
||||
StringCbCopy(g_globalConfig.proKeyBuf[type], 256, pProKey);
|
||||
StringCbCopy(g_globalConfig.proKeyBuf, 256, pProKey);
|
||||
|
||||
SPDLOG_DEBUG(TEXT("Protocol crypto type: {0} with key [{1}]"), static_cast<int>(type),
|
||||
pProKey ? pProKey : TEXT(""));
|
||||
|
||||
return ERR_SUCCESS;
|
||||
}
|
||||
|
||||
TUNNEL_API int CreateTunnel(LPCSTR lpszMsg)
|
||||
{
|
||||
OutputDebugStringA(lpszMsg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
TUNNEL_API const TCHAR* TestMessage()
|
||||
{
|
||||
return TEXT("Test Message");
|
||||
}
|
||||
|
||||
TUNNEL_API int Add(int a, int b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
|
|
@ -8,58 +8,202 @@
|
|||
#define TUNNEL_API __declspec(dllimport)
|
||||
#endif
|
||||
|
||||
#define WG_KEY_MAX (64)
|
||||
#define NET_CARD_MAX (32)
|
||||
/**
|
||||
* @brief WireGuard key 最大长度
|
||||
*/
|
||||
constexpr auto WG_KEY_MAX = (64);
|
||||
|
||||
typedef enum
|
||||
{
|
||||
CRYPTO_NONE = 0,
|
||||
CRYPTO_BASE64 = 1,
|
||||
CRYPTO_AES128 = 2,
|
||||
CRYPTO_3DES = 3,
|
||||
CRYPTO_AES256 = 4,
|
||||
/**
|
||||
* @brief 操作系统最大网卡数
|
||||
*/
|
||||
#define NET_CARD_MAX (32)
|
||||
|
||||
/**
|
||||
* @brief 协议加密类型
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
CRYPTO_NONE = 0, ///< 不加密
|
||||
CRYPTO_BASE64 = 1, ///< BASE64 字符串编码
|
||||
CRYPTO_AES128 = 2, ///< AES 128位秘钥 加密
|
||||
CRYPTO_3DES = 3, ///< 3DES 加密
|
||||
CRYPTO_AES256 = 4, ///< AES 256 位秘钥加密
|
||||
CRYPTO_MAX,
|
||||
} PROTO_CRYPTO_TYPE;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
LOG_TRACE = 0,
|
||||
LOG_DEBUG,
|
||||
LOG_INFO,
|
||||
LOG_WARN,
|
||||
LOG_ERROR,
|
||||
LOG_CRITICAL,
|
||||
LOG_MAX
|
||||
} LOG_LEVEL;
|
||||
/**
|
||||
* @brief 日志等级
|
||||
*
|
||||
*/
|
||||
enum LOG_LEVEL {
|
||||
LOG_TRACE = 0, ///< TRACE 日志等级
|
||||
LOG_DEBUG, ///< DEBUG 日志等级
|
||||
LOG_INFO, ///< INFO 日志等级
|
||||
LOG_WARN, ///< WARN 日志等级
|
||||
LOG_ERROR, ///< ERROR 日志等级
|
||||
LOG_CRITICAL, ///< CRITICAL 日志等级
|
||||
LOG_OFF ///< 关闭日志
|
||||
};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
TCHAR NetCardName[256];
|
||||
TCHAR NetCardDescription[256];
|
||||
TCHAR NetCardIpaddr[256];
|
||||
TCHAR NetCardNetmask[256];
|
||||
TCHAR NetCardMacAddr[256];
|
||||
/**
|
||||
*
|
||||
* @brief 本地计算机网卡信息
|
||||
*/
|
||||
typedef struct {
|
||||
TCHAR NetCardName[260]; ///< 网卡名称, Windows标识为 UUID
|
||||
TCHAR NetCardDescription[132]; ///< 网卡描述
|
||||
TCHAR NetCardIpaddr[20]; ///< 网卡 IP 地址
|
||||
TCHAR NetCardNetmask[20]; ///< 网卡子网掩码
|
||||
TCHAR NetCardMacAddr[20]; ///< 网卡 MAC 地址
|
||||
} NIC_CONTENT, *PNIC_CONTENT;
|
||||
|
||||
/**
|
||||
* @brief WireGuard 服务端配置信息
|
||||
*/
|
||||
typedef struct {
|
||||
TCHAR Name[64]; ///< WireGuard 网卡名称
|
||||
TCHAR Address[32]; ///< WireGuard 本地网络IP地址
|
||||
TCHAR PrivateKey[64]; ///< WireGuard 本机私钥
|
||||
int ListenPort; ///< WireGuard 服务端监听端口
|
||||
|
||||
// 根据系统设计,不支持多个客户端同时连接
|
||||
TCHAR CliPubKey[64]; ///< WireGuard 客户端公钥
|
||||
TCHAR AllowNet[256]; ///< WireGuard 允许对端访问本地网络的配置
|
||||
} WGSERVER_CONFIG, *PWGSERVER_CONFIG;
|
||||
|
||||
/**
|
||||
* @brief WireGuard 客户端配置信息
|
||||
*/
|
||||
typedef struct {
|
||||
TCHAR Name[64]; ///< WireGuard 网卡名称
|
||||
TCHAR PrivateKey[64]; ///< WireGuard 本机私钥
|
||||
TCHAR Address[32]; ///< WireGuard 本地网络IP地址
|
||||
|
||||
// Peer Server
|
||||
TCHAR SvrPubKey[64]; ///< WireGuard 服务端公钥
|
||||
TCHAR AllowNet[256]; ///< WireGuard 允许对端访问本地网络的配置
|
||||
TCHAR ServerURL[256]; ///< WireGuard 服务端IP地址和端口
|
||||
} WGCLIENT_CONFIG, *PWGCLIENT_CONFIG;
|
||||
|
||||
#ifdef __cplusplus // If used by C++ code,
|
||||
extern "C" {
|
||||
// we need to export the C interface
|
||||
#endif
|
||||
|
||||
TUNNEL_API int __cdecl CreateTunnel(LPCSTR lpszMsg);
|
||||
TUNNEL_API const TCHAR* __cdecl TestMessage();
|
||||
TUNNEL_API int __cdecl Add(int a, int b);
|
||||
TUNNEL_API int __cdecl TunnelSDKInitEnv(const TCHAR* pWorkDir);
|
||||
TUNNEL_API int __cdecl SetProtocolEncryptType(PROTO_CRYPTO_TYPE type, const TCHAR* pProKey);
|
||||
TUNNEL_API void __cdecl InitTunnelSDKLog(const TCHAR* pLogFile, LOG_LEVEL level);
|
||||
TUNNEL_API void __cdecl TunnelLogEnable(bool enLog);
|
||||
TUNNEL_API int __cdecl FindWireguardExe(TCHAR* pFullPath, int maxSize);
|
||||
TUNNEL_API int __cdecl SetWireguardPath(TCHAR* pPath);
|
||||
TUNNEL_API void __cdecl TunnelSDKUnInit();
|
||||
TUNNEL_API int __cdecl GenerateWireguardKeyPairs(TCHAR* pPubKey, int pubkeySize, TCHAR* pPrivKey, int privKeySize);
|
||||
TUNNEL_API int __cdecl GetAllNICInfo(PNIC_CONTENT pInfo, int* pItemCounts);
|
||||
/**
|
||||
* @brief 初始化 SDK 运行环境
|
||||
* @param[in] pWorkDir 程序工作路径,如果不设置系统自动获取
|
||||
* @return 函数执行结果 0: 成功, 小于0 失败 @see USER_ERRNO
|
||||
* - -ERR_ITEM_EXISTS 未找到 WireGuard 程序
|
||||
* - ERR_SUCCESS 成功
|
||||
*/
|
||||
TUNNEL_API int __cdecl TunnelSDKInitEnv(const TCHAR *pWorkDir);
|
||||
|
||||
/**
|
||||
* @brief 设置传输协议加密方式,默认 CRYPTO_NONE
|
||||
* @param[in] type 协议加密类型 @see PROTO_CRYPTO_TYPE
|
||||
* @param[in] pProKey 加密秘钥,CRYPTO_NONE, CRYPTO_BASE64 无效忽略
|
||||
* @return 函数执行结果 0: 成功, 小于0 失败 @see USER_ERRNO
|
||||
* - -ERR_INPUT_PARAMS 输入参数错误
|
||||
* - ERR_SUCCESS 成功
|
||||
*/
|
||||
TUNNEL_API int __cdecl SetProtocolEncryptType(const PROTO_CRYPTO_TYPE type, const TCHAR *pProKey);
|
||||
|
||||
/**
|
||||
* @brief 初始化 SDK 日志
|
||||
* @param[in] pLogFile 日志文件名称/完整路径
|
||||
* @param[in] level 日志最低有效等级
|
||||
*/
|
||||
TUNNEL_API void __cdecl InitTunnelSDKLog(const TCHAR *pLogFile, LOG_LEVEL level);
|
||||
|
||||
/**
|
||||
* @brief 打开/关闭 SDK 日志开关
|
||||
* @param enLog 日志开关
|
||||
* - TRUE 打开日志
|
||||
* - FALSE 关闭日志
|
||||
*/
|
||||
TUNNEL_API void __cdecl TunnelLogEnable(bool enLog);
|
||||
|
||||
/**
|
||||
* @brief 查找 WireGuard 运行环境
|
||||
* @param[out] pFullPath wireguard.exe 程序路径
|
||||
* @param[in] maxSize pFullPath 缓冲区最大字节数
|
||||
* @return 函数执行结果 0: 成功, 小于0 失败 @see USER_ERRNO
|
||||
* - -ERR_INPUT_PARAMS 输入参数错误
|
||||
* - -ERR_MALLOC_MEMORY 分配内存失败
|
||||
* - -ERR_FILE_NOT_EXISTS 文件不存在
|
||||
* - ERR_SUCCESS 成功
|
||||
*/
|
||||
TUNNEL_API int __cdecl FindWireguardExe(TCHAR *pFullPath, int maxSize);
|
||||
|
||||
/**
|
||||
* @brief 设置 wireguard.exe 程序路径
|
||||
* @param[in] pPath wireguard.exe 程序路径
|
||||
* @return 函数执行结果 0: 成功, 小于0 失败 @see USER_ERRNO
|
||||
* - -ERR_INPUT_PARAMS 输入参数错误
|
||||
* - -ERR_ITEM_UNEXISTS 文件不存在
|
||||
* - ERR_SUCCESS 成功
|
||||
*/
|
||||
TUNNEL_API int __cdecl SetWireguardPath(const TCHAR *pPath);
|
||||
|
||||
/**
|
||||
* @brief 创建 WireGuard 密钥对
|
||||
* @param[out] pPubKey 公钥缓冲区
|
||||
* @param[in] pubkeySize 公钥缓冲区大小(字节数)
|
||||
* @param[out] pPrivKey 私钥缓冲区
|
||||
* @param[in] privKeySize 私钥缓冲区大小(字节数)
|
||||
* @return 函数执行结果 0: 成功, 小于0 失败 @see USER_ERRNO
|
||||
* - -ERR_ITEM_UNEXISTS WireGuard 未配置或未安装
|
||||
* - -ERR_CALL_SHELL 调用操作系统命令行工具失败
|
||||
* - ERR_SUCCESS 成功
|
||||
*/
|
||||
TUNNEL_API int __cdecl GenerateWireguardKeyPairs(TCHAR *pPubKey, int pubkeySize, TCHAR *pPrivKey, int privKeySize);
|
||||
|
||||
/**
|
||||
* @brief 创建 WireGuard 服务端配置文件
|
||||
* @param pWgConfig 配置文件相关配置项 @see WGSERVER_CONFIG
|
||||
* @return 函数执行结果 0: 成功, 小于0 失败 @see USER_ERRNO
|
||||
* - -ERR_INPUT_PARAMS 输入参数错误
|
||||
* - -ERR_MALLOC_MEMORY 分配内存失败
|
||||
* - -ERR_OPEN_FILE 打开文件失败
|
||||
* - -ERR_MEMORY_STR 字符串处理失败
|
||||
* - ERR_SUCCESS 成功
|
||||
*/
|
||||
TUNNEL_API int __cdecl WireGuardCreateServerConfig(const PWGSERVER_CONFIG pWgConfig);
|
||||
|
||||
/**
|
||||
* @brief 创建 WireGuard 客户端配置文件
|
||||
* @param pWgConfig 配置文件相关配置项 @see WGCLIENT_CONFIG
|
||||
* @return 函数执行结果 0: 成功, 小于0 失败 @see USER_ERRNO
|
||||
* - -ERR_INPUT_PARAMS 输入参数错误
|
||||
* - -ERR_MALLOC_MEMORY 分配内存失败
|
||||
* - -ERR_OPEN_FILE 打开文件失败
|
||||
* - -ERR_MEMORY_STR 字符串处理失败
|
||||
* - ERR_SUCCESS 成功
|
||||
*/
|
||||
TUNNEL_API int __cdecl WireGuardCreateClientConfig(const PWGCLIENT_CONFIG pWgConfig);
|
||||
|
||||
/**
|
||||
* @brief 清理 SDK 运行资源
|
||||
*/
|
||||
TUNNEL_API void __cdecl TunnelSDKUnInit();
|
||||
/**
|
||||
* @brief 获取本机网卡信息
|
||||
* @param[in,out] pInfo 网卡信息 @see NIC_CONTENT
|
||||
* @param[out] pItemCounts 计算机当前操作系统中网卡总数 最大值 32
|
||||
* @return 函数执行结果 0: 成功, 小于0 失败 @see USER_ERRNO
|
||||
* - -ERR_INPUT_PARAMS 输入参数错误
|
||||
* - -ERR_MALLOC_MEMORY 分配内存失败
|
||||
* - ERR_SUCCESS 成功
|
||||
*/
|
||||
TUNNEL_API int __cdecl GetAllNICInfo(PNIC_CONTENT pInfo, int *pItemCounts);
|
||||
|
||||
/**
|
||||
* @brief IPv4 子网掩码转 CIDR 掩码
|
||||
* @param pNetMask IPv4 子网掩码字符串
|
||||
* @return IPv4 CIDR 掩码
|
||||
*/
|
||||
TUNNEL_API int __cdecl NetmaskToCIDR(const TCHAR *pNetMask);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
|
@ -1,33 +0,0 @@
|
|||
#include "usrerr.h"
|
||||
#include "misc.h"
|
||||
|
||||
#define MAX_DESC_LENGTH (256)
|
||||
#define GENERATE_STRING(STRING,x, desc) {#STRING, desc},
|
||||
|
||||
|
||||
static const char* g_enumStrVal[][MAX_DESC_LENGTH] = {
|
||||
DEF_ERR_CODE(GENERATE_STRING) {"ERR_UNKNOWN", "未知错误"},
|
||||
};
|
||||
|
||||
const char* getErrorEnumNameString(int errCode)
|
||||
{
|
||||
if (errCode < 0)
|
||||
{
|
||||
errCode = -errCode;
|
||||
}
|
||||
|
||||
if (errCode >= ARRAY_SIZE(g_enumStrVal) || errCode < 0)
|
||||
{
|
||||
return g_enumStrVal[ARRAY_SIZE(g_enumStrVal) - 1][0];
|
||||
}
|
||||
return g_enumStrVal[errCode][0];
|
||||
}
|
||||
|
||||
const char* getErrorEnumDesc(int errCode)
|
||||
{
|
||||
if (errCode >= ARRAY_SIZE(g_enumStrVal) || errCode < 0)
|
||||
{
|
||||
return g_enumStrVal[ARRAY_SIZE(g_enumStrVal) - 1][0];
|
||||
}
|
||||
return g_enumStrVal[errCode][1];
|
||||
}
|
|
@ -1,28 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
|
||||
#define DEF_ERR_CODE(ERR_CODE) \
|
||||
ERR_CODE(ERR_SUCCESS, 0, "成功") \
|
||||
ERR_CODE(ERR_INPUT_PARAMS, 1, "输入参数错误") \
|
||||
ERR_CODE(ERR_UN_SUPPORT, 2, "不支持的操作") \
|
||||
ERR_CODE(ERR_CALL_SHELL, 3, "调用Shell命令失败") \
|
||||
ERR_CODE(ERR_ITEM_EXISTS, 4, "该内容已经存在") \
|
||||
ERR_CODE(ERR_ITEM_UNEXISTS, 5, "该内容不存在") \
|
||||
ERR_CODE(ERR_SYS_INIT, 6, "系统中断") \
|
||||
ERR_CODE(ERR_SYS_CALL, 7, "系统调用") \
|
||||
ERR_CODE(ERR_OPEN_FILE, 8, "打开文件失败") \
|
||||
ERR_CODE(ERR_READ_FILE, 9, "读取文件失败") \
|
||||
ERR_CODE(ERR_FILE_NOT_EXISTS, 10, "文件不存在") \
|
||||
ERR_CODE(ERR_FILE_LOCKED, 11, "文件被锁定") \
|
||||
ERR_CODE(ERR_GET_FILE_SIZE, 12, "获取文件大小失败") \
|
||||
ERR_CODE(ERR_COPY_FILE, 13, "复制文件失败") \
|
||||
ERR_CODE(ERR_MALLOC_MEMORY, 14, "分配内存失败") \
|
||||
ERR_CODE(ERR_MMAP_MEMORY, 15, "共享内存失败")
|
||||
|
||||
|
||||
#define GENERATE_ENUM(ENUM, n, x) ENUM,
|
||||
|
||||
typedef enum
|
||||
{
|
||||
DEF_ERR_CODE(GENERATE_ENUM)
|
||||
} USER_ERRNO;
|
||||
/**
|
||||
* @brief SDK 常用错误码
|
||||
*/
|
||||
enum USER_ERRNO {
|
||||
ERR_SUCCESS, ///< 成功
|
||||
ERR_INPUT_PARAMS, ///< 输入参数错误
|
||||
ERR_UN_SUPPORT, ///< 不支持的操作
|
||||
ERR_CALL_SHELL, ///< 调用Shell命令失败
|
||||
ERR_ITEM_EXISTS, ///< 该内容已经存在
|
||||
ERR_ITEM_UNEXISTS, ///< 该内容不存在
|
||||
ERR_SYS_INIT, ///< 系统中断
|
||||
ERR_SYS_CALL, ///< 系统调用
|
||||
ERR_OPEN_FILE, ///< 打开文件失败
|
||||
ERR_READ_FILE, ///< 读取文件失败
|
||||
ERR_WRITE_FILE, ///< 写入文件失败
|
||||
ERR_FILE_NOT_EXISTS, ///< 文件不存在
|
||||
ERR_FILE_LOCKED, ///< 文件被锁定
|
||||
ERR_GET_FILE_SIZE, ///< 获取文件大小失败
|
||||
ERR_COPY_FILE, ///< 复制文件失败
|
||||
ERR_MALLOC_MEMORY, ///< 分配内存失败
|
||||
ERR_MMAP_MEMORY, ///< 共享内存失败
|
||||
ERR_MEMORY_STR ///< 字符串操作失败
|
||||
};
|
|
@ -7,23 +7,246 @@
|
|||
#include "globalcfg.h"
|
||||
#include "misc.h"
|
||||
|
||||
#define WINENVBUF_SIZE (4096)
|
||||
#define CONFIG_FILE_NAME TEXT("tunnelsdk.ini")
|
||||
#define CFG_WIREGUARD_SECTION TEXT("WireGuard")
|
||||
#define CFG_WIREGUARD_PATH TEXT("WireGuardExe")
|
||||
#define CFG_WG_PATH TEXT("WgExe")
|
||||
constexpr auto WINENVBUF_SIZE = (4096);
|
||||
|
||||
#define CFG_WIREGUARD_SECTION TEXT("WireGuard")
|
||||
#define CFG_WIREGUARD_PATH TEXT("WireGuardExe")
|
||||
#define CFG_WGCLI_PATH TEXT("ClientConfig")
|
||||
#define CFG_WGSVR_PATH TEXT("ServerConfig")
|
||||
#define CFG_WG_PATH TEXT("WgExe")
|
||||
|
||||
TUNNEL_API int GenerateWireguardKeyPairs(TCHAR* pPubKey, int pubkeySize, TCHAR* pPrivKey, int privKeySize)
|
||||
{
|
||||
int ret;
|
||||
TCHAR cmdBuffer[MAX_PATH];
|
||||
TCHAR cmdResult[MAX_PATH];
|
||||
int WireGuardCreateClientConfig(const PWGCLIENT_CONFIG pWgConfig) {
|
||||
const size_t bufSize = 4096 * sizeof(TCHAR);
|
||||
const TCHAR cfgFormat[] = TEXT(
|
||||
"[Interface]\nPrivateKey = %s\nAddress = %s\n\n[Peer]\nPublicKey = %s\nAllowedIPs = %s\nEndpoint = "
|
||||
"%s\nPersistentKeepalive = 30\n");
|
||||
TCHAR cfgPath[MAX_PATH];
|
||||
size_t length;
|
||||
HANDLE hFile;
|
||||
TCHAR *pBuf;
|
||||
|
||||
#pragma region
|
||||
if (pWgConfig == nullptr) {
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
|
||||
if (FAILED(StringCbLength(pWgConfig->Name, 64, &length)) || 0 == length) {
|
||||
SPDLOG_ERROR("WireGuard Name error: {0}", pWgConfig->Name);
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
|
||||
if (FAILED(StringCbLength(pWgConfig->Address, 32, &length)) || 0 == length) {
|
||||
SPDLOG_ERROR("WireGuard Address error: {0}", pWgConfig->Address);
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
|
||||
if (FAILED(StringCbLength(pWgConfig->PrivateKey, 64, &length)) || 0 == length) {
|
||||
SPDLOG_ERROR("WireGuard Private key error: {0}", pWgConfig->PrivateKey);
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
|
||||
if (FAILED(StringCbLength(pWgConfig->SvrPubKey, 64, &length)) || 0 == length) {
|
||||
SPDLOG_ERROR("WireGuard Server Public key error: {0}", pWgConfig->SvrPubKey);
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
|
||||
if (FAILED(StringCbLength(pWgConfig->AllowNet, 256, &length)) || 0 == length) {
|
||||
SPDLOG_ERROR("WireGuard Allow Client Network error: {0}", pWgConfig->AllowNet);
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
|
||||
if (FAILED(StringCbLength(pWgConfig->ServerURL, 256, &length)) || 0 == length) {
|
||||
SPDLOG_ERROR("WireGuard Server Network error: {0}", pWgConfig->ServerURL);
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
#pragma endregion 参数检查
|
||||
|
||||
pBuf = static_cast<TCHAR *>(malloc(bufSize));
|
||||
|
||||
if (pBuf == nullptr) {
|
||||
SPDLOG_ERROR("Malloc {1} bytes memory error: {0}", GetLastError(), bufSize);
|
||||
return -ERR_MALLOC_MEMORY;
|
||||
}
|
||||
|
||||
memset(pBuf, 0, bufSize);
|
||||
|
||||
StringCbPrintf(cfgPath, MAX_PATH, "%s\\%s.conf", GetGlobalCfgInfo()->workDirectory, pWgConfig->Name);
|
||||
|
||||
hFile = CreateFile(cfgPath, // name of the write
|
||||
GENERIC_WRITE | GENERIC_READ, // open for writing
|
||||
FILE_SHARE_READ, // do not share
|
||||
nullptr, // default security
|
||||
CREATE_ALWAYS, // create new file only
|
||||
FILE_ATTRIBUTE_NORMAL, // normal file
|
||||
nullptr); // no attr. template
|
||||
|
||||
if (hFile == INVALID_HANDLE_VALUE) {
|
||||
SPDLOG_ERROR("CreatFile [{0}] error: {1}", cfgPath, GetLastError());
|
||||
free(pBuf);
|
||||
return -ERR_OPEN_FILE;
|
||||
}
|
||||
|
||||
// 保存到配置文件中
|
||||
WritePrivateProfileString(CFG_WIREGUARD_SECTION, CFG_WGCLI_PATH, cfgPath, GetGlobalCfgInfo()->cfgPath);
|
||||
|
||||
if (FAILED(StringCbPrintf(pBuf,
|
||||
bufSize,
|
||||
cfgFormat,
|
||||
pWgConfig->PrivateKey,
|
||||
pWgConfig->Address,
|
||||
pWgConfig->SvrPubKey,
|
||||
pWgConfig->AllowNet,
|
||||
pWgConfig->ServerURL))) {
|
||||
SPDLOG_ERROR("Format string error: {0}", GetLastError());
|
||||
free(pBuf);
|
||||
::CloseHandle(hFile);
|
||||
return -ERR_MEMORY_STR;
|
||||
}
|
||||
|
||||
if (FAILED(StringCbLength(pBuf, bufSize, &length))) {
|
||||
SPDLOG_ERROR("Get string \'{0}\' length error: {1}", pBuf, GetLastError());
|
||||
free(pBuf);
|
||||
::CloseHandle(hFile);
|
||||
return -ERR_MEMORY_STR;
|
||||
}
|
||||
|
||||
SPDLOG_DEBUG("WG Client Configure:\n{0}", pBuf);
|
||||
|
||||
if (!WriteFile(hFile, // open file handle
|
||||
pBuf, // start of data to write
|
||||
static_cast<DWORD>(length), // number of bytes to write
|
||||
nullptr, // number of bytes that were written
|
||||
nullptr)) {
|
||||
SPDLOG_ERROR("WriteFile [{0}] error: {1}", cfgPath, GetLastError());
|
||||
free(pBuf);
|
||||
::CloseHandle(hFile);
|
||||
return -ERR_OPEN_FILE;
|
||||
}
|
||||
|
||||
::CloseHandle(hFile);
|
||||
return ERR_SUCCESS;
|
||||
}
|
||||
|
||||
int WireGuardCreateServerConfig(const PWGSERVER_CONFIG pWgConfig) {
|
||||
const size_t bufSize = 4096 * sizeof(TCHAR);
|
||||
const TCHAR cfgFormat[] = TEXT(
|
||||
"[Interface]\nAddress = %s\nListenPort = %d\nPrivateKey = %s\n\n[Peer]\nPublicKey = %s\nAllowedIPs = %s\n");
|
||||
TCHAR cfgPath[MAX_PATH];
|
||||
size_t length;
|
||||
HANDLE hFile;
|
||||
TCHAR *pBuf;
|
||||
|
||||
#pragma region
|
||||
if (pWgConfig == nullptr) {
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
|
||||
if (FAILED(StringCbLength(pWgConfig->Name, 64, &length)) || 0 == length) {
|
||||
SPDLOG_ERROR("WireGuard Name error: {0}", pWgConfig->Name);
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
|
||||
if (FAILED(StringCbLength(pWgConfig->Address, 32, &length)) || 0 == length) {
|
||||
SPDLOG_ERROR("WireGuard Address error: {0}", pWgConfig->Address);
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
|
||||
if (FAILED(StringCbLength(pWgConfig->PrivateKey, 64, &length)) || 0 == length) {
|
||||
SPDLOG_ERROR("WireGuard Private key error: {0}", pWgConfig->PrivateKey);
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
|
||||
if (pWgConfig->ListenPort <= 1024 || pWgConfig->ListenPort >= 65535) {
|
||||
SPDLOG_ERROR("WireGuard Listen port error: {0}, should be in arrange (1024, 65535)", pWgConfig->ListenPort);
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
|
||||
if (FAILED(StringCbLength(pWgConfig->CliPubKey, 64, &length)) || 0 == length) {
|
||||
SPDLOG_ERROR("WireGuard Client Public key error: {0}", pWgConfig->CliPubKey);
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
|
||||
if (FAILED(StringCbLength(pWgConfig->AllowNet, 256, &length)) || 0 == length) {
|
||||
SPDLOG_ERROR("WireGuard Allow Client Network error: {0}", pWgConfig->AllowNet);
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
#pragma endregion 参数检查
|
||||
|
||||
pBuf = static_cast<TCHAR *>(malloc(bufSize));
|
||||
|
||||
if (pBuf == nullptr) {
|
||||
SPDLOG_ERROR("Malloc {1} bytes memory error: {0}", GetLastError(), bufSize);
|
||||
return -ERR_MALLOC_MEMORY;
|
||||
}
|
||||
|
||||
memset(pBuf, 0, bufSize);
|
||||
|
||||
StringCbPrintf(cfgPath, MAX_PATH, "%s\\%s.conf", GetGlobalCfgInfo()->workDirectory, pWgConfig->Name);
|
||||
|
||||
hFile = CreateFile(cfgPath, // name of the write
|
||||
GENERIC_WRITE | GENERIC_READ, // open for writing
|
||||
FILE_SHARE_READ, // do not share
|
||||
nullptr, // default security
|
||||
CREATE_ALWAYS, // create new file only
|
||||
FILE_ATTRIBUTE_NORMAL, // normal file
|
||||
nullptr); // no attr. template
|
||||
|
||||
if (hFile == INVALID_HANDLE_VALUE) {
|
||||
SPDLOG_ERROR("CreatFile [{0}] error: {1}", cfgPath, GetLastError());
|
||||
free(pBuf);
|
||||
return -ERR_OPEN_FILE;
|
||||
}
|
||||
|
||||
WritePrivateProfileString(CFG_WIREGUARD_SECTION, CFG_WGSVR_PATH, cfgPath, GetGlobalCfgInfo()->cfgPath);
|
||||
|
||||
if (FAILED(StringCbPrintf(pBuf,
|
||||
bufSize,
|
||||
cfgFormat,
|
||||
pWgConfig->Address,
|
||||
pWgConfig->ListenPort,
|
||||
pWgConfig->PrivateKey,
|
||||
pWgConfig->CliPubKey,
|
||||
pWgConfig->AllowNet))) {
|
||||
SPDLOG_ERROR("Format string error: {0}", GetLastError());
|
||||
free(pBuf);
|
||||
::CloseHandle(hFile);
|
||||
return -ERR_MEMORY_STR;
|
||||
}
|
||||
|
||||
if (FAILED(StringCbLength(pBuf, bufSize, &length))) {
|
||||
SPDLOG_ERROR("Get string \'{0}\' length error: {1}", pBuf, GetLastError());
|
||||
free(pBuf);
|
||||
::CloseHandle(hFile);
|
||||
return -ERR_MEMORY_STR;
|
||||
}
|
||||
|
||||
SPDLOG_DEBUG("WG Server Configure:\n{0}", pBuf);
|
||||
|
||||
if (FALSE ==
|
||||
WriteFile(hFile, // open file handle
|
||||
pBuf, // start of data to write
|
||||
static_cast<DWORD>(length), // number of bytes to write
|
||||
nullptr, // number of bytes that were written
|
||||
nullptr)) // no overlapped structure)
|
||||
{
|
||||
SPDLOG_ERROR("WriteFile [{0}] error: {1}", cfgPath, GetLastError());
|
||||
free(pBuf);
|
||||
::CloseHandle(hFile);
|
||||
return -ERR_OPEN_FILE;
|
||||
}
|
||||
|
||||
::CloseHandle(hFile);
|
||||
return ERR_SUCCESS;
|
||||
}
|
||||
|
||||
int GenerateWireguardKeyPairs(TCHAR *pPubKey, int pubkeySize, TCHAR *pPrivKey, int privKeySize) {
|
||||
int ret;
|
||||
TCHAR cmdBuffer[MAX_PATH];
|
||||
TCHAR cmdResult[MAX_PATH];
|
||||
PSDK_CONFIG pCfg = GetGlobalCfgInfo();
|
||||
|
||||
// WireGuard 不存在或者未配置目录
|
||||
if (!pCfg->wireguardCfg.wgExists || !pCfg->wireguardCfg.wireguardExists)
|
||||
{
|
||||
if (!pCfg->wireguardCfg.wgExists || !pCfg->wireguardCfg.wireguardExists) {
|
||||
return -ERR_ITEM_UNEXISTS;
|
||||
}
|
||||
|
||||
|
@ -32,8 +255,7 @@ TUNNEL_API int GenerateWireguardKeyPairs(TCHAR* pPubKey, int pubkeySize, TCHAR*
|
|||
|
||||
StringCbPrintf(cmdBuffer, MAX_PATH, TEXT("cmd.exe /C \"%s\" genkey"), pCfg->wireguardCfg.wgPath);
|
||||
|
||||
if ((ret = RunPipeCmd(cmdBuffer, cmdResult, MAX_PATH)) != ERR_SUCCESS)
|
||||
{
|
||||
if ((ret = RunPipeCmd(cmdBuffer, cmdResult, MAX_PATH)) != ERR_SUCCESS) {
|
||||
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuffer, ret);
|
||||
return -ERR_CALL_SHELL;
|
||||
}
|
||||
|
@ -42,12 +264,14 @@ TUNNEL_API int GenerateWireguardKeyPairs(TCHAR* pPubKey, int pubkeySize, TCHAR*
|
|||
|
||||
StringCbCopy(pPrivKey, privKeySize, cmdResult);
|
||||
memset(cmdBuffer, 0, MAX_PATH);
|
||||
StringCbPrintf(cmdBuffer, MAX_PATH, TEXT("cmd.exe /C echo %s | \"%s\" pubkey"), cmdResult,
|
||||
StringCbPrintf(cmdBuffer,
|
||||
MAX_PATH,
|
||||
TEXT("cmd.exe /C echo %s | \"%s\" pubkey"),
|
||||
cmdResult,
|
||||
pCfg->wireguardCfg.wgPath);
|
||||
|
||||
memset(cmdResult, 0, MAX_PATH);
|
||||
if ((ret = RunPipeCmd(cmdBuffer, cmdResult, MAX_PATH)) != ERR_SUCCESS)
|
||||
{
|
||||
if ((ret = RunPipeCmd(cmdBuffer, cmdResult, MAX_PATH)) != ERR_SUCCESS) {
|
||||
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuffer, ret);
|
||||
return -ERR_CALL_SHELL;
|
||||
}
|
||||
|
@ -58,68 +282,59 @@ TUNNEL_API int GenerateWireguardKeyPairs(TCHAR* pPubKey, int pubkeySize, TCHAR*
|
|||
return ERR_SUCCESS;
|
||||
}
|
||||
|
||||
TUNNEL_API int SetWireguardPath(TCHAR* pPath)
|
||||
{
|
||||
int SetWireguardPath(const TCHAR *pPath) {
|
||||
WIN32_FIND_DATA FindFileData;
|
||||
HANDLE hFind;
|
||||
HANDLE hFind;
|
||||
|
||||
if (pPath == nullptr)
|
||||
{
|
||||
if (pPath == nullptr) {
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
|
||||
hFind = FindFirstFile(pPath, &FindFileData);
|
||||
|
||||
if (hFind != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
TCHAR path[MAX_PATH] = {0};
|
||||
if (hFind != INVALID_HANDLE_VALUE) {
|
||||
TCHAR wgPath[MAX_PATH];
|
||||
|
||||
StringCbPrintf(path, MAX_PATH, TEXT("%s\\%s"), GetGlobalCfgInfo()->workDirectory, CONFIG_FILE_NAME);
|
||||
SPDLOG_DEBUG(TEXT("Used configure file:{0}"), GetGlobalCfgInfo()->cfgPath);
|
||||
|
||||
SPDLOG_DEBUG(TEXT("Used configure file:{0}"), path);
|
||||
|
||||
WritePrivateProfileString(CFG_WIREGUARD_SECTION, CFG_WIREGUARD_PATH, pPath, path);
|
||||
WritePrivateProfileString(CFG_WIREGUARD_SECTION, CFG_WIREGUARD_PATH, pPath, GetGlobalCfgInfo()->cfgPath);
|
||||
SPDLOG_DEBUG(TEXT("Save configure: {1} --> {0}"), pPath, CFG_WIREGUARD_PATH);
|
||||
|
||||
StringCbCopy(wgPath, MAX_PATH, pPath);
|
||||
TCHAR* pIndex = _tcsrchr(wgPath, '\\');
|
||||
|
||||
if (pIndex)
|
||||
{
|
||||
if (TCHAR *pIndex = _tcsrchr(wgPath, '\\')) {
|
||||
*pIndex = 0;
|
||||
StringCbCat(wgPath, MAX_PATH, "\\wg.exe");
|
||||
WritePrivateProfileString(CFG_WIREGUARD_SECTION, CFG_WG_PATH, wgPath, path);
|
||||
WritePrivateProfileString(CFG_WIREGUARD_SECTION, CFG_WG_PATH, wgPath, GetGlobalCfgInfo()->cfgPath);
|
||||
SPDLOG_DEBUG(TEXT("Save configure: {1} --> {0}"), wgPath, CFG_WG_PATH);
|
||||
}
|
||||
|
||||
return ERR_SUCCESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
SPDLOG_ERROR(TEXT("WireGuard not found: {0}"), pPath);
|
||||
return -ERR_ITEM_UNEXISTS;
|
||||
}
|
||||
}
|
||||
|
||||
TUNNEL_API int FindWireguardExe(TCHAR* pFullPath, int maxSize)
|
||||
{
|
||||
TCHAR path[MAX_PATH];
|
||||
TCHAR wrieguardPath[MAX_PATH];
|
||||
int FindWireguardExe(TCHAR *pFullPath, int maxSize) {
|
||||
TCHAR path[MAX_PATH];
|
||||
TCHAR wrieguardPath[MAX_PATH];
|
||||
WIN32_FIND_DATA FindFileData;
|
||||
HANDLE hFind;
|
||||
DWORD dwRet, dwErr;
|
||||
LPSTR pEnvBuf;
|
||||
TCHAR *token, *p = nullptr;
|
||||
HANDLE hFind;
|
||||
DWORD dwRet;
|
||||
LPSTR pEnvBuf;
|
||||
TCHAR *token, *p = nullptr;
|
||||
|
||||
memset(path, 0, MAX_PATH);
|
||||
StringCbPrintf(path, MAX_PATH, TEXT("%s\\%s"), GetGlobalCfgInfo()->workDirectory, CONFIG_FILE_NAME);
|
||||
GetPrivateProfileString(CFG_WIREGUARD_SECTION,CFG_WIREGUARD_PATH,TEXT(""), wrieguardPath,MAX_PATH, path);
|
||||
GetPrivateProfileString(CFG_WIREGUARD_SECTION,
|
||||
CFG_WIREGUARD_PATH,
|
||||
TEXT(""),
|
||||
wrieguardPath,
|
||||
MAX_PATH,
|
||||
GetGlobalCfgInfo()->cfgPath);
|
||||
|
||||
hFind = FindFirstFile(wrieguardPath, &FindFileData);
|
||||
if (hFind != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
if (pFullPath && maxSize > 0)
|
||||
{
|
||||
if (hFind != INVALID_HANDLE_VALUE) {
|
||||
if (pFullPath && maxSize > 0) {
|
||||
StringCbCopy(pFullPath, maxSize, wrieguardPath);
|
||||
}
|
||||
|
||||
|
@ -128,13 +343,15 @@ TUNNEL_API int FindWireguardExe(TCHAR* pFullPath, int maxSize)
|
|||
|
||||
SPDLOG_DEBUG(TEXT("Ini found WireGuard at: {0}"), wrieguardPath);
|
||||
|
||||
memset(path, 0, MAX_PATH);
|
||||
StringCbPrintf(path, MAX_PATH, TEXT("%s\\%s"), GetGlobalCfgInfo()->workDirectory, CONFIG_FILE_NAME);
|
||||
GetPrivateProfileString(CFG_WIREGUARD_SECTION,CFG_WG_PATH,TEXT(""), wrieguardPath,MAX_PATH, path);
|
||||
GetPrivateProfileString(CFG_WIREGUARD_SECTION,
|
||||
CFG_WG_PATH,
|
||||
TEXT(""),
|
||||
wrieguardPath,
|
||||
MAX_PATH,
|
||||
GetGlobalCfgInfo()->cfgPath);
|
||||
|
||||
hFind = FindFirstFile(wrieguardPath, &FindFileData);
|
||||
if (hFind != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
if (hFind != INVALID_HANDLE_VALUE) {
|
||||
StringCbCopy(GetGlobalCfgInfo()->wireguardCfg.wgPath, MAX_PATH, wrieguardPath);
|
||||
GetGlobalCfgInfo()->wireguardCfg.wgExists = TRUE;
|
||||
SPDLOG_DEBUG(TEXT("Ini found WireGuard Tools at: {0}"), wrieguardPath);
|
||||
|
@ -144,35 +361,32 @@ TUNNEL_API int FindWireguardExe(TCHAR* pFullPath, int maxSize)
|
|||
}
|
||||
|
||||
pEnvBuf = static_cast<LPSTR>(malloc(WINENVBUF_SIZE));
|
||||
if (nullptr == pEnvBuf)
|
||||
{
|
||||
if (nullptr == pEnvBuf) {
|
||||
SPDLOG_ERROR(TEXT("Malloc {0} bytes memory error"), WINENVBUF_SIZE);
|
||||
return -ERR_MALLOC_MEMORY;
|
||||
}
|
||||
|
||||
dwRet = GetEnvironmentVariable(TEXT("path"), pEnvBuf, WINENVBUF_SIZE);
|
||||
|
||||
if (0 == dwRet)
|
||||
{
|
||||
if (0 == dwRet) {
|
||||
DWORD dwErr;
|
||||
dwErr = GetLastError();
|
||||
if (ERROR_ENVVAR_NOT_FOUND == dwErr)
|
||||
{
|
||||
if (ERROR_ENVVAR_NOT_FOUND == dwErr) {
|
||||
SPDLOG_DEBUG(TEXT("Environment variable path does not exist."));
|
||||
free(pEnvBuf);
|
||||
return -ERR_FILE_NOT_EXISTS;
|
||||
}
|
||||
}
|
||||
else if (WINENVBUF_SIZE < dwRet)
|
||||
{
|
||||
pEnvBuf = static_cast<LPSTR>(realloc(pEnvBuf, dwRet * sizeof(CHAR)));
|
||||
if (nullptr == pEnvBuf)
|
||||
{
|
||||
} else if (WINENVBUF_SIZE < dwRet) {
|
||||
LPSTR pBuf = static_cast<LPSTR>(realloc(pEnvBuf, dwRet * sizeof(CHAR)));
|
||||
if (nullptr == pBuf) {
|
||||
SPDLOG_ERROR(TEXT("Malloc {0} bytes memory error"), dwRet * sizeof(CHAR));
|
||||
free(pEnvBuf);
|
||||
return -ERR_MALLOC_MEMORY;
|
||||
}
|
||||
dwRet = GetEnvironmentVariable("path", pEnvBuf, dwRet);
|
||||
if (!dwRet)
|
||||
{
|
||||
|
||||
pEnvBuf = pBuf;
|
||||
dwRet = GetEnvironmentVariable("path", pEnvBuf, dwRet);
|
||||
if (!dwRet) {
|
||||
SPDLOG_ERROR(TEXT("GetEnvironmentVariable failed (%d)"), GetLastError());
|
||||
free(pEnvBuf);
|
||||
return -ERR_FILE_NOT_EXISTS;
|
||||
|
@ -181,17 +395,14 @@ TUNNEL_API int FindWireguardExe(TCHAR* pFullPath, int maxSize)
|
|||
|
||||
token = strtok_s(pEnvBuf, TEXT(";"), &p);
|
||||
|
||||
while (token != nullptr)
|
||||
{
|
||||
while (token != nullptr) {
|
||||
memset(path, 0, MAX_PATH);
|
||||
StringCbPrintfA(path, MAX_PATH, TEXT("%s\\wireguard.exe"), token);
|
||||
|
||||
hFind = FindFirstFile(path, &FindFileData);
|
||||
|
||||
if (hFind != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
if (pFullPath && maxSize > 0)
|
||||
{
|
||||
if (hFind != INVALID_HANDLE_VALUE) {
|
||||
if (pFullPath && maxSize > 0) {
|
||||
StringCbCopy(pFullPath, maxSize, path);
|
||||
}
|
||||
|
||||
|
@ -208,15 +419,13 @@ TUNNEL_API int FindWireguardExe(TCHAR* pFullPath, int maxSize)
|
|||
SPDLOG_DEBUG(TEXT("Find WireGuard tools at: {0}"), path);
|
||||
|
||||
hFind = FindFirstFile(path, &FindFileData);
|
||||
if (hFind != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
if (hFind != INVALID_HANDLE_VALUE) {
|
||||
StringCbCopy(GetGlobalCfgInfo()->wireguardCfg.wgPath, MAX_PATH, path);
|
||||
GetGlobalCfgInfo()->wireguardCfg.wgExists = TRUE;
|
||||
|
||||
SPDLOG_DEBUG(TEXT("Path Environment found WireGuard tools at: {0}"), path);
|
||||
}
|
||||
|
||||
|
||||
//TODO: throw exception by C# call, why??????
|
||||
//CloseHandle(hFind);
|
||||
free(pEnvBuf);
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:String x:Key="/Default/CodeInspection/Highlighting/AnalysisEnabled/@EntryValue">SOLUTION</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/Highlighting/SweaWarningsMode/@EntryValue">ShowAndRun</s:String></wpf:ResourceDictionary>
|
Loading…
Reference in New Issue