Files
mayfly-go/server/internal/es/esm/esi/conn.go

78 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package esi
import (
"crypto/tls"
"fmt"
"mayfly-go/internal/machine/mcm"
"mayfly-go/pkg/logx"
"net/http"
"net/http/httputil"
"net/url"
)
type EsConn struct {
Id uint64
Info *EsInfo
proxy *httputil.ReverseProxy
}
/******************* pool.Conn impl *******************/
func (d *EsConn) Close() error {
// 如果是使用了ssh隧道转发则需要手动将其关闭
if d.Info.useSshTunnel {
mcm.CloseSshTunnelMachine(uint64(d.Info.SshTunnelMachineId), fmt.Sprintf("es:%d", d.Id))
}
return nil
}
func (d *EsConn) Ping() error {
// 首先检查d是否为nil
if d == nil {
return fmt.Errorf("es connection is nil")
}
// 然后检查d.Info是否为nil这是避免空指针异常的关键
if d.Info == nil {
return fmt.Errorf("es Info is nil")
}
_, err := d.Info.Ping()
return err
}
// StartProxy 开始代理
func (d *EsConn) StartProxy() error {
// 目标 URL
targetURL, err := url.Parse(d.Info.baseUrl)
if err != nil {
logx.Errorf("Error parsing URL: %v", err)
return err
}
// 创建反向代理
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
}
func (d *EsConn) Proxy(w http.ResponseWriter, r *http.Request, path string) {
r.URL.Path = path
if d.Info.authorization != "" {
r.Header.Set("Authorization", d.Info.authorization)
}
r.Header.Set("connection", "keep-alive")
r.Header.Set("Accept", "application/json")
d.proxy.ServeHTTP(w, r)
}