feat(storage): browse SMB shares and bound connection checks
This commit is contained in:
1 parent
8417b1dff8
commit
a785fa73d4
15 files changed
+277
-39
No files matched your search
+37
-2
@@ -7,6 +7,27 @@ try {
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
public static class OrderSmbNative {
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
||||
public struct SHARE_INFO_1 { public string name; public uint type; public string remark; }
|
||||
[DllImport("Netapi32.dll", CharSet = CharSet.Unicode)]
|
||||
public static extern int NetShareEnum(string server, int level, out IntPtr buffer, int length, out int read, out int total, ref int resume);
|
||||
[DllImport("Netapi32.dll")] public static extern int NetApiBufferFree(IntPtr buffer);
|
||||
public static string[] ListShares(string server) {
|
||||
var names = new System.Collections.Generic.List<string>(); int resume = 0, code;
|
||||
do {
|
||||
IntPtr buffer; int read, total;
|
||||
code = NetShareEnum(server, 1, out buffer, 16384, out read, out total, ref resume);
|
||||
try {
|
||||
if (code != 0 && code != 234) throw new System.ComponentModel.Win32Exception(code);
|
||||
int stride = Marshal.SizeOf(typeof(SHARE_INFO_1));
|
||||
for (int i = 0; i < read && names.Count < 501; i++) {
|
||||
var item = (SHARE_INFO_1)Marshal.PtrToStructure(IntPtr.Add(buffer, i * stride), typeof(SHARE_INFO_1));
|
||||
if ((item.type & 255) == 0 && !item.name.EndsWith("$")) names.Add(item.name);
|
||||
}
|
||||
} finally { if (buffer != IntPtr.Zero) NetApiBufferFree(buffer); }
|
||||
} while (code == 234 && names.Count < 501);
|
||||
return names.ToArray();
|
||||
}
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
||||
public struct NETRESOURCE {
|
||||
public int scope, type, displayType, usage;
|
||||
@@ -18,7 +39,18 @@ public static class OrderSmbNative {
|
||||
public static extern bool MoveFileExW(string existingName, string newName, int flags);
|
||||
}
|
||||
'@
|
||||
if ($orderSmbRequest.operation -eq 'connect') {
|
||||
if ($orderSmbRequest.operation -eq 'shares') {
|
||||
if ($orderSmbRequest.authMode -eq 'credentials') {
|
||||
$orderSmbResource = New-Object OrderSmbNative+NETRESOURCE
|
||||
$orderSmbResource.type = 0
|
||||
$orderSmbResource.remoteName = '\\' + $orderSmbRequest.host + '\IPC$'
|
||||
$orderSmbCode = [OrderSmbNative]::WNetAddConnection2W([ref]$orderSmbResource, $orderSmbRequest.password, $orderSmbRequest.username, 4)
|
||||
if ($orderSmbCode -ne 0) { @{ok=$false;code=$orderSmbCode} | ConvertTo-Json -Compress; exit }
|
||||
}
|
||||
$orderSmbNames = @([OrderSmbNative]::ListShares('\\' + $orderSmbRequest.host))
|
||||
@{ok=$true;code=0;shares=$orderSmbNames} | ConvertTo-Json -Compress -Depth 4
|
||||
exit
|
||||
} elseif ($orderSmbRequest.operation -eq 'connect') {
|
||||
$orderSmbResource = New-Object OrderSmbNative+NETRESOURCE
|
||||
$orderSmbResource.type = 1
|
||||
$orderSmbResource.remoteName = $orderSmbRequest.remote
|
||||
@@ -29,5 +61,8 @@ public static class OrderSmbNative {
|
||||
} else { throw 'Unsupported operation' }
|
||||
@{ ok = ($orderSmbCode -eq 0); code = $orderSmbCode } | ConvertTo-Json -Compress
|
||||
} catch {
|
||||
@{ ok = $false; code = -1 } | ConvertTo-Json -Compress
|
||||
$orderSmbCause = $_.Exception
|
||||
while ($orderSmbCause.InnerException) { $orderSmbCause = $orderSmbCause.InnerException }
|
||||
$orderSmbErrorCode = if ($orderSmbCause -is [ComponentModel.Win32Exception]) { $orderSmbCause.NativeErrorCode } else { -1 }
|
||||
@{ ok = $false; code = $orderSmbErrorCode } | ConvertTo-Json -Compress
|
||||
}
|
||||
Reference in new issue
Block a user