-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTailscale_ACL_Switcher_Interactive.ps1
84 lines (70 loc) · 2.57 KB
/
Tailscale_ACL_Switcher_Interactive.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
###POWERSHELL###
# Set OAuth client ID and client secret
$clientID = "your_client_ID"
$clientSecret = "your_client_secret"
# Define IP address pairs with names
$ipPairs = @{
"1" = @{
Name = "OPTION 1"
OldIPAddress = "100.100.100.100"
NewIPAddress = "101.101.101.101"
}
"2" = @{
Name = "OPTION 2"
OldIPAddress = "101.101.101.101"
NewIPAddress = "100.100.100.100"
}
}
# Log start of the script
Write-Output "Starting Tailscale ACL update script..."
# Send request to obtain an access token
Write-Output "Requesting access token..."
$tokenResponse = Invoke-RestMethod -Method Post -Uri "https://api.tailscale.com/api/v2/oauth/token" -Body @{
client_id = $clientID
client_secret = $clientSecret
}
Write-Output "Token response: $($tokenResponse | ConvertTo-Json)"
# Extract access token from the response
$accessToken = $tokenResponse.access_token
Write-Output "Access token obtained: $accessToken"
# Function to update ACL policy
function Update-ACL {
param (
[string]$oldIP,
[string]$newIP
)
Write-Output "Updating ACL policy for IP address: $oldIP -> $newIP"
# Retrieve current ACL policy
$aclPolicyResponse = Invoke-RestMethod -Method Get -Uri "https://api.tailscale.com/api/v2/tailnet/-/acl" -Headers @{
Authorization = "Bearer $accessToken"
}
#Write-Output "ACL policy response: $($aclPolicyResponse | ConvertTo-Json)"
# Modify ACL policy
$modifiedPolicy = $aclPolicyResponse -replace $oldIP, $newIP
# Send modified ACL policy back to Tailscale
$response = Invoke-RestMethod -Method Post -Uri "https://api.tailscale.com/api/v2/tailnet/-/acl" -Headers @{
Authorization = "Bearer $accessToken"
"Content-Type" = "application/json"
} -Body $modifiedPolicy
Write-Output "ACL update response: $($response | ConvertTo-Json)"
# Log end of the function
Write-Output "ACL update completed."
}
# Display options to the user
Write-Output "Choose an option:"
$ipPairs.GetEnumerator() | Sort-Object { [int] $_.Key } | ForEach-Object {
Write-Output "$($_.Key). Use $($_.Value.Name) ($($_.Value.OldIPAddress) -> $($_.Value.NewIPAddress))"
}
$choice = Read-Host "Enter your choice"
if ($ipPairs.ContainsKey($choice)) {
$selectedIPs = $ipPairs[$choice]
$oldIPAddress = $selectedIPs.OldIPAddress
$newIPAddress = $selectedIPs.NewIPAddress
} else {
Write-Output "Invalid choice. Exiting."
exit 1
}
# Call the Update-ACL function with the chosen IP addresses
Update-ACL $oldIPAddress $newIPAddress
# Pause to keep PowerShell window open
#pause