Maintenance: refactor servers updater code
- Require at least 80% of number of servers now to pass - Each provider is in its own package with a common structure - Unzip package with unzipper interface - Openvpn package with extraction and download functions
This commit is contained in:
66
internal/updater/openvpn/extract.go
Normal file
66
internal/updater/openvpn/extract.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package openvpn
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNoRemoteHost = errors.New("remote host not found")
|
||||
ErrNoRemoteIP = errors.New("remote IP not found")
|
||||
)
|
||||
|
||||
func ExtractHost(b []byte) (host, warning string, err error) {
|
||||
const (
|
||||
rejectIP = true
|
||||
rejectDomain = false
|
||||
)
|
||||
hosts := extractRemoteHosts(b, rejectIP, rejectDomain)
|
||||
if len(hosts) == 0 {
|
||||
return "", "", ErrNoRemoteHost
|
||||
} else if len(hosts) > 1 {
|
||||
warning = fmt.Sprintf(
|
||||
"only using the first host %q and discarding %d other hosts",
|
||||
hosts[0], len(hosts)-1)
|
||||
}
|
||||
return hosts[0], warning, nil
|
||||
}
|
||||
|
||||
func ExtractIP(b []byte) (ip net.IP, warning string, err error) {
|
||||
const (
|
||||
rejectIP = false
|
||||
rejectDomain = true
|
||||
)
|
||||
ips := extractRemoteHosts(b, rejectIP, rejectDomain)
|
||||
if len(ips) == 0 {
|
||||
return nil, "", ErrNoRemoteIP
|
||||
} else if len(ips) > 1 {
|
||||
warning = fmt.Sprintf(
|
||||
"only using the first IP address %s and discarding %d other hosts",
|
||||
ips[0], len(ips)-1)
|
||||
}
|
||||
return net.ParseIP(ips[0]), warning, nil
|
||||
}
|
||||
|
||||
func extractRemoteHosts(content []byte, rejectIP, rejectDomain bool) (hosts []string) {
|
||||
lines := strings.Split(string(content), "\n")
|
||||
for _, line := range lines {
|
||||
if !strings.HasPrefix(line, "remote ") {
|
||||
continue
|
||||
}
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) == 1 || len(fields[1]) == 0 {
|
||||
continue
|
||||
}
|
||||
host := fields[1]
|
||||
parsedIP := net.ParseIP(host)
|
||||
if (rejectIP && parsedIP != nil) ||
|
||||
(rejectDomain && parsedIP == nil) {
|
||||
continue
|
||||
}
|
||||
hosts = append(hosts, host)
|
||||
}
|
||||
return hosts
|
||||
}
|
||||
41
internal/updater/openvpn/fetch.go
Normal file
41
internal/updater/openvpn/fetch.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package openvpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func FetchFile(ctx context.Context, client *http.Client, url string) (
|
||||
host string, err error) {
|
||||
b, err := fetchData(ctx, client, url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
const rejectIP = true
|
||||
const rejectDomain = false
|
||||
hosts := extractRemoteHosts(b, rejectIP, rejectDomain)
|
||||
if len(hosts) == 0 {
|
||||
return "", fmt.Errorf("%w for url %s", ErrNoRemoteHost, url)
|
||||
}
|
||||
|
||||
return hosts[0], nil
|
||||
}
|
||||
|
||||
func fetchData(ctx context.Context, client *http.Client, url string) (
|
||||
b []byte, err error) {
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
return io.ReadAll(response.Body)
|
||||
}
|
||||
59
internal/updater/openvpn/multifetch.go
Normal file
59
internal/updater/openvpn/multifetch.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package openvpn
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// FetchMultiFiles fetches multiple Openvpn files in parallel and
|
||||
// parses them to extract each of their host. A mapping from host to
|
||||
// URL is returned.
|
||||
func FetchMultiFiles(ctx context.Context, client *http.Client, urls []string) (
|
||||
hostToURL map[string]string, err error) {
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
|
||||
hostToURL = make(map[string]string, len(urls))
|
||||
|
||||
type Result struct {
|
||||
url string
|
||||
host string
|
||||
}
|
||||
|
||||
results := make(chan Result)
|
||||
defer close(results)
|
||||
errors := make(chan error)
|
||||
defer close(errors)
|
||||
|
||||
for _, url := range urls {
|
||||
go func(url string) {
|
||||
host, err := FetchFile(ctx, client, url)
|
||||
if err != nil {
|
||||
errors <- err
|
||||
return
|
||||
}
|
||||
results <- Result{
|
||||
url: url,
|
||||
host: host,
|
||||
}
|
||||
}(url)
|
||||
}
|
||||
|
||||
for range urls {
|
||||
select {
|
||||
case newErr := <-errors:
|
||||
if err == nil { // only assign to the first error
|
||||
err = newErr
|
||||
cancel() // stop other operations, this will trigger other errors we ignore
|
||||
}
|
||||
case result := <-results:
|
||||
hostToURL[result.host] = result.url
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return hostToURL, nil
|
||||
}
|
||||
Reference in New Issue
Block a user