|
| 1 | +Set-StrictMode -version latest; |
| 2 | +$ErrorActionPreference = "Stop"; |
| 3 | +$VerbosePreference="Continue"; |
| 4 | + |
| 5 | + |
| 6 | +$publicKey=(curl https://github.com/$($env:GITHUB_REPOSITORY_OWNER).keys) + " gh" |
| 7 | +Function InstallSshServer(){ |
| 8 | + [CmdletBinding()] |
| 9 | + param( |
| 10 | + [Parameter(Mandatory=$true)] |
| 11 | + [String] $publicKey |
| 12 | + ) |
| 13 | + Add-WindowsCapability -Online -Name OpenSSH.Server |
| 14 | + echo "PubkeyAuthentication yes`nPasswordAuthentication no`nSubsystem sftp sftp-server.exe`nMatch Group administrators`n`tAuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys`n" | out-file -Encoding ASCII $env:programData/ssh/sshd_config |
| 15 | + ssh-keygen -A |
| 16 | + echo "$publicKey`n" | out-file -Encoding ASCII $env:programData/ssh/administrators_authorized_keys |
| 17 | + icacls.exe "$env:programData\ssh\administrators_authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F" |
| 18 | + cat $env:programData/ssh/administrators_authorized_keys |
| 19 | + Write-Host "SSH Installed" |
| 20 | +} |
| 21 | +Function DownloadStartCloudflareServer(){ |
| 22 | + [CmdletBinding()] |
| 23 | + param( |
| 24 | + [Parameter(Mandatory=$true)] |
| 25 | + [String] $LocalHostnameAndPort, #ie 127.0.0.1:22 |
| 26 | + [Parameter(Mandatory=$false)] |
| 27 | + [String] $SaveToFilename="cloudflared.exe" #can include path |
| 28 | + ) |
| 29 | + if (([System.IO.File]::Exists($SaveToFilename)) -eq $false) { |
| 30 | + Invoke-WebRequest -URI https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-windows-amd64.exe -OutFile $SaveToFilename |
| 31 | + } |
| 32 | + $myargs = "tunnel --no-autoupdate --url tcp://$LocalHostnameAndPort --logfile cfd.log" |
| 33 | + #$scriptBlock = [Scriptblock]::Create("Start-Process -NoNewWindow -Wait `"$SaveToFilename`" $myargs ") |
| 34 | + $myjob = Start-Process -PassThru -NoNewWindow `"$SaveToFilename`" -ArgumentList $myargs |
| 35 | + |
| 36 | + #Start-Job -Name CFD -ScriptBlock $scriptBlock |
| 37 | + #$myjob= Receive-Job -Name CFD |
| 38 | + Write-Host "Cloudflared Installed" |
| 39 | + return $myjob |
| 40 | +} |
| 41 | +Function InstallSSHStartCF(){ |
| 42 | + [CmdletBinding()] |
| 43 | + param( |
| 44 | + [Parameter(Mandatory=$true)] |
| 45 | + [String] $publicKey, |
| 46 | + [Parameter(Mandatory=$false)] |
| 47 | + [String] $SaveToFilename="cloudflared.exe" #can include path |
| 48 | + ) |
| 49 | + InstallSshServer $publicKey |
| 50 | + $server = DownloadStartCloudflareServer("127.0.0.1:22") |
| 51 | + $scriptBlock = [Scriptblock]::Create("Start-Process -NoNewWindow -Wait `"sshd.exe`" ") |
| 52 | + |
| 53 | + Start-Job -Name SSHD -ScriptBlock $scriptBlock |
| 54 | + return $server |
| 55 | +} |
| 56 | +InstallSSHStartCF $publicKey |
| 57 | +while ($true) { |
| 58 | + Start-Sleep -Seconds 30 |
| 59 | + cat cfd.log |
| 60 | +} |
| 61 | +#Wait-Job SSHD |
0 commit comments