Skip to content

Instantly share code, notes, and snippets.

@meistermeier
Created August 28, 2024 07:38
Show Gist options
  • Select an option

  • Save meistermeier/df1aec33c53b5b03a26b7cd842faf708 to your computer and use it in GitHub Desktop.

Select an option

Save meistermeier/df1aec33c53b5b03a26b7cd842faf708 to your computer and use it in GitHub Desktop.

Revisions

  1. meistermeier created this gist Aug 28, 2024.
    69 changes: 69 additions & 0 deletions proxy.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    $server = "<your_host>"
    $port = "<your_port>"

    # Set the proxy without any check (for configuring it before entering the network)
    function Set-Proxy-No-Check {
    Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyServer -Value "$($server):$($port)"
    Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyEnable -Value 1
    }

    # Set the proxy with a check (needs to be already in the same network)
    function Set-Proxy {
    #Test if the TCP Port on the server is open before applying the settings
    If ((Test-NetConnection -ComputerName $server -Port $port).TcpTestSucceeded) {
    Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyServer -Value "$($server):$($port)"
    Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyEnable -Value 1
    }
    Else {
    Write-Error -Message "The proxy address is not valid: $($server):$($port)"
    }
    }

    function Remove-Proxy (){
    Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyServer -Value ""
    Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' -name ProxyEnable -Value 0
    }

    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Proxy'
    $form.Size = New-Object System.Drawing.Size(200,200)
    $form.StartPosition = 'CenterScreen'
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(25,120)
    $OKButton.Size = New-Object System.Drawing.Size(75,25)
    $OKButton.Text = 'OK'
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $form.AcceptButton = $OKButton
    $form.Controls.Add($OKButton)
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(100,120)
    $CancelButton.Size = New-Object System.Drawing.Size(75,25)
    $CancelButton.Text = 'Cancel'
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $form.CancelButton = $CancelButton
    $form.Controls.Add($CancelButton)
    $listBox = New-Object System.Windows.Forms.ListBox
    $listBox.Height = 80
    $listBox.Width = 200
    [void] $listBox.Items.Add('Proxy need')
    [void] $listBox.Items.Add('Proxy need no check')
    [void] $listBox.Items.Add('Home')
    $form.Controls.Add($listBox)
    $form.Topmost = $true
    $result = $form.ShowDialog()
    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
    $x = $listBox.SelectedItem
    If ($x -eq "Proxy need") {
    Set-Proxy
    }
    ElseIf ($x -eq "Proxy need no check") {
    Set-Proxy-No-Check
    }
    Else {
    Remove-Proxy
    }
    }