Skip to content

Commit 8946f1a

Browse files
authored
Convert PSWSMan to binary module (#40)
This change sets up PSWSMan as a binary module in prepration for auto loading and unloading behaviour when the module is imported.
1 parent 2c38367 commit 8946f1a

24 files changed

+604
-279
lines changed

.gitignore

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ nits.log
2020

2121
__pycache__/
2222
.idea/
23-
.vscode/
2423
*.pyc
2524
*.swp
2625
/build-*.sh
@@ -33,5 +32,21 @@ __pycache__/
3332
/integration_environment/cert_setup
3433
/integration_environment/exchange*
3534
/integration_environment/test_results.md
36-
/PSWSMan/lib/*
35+
/build
3736
/psl-omi-provider/repo
37+
38+
# C# artifacts
39+
[Dd]ebug/
40+
[Dd]ebugPublic/
41+
[Rr]elease/
42+
[Rr]eleases/
43+
x64/
44+
x86/
45+
[Ww][Ii][Nn]32/
46+
[Aa][Rr][Mm]/
47+
[Aa][Rr][Mm]64/
48+
bld/
49+
[Bb]in/
50+
[Oo]bj/
51+
[Ll]og/
52+
[Ll]ogs/

.vscode/extensions.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [
5+
"ms-dotnettools.csharp",
6+
"ms-vscode.powershell",
7+
],
8+
}

.vscode/launch.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "PowerShell launch",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build module",
12+
"program": "pwsh",
13+
"args": [
14+
"-NoExit",
15+
"-NoProfile",
16+
"-Command",
17+
"Import-Module ./build/PSWSMan"
18+
],
19+
"cwd": "${workspaceFolder}",
20+
"stopAtEntry": false,
21+
"console": "externalTerminal",
22+
},
23+
{
24+
"name": ".NET CoreCLR Attach",
25+
"type": "coreclr",
26+
"request": "attach",
27+
"processId": "${command:pickProcess}",
28+
"justMyCode": true,
29+
},
30+
],
31+
}

.vscode/settings.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
//-------- Files configuration --------
3+
4+
// When enabled, will trim trailing whitespace when you save a file.
5+
"files.trimTrailingWhitespace": true,
6+
7+
// When enabled, insert a final new line at the end of the file when saving it.
8+
"files.insertFinalNewline": true,
9+
10+
//-------- PowerShell configuration --------
11+
// Binary modules cannot be unloaded so running in separate processes solves that problem
12+
"powershell.debugging.createTemporaryIntegratedConsole": true,
13+
}

.vscode/tasks.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "build module",
8+
"command": "python",
9+
"type": "shell",
10+
"args": [
11+
"${workspaceFolder}/build.py",
12+
"module",
13+
"--debug"
14+
],
15+
"problemMatcher": "$msCompile"
16+
}
17+
]
18+
}

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ It documents the changes in each of the tagged releases
77

88
+ Added universal build for macOS to work with both x86_64 and arm64 processes
99
+ Fixed up logic used to determine what OpenSSL library is used on macOS
10+
+ Changed `PSWSMan` to be a hybrid module for more robust loading and unloading behaviour
1011

1112
## 2.2.1 - 2021-07-14
1213

PSWSMan/lib/.keep

Whitespace-only changes.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
@{
2-
ModuleVersion = '2.2.1'
2+
ModuleVersion = '2.3.0'
33
RootModule = 'PSWSMan'
44
GUID = '92ec96bf-3ff4-41b2-8694-cd3ee636d3fd'
55
Author = 'Jordan Borean'
66
Copyright = 'Copyright (c) 2020 by Jordan Borean'
77
Description = "Module to install and manage the forked WSMan client libraries for Linux and macOS.`nSee https://github.com/jborean93/omi for more details."
8-
PowerShellVersion = '6.0'
8+
PowerShellVersion = '7.0'
99
CompatiblePSEditions = 'Core'
10+
CmdletsToExport = @()
1011
FunctionsToExport = @(
1112
'Disable-WSManCertVerification',
1213
'Enable-WSManCertVerification',
Lines changed: 9 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
using namespace System.Security.Cryptography.X509Certificates
22
using namespace System.Management.Automation
33

4-
$Script:LibPath = Join-Path -Path $PSScriptRoot -ChildPath lib
4+
$importModule = Get-Command -Name Import-Module -Module Microsoft.PowerShell.Core
5+
if ('PSWSMan.OnModuleImportAndRemove' -as [type]) {
6+
&$importModule -Force -Assembly ([PSWSMan.OnModuleImportAndRemove].Assembly)
7+
}
8+
else {
9+
&$importModule ([IO.Path]::Combine($PSScriptRoot, 'bin', 'PSWSMan.dll')) -ErrorAction Stop
10+
}
11+
12+
$Script:LibPath = Join-Path -Path $PSScriptRoot -ChildPath bin
513

614
class X509CertificateChainAttribute : ArgumentTransformationAttribute {
715
[object] Transform([EngineIntrinsics]$EngineIntrinsics, [object]$InputData) {
@@ -23,136 +31,6 @@ class X509CertificateChainAttribute : ArgumentTransformationAttribute {
2331
}
2432
}
2533

26-
Add-Type -TypeDefinition @'
27-
using System;
28-
using System.Collections.Generic;
29-
using System.Reflection;
30-
using System.Runtime.InteropServices;
31-
32-
namespace PSWSMan
33-
{
34-
public class Native
35-
{
36-
[StructLayout(LayoutKind.Sequential)]
37-
public class PWSH_Version
38-
{
39-
public Int32 Major;
40-
public Int32 Minor;
41-
public Int32 Build;
42-
public Int32 Revision;
43-
44-
public static explicit operator Version(PWSH_Version v)
45-
{
46-
return new Version(v.Major, v.Minor, v.Build, v.Revision);
47-
}
48-
}
49-
50-
[DllImport("libc")]
51-
public static extern void setenv(string name, string value);
52-
53-
[DllImport("libc")]
54-
public static extern void unsetenv(string name);
55-
56-
[DllImport("libc")]
57-
public static extern IntPtr gnu_get_libc_version();
58-
59-
[DllImport("libmi")]
60-
public static extern void MI_Version_Info(PWSH_Version version);
61-
62-
[DllImport("libpsrpclient")]
63-
public static extern void PSRP_Version_Info(PWSH_Version version);
64-
65-
private delegate uint OpenSSL_version_num_ptr();
66-
67-
public static uint OpenSSL_version_num(string[] libSSLPaths)
68-
{
69-
IntPtr lib = LoadLibrary(libSSLPaths);
70-
if (lib == IntPtr.Zero)
71-
return 0;
72-
73-
try
74-
{
75-
// OpenSSL_version_num was introduced in 1.1.x, use SSLeay for older versions.
76-
string[] functionNames = {"OpenSSL_version_num", "SSLeay"};
77-
78-
foreach (string name in functionNames)
79-
{
80-
IntPtr functionAddr = IntPtr.Zero;
81-
try
82-
{
83-
functionAddr = NativeLibrary.GetExport(lib, name);
84-
}
85-
catch (EntryPointNotFoundException) {}
86-
87-
if (functionAddr == IntPtr.Zero)
88-
continue;
89-
90-
var function = (OpenSSL_version_num_ptr)Marshal.GetDelegateForFunctionPointer(
91-
functionAddr, typeof(OpenSSL_version_num_ptr));
92-
return function();
93-
}
94-
95-
return 0;
96-
}
97-
finally {
98-
NativeLibrary.Free(lib);
99-
}
100-
}
101-
102-
private delegate IntPtr OpenSSL_version_ptr(int t);
103-
104-
public static string OpenSSL_version(string[] libSSLPaths, int t)
105-
{
106-
IntPtr lib = LoadLibrary(libSSLPaths);
107-
if (lib == IntPtr.Zero)
108-
return null;
109-
110-
try
111-
{
112-
IntPtr functionAddr = IntPtr.Zero;
113-
114-
try
115-
{
116-
functionAddr = NativeLibrary.GetExport(lib, "OpenSSL_version");
117-
}
118-
catch (EntryPointNotFoundException) {}
119-
120-
if (functionAddr == IntPtr.Zero)
121-
return null;
122-
123-
var function = (OpenSSL_version_ptr)Marshal.GetDelegateForFunctionPointer(
124-
functionAddr, typeof(OpenSSL_version_ptr));
125-
126-
return Marshal.PtrToStringAuto(function(t));
127-
}
128-
finally {
129-
NativeLibrary.Free(lib);
130-
}
131-
}
132-
133-
private static IntPtr LoadLibrary(string[] loadPaths)
134-
{
135-
foreach(string path in loadPaths)
136-
{
137-
IntPtr handle = IntPtr.Zero;
138-
try
139-
{
140-
if (NativeLibrary.TryLoad(path, out handle))
141-
return handle;
142-
}
143-
catch
144-
{
145-
// TryLoad can actually through an exception so we just ignore it and continue on.
146-
continue;
147-
}
148-
}
149-
150-
return IntPtr.Zero;
151-
}
152-
}
153-
}
154-
'@
155-
15634
Function exec {
15735
<#
15836
.SYNOPSIS

0 commit comments

Comments
 (0)