源站地址填写为HTTP时,自动检测HTTPS是否可用

This commit is contained in:
GoEdgeLab
2024-04-18 11:31:47 +08:00
parent 6be870d1d8
commit b2b6918329
9 changed files with 166 additions and 12 deletions

View File

@@ -0,0 +1,58 @@
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package origins
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"net"
"net/url"
"strings"
"time"
)
type DetectHTTPSAction struct {
actionutils.ParentAction
}
func (this *DetectHTTPSAction) RunPost(params struct {
Addr string
}) {
this.Data["isOk"] = false
// parse from url
if strings.HasPrefix(params.Addr, "http://") || strings.HasPrefix(params.Addr, "https://") {
u, err := url.Parse(params.Addr)
if err == nil {
params.Addr = u.Host
}
}
this.Data["addr"] = params.Addr
if len(params.Addr) == 0 {
this.Success()
return
}
var realHost = params.Addr
host, port, err := net.SplitHostPort(params.Addr)
if err == nil {
if port != "80" {
this.Success()
return
}
realHost = host
}
conn, err := net.DialTimeout("tcp", configutils.QuoteIP(realHost)+":443", 3*time.Second)
if err != nil {
this.Success()
return
}
_ = conn.Close()
this.Data["isOk"] = true
this.Success()
}

View File

@@ -15,6 +15,7 @@ func init() {
Post("/delete", new(DeleteAction)).
GetPost("/updatePopup", new(UpdatePopupAction)).
Post("/updateIsOn", new(UpdateIsOnAction)).
Post("/detectHTTPS", new(DetectHTTPSAction)).
EndAll()
})
}

View File

@@ -224,12 +224,14 @@ func (this *userMustAuth) BeforeAction(actionPtr actions.ActionWrapper, paramNam
}
// 是否正在使用反向代理模式
action.Data["teaXFFPrompt"] = false
if !securityXFFPromptDisabled &&
(len(action.Header("X-Forwarded-For")) > 0 || len(action.Header("X-Real-Ip")) > 0 || len(action.Header("Cf-Connecting-Ip")) > 0) &&
securityConfig != nil &&
len(securityConfig.ClientIPHeaderNames) == 0 {
action.Data["teaXFFPrompt"] = true
if action.Request.Method == http.MethodGet {
action.Data["teaXFFPrompt"] = false
if !securityXFFPromptDisabled &&
(len(action.Header("X-Forwarded-For")) > 0 || len(action.Header("X-Real-Ip")) > 0 || len(action.Header("Cf-Connecting-Ip")) > 0) &&
securityConfig != nil &&
len(securityConfig.ClientIPHeaderNames) == 0 {
action.Data["teaXFFPrompt"] = true
}
}
// 检查用户是否存在