forked from jonssonyan/h-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhysteria2.go
147 lines (133 loc) · 3.76 KB
/
hysteria2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package service
import (
"errors"
"fmt"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"h-ui/dao"
"h-ui/model/constant"
"h-ui/model/vo"
"h-ui/proxy"
"h-ui/util"
"os"
)
func InitHysteria2() error {
if !util.Exists(util.GetHysteria2BinPath()) {
if err := util.DownloadHysteria2(""); err != nil {
logrus.Errorf("download hysteria2 bin err: %v", err)
return errors.New("download hysteria2 bin err")
}
}
config, err := dao.GetConfig("key = ?", constant.Hysteria2Enable)
if err != nil {
return err
}
if *config.Value == "1" {
if err = StartHysteria2(); err != nil {
return err
}
}
return nil
}
func setHysteria2ConfigYAML() error {
serverConfig, err := GetHysteria2Config()
if err != nil {
return err
}
if serverConfig.Listen == nil || *serverConfig.Listen == "" {
return errors.New("hysteria2 config is empty")
}
authHttpUrl, err := GetAuthHttpUrl()
if err != nil {
return err
}
// update auth http url
if *serverConfig.Auth.HTTP.URL != authHttpUrl {
serverConfig.Auth.HTTP.URL = &authHttpUrl
if err := UpdateHysteria2Config(serverConfig); err != nil {
return err
}
}
hysteria2Config, err := yaml.Marshal(&serverConfig)
if err != nil {
logrus.Errorf("marshal hysteria2 config err: %v", err)
return errors.New("marshal hysteria2 config err")
}
file, err := os.OpenFile(constant.Hysteria2ConfigPath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
logrus.Errorf("create hysteria2 server config file err: %v", err)
return errors.New("create hysteria2 server config file err")
}
_, err = file.WriteString(string(hysteria2Config))
if err != nil {
logrus.Errorf("write hysteria2 config.json file err: %v", err)
return errors.New("hysteria2 config.json file write err")
}
return nil
}
func Hysteria2IsRunning() bool {
return proxy.NewHysteria2Instance().IsRunning()
}
func StartHysteria2() error {
if err := setHysteria2ConfigYAML(); err != nil {
return err
}
if err := proxy.NewHysteria2Instance().StartHysteria2(); err != nil {
return err
}
return nil
}
func StopHysteria2() error {
return proxy.NewHysteria2Instance().StopHysteria2()
}
func RestartHysteria2() error {
if err := StopHysteria2(); err != nil {
return err
}
if err := StartHysteria2(); err != nil {
return err
}
return nil
}
func ReleaseHysteria2() error {
return proxy.NewHysteria2Instance().Release()
}
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")
}