diff --git a/session/modules/http_proxy_js_request.go b/session/modules/http_proxy_js_request.go new file mode 100644 index 00000000..b1a1dfe9 --- /dev/null +++ b/session/modules/http_proxy_js_request.go @@ -0,0 +1,19 @@ +package session_modules + +type JSHeader struct { + Name string + Value string +} + +type JSRequest struct { + Method string + Version string + Path string + Hostname string + Headers []JSHeader + Body string +} + +func (j *JSRequest) ReadBody() string { + return "TODO: read body" +} diff --git a/session/modules/http_proxy_js_response.go b/session/modules/http_proxy_js_response.go new file mode 100644 index 00000000..1dd2aeb7 --- /dev/null +++ b/session/modules/http_proxy_js_response.go @@ -0,0 +1,56 @@ +package session_modules + +import ( + "io/ioutil" + "net/http" + "strings" + + "github.com/elazarl/goproxy" +) + +type JSResponse struct { + Status int + ContentType string + Headers string + Body string + + wasUpdated bool + resp *http.Response +} + +func (j *JSResponse) Updated() { + j.wasUpdated = true +} + +func (j *JSResponse) ToResponse(req *http.Request) (resp *http.Response) { + resp = goproxy.NewResponse(req, j.ContentType, j.Status, j.Body) + if j.Headers != "" { + for _, header := range strings.Split(j.Headers, "\n") { + header = strings.Trim(header, "\n\r\t ") + if header == "" { + continue + } + parts := strings.SplitN(header, ":", 2) + if len(parts) == 2 { + resp.Header.Add(parts[0], parts[1]) + } else { + log.Warningf("Unexpected header '%s'", header) + } + } + } + return +} + +func (j *JSResponse) ReadBody() string { + defer j.resp.Body.Close() + + raw, err := ioutil.ReadAll(j.resp.Body) + if err != nil { + log.Errorf("Could not read response body: %s", err) + return "" + } + + j.Body = string(raw) + + return j.Body +} diff --git a/session/modules/proxy_script.go b/session/modules/http_proxy_script.go similarity index 73% rename from session/modules/proxy_script.go rename to session/modules/http_proxy_script.go index 9da9ce75..4cfa37c8 100644 --- a/session/modules/proxy_script.go +++ b/session/modules/http_proxy_script.go @@ -4,11 +4,8 @@ import ( "fmt" "io/ioutil" "net/http" - "strings" "sync" - "github.com/elazarl/goproxy" - "github.com/robertkrimen/otto" ) @@ -19,71 +16,6 @@ type ProxyScript struct { gil *sync.Mutex } -type JSHeader struct { - Name string - Value string -} - -type JSRequest struct { - Method string - Version string - Path string - Hostname string - Headers []JSHeader - Body string -} - -type JSResponse struct { - Status int - ContentType string - Headers string - Body string - - wasUpdated bool - resp *http.Response -} - -func (j *JSResponse) Updated() { - j.wasUpdated = true -} - -func (j *JSResponse) ToResponse(req *http.Request) (resp *http.Response) { - resp = goproxy.NewResponse(req, j.ContentType, j.Status, j.Body) - if j.Headers != "" { - for _, header := range strings.Split(j.Headers, "\n") { - header = strings.Trim(header, "\n\r\t ") - if header == "" { - continue - } - parts := strings.SplitN(header, ":", 2) - if len(parts) == 2 { - resp.Header.Add(parts[0], parts[1]) - } else { - log.Warningf("Unexpected header '%s'", header) - } - } - } - return -} - -func (j *JSResponse) ReadBody() string { - defer j.resp.Body.Close() - - raw, err := ioutil.ReadAll(j.resp.Body) - if err != nil { - log.Errorf("Could not read response body: %s", err) - return "" - } - - j.Body = string(raw) - - return j.Body -} - -func (jsr JSRequest) ReadBody() string { - return "TODO: read body" -} - func LoadProxyScript(path string) (err error, s *ProxyScript) { log.Infof("Loading proxy script %s ...", path)