管理端全部功能跑通

This commit is contained in:
robin
2026-02-27 10:35:22 +08:00
parent 4d275c921d
commit 150799f41d
263 changed files with 22664 additions and 4053 deletions

View File

@@ -0,0 +1,18 @@
package utils
import (
"encoding/binary"
"net"
)
func IP2Long(ip string) uint32 {
s := net.ParseIP(ip)
if s == nil {
return 0
}
if len(s) == 16 {
return binary.BigEndian.Uint32(s[12:16])
}
return binary.BigEndian.Uint32(s)
}

View File

@@ -0,0 +1,46 @@
package utils
import (
"os"
"path/filepath"
"sync"
"github.com/iwind/TeaGo/Tea"
)
type ServiceManager struct {
Name string
Description string
onceLocker sync.Once
}
func NewServiceManager(name, description string) *ServiceManager {
manager := &ServiceManager{
Name: name,
Description: description,
}
manager.resetRoot()
return manager
}
func (m *ServiceManager) setup() {
m.onceLocker.Do(func() {})
}
func (m *ServiceManager) resetRoot() {
if !Tea.IsTesting() {
exePath, err := os.Executable()
if err != nil {
exePath = os.Args[0]
}
link, err := filepath.EvalSymlinks(exePath)
if err == nil {
exePath = link
}
fullPath, err := filepath.Abs(exePath)
if err == nil {
Tea.UpdateRoot(filepath.Dir(filepath.Dir(fullPath)))
}
}
}

View File

@@ -0,0 +1,65 @@
//go:build linux
// +build linux
package utils
import (
"errors"
"os"
"os/exec"
teaconst "github.com/TeaOSLab/EdgeHttpDNS/internal/const"
)
var systemdServiceFile = "/etc/systemd/system/" + teaconst.SystemdServiceName + ".service"
func (m *ServiceManager) Install(exePath string, args []string) error {
if os.Getgid() != 0 {
return errors.New("only root users can install the service")
}
systemd, err := exec.LookPath("systemctl")
if err != nil {
return err
}
desc := `[Unit]
Description=GoEdge HTTPDNS Node Service
Before=shutdown.target
After=network-online.target
[Service]
Type=simple
Restart=always
RestartSec=1s
ExecStart=` + exePath + ` daemon
ExecStop=` + exePath + ` stop
ExecReload=` + exePath + ` restart
[Install]
WantedBy=multi-user.target`
err = os.WriteFile(systemdServiceFile, []byte(desc), 0777)
if err != nil {
return err
}
_ = exec.Command(systemd, "stop", teaconst.SystemdServiceName+".service").Run()
_ = exec.Command(systemd, "daemon-reload").Run()
return exec.Command(systemd, "enable", teaconst.SystemdServiceName+".service").Run()
}
func (m *ServiceManager) Uninstall() error {
if os.Getgid() != 0 {
return errors.New("only root users can uninstall the service")
}
systemd, err := exec.LookPath("systemctl")
if err != nil {
return err
}
_ = exec.Command(systemd, "disable", teaconst.SystemdServiceName+".service").Run()
_ = exec.Command(systemd, "daemon-reload").Run()
return os.Remove(systemdServiceFile)
}

View File

@@ -0,0 +1,14 @@
//go:build !linux
// +build !linux
package utils
import "errors"
func (m *ServiceManager) Install(exePath string, args []string) error {
return errors.New("service install is only supported on linux in this version")
}
func (m *ServiceManager) Uninstall() error {
return errors.New("service uninstall is only supported on linux in this version")
}

View File

@@ -0,0 +1,15 @@
package utils
import "strings"
func VersionToLong(version string) uint32 {
countDots := strings.Count(version, ".")
if countDots == 2 {
version += ".0"
} else if countDots == 1 {
version += ".0.0"
} else if countDots == 0 {
version += ".0.0.0"
}
return IP2Long(version)
}