src/IPvMe.psm1

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
<#
IPvMe PowerShell module -- module source code
Copyright (C) 2022 Colin Cogle.

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along
with this program. If not, see <https://www.gnu.org/licenses/>.
#>


#Requires -Version 5.1

Function Get-UserAgent {
    [OutputType([String])]
    Param()

    Return "PowerShell/$($PSVersionTable.PSVersion) IPvMe/$((Get-Module -Name 'IPvMe').Version)"
}

Function Get-WanIpAddress {
    [CmdletBinding(DefaultParameterSetName='DualStack')]
    [OutputType([IPAddress[]])]
    [Alias('Get-WanIpAddresses', 'Get-PublicIPAddress', 'Get-PublicIPAddresses')]
    Param(
        [Parameter(ParameterSetName='IPv4Only')]
        [Switch] $IPv4Only,

        [Parameter(ParameterSetName='IPv6Only')]
        [Switch] $IPv6Only,

        [ValidateSet('ipify', 'ip6.me', 'MyIP')]
        [String] $Service = 'ip6.me'
    )

    $IPAddresses = [IPAddress[]]@()

    If (-Not $IPv6Only) {
        Write-Debug "Connecting to ${Service} to find an IPv4 adddress."
        $Params = @{'AddressFamily' = 'IPv4'}
        $IPAddresses += &$("Invoke-${Service}") @Params
    }
    If (-Not $IPv4Only) {
        Write-Debug "Connecting to ${Service} to find an IPv6 adddress."
        $Params = @{'AddressFamily' = 'IPv6'}
        $IPAddresses += &$("Invoke-${Service}") @Params
    }

    Return $IPAddresses
}

Function Invoke-ipify {
    [OutputType([IPAddress])]
    Param(
        [Parameter(Mandatory)]
        [ValidateSet('IPv4','IPv6')]
        [String] $AddressFamily
    )

    $IPAddress = $null
    Try {
        If ($AddressFamily = 'IPv4') {
            $IPAddress = [IPAddress](
                Invoke-RestMethod -Uri 'https://api.ipify.org' -UserAgent (Get-UserAgent)
            )
        }
        Else <# IPv6 #> {
            $IPAddress = [IPAddress](
                Invoke-RestMethod -Uri 'https://api64.ipify.org' -UserAgent (Get-UserAgent)
            )
            If ($IPAddress.AddressFamily -ne 'InterNetworkV6') {
                $IPAddress = $null
            }
        }
    }
    Catch {
        Write-Warning "Failed to get $AddressFamily address from api.ipify.org."
    }

    Return $IPAddress
}

Function Invoke-ip6.me {
    [OutputType([IPAddress])]
    Param(
        [Parameter(Mandatory)]
        [ValidateSet('IPv4','IPv6')]
        [String] $AddressFamily
    )

    $IPAddress = $null

    Try {
        $IPAddress = [IPAddress](
            ((Invoke-RestMethod -Uri "https://ip$($AddressFamily[3])only.me/api/" -UserAgent (Get-UserAgent)) -Split ',')[1]
        )
    }
    Catch {
        Write-Warning "Failed to get $AddressFamily address from ip$($AddressFamily[3])only.me."
    }

    Return $IPAddress
}

Function Invoke-MyIP {
    [OutputType([IPAddress])]
    Param(
        [ValidateSet('IPv4','IPv6')]
        [String] $AddressFamily
    )

    $IPAddress = $null

    Try {
        $IPAddress = [IPAddress](
            Invoke-RestMethod -Uri "https://api$($AddressFamily[3]).my-ip.io/ip.txt" -UserAgent (Get-UserAgent)
        )
    }
    Catch {
        Write-Warning "Failed to get $AddressFamily address from api$($AddressFamily[3]).my-ip.io."
    }

    Return $IPAddress
}