From cf5499db368a1cb52ab734d204f08bbcc114a890 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Fri, 9 May 2025 10:53:09 -0400 Subject: addes user agents. added aliases. --- useragents.go | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 useragents.go (limited to 'useragents.go') diff --git a/useragents.go b/useragents.go new file mode 100644 index 0000000..0c51e61 --- /dev/null +++ b/useragents.go @@ -0,0 +1,97 @@ +package main + +import ( + "bytes" + "context" + "crypto/tls" + "encoding/json" + "fmt" + "log" + "net/http" + "time" +) + +const ( + UserAgentsURL = "https://useragents:443/api/v1/agent" +) + +func UserAgentsGet(uaActionName, query string, appConfig *TomlConfig) string { + var userAgentRequest UserAgentRequest + + var userAgentResponse UserAgentResponse + + _, ok := appConfig.UserAgentActions[uaActionName] + if !ok { + log.Println("UserAgentAction not found:", uaActionName) + + return fmt.Sprintf("useragents: %s: action not found", uaActionName) + } + + userAgentRequest.Agent_Name = appConfig.UserAgentActions[uaActionName].Agent_Name + userAgentRequest.Instructions = appConfig.UserAgentActions[uaActionName].Instructions + userAgentRequest.Query = appConfig.UserAgentActions[uaActionName].Query + + if query != "" { + userAgentRequest.Query = query + } + + log.Println("UserAgentRequest:", appConfig.UserAgentActions[uaActionName]) + log.Println(userAgentRequest) + + jsonData, err := json.Marshal(userAgentRequest) + if err != nil { + log.Println(err) + + return fmt.Sprintf("useragents: %s: could not marshall json request", userAgentRequest.Agent_Name) + } + + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(appConfig.RequestTimeout)*time.Second) + defer cancel() + + req, err := http.NewRequest( + http.MethodPost, + UserAgentsURL, + bytes.NewBuffer(jsonData), + ) + if err != nil { + log.Println(err) + + return fmt.Sprintf("useragents: %s: could not create request", userAgentRequest.Agent_Name) + } + + req = req.WithContext(ctx) + + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, + }, + } + + response, err := client.Do(req) + if err != nil { + log.Println(err) + + return fmt.Sprintf("useragents: %s: could not send request", userAgentRequest.Agent_Name) + } + + defer response.Body.Close() + + if err != nil { + log.Println(err) + + return fmt.Sprintf("useragents: %s: could not read response", userAgentRequest.Agent_Name) + } + + err = json.NewDecoder(response.Body).Decode(&userAgentResponse) + if err != nil { + log.Println(err) + + return fmt.Sprintf("useragents: %s: could not unmarshall json response", userAgentRequest.Agent_Name) + } + + return userAgentResponse.Response +} -- cgit v1.2.3