package updater import ( "context" "errors" "fmt" "net/http" "regexp" "strings" "unicode" "unicode/utf8" "github.com/qdm12/gluetun/internal/models" "github.com/qdm12/gluetun/internal/provider/common" htmlutils "github.com/qdm12/gluetun/internal/updater/html" "golang.org/x/net/html" ) func fetchServers(ctx context.Context, client *http.Client, warner common.Warner, ) (servers []models.Server, err error) { const url = "https://www.vpnsecure.me/locations/" rootNode, err := htmlutils.Fetch(ctx, client, url) if err != nil { return nil, fmt.Errorf("fetching HTML code: %w", err) } servers, warnings, err := parseHTML(rootNode) for _, warning := range warnings { warner.Warn(warning) } if err != nil { return nil, fmt.Errorf("parsing HTML code: %w", err) } return servers, nil } const divString = "div" func parseHTML(rootNode *html.Node) (servers []models.Server, warnings []string, err error, ) { // Find div container for all servers, searching with BFS. serversDiv := htmlutils.BFS(rootNode, htmlutils.MatchID("servers")) if serversDiv == nil { return nil, nil, htmlutils.WrapError(errors.New("HTML servers container div not found"), rootNode) } for countryNode := serversDiv.FirstChild; countryNode != nil; countryNode = countryNode.NextSibling { if countryNode.Data != divString || !htmlutils.HasClassStrings(countryNode, "box", "dark-gray") { continue } country := findCountry(countryNode) if country == "" { warnings = append(warnings, htmlutils.WrapWarning("country not found", countryNode)) continue } // Find all server tables within this country container for serverNode := countryNode.FirstChild; serverNode != nil; serverNode = serverNode.NextSibling { if serverNode.Data != divString { continue } if !htmlutils.HasClassStrings(serverNode, "box", "white") { continue } server, warning := parseServerNode(serverNode, country) if warning != "" { warnings = append(warnings, warning) continue } servers = append(servers, server) } } return servers, warnings, nil } func parseServerNode(node *html.Node, country string) ( server models.Server, warning string, ) { // Find the table within this server box tableNode := htmlutils.DirectChild(node, func(n *html.Node) bool { return n != nil && n.Data == "table" }) if tableNode == nil { return server, htmlutils.WrapWarning("server table not found", node) } // Check status from green-circle in thead isUp := hasGreenCircle(tableNode) if !isUp { warning := "skipping server which is not up" return server, htmlutils.WrapWarning(warning, tableNode) } // Extract city from thead city := findCity(tableNode) if city == "" { return server, htmlutils.WrapWarning("city not found", tableNode) } // Extract server identifier (e.g., "AU #01") from thead serverID := findServerID(tableNode) if serverID == "" { return server, htmlutils.WrapWarning("server ID not found", tableNode) } hostname, err := buildHostname(serverID) if err != nil { return server, htmlutils.WrapWarning(fmt.Sprintf("invalid server ID %q: %v", serverID, err), tableNode) } // Check for Dedicated IP feature (maps to Premium) premium := hasFeature(tableNode, "Dedicated IP") return models.Server{ Country: country, City: city, Hostname: hostname, Premium: premium, }, "" } var serverIDPattern = regexp.MustCompile(`^([A-Z]{2})\s*#\s*(\d+)$`) func buildHostname(serverID string) (string, error) { const expectedSubmatches = 3 matches := serverIDPattern.FindStringSubmatch(serverID) if len(matches) != expectedSubmatches { return "", errors.New("unexpected format") } countryCode := strings.ToLower(matches[1]) serverNum := strings.TrimLeft(matches[2], "0") return fmt.Sprintf("%s%s.isponeder.com", countryCode, serverNum), nil } func findCountry(countryNode *html.Node) (country string) { h3Node := htmlutils.DirectChild(countryNode, func(n *html.Node) bool { return n != nil && n.Data == "h3" }) if h3Node == nil { return "" } // Extract text content from h3, trimming the flag emoji and whitespace var sb strings.Builder for child := h3Node.FirstChild; child != nil; child = child.NextSibling { if child.Type == html.TextNode { sb.WriteString(child.Data) } } country = strings.TrimSpace(sb.String()) // Strip leading emoji (flag) - flags are typically composed emoji characters for len(country) > 0 { r, size := utf8.DecodeRuneInString(country) if !isEmojiRune(r) { break } country = country[size:] } country = strings.TrimSpace(country) return country } func findCity(tableNode *html.Node) (city string) { const minThCount = 2 theadNode := htmlutils.DirectChild(tableNode, func(n *html.Node) bool { return n != nil && n.Data == "thead" }) if theadNode == nil { return "" } // Find the second