mirror of
https://github.com/qdm12/gluetun.git
synced 2026-05-09 20:29:23 +02:00
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:
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user