Skip to content

Commit

Permalink
add mt_net_response_ex.go
Browse files Browse the repository at this point in the history
  • Loading branch information
colixxxx committed Nov 25, 2024
1 parent b6c5fec commit 81d8a06
Showing 1 changed file with 38 additions and 54 deletions.
92 changes: 38 additions & 54 deletions plugins/inputs/mt_net_response_ex/mt_net_response_ex.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//go:debug x509negativeserial=1
//go:generate ../../../tools/readme_config_includer/generator
package net_response

Expand Down Expand Up @@ -52,6 +51,7 @@ type MtNetResponse struct {
ConnStatus []string
MD5 string
MT5User string
IgnoreSSL bool
}

type MT5Request struct {
Expand All @@ -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
}
Expand All @@ -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) {
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 81d8a06

Please sign in to comment.