File size: 3,225 Bytes
7b9f3e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
param (
        [Parameter(Mandatory = $true)]
        [string] $Node,
        [Parameter(Mandatory = $true)]
        [string] $SessionId,
        [Parameter(Mandatory = $true)]
        [string] $FQDN
)


function GetDirectUrlFromIp ($ip) {
        $ip_dash=$ip -replace "\.","-"
        $url="https://ip${ip_dash}-${SessionId}.direct.${FQDN}"
        return $url
}

function WaitForUrl ($url) {
    write-host $url
        do {
                try{
            invoke-webrequest -UseBasicParsing -uri $url | Out-Null
        } catch {}
        $status = $?
        sleep 1
        } until($status)
}

function GetNodeRoutableIp ($nodeName) {
  $JQFilter='.instances[] | select (.hostname == \"{0}\") | .routable_ip' -f $nodeName
  $rip = (invoke-webrequest -UseBasicParsing -uri "https://$FQDN/sessions/$SessionId").Content |  jq -r $JQFilter

  IF([string]::IsNullOrEmpty($rip)) {
    Write-Host "Could not fetch IP for node $nodeName"
    exit 1
  }
  return $rip
}

function Set-UseUnsafeHeaderParsing
{
    param(
        [Parameter(Mandatory,ParameterSetName='Enable')]
        [switch]$Enable,

        [Parameter(Mandatory,ParameterSetName='Disable')]
        [switch]$Disable
    )

    $ShouldEnable = $PSCmdlet.ParameterSetName -eq 'Enable'

    $netAssembly = [Reflection.Assembly]::GetAssembly([System.Net.Configuration.SettingsSection])

    if($netAssembly)
    {
        $bindingFlags = [Reflection.BindingFlags] 'Static,GetProperty,NonPublic'
        $settingsType = $netAssembly.GetType('System.Net.Configuration.SettingsSectionInternal')

        $instance = $settingsType.InvokeMember('Section', $bindingFlags, $null, $null, @())

        if($instance)
        {
            $bindingFlags = 'NonPublic','Instance'
            $useUnsafeHeaderParsingField = $settingsType.GetField('useUnsafeHeaderParsing', $bindingFlags)

            if($useUnsafeHeaderParsingField)
            {
              $useUnsafeHeaderParsingField.SetValue($instance, $ShouldEnable)
            }
        }
    }
}


$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'

Set-UseUnsafeHeaderParsing -Enable

Start-Transcript -path ("C:\{0}.log" -f $MyInvocation.MyCommand.Name) -append

add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;

    public class IDontCarePolicy : ICertificatePolicy {
        public IDontCarePolicy() {}
        public bool CheckValidationResult(
            ServicePoint sPoint, X509Certificate cert,
            WebRequest wRequest, int certProb) {
            return true;
        }
    }
"@

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

[System.Net.ServicePointManager]::CertificatePolicy = new-object IDontCarePolicy


$dtr_ip = GetNodeRoutableIp $Node
$dtr_url = GetDirectUrlFromIp $dtr_ip
$dtr_hostname = $dtr_url -replace "https://",""

WaitForUrl "${dtr_url}/ca"

invoke-webrequest -UseBasicParsing -uri "$dtr_url/ca" -o c:\ca.crt

$cert = new-object System.Security.Cryptography.X509Certificates.X509Certificate2 c:\ca.crt
$store = new-object System.Security.Cryptography.X509Certificates.X509Store('Root','localmachine')
$store.Open('ReadWrite')
$store.Add($cert)
$store.Close()

Stop-Transcript