2024-08-28 12:31:17 +02:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2024-09-11 11:30:48 +02:00
|
|
|
"gitea.codeblob.work/pk/hoster/internals/types"
|
|
|
|
|
2024-08-28 12:31:17 +02:00
|
|
|
"github.com/gookit/goutil/fsutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func FindHostConfigs(path string) ([]types.Project, error) {
|
|
|
|
projects := []types.Project{}
|
|
|
|
|
|
|
|
fsutil.FindInDir(path, func(filePath string, de fs.DirEntry) error {
|
|
|
|
project := types.Project{}
|
|
|
|
parseHostFile(filePath, &project)
|
|
|
|
|
|
|
|
if project.Domain != "" {
|
|
|
|
projects = append(projects, project)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return projects, nil
|
|
|
|
}
|
|
|
|
|
2024-08-28 12:42:07 +02:00
|
|
|
var portRegex = regexp.MustCompile(`proxy_pass http://.+:(\d+)`)
|
2024-08-28 12:31:17 +02:00
|
|
|
var serverNameRegex = regexp.MustCompile(`server_name\s+(.+);`)
|
|
|
|
|
|
|
|
func parseHostFile(filePath string, project *types.Project) error {
|
|
|
|
file, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := strings.TrimSpace(scanner.Text())
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-08-28 12:42:07 +02:00
|
|
|
if strings.HasPrefix(line, "proxy_pass ") {
|
2024-08-28 12:31:17 +02:00
|
|
|
port := portRegex.FindStringSubmatch(line)
|
2024-08-28 12:42:07 +02:00
|
|
|
|
|
|
|
if len(port) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-08-28 12:31:17 +02:00
|
|
|
project.InternalPort, err = strconv.Atoi(port[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(line, "server_name ") {
|
|
|
|
serverName := serverNameRegex.FindStringSubmatch(line)
|
|
|
|
project.Domain = serverName[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|