-
Notifications
You must be signed in to change notification settings - Fork 0
138 lines (113 loc) · 5.02 KB
/
main.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#.github/workflows/main.yml
name: Windows - Ngrok
on: workflow_dispatch
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
env:
NGROK_AUTH_TOKEN: ${{ secrets.NGROK_AUTH_TOKEN }}
RDP_USER: admin
RDP_PASS: p@ssw0rd!
jobs:
ngrok-rdp:
runs-on: windows-latest
timeout-minutes: 9999
steps:
- name: Setup Windows Remote Desktop
run: |
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -Name "fDenyTSConnections" -Value 0
Enable-NetFirewallRule -DisplayGroup "Remote Desktop"
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Name "UserAuthentication" -Value 1
if ("$env:RDP_USER" -eq "runneradmin") {
Write-Host "Warning: Using default user account!"
Set-LocalUser -Name $env:RDP_USER -Password (ConvertTo-SecureString "$env:RDP_PASS" -AsPlainText -Force)
} else {
New-LocalUser -Name "$env:RDP_USER" -Password (ConvertTo-SecureString "$env:RDP_PASS" -AsPlainText -Force) *>$null
Add-LocalGroupMember -Group "Administrators" -Member "$env:RDP_USER"
Enable-LocalUser -Name "$env:RDP_USER"
}
icacls "C:\Windows\Temp" /grant "$env:RDP_USER`:F" *>$null
icacls "C:\Windows\installer" /grant "$env:RDP_USER`:F" *>$null
Set-Service -Name "W32Time" -StartupType Automatic -ErrorAction Stop *>$null
Restart-Service W32Time *>$null
w32tm /resync /force *>$null
w32tm /config /manualpeerlist:"time.windows.com" /syncfromflags:manual /reliable:yes /update *>$null
diskperf -Y *>$null
"$env:TEMP\*" | Remove-Item -Recurse -Force
"C:\Windows\Temp\*" | Remove-Item -Recurse -Force
"C:\Windows\Prefetch\*" | Remove-Item -Recurse -Force
"C:\Users\Public\Desktop\*" | Remove-Item -Recurse -Force
(New-Object -ComObject Shell.Application).Namespace(0xA).Items() | %{Remove-Item $_.Path -Recurse -Confirm:$false}
(New-Object -ComObject Shell.Application).minimizeall()
- name: Install and Configure Ngrok
run: |
Invoke-WebRequest https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-windows-amd64.zip -OutFile ngrok.zip
Expand-Archive ngrok.zip -DestinationPath .\ngrok
.\ngrok\ngrok.exe authtoken $env:NGROK_AUTH_TOKEN
- name: Start Ngrok Tunnel
run: |
$maxRetries = 3
$retryCount = 0
$success = $false
while (-not $success -and $retryCount -lt $maxRetries) {
Start-Process -FilePath ".\ngrok\ngrok.exe" -ArgumentList "tcp 3389" -WindowStyle Hidden
$attempts = 0
$maxAttempts = 10
$ngrokUrl = $null
while (-not $ngrokUrl -and $attempts -lt $maxAttempts) {
Start-Sleep -Seconds 5
$attempts++
try {
$tunnels = Invoke-RestMethod -Uri http://localhost:4040/api/tunnels
$ngrokUrl = ($tunnels.tunnels | Where-Object { $_.proto -eq 'tcp' }).public_url
} catch {
Write-Host "Attempt $attempts`: Waiting for Ngrok to start..."
}
}
if (-not $ngrokUrl) {
Write-Host "Failed to retrieve Ngrok URL after $maxAttempts attempts."
$retryCount++
continue
}
$ngrokUrl = $ngrokUrl -replace "^tcp://", ""
$address, $port = $ngrokUrl -split ":"
$testResult = Test-NetConnection -ComputerName $address -Port $port
if ($testResult.TcpTestSucceeded) {
$success = $true
$remoteAddress = $testResult.RemoteAddress.IPAddressToString
Write-Host "RDP Connection Details"
Write-Host "URL: $remoteAddress`:$port"
Write-Host "Username: $env:RDP_USER"
Write-Host "Password: $env:RDP_PASS"
} else {
Write-Host "Connection test failed. Retrying..."
Get-Process ngrok | Stop-Process -Force
$retryCount++
Start-Sleep -Seconds 5
}
}
if (-not $success) {
throw "Failed to establish a working Ngrok tunnel after $maxRetries attempts."
}
- name: Keep Alive
run: |
$duration = 21600 # 6 hours in seconds
$interval = 300 # Check every 5 minutes
$endTime = (Get-Date).AddSeconds($duration)
while ((Get-Date) -lt $endTime) {
$ngrokProcess = Get-Process -Name "ngrok" -ErrorAction SilentlyContinue
if (-not $ngrokProcess) {
Write-Host "Ngrok process not found. Exiting workflow."
exit 1
}
Start-Sleep -Seconds $interval
}
- name: Cleanup
if: always()
run: |
$ngrokProcess = Get-Process -Name "ngrok" -ErrorAction SilentlyContinue
if ($ngrokProcess) {
Write-Host "Terminating Ngrok process..."
$ngrokProcess | Stop-Process -Force
}
Write-Host "Cleanup complete."