From 5c77eb1783181a71b2eac5e984ece6cd62c0b59a Mon Sep 17 00:00:00 2001 From: root Date: Fri, 16 Feb 2024 19:11:37 +0300 Subject: [PATCH] Add HttpPost --- cmpSnipper/cmpSnipper.go | 31 +++-- cmpSnipper/cmpSnipperConf.go | 62 ++++++++- cmpSnipper/cmpSnipperHttpServ.go | 1 + cmpSnipper/cmpSnipperLog.go | 27 +++- cmpSnipper/cmpSnippetHttpCli.go | 222 ++++++++++++++++++++++++++++++- main.go | 2 +- 6 files changed, 314 insertions(+), 31 deletions(-) diff --git a/cmpSnipper/cmpSnipper.go b/cmpSnipper/cmpSnipper.go index 16d67f7..4d46b2f 100644 --- a/cmpSnipper/cmpSnipper.go +++ b/cmpSnipper/cmpSnipper.go @@ -11,20 +11,33 @@ Plug *plugin.Plugin } - type App struct { - Conf interface{} - ErrIsDir error // = errors.New("Is directory") + Conf interface{} + ErrIsDir error // = errors.New("Is directory") - CliList []cmpWbsCliStruct + HttpCliDebug uint32 + HttpCliSslVerify bool - cmpSnipperHttpServ cmpSnipperHttpServ + CliList []cmpWbsCliStruct - Rand4d int + cmpSnipperHttpServ cmpSnipperHttpServ - PlugList []cmpPluginStruct + Rand4d int - SshList []cmpSshCliStruct + PlugList []cmpPluginStruct + + SshList []cmpSshCliStruct + } + + func NewApp() (App) { + var app App + + app.ErrIsDir = errors.New("Is directory") + + app.HttpCliDebug = 1 + app.HttpCliSslVerify = true + + return app } func (app *App) PlugInit(name string, file string) { @@ -84,8 +97,6 @@ } func (app *App) Prep() (error) { - app.ErrIsDir = errors.New("Is directory") - app.LogSetup() var err = app.ConfLoadFile("secret/zest.json"); diff --git a/cmpSnipper/cmpSnipperConf.go b/cmpSnipper/cmpSnipperConf.go index b89f3ca..f59054f 100644 --- a/cmpSnipper/cmpSnipperConf.go +++ b/cmpSnipper/cmpSnipperConf.go @@ -31,7 +31,7 @@ } if ifc == nil { - return nil, errors.New("Undefine variable " + idx); + return nil, errors.New("Undefine variable '" + idx + "'"); } // app.Log("Found ifc %s %T", idx, ifc) @@ -43,25 +43,73 @@ return app.IfcGet(app.Conf, arr...); } - func (app *App) ConfGetStr(arr ...string) (string, error) { - ifc, err := app.ConfGetIfc(arr...); + + func (app *App) IfcGetStr(ifc0 interface{}, arr ...string) (string, error) { + ifc, err := app.IfcGet(ifc0, arr...) if err != nil { - return "", err; + app.Log(err) + return "", err } switch ifc.(type) { case string: return ifc.(string), nil; + case float64: f := ifc.(float64) u := uint64(f) return strconv.FormatUint(u, 10), nil; - default: - return "", errors.New("Invalid type value !s"); + + case bool: + if ifc.(bool) { + return "true", nil + } + + return "false", nil } - return "", errors.New("Invalid type value"); + return "", errors.New("Invalid type value not string or float64/bool"); + } + + func (app *App) ConfGetStr(arr ...string) (string, error) { + return app.IfcGetStr(app.Conf, arr...) + } + + func (app *App) IfcGetFlt(ifc0 interface{}, arr ...string) (float64, error) { + ifc, err := app.IfcGet(ifc0, arr...) + + if err != nil { + app.Log(err) + return 0, err + } + + switch ifc.(type) { + case string: + flt, err := strconv.ParseFloat(ifc.(string), 64) + + if err != nil { + app.Log(err) + return 0, err + } + + return flt, nil; + case float64: + return ifc.(float64), nil; + + case bool: + if ifc.(bool) { + return 1, nil + } + + return 0, nil + } + + return 0, errors.New("Invalid type value not float64 or string/bool"); + } + + func (app *App) ConfGetFlt(arr ...string) (float64, error) { + return app.IfcGetFlt(app.Conf, arr...) } func (app *App) ConfGetArr(arr ...string) ([]interface{}, error) { diff --git a/cmpSnipper/cmpSnipperHttpServ.go b/cmpSnipper/cmpSnipperHttpServ.go index 0b0e6d7..c7e6f7b 100644 --- a/cmpSnipper/cmpSnipperHttpServ.go +++ b/cmpSnipper/cmpSnipperHttpServ.go @@ -50,6 +50,7 @@ http.HandleFunc("/" , app.HttpReq); http.HandleFunc("/go-prefix/ws" , app.HttpWebSockHand) + var addr = host + ":" + port server := &http.Server{Addr: addr} diff --git a/cmpSnipper/cmpSnipperLog.go b/cmpSnipper/cmpSnipperLog.go index 0aa6848..7b77eee 100644 --- a/cmpSnipper/cmpSnipperLog.go +++ b/cmpSnipper/cmpSnipperLog.go @@ -9,30 +9,43 @@ ) func (app *App) LogMod(arg interface{}) string { + var add bool = false var str string + var prf string switch t := arg.(type) { case nil: - str = /* "N:" + */ "null" + prf = "N:" + str = "null" case string: - str = /* "S:" + */ t + prf = "S:" + str = t case error: - str = /* "E:" + */ t.Error() + prf = "E:" + str = t.Error() case byte: - str = /* "B:" + */ string(t) + prf = "B:" + str = string(t) case []byte: - str = /* "A:" + */ string(t) + prf = "A:" + str = string(t) case interface{}: + prf = "J:" obyte, _ := json.MarshalIndent(arg, "", "\t") - str = /* "J:" + */ string(obyte) + str = string(obyte) default: - str = /* "U:" + */ fmt.Sprintf("Unknown type %T", t) + prf = "U:" + str = fmt.Sprintf("Unknown type %T", t) + } + + if add { + str = prf + str } return str diff --git a/cmpSnipper/cmpSnippetHttpCli.go b/cmpSnipper/cmpSnippetHttpCli.go index ed4fd49..dfd4082 100644 --- a/cmpSnipper/cmpSnippetHttpCli.go +++ b/cmpSnipper/cmpSnippetHttpCli.go @@ -4,6 +4,8 @@ import ( // "crypto/md5" "net/http" + "net/url" + "crypto/tls" // "os" "io" // "fmt" @@ -19,7 +21,7 @@ "github.com/andybalholm/brotli" ) - func (app *App) HttpGet(url string) (map[string]interface{}) { + func (app *App) HttpPost(url string, head map[string]string, cookie []http.Cookie, data url.Values) (map[string]interface{}) { ret := map[string]interface{}{ "Error" : nil , "RequestUrl" : url , @@ -32,9 +34,17 @@ "Content-Length" : "" , } - app.Log(url) + app.Log("%s", url) - cli := &http.Client{} + var tr *http.Transport = &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: false}, + } + + if !app.HttpCliSslVerify { + tr.TLSClientConfig.InsecureSkipVerify = true + } + + cli := &http.Client{Transport: tr} ifcRawHead, err := app.ConfGetIfc("httpTest", "head") @@ -52,10 +62,33 @@ mapStrHead[key] = []string{ val.(string) } } - req, err := http.NewRequest("GET", url, nil) + // app.Log(head) + + + for key, val := range head { + mapStrHead[key] = []string{ val } + } + + req, err := http.NewRequest( + "POST", + url, + strings.NewReader(data.Encode()), + ) + + if err != nil { + app.Log(err) + ret["Error"] = err + return ret + } req.Header = mapStrHead + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + + for _, coo := range cookie { + req.AddCookie(&coo) + } + res, err := cli.Do(req) if err != nil { @@ -75,6 +108,14 @@ ret["StatusCode" ] = res.StatusCode ret["Content-Length"] = res.ContentLength ret["Header" ] = res.Header + ret["Cookies" ] = res.Cookies() + + if res.StatusCode != 200 { + app.Log("StatusCode != 200") + app.Log(req) + app.Log(req.Header) + app.Log(mapStrHead) + } // app.Log("Response status: %s", res.Status) // app.Log("Header: %+v", res.Header) @@ -132,7 +173,175 @@ return ret } - app.Log("Read %s", size) + if app.HttpCliDebug > 1 { + app.Log("Read %s", size) + } + + var contRaw []byte = out.Bytes() + ret["ContentRaw"] = contRaw + + if ret["Content-Type"] == "application/json" { + var contJson interface{} + + err = json.Unmarshal(contRaw, &contJson) + + if err != nil { + app.Log(err) + ret["Error"] = err + return ret + } + + ret["ContentJson"] = contJson + } + + return ret + } + + + func (app *App) HttpGet(url string, head map[string]string, cookie []http.Cookie) (map[string]interface{}) { + ret := map[string]interface{}{ + "Error" : nil , + "RequestUrl" : url , + "StatusCode" : 0 , + "ContentRaw" : nil , + "ContentJson" : nil , + "Header" : nil , + "Content-Encoding" : "" , + "Content-Type" : "" , + "Content-Length" : "" , + } + + app.Log("%s", url) + + var tr *http.Transport = &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: false}, + } + + if !app.HttpCliSslVerify { + tr.TLSClientConfig.InsecureSkipVerify = true + } + + cli := &http.Client{Transport: tr} + + ifcRawHead, err := app.ConfGetIfc("httpTest", "head") + + if err != nil { + app.Log(err) + ret["Error"] = err + return ret + } + + mapIfcHead := ifcRawHead.(map[string]interface{}) + + mapStrHead := make(map[string][]string) + + for key, val := range mapIfcHead { + mapStrHead[key] = []string{ val.(string) } + } + + + // app.Log(head) + + + for key, val := range head { + mapStrHead[key] = []string{ val } + } + + req, err := http.NewRequest("GET", url, nil) + + req.Header = mapStrHead + + for _, coo := range cookie { + req.AddCookie(&coo) + } + + res, err := cli.Do(req) + + if err != nil { + app.Log(err) + ret["Error"] = err + return ret + } + + defer res.Body.Close() + + if err != nil { + app.Log(err) + ret["Error"] = err + return ret + } + + ret["StatusCode" ] = res.StatusCode + ret["Content-Length"] = res.ContentLength + ret["Header" ] = res.Header + ret["Cookies" ] = res.Cookies() + + if res.StatusCode != 200 { + app.Log("StatusCode != 200") + app.Log(req) + app.Log(req.Header) + app.Log(mapStrHead) + } + + // app.Log("Response status: %s", res.Status) + // app.Log("Header: %+v", res.Header) + + // app.Log("TransferEncoding: %+v", res.TransferEncoding) + // app.Log("ContentLength: %+v", res.ContentLength) + + var tmpl []string + + tmpl = res.Header["Content-Encoding"] + if len(tmpl) > 0 { + ret["Content-Encoding"] = tmpl[0] + } + + tmpl = res.Header["Content-Type"] + if len(tmpl) > 0 { + tmpl = strings.Split(tmpl[0], ";") + + if len(tmpl) > 0 { + ret["Content-Type"] = tmpl[0] + } + } + + // Content-Disposition: attachment; name="fieldName"; filename="myfile.txt" + + + out := new(bytes.Buffer) + var encReader io.Reader + + switch ret["Content-Encoding"] { + case "": + encReader = bufio.NewReader(res.Body) + case "br": + encReader = brotli.NewReader(res.Body) + case "gzip": + encReader, err = gzip.NewReader(res.Body) + case "deflate": + encReader = flate.NewReader(res.Body) + default: + app.Log("Unknown Content-Encoding %s", ret["Content-Encoding"]) + encReader = bufio.NewReader(res.Body) + } + + if err != nil { + app.Log(err) + ret["Error"] = err + return ret + } + + size, err := out.ReadFrom(encReader) + + if err != nil { + app.Log(err) + ret["Error"] = err + return ret + } + + if app.HttpCliDebug > 1 { + app.Log("Read %s", size) + } var contRaw []byte = out.Bytes() ret["ContentRaw"] = contRaw @@ -178,7 +387,7 @@ go func() { var a map[string]interface{} - a = app.HttpGet(url) + a = app.HttpGet(url, nil, nil) app.Log(a) @@ -194,3 +403,4 @@ "request": url, }, nil } + diff --git a/main.go b/main.go index 9c6054c..aeb11a8 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,7 @@ ) func main() { - var app cmpSnipper.App; + app := cmpSnipper.NewApp() var err error err = app.Prep()