89 lines
2.5 KiB
Go
89 lines
2.5 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"github.com/sirupsen/logrus"
|
|
"hy2xs-admin/dao"
|
|
"hy2xs-admin/model/constant"
|
|
"hy2xs-admin/model/vo"
|
|
"hy2xs-admin/util"
|
|
)
|
|
|
|
func InitHysteria2() error {
|
|
if !util.Exists(util.GetHysteria2BinPath()) {
|
|
return errors.New("systemd-managed hysteria binary not found")
|
|
}
|
|
|
|
config, err := dao.GetConfig("key = ?", constant.Hysteria2Enable)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if *config.Value == "1" {
|
|
logrus.Infof("hysteria2 lifecycle is managed by systemd in HY2XS production package")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Hysteria2IsRunning() bool {
|
|
_, err := util.Exec("systemctl is-active --quiet hysteria-server")
|
|
return err == nil
|
|
}
|
|
|
|
func StartHysteria2() error {
|
|
return errors.New("managed by orchestrator: use hy2xs-orchestrator reconfigure")
|
|
}
|
|
|
|
func StopHysteria2() error {
|
|
return errors.New("managed by orchestrator: use hy2xs-orchestrator reconfigure")
|
|
}
|
|
|
|
func RestartHysteria2() error {
|
|
return errors.New("managed by orchestrator: use hy2xs-orchestrator reconfigure")
|
|
}
|
|
|
|
func ReleaseHysteria2() error {
|
|
return nil
|
|
}
|
|
|
|
func Hysteria2AcmePath() (vo.Hysteria2AcmePathVo, error) {
|
|
hysteria2AcmePathVo := vo.Hysteria2AcmePathVo{}
|
|
hysteria2Config, err := GetHysteria2Config()
|
|
if err != nil {
|
|
return hysteria2AcmePathVo, err
|
|
}
|
|
if hysteria2Config.TLS != nil &&
|
|
hysteria2Config.TLS.Cert != nil && *hysteria2Config.TLS.Cert != "" &&
|
|
hysteria2Config.TLS.Key != nil && *hysteria2Config.TLS.Key != "" {
|
|
if util.Exists(*hysteria2Config.TLS.Cert) && util.Exists(*hysteria2Config.TLS.Key) {
|
|
hysteria2AcmePathVo.CrtPath = *hysteria2Config.TLS.Cert
|
|
hysteria2AcmePathVo.KeyPath = *hysteria2Config.TLS.Key
|
|
return hysteria2AcmePathVo, nil
|
|
}
|
|
return hysteria2AcmePathVo, errors.New("cert not found")
|
|
} else if hysteria2Config.ACME != nil &&
|
|
hysteria2Config.ACME.Domains != nil &&
|
|
len(hysteria2Config.ACME.Domains) > 0 &&
|
|
hysteria2Config.ACME.CA != nil &&
|
|
*hysteria2Config.ACME.CA != "" &&
|
|
hysteria2Config.ACME.Dir != nil &&
|
|
*hysteria2Config.ACME.Dir != "" {
|
|
acmeDir := *hysteria2Config.ACME.Dir
|
|
for _, domain := range hysteria2Config.ACME.Domains {
|
|
crtPath, err := util.FindFile(acmeDir, fmt.Sprintf("%s.crt", domain))
|
|
if err != nil {
|
|
continue
|
|
}
|
|
keyPath, err := util.FindFile(acmeDir, fmt.Sprintf("%s.key", domain))
|
|
if err != nil {
|
|
continue
|
|
}
|
|
hysteria2AcmePathVo.CrtPath = crtPath
|
|
hysteria2AcmePathVo.KeyPath = keyPath
|
|
return hysteria2AcmePathVo, nil
|
|
}
|
|
}
|
|
return vo.Hysteria2AcmePathVo{}, errors.New("cert not found")
|
|
}
|