Browse Source

cosmetic fixes and non-admin rollback

master
NelsonDaniel 4 years ago
parent
commit
4962c255fa
  1. 11
      UsbIpServer/ConnectedClient.cs
  2. 14
      UsbIpServer/DeviceChecker.cs
  3. 21
      UsbIpServer/RegistryUtils.cs
  4. 7
      UsbIpServer/Server.cs
  5. 2
      WSL_USBIP.md
  6. 1
      usbipd-win.sln

11
UsbIpServer/ConnectedClient.cs

@ -63,9 +63,14 @@ namespace UsbIpServer
static async Task<ExportedDevice[]> GetAvailableDevicesAsync(CancellationToken cancellationToken)
{
return (await ExportedDevice.GetAll(cancellationToken))
.Where(x => RegistryUtils.IsDeviceAvailable(x.BusId))
.ToArray();
if (RegistryUtils.HasRegistryAccess())
{
return (await ExportedDevice.GetAll(cancellationToken))
.Where(x => RegistryUtils.IsDeviceAvailable(x.BusId))
.ToArray();
}
return await ExportedDevice.GetAll(cancellationToken);
}
async Task HandleRequestDeviceListAsync(CancellationToken cancellationToken)

14
UsbIpServer/DeviceChecker.cs

@ -4,7 +4,7 @@ using System.Linq;
namespace UsbIpServer
{
class DeviceInfoChecker
internal sealed class DeviceInfoChecker
{
List<DeviceInfo> devices = new List<DeviceInfo>();
@ -19,9 +19,9 @@ namespace UsbIpServer
foreach (ManagementObject device in collection)
{
devices.Add(new DeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")
));
}
@ -33,6 +33,10 @@ namespace UsbIpServer
var possibleDeviceNames = new SortedSet<string>();
foreach (var usbDevice in devices)
{
// Example Path: USB\VID_046D&PID_C539\7&674AA44&0&3
// The first part is device type, second is vid and pid and third is specific to the device,
// but we deal with composite devices which have multiple devices in a single device.
// We get all names/description to give a hint to the user.
var parts = path.Split(@"\");
var type = parts[0];
var vid_pid = parts[1];
@ -47,7 +51,7 @@ namespace UsbIpServer
}
}
class DeviceInfo
internal sealed class DeviceInfo
{
public DeviceInfo(string deviceID, string pnpDeviceID, string description)
{

21
UsbIpServer/RegistryUtils.cs

@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.Win32;
using System.Threading;
using System.Security.Principal;
namespace UsbIpServer
{
@ -20,7 +17,7 @@ namespace UsbIpServer
{
if (enable)
{
var key = Registry.LocalMachine.CreateSubKey($@"{devicesRegistryPath}\{busId}");
Registry.LocalMachine.CreateSubKey($@"{devicesRegistryPath}\{busId}");
} else
{
Registry.LocalMachine.DeleteSubKey($@"{devicesRegistryPath}\{busId}");
@ -37,5 +34,17 @@ namespace UsbIpServer
Registry.LocalMachine.DeleteSubKeyTree(devicesRegistryPath);
Registry.LocalMachine.CreateSubKey(devicesRegistryPath);
}
public static bool HasRegistryAccess()
{
bool isElevated;
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);
}
return isElevated;
}
}
}

7
UsbIpServer/Server.cs

@ -43,7 +43,12 @@ namespace UsbIpServer
}
TcpListener.Start();
RegistryUtils.InitializeRegistry();
if (RegistryUtils.HasRegistryAccess())
{
RegistryUtils.InitializeRegistry();
}
using var cancellationTokenRegistration = stoppingToken.Register(() => TcpListener.Stop());
while (true)

2
WSL_USBIP.md

@ -83,4 +83,4 @@ Create a `.wslconfig` file on `/mnt/c/Users/<user>/` and add a reference to the
kernel=c:\\users\\t-nelsont\\configurations\\wsl-new
```
READY TO USE
Your WSL distro is now ready to use!

1
usbipd-win.sln

@ -18,6 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
nuget.config = nuget.config
README.md = README.md
SECURITY.md = SECURITY.md
WSL_USBIP.md = WSL_USBIP.md
EndProjectSection
EndProject
Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "Installer", "Installer\Installer.wixproj", "{080EF94C-A8EC-4975-A697-BBF8C1A56055}"

Loading…
Cancel
Save