feat: SlickVPN Support (#961)

- `internal/updater/html` package
- Add unit tests for slickvpn updating code
- Change shared html package to be more share-able
- Split html utilities in multiple files
- Fix processing .ovpn files with prefix space

Authored by @Rohaq 
Co-authored-by: Quentin McGaw <quentin.mcgaw@gmail.com>
This commit is contained in:
Richard Hodgson
2022-08-15 16:25:06 +01:00
committed by GitHub
parent 617bd0c600
commit d0dfc21e2b
27 changed files with 2582 additions and 16 deletions

View File

@@ -0,0 +1,12 @@
package html
import "golang.org/x/net/html"
func Attribute(node *html.Node, key string) (value string) {
for _, attribute := range node.Attr {
if attribute.Key == key {
return attribute.Val
}
}
return ""
}

View File

@@ -0,0 +1,43 @@
package html
import (
"container/list"
"fmt"
"golang.org/x/net/html"
)
// BFS returns the node matching the match function and nil
// if no node is found.
func BFS(rootNode *html.Node, match MatchFunc) (node *html.Node) {
visited := make(map[*html.Node]struct{})
queue := list.New()
_ = queue.PushBack(rootNode)
for queue.Len() > 0 {
listElement := queue.Front()
node, ok := queue.Remove(listElement).(*html.Node)
if !ok {
panic(fmt.Sprintf("linked list has bad type %T", listElement.Value))
}
if node == nil {
continue
}
if _, ok := visited[node]; ok {
continue
}
visited[node] = struct{}{}
if match(node) {
return node
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
_ = queue.PushBack(child)
}
}
return nil
}

View File

@@ -0,0 +1,22 @@
package html
import (
"strings"
"golang.org/x/net/html"
)
func HasClassStrings(node *html.Node, classStrings ...string) (match bool) {
targetClasses := make(map[string]struct{}, len(classStrings))
for _, classString := range classStrings {
targetClasses[classString] = struct{}{}
}
classAttribute := Attribute(node, "class")
classes := strings.Fields(classAttribute)
for _, class := range classes {
delete(targetClasses, class)
}
return len(targetClasses) == 0
}

View File

@@ -0,0 +1,27 @@
package html
import (
"bytes"
"fmt"
"golang.org/x/net/html"
)
func WrapError(sentinelError error, node *html.Node) error {
return fmt.Errorf("%w: in HTML code: %s",
sentinelError, mustRenderHTML(node))
}
func WrapWarning(warning string, node *html.Node) string {
return fmt.Sprintf("%s: in HTML code: %s",
warning, mustRenderHTML(node))
}
func mustRenderHTML(node *html.Node) (rendered string) {
stringBuffer := bytes.NewBufferString("")
err := html.Render(stringBuffer, node)
if err != nil {
panic(err)
}
return stringBuffer.String()
}

View File

@@ -0,0 +1,43 @@
package html
import (
"golang.org/x/net/html"
)
type MatchFunc func(node *html.Node) (match bool)
func MatchID(id string) MatchFunc {
return func(node *html.Node) (match bool) {
if node == nil {
return false
}
return Attribute(node, "id") == id
}
}
func MatchData(data string) MatchFunc {
return func(node *html.Node) (match bool) {
return node != nil && node.Type == html.ElementNode && node.Data == data
}
}
func DirectChild(parent *html.Node,
matchFunc MatchFunc) (child *html.Node) {
for child := parent.FirstChild; child != nil; child = child.NextSibling {
if matchFunc(child) {
return child
}
}
return nil
}
func DirectChildren(parent *html.Node,
matchFunc MatchFunc) (children []*html.Node) {
for child := parent.FirstChild; child != nil; child = child.NextSibling {
if matchFunc(child) {
children = append(children, child)
}
}
return children
}