refact: some refactoring to the http proxy code

This commit is contained in:
evilsocket 2018-01-08 00:51:50 +01:00
parent 01bf914cd4
commit 3719c6d806
3 changed files with 75 additions and 68 deletions

View file

@ -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"
}

View file

@ -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
}

View file

@ -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)