Skip to content

Commit 205ea02

Browse files
authored
Merge pull request #1 from loop0/add-viaip-provider
Add viaip public ip address provider
2 parents f04b979 + a4a2ff6 commit 205ea02

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ This is the list of currently supported providers for public ip address resoluti
4444
| - | - | - |
4545
| `ipify` | https://www.ipify.org | None |
4646
| `myipio` | https://www.my-ip.io | None |
47+
| `viaip` | https://viaip.com.br | None |
4748

4849
### DNS
4950

providers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/loop0/pugdns/providers/cloudflare"
88
"github.com/loop0/pugdns/providers/ipify"
99
"github.com/loop0/pugdns/providers/myipio"
10+
"github.com/loop0/pugdns/providers/viaip"
1011
)
1112

1213
type IPAddressService interface {
@@ -28,6 +29,8 @@ func getIPAddressProvider() IPAddressService {
2829
return ipify.NewClient()
2930
case "myipio":
3031
return myipio.NewClient()
32+
case "viaip":
33+
return viaip.NewClient()
3134
default:
3235
slog.Error("Unsupported ip address provider", "provider", provider)
3336
os.Exit(1)

providers/viaip/viaip.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package viaip
2+
3+
import (
4+
"encoding/json"
5+
"io"
6+
"net/http"
7+
)
8+
9+
type ViaIPClient struct {
10+
BaseURL string
11+
}
12+
13+
type PublicIP struct {
14+
IP string `json:"ip"`
15+
}
16+
17+
func (client *ViaIPClient) GetPublicIP() (string, error) {
18+
req, err := http.NewRequest("GET", client.BaseURL, nil)
19+
if err != nil {
20+
return "", err
21+
}
22+
23+
req.Header.Set("Content-Type", "application/json")
24+
25+
httpClient := &http.Client{}
26+
resp, err := httpClient.Do(req)
27+
if err != nil {
28+
return "", err
29+
}
30+
defer resp.Body.Close()
31+
32+
body, err := io.ReadAll(resp.Body)
33+
if err != nil {
34+
return "", err
35+
}
36+
37+
publicIP := PublicIP{}
38+
json.Unmarshal(body, &publicIP)
39+
return publicIP.IP, nil
40+
}
41+
42+
func NewClient() *ViaIPClient {
43+
return &ViaIPClient{
44+
"https://viaip.com.br",
45+
}
46+
}

0 commit comments

Comments
 (0)