forked from rvinnie/yookassa-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticity.go
51 lines (41 loc) · 821 Bytes
/
authenticity.go
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
package yoonotify
import (
"net/netip"
)
var YooKassaIPs = []string{
"185.71.76.0/27",
"185.71.77.0/27",
"77.75.153.0/25",
"77.75.156.11",
"77.75.156.35",
"77.75.154.128/25",
"2a02:5180::/32",
}
type Authenticity struct {
networks []netip.Prefix
}
func InitAuthenticity(ips []string) (*Authenticity, error) {
a := &Authenticity{
networks: make([]netip.Prefix, 0, len(ips)),
}
for _, ip := range ips {
network, err := netip.ParsePrefix(ip)
if err != nil {
return nil, err
}
a.networks = append(a.networks, network)
}
return a, nil
}
func (r *Authenticity) Allowed(userIP string) (bool, error) {
ip, err := netip.ParseAddr(userIP)
if err != nil {
return false, err
}
for _, network := range r.networks {
if network.Contains(ip) {
return true, nil
}
}
return false, nil
}