From 81d8a0647ec44e7fd91a420d1dba0a4bc79c3e1d Mon Sep 17 00:00:00 2001 From: Vladislav Radin Date: Mon, 25 Nov 2024 14:52:56 +0200 Subject: [PATCH] add mt_net_response_ex.go --- .../mt_net_response_ex/mt_net_response_ex.go | 92 ++++++++----------- 1 file changed, 38 insertions(+), 54 deletions(-) diff --git a/plugins/inputs/mt_net_response_ex/mt_net_response_ex.go b/plugins/inputs/mt_net_response_ex/mt_net_response_ex.go index d972935ee5428..67c1bcae6a3cb 100644 --- a/plugins/inputs/mt_net_response_ex/mt_net_response_ex.go +++ b/plugins/inputs/mt_net_response_ex/mt_net_response_ex.go @@ -1,4 +1,3 @@ -//go:debug x509negativeserial=1 //go:generate ../../../tools/readme_config_includer/generator package net_response @@ -52,6 +51,7 @@ type MtNetResponse struct { ConnStatus []string MD5 string MT5User string + IgnoreSSL bool } type MT5Request struct { @@ -71,53 +71,57 @@ func (*MtNetResponse) SampleConfig() string { return sampleConfig } -func NewMT5Request(server string, port int) *MT5Request { - return &MT5Request{ - server: server, - port: port, - client: &http.Client{Timeout: 10 * time.Second}, +func NewMT5Request(server string, port int, ssl bool) *MT5Request { + if ssl { + return &MT5Request{ + server: server, + port: port, + client: &http.Client{Timeout: 10 * time.Second, + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}, + }, + } + } else { + return &MT5Request{ + server: server, + port: port, + client: &http.Client{Timeout: 10 * time.Second}, + } } } -func (mt5 *MT5Request) Get(path string) (string, error) { - url := fmt.Sprintf("https://%s:%d%s", mt5.server, mt5.port, path) - http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} - mt5.client.Transport = http.DefaultTransport - resp, err := mt5.client.Get(url) - if err != nil { - return "", err - } - defer resp.Body.Close() +func ProcessAuth(md5Combined, SrvRand string) string { - body, err := io.ReadAll(resp.Body) + // Get bytes of MD5 hash + md5CombinedBytes, err := hex.DecodeString(md5Combined) if err != nil { - return "", err + fmt.Printf("failed hex decode") } + // Get bytes of SrvRand + srvRandBytes, _ := hex.DecodeString(SrvRand) - if resp.StatusCode != http.StatusOK { - return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode) - } + // Join bytes MD5 hash and SrvRand + finalCombinedBytes := append(md5CombinedBytes, srvRandBytes...) - return string(body), nil + // Final bytes srv_rand_answer + md5FinalAnswer := md5.Sum(finalCombinedBytes) + + // Шаг 6: Результат в HEX-представлении + finalHex := hex.EncodeToString(md5FinalAnswer[:]) + return finalHex } -func (mt5 *MT5Request) Post(path, body string) (string, error) { +func (mt5 *MT5Request) Get(path string) (string, error) { url := fmt.Sprintf("https://%s:%d%s", mt5.server, mt5.port, path) - req, err := http.NewRequest("POST", url, strings.NewReader(body)) - if err != nil { - return "", err - } - - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - req.Header.Set("Content-Length", fmt.Sprintf("%d", len(body))) - resp, err := mt5.client.Do(req) + resp, err := mt5.client.Get(url) if err != nil { return "", err } + defer resp.Body.Close() - responseBody, err := io.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return "", err } @@ -126,7 +130,7 @@ func (mt5 *MT5Request) Post(path, body string) (string, error) { return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode) } - return string(responseBody), nil + return string(body), nil } func (mt5 *MT5Request) ParseBodyJSON(body string) (AuthResponse, error) { @@ -142,27 +146,6 @@ func (mt5 *MT5Request) ParseBodyJSON(body string) (AuthResponse, error) { return answer, nil } -func ProcessAuth(md5Combined, SrvRand string) string { - - // Get bytes of MD5 hash - md5CombinedBytes, err := hex.DecodeString(md5Combined) - if err != nil { - fmt.Printf("failed hex decode") - } - // Get bytes of SrvRand - srvRandBytes, _ := hex.DecodeString(SrvRand) - - // Join bytes MD5 hash and SrvRand - finalCombinedBytes := append(md5CombinedBytes, srvRandBytes...) - - // Final bytes srv_rand_answer - md5FinalAnswer := md5.Sum(finalCombinedBytes) - - // Шаг 6: Результат в HEX-представлении - finalHex := hex.EncodeToString(md5FinalAnswer[:]) - return finalHex -} - func (mt5 *MT5Request) Auth(md5Combined, login, build, agent string) error { if login == "" || build == "" || agent == "" { return errors.New("missing required parameters") @@ -313,8 +296,9 @@ func (m *MtNetResponse) ACGather() (map[string]string, map[string]interface{}, e //Start timer start := time.Now() - mt5 := NewMT5Request(ip, 443) + mt5 := NewMT5Request(host, 443, m.IgnoreSSL) err := mt5.Auth(m.MD5, m.MT5User, "4656", "test") + mt5.client.CloseIdleConnections() // Stop timer responseTime := time.Since(start).Seconds() // Handle error