init
Some checks failed
Build and Release / release (push) Failing after 26s

This commit is contained in:
2025-12-12 17:37:34 +07:00
commit 9d769ed08c
32 changed files with 895 additions and 0 deletions

67
utils.go Normal file
View File

@@ -0,0 +1,67 @@
package main
import (
"net"
"os"
"slices"
"strconv"
"strings"
)
func matchDomain(host string, list []string) bool {
for _, d := range list {
if strings.HasSuffix(host, d) {
return true
}
}
return false
}
func matchURL(url string, list []string) bool {
for _, u := range list {
if strings.HasPrefix(url, u) {
return true
}
}
return false
}
func cleanHost(h string) string {
if idx := strings.LastIndex(h, ":"); idx != -1 {
return h[:idx]
}
return h
}
func findFreePort(blocked []int) string {
for {
ln, err := net.Listen("tcp", ":0")
if err != nil {
return "-1"
}
port := ln.Addr().(*net.TCPAddr).Port
ln.Close()
if !slices.Contains(blocked, port) {
return strconv.Itoa(port)
}
}
}
func parseBlockedPorts(s string) []int {
var ports []int
if s == "" {
return ports
}
for _, p := range strings.Split(s, ",") {
if port, err := strconv.Atoi(strings.TrimSpace(p)); err == nil {
ports = append(ports, port)
}
}
return ports
}
func exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}