Довёл до прода интеграцию Hysteria2: безопасный URI, тесты, актуализация доков и hardening env
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"hy2xs-admin/model/bo"
|
||||
"hy2xs-admin/model/constant"
|
||||
"hy2xs-admin/proxy"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -255,34 +256,54 @@ func Hysteria2Url(accountId int64) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
urlConfig := ""
|
||||
if hysteria2Config.Obfs != nil &&
|
||||
hysteria2Config.Obfs.Type != nil &&
|
||||
*hysteria2Config.Obfs.Type == "salamander" &&
|
||||
hysteria2Config.Obfs.Salamander != nil &&
|
||||
hysteria2Config.Obfs.Salamander.Password != nil &&
|
||||
*hysteria2Config.Obfs.Salamander.Password != "" {
|
||||
urlConfig += fmt.Sprintf("&obfs=salamander&obfs-password=%s", *hysteria2Config.Obfs.Salamander.Password)
|
||||
}
|
||||
|
||||
if hysteria2Config.ACME != nil &&
|
||||
hysteria2Config.ACME.Domains != nil &&
|
||||
len(hysteria2Config.ACME.Domains) > 0 {
|
||||
urlConfig += fmt.Sprintf("&sni=%s", hysteria2Config.ACME.Domains[0])
|
||||
}
|
||||
|
||||
urlConfig += "&insecure=0"
|
||||
|
||||
hysteria2ConfigRemark, err := dao.GetConfig("key = ?", constant.Hysteria2ConfigRemark)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if *hysteria2ConfigRemark.Value != "" {
|
||||
urlConfig += fmt.Sprintf("#%s", *hysteria2ConfigRemark.Value)
|
||||
|
||||
remark := ""
|
||||
if hysteria2ConfigRemark.Value != nil {
|
||||
remark = *hysteria2ConfigRemark.Value
|
||||
}
|
||||
if urlConfig != "" {
|
||||
urlConfig = "/?" + strings.TrimPrefix(urlConfig, "&")
|
||||
|
||||
obfsType := ""
|
||||
obfsPassword := ""
|
||||
if hysteria2Config.Obfs != nil &&
|
||||
hysteria2Config.Obfs.Type != nil &&
|
||||
hysteria2Config.Obfs.Salamander != nil &&
|
||||
hysteria2Config.Obfs.Salamander.Password != nil {
|
||||
obfsType = *hysteria2Config.Obfs.Type
|
||||
obfsPassword = *hysteria2Config.Obfs.Salamander.Password
|
||||
}
|
||||
return fmt.Sprintf("hysteria2://%s@%s:%d", *account.ConPass, hostname, port) + urlConfig, nil
|
||||
|
||||
sni := ""
|
||||
if hysteria2Config.ACME != nil && len(hysteria2Config.ACME.Domains) > 0 {
|
||||
sni = hysteria2Config.ACME.Domains[0]
|
||||
}
|
||||
|
||||
return buildHysteria2Url(*account.ConPass, hostname, port, obfsType, obfsPassword, sni, remark), nil
|
||||
}
|
||||
|
||||
func buildHysteria2Url(conPass string, hostname string, port int, obfsType string, obfsPassword string, sni string, remark string) string {
|
||||
query := url.Values{}
|
||||
if obfsType == "salamander" && obfsPassword != "" {
|
||||
query.Set("obfs", "salamander")
|
||||
query.Set("obfs-password", obfsPassword)
|
||||
}
|
||||
if sni != "" {
|
||||
query.Set("sni", sni)
|
||||
}
|
||||
query.Set("insecure", "0")
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "hysteria2",
|
||||
User: url.User(conPass),
|
||||
Host: net.JoinHostPort(hostname, strconv.Itoa(port)),
|
||||
Path: "/",
|
||||
RawQuery: query.Encode(),
|
||||
}
|
||||
if strings.TrimSpace(remark) != "" {
|
||||
u.Fragment = remark
|
||||
}
|
||||
return u.String()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user