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:
12
internal/updater/html/attribute.go
Normal file
12
internal/updater/html/attribute.go
Normal 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 ""
|
||||
}
|
||||
43
internal/updater/html/bfs.go
Normal file
43
internal/updater/html/bfs.go
Normal 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
|
||||
}
|
||||
22
internal/updater/html/css.go
Normal file
22
internal/updater/html/css.go
Normal 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
|
||||
}
|
||||
27
internal/updater/html/errors.go
Normal file
27
internal/updater/html/errors.go
Normal 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()
|
||||
}
|
||||
43
internal/updater/html/match.go
Normal file
43
internal/updater/html/match.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user