-
Notifications
You must be signed in to change notification settings - Fork 12
/
Optimize-Peers.ps1
101 lines (96 loc) · 2.96 KB
/
Optimize-Peers.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
# Compare peers on nodes, remove redundant peers
function Optimize-ETHPeers {
[CmdletBinding()]
param (
$Nodes = @(
"http://node1:8545",
"http://node2:8545",
"http://node3:8545"
)
)
begin {
$PeerDict = [hashtable]@{}
}
process {
# Get all of the current peers
foreach ($Node in $Nodes) {
$Peers = (Get-ETHPeers -Nodes $Node).enode
ForEach ($Peer in $Peers) {
if ($Peer -match "enode://(.*)@(.*):(.*)") {
$Enode = $Matches[1]
$IP = $Matches[2]
$Port = $Matches[3]
if (!$PeerDict.$Enode) {
$PeerDict.Add($Enode,[System.Collections.Generic.List[Hashtable]]::new())
}
$PeerDict.$Enode.Add(
@{
"Node" = $Node
"IP" = $IP
"Port" = $Port
}
)
}
}
}
ForEach ($Peer in $PeerDict.Keys) {
# Ignore Private IP Space (we want local nodes peered)
if ($PeerDict.$Peer.IP -match "@192\.168|@172\.1[6-9]\.|@172\.2[0-9]\.|172.3[0-2]\.|@10\.") {
continue
}
if ($PeerDict.$Peer.Count -gt 1) {
Get-Random ($PeerDict.$Peer -as [array]) -Count ($PeerDict.$Peer.Count - 1) | ForEach-Object {
Write-Host "Removing $Peer from $($_.Node)" -ForegroundColor Cyan
try {
$null = Remove-ETHPeers -Node $_.Node -Peers "enode://$Peer@$($_.IP):$($_.Port)"
Write-Host "Success" -ForegroundColor Green
}
catch {
Write-Host "Failed" -ForegroundColor Red
}
}
}
}
}
}
function Get-ETHPeers {
[CmdletBinding()]
param (
[Alias("Node")]
[uri[]]$Nodes
)
begin {
$Body = @{
"jsonrpc" = "2.0"
"method" = "admin_peers"
"id" = Get-Random
}
}
process {
ForEach ($Node in $Nodes) {
(Invoke-RestMethod -Method Post -Uri $Node -Body ($Body | ConvertTo-Json) -ContentType 'application/json').result
}
}
}
function Remove-ETHPeers {
[CmdletBinding()]
param (
[uri]$Node,
[Alias("Peer")]
[uri[]]$Peers
)
begin {
$Body = @{
"jsonrpc" = "2.0"
"method" = "admin_removePeer"
"params" = [System.Object]::new()
"id" = Get-Random
}
}
process {
ForEach ($Peer in $Peers) {
$Body.params = $Peer -as [array]
(Invoke-RestMethod -Method Post -Uri $Node -Body ($Body | ConvertTo-Json) -ContentType 'application/json').result
}
}
}