feat(es):增加ES实例中对HTTPS协议的支持,默认证书免校验

This commit is contained in:
fudawei
2025-10-23 15:29:27 +08:00
parent dba19b1e66
commit e6c89fad1b
10 changed files with 84 additions and 10 deletions

View File

@@ -6,6 +6,7 @@ import (
type InstanceForm struct {
Id uint64 `json:"id"`
Protocol string `binding:"required" json:"protocol"`
Name string `binding:"required" json:"name"`
Host string `binding:"required" json:"host"`
Port int `binding:"required" json:"port"`

View File

@@ -9,13 +9,14 @@ type InstanceListVO struct {
tagentity.AuthCerts // 授权凭证信息
tagentity.ResourceTags
Id *int64 `json:"id"`
Code string `json:"code"`
Name *string `json:"name"`
Host *string `json:"host"`
Port *int `json:"port"`
Version *string `json:"version"`
Remark *string `json:"remark"`
Id *int64 `json:"id"`
Code string `json:"code"`
Name *string `json:"name"`
Protocol *string `json:"protocol"`
Host *string `json:"host"`
Port *int `json:"port"`
Version *string `json:"version"`
Remark *string `json:"remark"`
CreateTime *time.Time `json:"createTime"`
Creator *string `json:"creator"`

View File

@@ -10,6 +10,7 @@ type EsInstance struct {
Code string `json:"code" gorm:"size:32;not null;"`
Name string `json:"name" gorm:"size:32;not null;"`
Protocol string `json:"protocol" gorm:"size:10;not null;"`
Host string `json:"host" gorm:"size:255;not null;"`
Port int `json:"port"`
Network string `json:"network" gorm:"size:20;"`

View File

@@ -1,6 +1,7 @@
package esi
import (
"crypto/tls"
"fmt"
"mayfly-go/internal/machine/mcm"
"mayfly-go/pkg/logx"
@@ -52,6 +53,16 @@ func (d *EsConn) StartProxy() error {
d.proxy = httputil.NewSingleHostReverseProxy(targetURL)
// 设置 proxy buffer pool
d.proxy.BufferPool = NewBufferPool()
// Configure TLS to skip certificate verification for non-compliant certificates
if targetURL.Scheme == "https" {
d.proxy.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
return nil
}

View File

@@ -23,6 +23,7 @@ type EsInfo struct {
InstanceId uint64 // 实例id
Name string
Protocol string // 协议默认http
Host string
Port int
Network string
@@ -90,7 +91,14 @@ func (di *EsInfo) Ping() (map[string]any, error) {
// ExecApi 执行api
func (di *EsInfo) ExecApi(method, path string, data any, timeoutSecond ...int) (map[string]any, error) {
request := httpx.NewReq(di.baseUrl + path)
var request *httpx.Req
// Use insecure TLS client for HTTPS connections to handle non-compliant certificates
if di.Protocol == "https" {
request = httpx.NewReqWithInsecureTLS(di.baseUrl + path)
} else {
request = httpx.NewReq(di.baseUrl + path)
}
if di.authorization != "" {
request.Header("Authorization", di.authorization)
}
@@ -117,6 +125,11 @@ func (di *EsInfo) ExecApi(method, path string, data any, timeoutSecond ...int) (
// 如果使用了ssh隧道将其host port改变其本地映射host port
func (di *EsInfo) IfUseSshTunnelChangeIpPort(ctx context.Context) error {
// 设置默认协议
if di.Protocol == "" {
di.Protocol = "http"
}
// 开启ssh隧道
if di.SshTunnelMachineId > 0 {
stm, err := GetSshTunnel(ctx, di.SshTunnelMachineId)
@@ -130,9 +143,9 @@ func (di *EsInfo) IfUseSshTunnelChangeIpPort(ctx context.Context) error {
di.Host = exposedIp
di.Port = exposedPort
di.useSshTunnel = true
di.baseUrl = fmt.Sprintf("http://%s:%d", exposedIp, exposedPort)
di.baseUrl = fmt.Sprintf("%s://%s:%d", di.Protocol, exposedIp, exposedPort)
} else {
di.baseUrl = fmt.Sprintf("http://%s:%d", di.Host, di.Port)
di.baseUrl = fmt.Sprintf("%s://%s:%d", di.Protocol, di.Host, di.Port)
}
return nil
}