2022-06-05 14:58:46 +00:00
|
|
|
package storage
|
2021-05-11 17:10:51 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
"github.com/qdm12/gluetun/internal/configuration/settings"
|
2021-05-11 17:10:51 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/constants"
|
2024-10-23 09:05:32 +00:00
|
|
|
"github.com/qdm12/gluetun/internal/constants/vpn"
|
2021-05-11 17:10:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func commaJoin(slice []string) string {
|
|
|
|
|
return strings.Join(slice, ", ")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ErrNoServerFound = errors.New("no server found")
|
|
|
|
|
|
2024-10-23 09:05:32 +00:00
|
|
|
func noServerFoundError(selection settings.ServerSelection) (err error) { //nolint:gocyclo
|
2021-05-11 17:10:51 +00:00
|
|
|
var messageParts []string
|
|
|
|
|
|
2021-08-22 14:58:39 -07:00
|
|
|
messageParts = append(messageParts, "VPN "+selection.VPN)
|
|
|
|
|
|
2021-05-11 17:10:51 +00:00
|
|
|
protocol := constants.UDP
|
2024-03-23 14:56:42 +00:00
|
|
|
if selection.OpenVPN.Protocol == constants.TCP {
|
2021-05-11 17:10:51 +00:00
|
|
|
protocol = constants.TCP
|
|
|
|
|
}
|
|
|
|
|
messageParts = append(messageParts, "protocol "+protocol)
|
|
|
|
|
|
|
|
|
|
switch len(selection.Countries) {
|
|
|
|
|
case 0:
|
|
|
|
|
case 1:
|
|
|
|
|
part := "country " + selection.Countries[0]
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
default:
|
|
|
|
|
part := "countries " + commaJoin(selection.Countries)
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-22 09:02:31 +00:00
|
|
|
switch len(selection.Categories) {
|
|
|
|
|
case 0:
|
|
|
|
|
case 1:
|
|
|
|
|
part := "category " + selection.Categories[0]
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
default:
|
|
|
|
|
part := "categories " + commaJoin(selection.Categories)
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 17:10:51 +00:00
|
|
|
switch len(selection.Regions) {
|
|
|
|
|
case 0:
|
|
|
|
|
case 1:
|
|
|
|
|
part := "region " + selection.Regions[0]
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
default:
|
|
|
|
|
part := "regions " + commaJoin(selection.Regions)
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch len(selection.Cities) {
|
|
|
|
|
case 0:
|
|
|
|
|
case 1:
|
|
|
|
|
part := "city " + selection.Cities[0]
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
default:
|
|
|
|
|
part := "cities " + commaJoin(selection.Cities)
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
if *selection.OwnedOnly {
|
2021-05-11 17:10:51 +00:00
|
|
|
messageParts = append(messageParts, "owned servers only")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch len(selection.ISPs) {
|
|
|
|
|
case 0:
|
|
|
|
|
case 1:
|
|
|
|
|
part := "ISP " + selection.ISPs[0]
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
default:
|
|
|
|
|
part := "ISPs " + commaJoin(selection.ISPs)
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch len(selection.Hostnames) {
|
|
|
|
|
case 0:
|
|
|
|
|
case 1:
|
|
|
|
|
part := "hostname " + selection.Hostnames[0]
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
default:
|
|
|
|
|
part := "hostnames " + commaJoin(selection.Hostnames)
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch len(selection.Names) {
|
|
|
|
|
case 0:
|
|
|
|
|
case 1:
|
|
|
|
|
part := "name " + selection.Names[0]
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
default:
|
|
|
|
|
part := "names " + commaJoin(selection.Names)
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch len(selection.Numbers) {
|
|
|
|
|
case 0:
|
|
|
|
|
case 1:
|
|
|
|
|
part := "server number " + strconv.Itoa(int(selection.Numbers[0]))
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
default:
|
|
|
|
|
serverNumbers := make([]string, len(selection.Numbers))
|
|
|
|
|
for i := range selection.Numbers {
|
|
|
|
|
serverNumbers[i] = strconv.Itoa(int(selection.Numbers[i]))
|
|
|
|
|
}
|
|
|
|
|
part := "server numbers " + commaJoin(serverNumbers)
|
|
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
if *selection.OpenVPN.PIAEncPreset != "" {
|
|
|
|
|
part := "encryption preset " + *selection.OpenVPN.PIAEncPreset
|
2021-05-11 17:10:51 +00:00
|
|
|
messageParts = append(messageParts, part)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-06 06:40:23 -05:00
|
|
|
if *selection.FreeOnly {
|
2021-05-23 21:51:12 +00:00
|
|
|
messageParts = append(messageParts, "free tier only")
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-15 19:54:58 -04:00
|
|
|
if *selection.PremiumOnly {
|
|
|
|
|
messageParts = append(messageParts, "premium tier only")
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-28 17:47:56 +00:00
|
|
|
if *selection.StreamOnly {
|
|
|
|
|
messageParts = append(messageParts, "stream only")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if *selection.MultiHopOnly {
|
|
|
|
|
messageParts = append(messageParts, "multihop only")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if *selection.PortForwardOnly {
|
|
|
|
|
messageParts = append(messageParts, "port forwarding only")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if *selection.SecureCoreOnly {
|
|
|
|
|
messageParts = append(messageParts, "secure core only")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if *selection.TorOnly {
|
|
|
|
|
messageParts = append(messageParts, "tor only")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if selection.TargetIP.IsValid() {
|
|
|
|
|
messageParts = append(messageParts,
|
|
|
|
|
"target ip address "+selection.TargetIP.String())
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-23 09:05:32 +00:00
|
|
|
customPort := *selection.OpenVPN.CustomPort
|
|
|
|
|
if selection.VPN == vpn.Wireguard {
|
|
|
|
|
customPort = *selection.Wireguard.EndpointPort
|
|
|
|
|
}
|
|
|
|
|
if customPort > 0 {
|
|
|
|
|
messageParts = append(messageParts,
|
|
|
|
|
fmt.Sprintf("%s endpoint port %d", selection.VPN, customPort))
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 17:10:51 +00:00
|
|
|
message := "for " + strings.Join(messageParts, "; ")
|
|
|
|
|
|
|
|
|
|
return fmt.Errorf("%w: %s", ErrNoServerFound, message)
|
|
|
|
|
}
|