Faster servers information updater (#248)
* Asynchronous repeatResolve * Parallel cyberghost and PIA (v3) processing, with a 10 goroutines limit * Add missing vyprvpn cli flag to updater * Increase DNS repetitions to 5 in order to obtain more IP addresses * Update old PIA IP addresses * Add Surfshark servers by API (unused for now)
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net"
|
||||
"sort"
|
||||
)
|
||||
|
||||
func newResolver(resolverAddress string) *net.Resolver {
|
||||
@@ -30,12 +32,49 @@ func newLookupIP(r *net.Resolver) lookupIPFunc {
|
||||
}
|
||||
|
||||
func resolveRepeat(ctx context.Context, lookupIP lookupIPFunc, host string, n int) (ips []net.IP, err error) {
|
||||
foundIPs := make(chan []net.IP)
|
||||
errors := make(chan error)
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
for i := 0; i < n; i++ {
|
||||
newIPs, err := lookupIP(ctx, host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ips = append(ips, newIPs...)
|
||||
go func() {
|
||||
newIPs, err := lookupIP(ctx, host)
|
||||
if err != nil {
|
||||
errors <- err
|
||||
} else {
|
||||
foundIPs <- newIPs
|
||||
}
|
||||
}()
|
||||
}
|
||||
return uniqueSortedIPs(ips), nil
|
||||
|
||||
uniqueIPs := make(map[string]struct{})
|
||||
for i := 0; i < n; i++ {
|
||||
select {
|
||||
case newIPs := <-foundIPs:
|
||||
for _, ip := range newIPs {
|
||||
key := ip.String()
|
||||
uniqueIPs[key] = struct{}{}
|
||||
}
|
||||
case newErr := <-errors:
|
||||
if err == nil {
|
||||
err = newErr
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ips = make([]net.IP, 0, len(uniqueIPs))
|
||||
for key := range uniqueIPs {
|
||||
ip := net.ParseIP(key)
|
||||
if ipv4 := ip.To4(); ipv4 != nil {
|
||||
ip = ipv4
|
||||
}
|
||||
ips = append(ips, ip)
|
||||
}
|
||||
|
||||
sort.Slice(ips, func(i, j int) bool {
|
||||
return bytes.Compare(ips[i], ips[j]) < 1
|
||||
})
|
||||
|
||||
return ips, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user