Files
gluetun/internal/provider/surfshark/updater/zip.go
Quentin McGaw 1a6e8d74d6 wip
2024-08-01 07:51:35 +00:00

77 lines
2.0 KiB
Go

package updater
import (
"context"
"strings"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/provider/common"
"github.com/qdm12/gluetun/internal/provider/surfshark/servers"
"github.com/qdm12/gluetun/internal/updater/openvpn"
)
func addOpenVPNServersFromZip(ctx context.Context,
unzipper common.Unzipper, hts hostToServer) (
warnings []string, err error) {
const url = "https://my.surfshark.com/vpn/api/v1/server/configurations"
contents, err := unzipper.FetchAndExtract(ctx, url)
if err != nil {
return nil, err
}
hostnamesDone := hts.toHostsSlice()
hostnamesDoneSet := make(map[string]struct{}, len(hostnamesDone))
for _, hostname := range hostnamesDone {
hostnamesDoneSet[hostname] = struct{}{}
}
locationData := servers.LocationData()
hostToLocation := hostToLocation(locationData)
for fileName, content := range contents {
if !strings.HasSuffix(fileName, ".ovpn") {
continue // not an OpenVPN file
}
host, warning, err := openvpn.ExtractHost(content)
if warning != "" {
warnings = append(warnings, warning)
}
if err != nil {
// treat error as warning and go to next file
warning := err.Error() + " in " + fileName
// TODO gather location data for IP address Openvpn files
// and process those when this error triggers.
warnings = append(warnings, warning)
continue
}
_, ok := hostnamesDoneSet[host]
if ok {
continue // already done in API
}
tcp, udp, err := openvpn.ExtractProto(content)
if err != nil {
// treat error as warning and go to next file
warning := err.Error() + " in " + fileName
warnings = append(warnings, warning)
continue
}
data, err := getHostInformation(host, hostToLocation)
if err != nil {
// treat error as warning and go to next file
warning := err.Error()
warnings = append(warnings, warning)
continue
}
const wgPubKey = ""
hts.add(host, vpn.OpenVPN, data.Region, data.Country, data.City,
data.RetroLoc, wgPubKey, tcp, udp)
}
return warnings, nil
}