mirror of
https://github.com/bettercap/bettercap
synced 2025-07-06 04:52:10 -07:00
Update http.go
This commit is contained in:
parent
c4e45b368d
commit
18c5c5b66e
1 changed files with 55 additions and 56 deletions
111
js/http.go
111
js/http.go
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -12,25 +11,22 @@ import (
|
||||||
"github.com/robertkrimen/otto"
|
"github.com/robertkrimen/otto"
|
||||||
)
|
)
|
||||||
|
|
||||||
type httpPackage struct {
|
type httpPackage struct{}
|
||||||
}
|
|
||||||
|
|
||||||
type httpResponse struct {
|
type httpResponse struct {
|
||||||
Error error
|
Error error
|
||||||
Response *http.Response
|
Response *http.Response
|
||||||
Raw []byte
|
Raw []byte
|
||||||
Body string
|
Body string
|
||||||
JSON interface{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Encode encodes a string for use in a URL query.
|
||||||
func (c httpPackage) Encode(s string) string {
|
func (c httpPackage) Encode(s string) string {
|
||||||
return url.QueryEscape(s)
|
return url.QueryEscape(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c httpPackage) Request(method string, uri string,
|
// Request sends an HTTP request with the specified method, URL, headers, form data, or JSON payload.
|
||||||
headers map[string]string,
|
func (c httpPackage) Request(method, uri string, headers map[string]string, form map[string]string, json string) httpResponse {
|
||||||
form map[string]string,
|
|
||||||
json string) httpResponse {
|
|
||||||
var reader io.Reader
|
var reader io.Reader
|
||||||
|
|
||||||
if form != nil {
|
if form != nil {
|
||||||
|
@ -38,14 +34,14 @@ func (c httpPackage) Request(method string, uri string,
|
||||||
for k, v := range form {
|
for k, v := range form {
|
||||||
data.Set(k, v)
|
data.Set(k, v)
|
||||||
}
|
}
|
||||||
reader = bytes.NewBufferString(data.Encode())
|
reader = strings.NewReader(data.Encode())
|
||||||
} else if json != "" {
|
} else if json != "" {
|
||||||
reader = strings.NewReader(json)
|
reader = strings.NewReader(json)
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest(method, uri, reader)
|
req, err := http.NewRequest(method, uri, reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return httpResponse{Error: err}
|
return httpResponse{Error: fmt.Errorf("failed to create request: %w", err)}
|
||||||
}
|
}
|
||||||
|
|
||||||
if form != nil {
|
if form != nil {
|
||||||
|
@ -55,18 +51,18 @@ func (c httpPackage) Request(method string, uri string,
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, value := range headers {
|
for name, value := range headers {
|
||||||
req.Header.Add(name, value)
|
req.Header.Set(name, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return httpResponse{Error: err}
|
return httpResponse{Error: fmt.Errorf("request failed: %w", err)}
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
raw, err := ioutil.ReadAll(resp.Body)
|
raw, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return httpResponse{Error: err}
|
return httpResponse{Error: fmt.Errorf("failed to read response body: %w", err)}
|
||||||
}
|
}
|
||||||
|
|
||||||
res := httpResponse{
|
res := httpResponse{
|
||||||
|
@ -75,82 +71,85 @@ func (c httpPackage) Request(method string, uri string,
|
||||||
Body: string(raw),
|
Body: string(raw),
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode >= 400 {
|
||||||
res.Error = fmt.Errorf("%s", resp.Status)
|
res.Error = fmt.Errorf("HTTP error: %s", resp.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get sends a GET request.
|
||||||
func (c httpPackage) Get(url string, headers map[string]string) httpResponse {
|
func (c httpPackage) Get(url string, headers map[string]string) httpResponse {
|
||||||
return c.Request("GET", url, headers, nil, "")
|
return c.Request(http.MethodGet, url, headers, nil, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PostForm sends a POST request with form data.
|
||||||
func (c httpPackage) PostForm(url string, headers map[string]string, form map[string]string) httpResponse {
|
func (c httpPackage) PostForm(url string, headers map[string]string, form map[string]string) httpResponse {
|
||||||
return c.Request("POST", url, headers, form, "")
|
return c.Request(http.MethodPost, url, headers, form, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PostJSON sends a POST request with a JSON payload.
|
||||||
func (c httpPackage) PostJSON(url string, headers map[string]string, json string) httpResponse {
|
func (c httpPackage) PostJSON(url string, headers map[string]string, json string) httpResponse {
|
||||||
return c.Request("POST", url, headers, nil, json)
|
return c.Request(http.MethodPost, url, headers, nil, json)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// httpRequest processes JavaScript calls for HTTP requests.
|
||||||
func httpRequest(call otto.FunctionCall) otto.Value {
|
func httpRequest(call otto.FunctionCall) otto.Value {
|
||||||
argv := call.ArgumentList
|
if len(call.ArgumentList) < 2 {
|
||||||
argc := len(argv)
|
return ReportError("httpRequest: expected at least 2 arguments, got %d", len(call.ArgumentList))
|
||||||
if argc < 2 {
|
|
||||||
return ReportError("httpRequest: expected 2 or more, %d given instead.", argc)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
method := argv[0].String()
|
method := call.Argument(0).String()
|
||||||
url := argv[1].String()
|
url := call.Argument(1).String()
|
||||||
|
|
||||||
client := &http.Client{}
|
var reader io.Reader
|
||||||
req, err := http.NewRequest(method, url, nil)
|
if len(call.ArgumentList) >= 3 {
|
||||||
if argc >= 3 {
|
data := call.Argument(2).String()
|
||||||
data := argv[2].String()
|
reader = bytes.NewBufferString(data)
|
||||||
req, err = http.NewRequest(method, url, bytes.NewBuffer([]byte(data)))
|
}
|
||||||
if err != nil {
|
|
||||||
return ReportError("Could create request to url %s: %s", url, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if argc > 3 {
|
req, err := http.NewRequest(method, url, reader)
|
||||||
headers := argv[3].Object()
|
if err != nil {
|
||||||
for _, key := range headers.Keys() {
|
return ReportError("failed to create request for URL %s: %s", url, err)
|
||||||
v, err := headers.Get(key)
|
}
|
||||||
if err != nil {
|
|
||||||
return ReportError("Could add header %s to request: %s", key, err)
|
if len(call.ArgumentList) > 3 {
|
||||||
|
headers, _ := call.Argument(3).Export()
|
||||||
|
if headerMap, ok := headers.(map[string]interface{}); ok {
|
||||||
|
for key, value := range headerMap {
|
||||||
|
if strValue, ok := value.(string); ok {
|
||||||
|
req.Header.Set(key, strValue)
|
||||||
}
|
}
|
||||||
req.Header.Add(key, v.String())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if err != nil {
|
|
||||||
return ReportError("Could create request to url %s: %s", url, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ReportError("Could not request url %s: %s", url, err)
|
return ReportError("failed to execute request to URL %s: %s", url, err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ReportError("Could not read response: %s", err)
|
return ReportError("failed to read response body: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
object, err := otto.New().Object("({})")
|
responseObj, _ := otto.New().Object(`({})`)
|
||||||
if err != nil {
|
responseObj.Set("body", string(body))
|
||||||
return ReportError("Could not create response object: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = object.Set("body", string(body))
|
v, err := otto.ToValue(responseObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ReportError("Could not populate response object: %s", err)
|
return ReportError("failed to convert response to Otto value: %s", err)
|
||||||
}
|
|
||||||
|
|
||||||
v, err := otto.ToValue(object)
|
|
||||||
if err != nil {
|
|
||||||
return ReportError("Could not convert to object: %s", err)
|
|
||||||
}
|
}
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReportError formats and returns a JavaScript-compatible error.
|
||||||
|
func ReportError(format string, args ...interface{}) otto.Value {
|
||||||
|
errMessage := fmt.Sprintf(format, args...)
|
||||||
|
fmt.Println("Error:", errMessage) // Log the error
|
||||||
|
val, _ := otto.ToValue(fmt.Errorf(errMessage))
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue