-
Notifications
You must be signed in to change notification settings - Fork 170
/
TeamMachineSetup.ps1
183 lines (162 loc) · 5.47 KB
/
TeamMachineSetup.ps1
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Add-Type -AssemblyName System.Security
$MeEndpoint = "https://kessel-api.parsecgaming.com/me"
$userBinPath = "C:\ProgramData\Parsec\user.bin"
$configtxtdir = 'C:\ProgramData\Parsec\config.txt'
Class Post
{
[String]$name
[String]$peer_id
[int]$team_group_id
[int]$user_id
[string]$user_email
[string]$key
[string]$team_id
[bool]$is_guest_access = $false
[void] Fill(){
$config = fetchUserData
foreach ($line in $config) {
$this."$($line.split("=")[0])" = $($line.split("=")[1])
$this.peer_id = $(PeerIDGetter)
if (!$this.name) {$this.name = $env:COMPUTERNAME}
}
}
}
function fetchUserData {
$metadata = $(
try {
(Invoke-WebRequest -uri http://metadata.google.internal/computeMetadata/v1/instance/attributes/parsec -Method GET -header @{'metadata-flavor'='Google'} -TimeoutSec 5)
$stream = "bytes"
}
catch {
}
Try {
(Invoke-WebRequest -uri http://metadata.paperspace.com/meta-data/machine -TimeoutSec 5)
$stream = "bytes"
}
catch {
}
Try {
(Invoke-WebRequest -Uri "http://169.254.169.254/latest/user-data?" -TimeoutSec 5)
$stream = "bytes"
}
catch {
}
Try {
Invoke-Webrequest -Headers @{"Metadata"="true"} -Uri "http://169.254.169.254/metadata/instance/compute/userData?api-version=2021-01-01&format=text" -TimeoutSec 5
$stream = "base64"
}
Catch {
}
)
if ($metadata.StatusCode -eq 200) {
if (($metadata.Content.Length) -gt 1) {
if ($stream -eq "bytes") {
[System.Text.Encoding]::ASCII.GetString($metadata.content).split(':')
}
elseif ($stream -eq "base64") {
[System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($metadata.Content)).split(':')
}
}
else {
#no userdata found, exiting...
Exit
}
}
else {
#no userdata found, exiting...
Exit
}
}
Function PeerIDGetter {
$configfile = get-content $configtxtdir
foreach ($configline in $configfile) {
if ($configline -like 'app_host_peer_id*') {
$peerid = $configline.Split(' = ')[-1]
}
}
if ($peerid -eq $NULL) {$peerid = $NULL}
return $peerid
}
Function WritePeerID {
Param(
$peer_id
)
$configfile = Get-Content $configtxtdir
$File = Foreach ($configline in $configfile) {
if ($configline -like 'app_host_peer_id*') {
}
Else {$configline}
}
$file += "app_host_peer_id=$peer_id"
$file | Out-File $configtxtdir -Encoding ascii
}
Function WriteHostname {
Param(
$host_name
)
$configfile = Get-Content $configtxtdir
$File = Foreach ($configline in $configfile) {
if ($configline -like 'host_name*') {
}
Else {$configline}
}
$file += "host_name=$host_name"
$file | Out-File $configtxtdir -Encoding ascii
}
Function SessionIDFromUserBin {
if (Test-Path $userBinPath) {
Add-Type -AssemblyName System.Security
$scope = [System.Security.Cryptography.DataProtectionScope]::LocalMachine
[byte[]]$binbyte = Get-Content $userBinPath -encoding byte
$decryptuserBin = [System.Security.Cryptography.ProtectedData]::Unprotect($binbyte, $null, $scope)
$parseuserbin = [System.Text.Encoding]::ASCII.GetString($decryptuserBin)
return $parseuserbin.Split('"')[-2]
}
Else {
return 0
}
}
Function SessionIDToUserBin {
Param (
[string]$session
)
$userBinJson = @{
"session_id" = $session
} | ConvertTo-Json
$scope = [System.Security.Cryptography.DataProtectionScope]::LocalMachine
$input = [System.Text.UTF8Encoding]::UTF8.GetBytes($userBinJson)
$userBin = [System.Security.Cryptography.ProtectedData]::Protect($input, $null, $scope)
Set-Content $userBinPath -Value $userBin -Encoding Byte
}
Function PostTeamEndpoint {
param (
[post]$Params
)
$body = $Params | Select-Object * -ExcludeProperty key, team_id
$TeamEndpointHeaders = @{
"Content-Type" = "application/json"
"X-Machine-Key" = $params.key
}
$TeamEndpointRequest = Invoke-RestMethod -Uri $TeamEndoint -Method POST -Headers $TeamEndpointHeaders -Body ($body | ConvertTo-Json)
return $TeamEndpointRequest
}
Function GenerateMeData {
param(
[string]$Session
)
$SessionHeader = @{
"Authorization" = "Bearer $Session"
}
$array = (Invoke-RestMethod -Uri $MeEndpoint -Method GET -Headers $SessionHeader).data
$array
}
$RequestDetails = [Post]::new()
$RequestDetails.Fill()
$TeamEndoint = "https://kessel-api.parsecgaming.com/teams/{0}/machines" -f $RequestDetails.team_id
$TeamsEndpointRequest = PostTeamEndpoint -params $RequestDetails
WritePeerID -peer_id $TeamsEndpointRequest.data.host_peer_id
WriteHostname -host_name $RequestDetails.name
#$Details = GenerateMeData -Session $TeamsEndpointRequest.data.id
SessionIDToUserBin -session $TeamsEndpointRequest.data.id
Stop-Process -Name parsecd -Force