| 1 | package http_proxy |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "io/ioutil" |
| 7 | "net/http" |
| 8 | "net/url" |
| 9 | "regexp" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/bettercap/bettercap/session" |
| 13 | ) |
| 14 | |
| 15 | type JSRequest struct { |
| 16 | Client map[string]string |
| 17 | Method string |
| 18 | Version string |
| 19 | Scheme string |
| 20 | Path string |
| 21 | Query string |
| 22 | Hostname string |
| 23 | Port string |
| 24 | ContentType string |
| 25 | Headers string |
| 26 | Body string |
| 27 | |
| 28 | req *http.Request |
| 29 | refHash string |
| 30 | bodyRead bool |
| 31 | } |
| 32 | |
| 33 | var header_regexp = regexp.MustCompile(`^\s*(.*?)\s*:\s*(.*)\s*$`) |
| 34 | |
| 35 | func NewJSRequest(req *http.Request) *JSRequest { |
| 36 | headers := "" |
| 37 | cType := "" |
| 38 | |
| 39 | for name, values := range req.Header { |
| 40 | for _, value := range values { |
| 41 | headers += name + ": " + value + "\r\n" |
| 42 | |
| 43 | if strings.ToLower(name) == "content-type" { |
| 44 | cType = value |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | client_ip := strings.Split(req.RemoteAddr, ":")[0] |
| 50 | client_mac := "" |
| 51 | client_alias := "" |
| 52 | if endpoint := session.I.Lan.GetByIp(client_ip); endpoint != nil { |
| 53 | client_mac = endpoint.HwAddress |
| 54 | client_alias = endpoint.Alias |
| 55 | } |
| 56 | |
| 57 | jreq := &JSRequest{ |
| 58 | Client: map[string]string{"IP": client_ip, "MAC": client_mac, "Alias": client_alias}, |
| 59 | Method: req.Method, |
| 60 | Version: fmt.Sprintf("%d.%d", req.ProtoMajor, req.ProtoMinor), |
| 61 | Scheme: req.URL.Scheme, |
| 62 | Hostname: req.URL.Hostname(), |
| 63 | Port: req.URL.Port(), |
| 64 | Path: req.URL.Path, |
| 65 | Query: req.URL.RawQuery, |
| 66 | ContentType: cType, |
| 67 | Headers: headers, |
| 68 | |
| 69 | req: req, |
| 70 | bodyRead: false, |
| 71 | } |
| 72 | jreq.UpdateHash() |
| 73 | |
| 74 | return jreq |
| 75 | } |
| 76 | |
| 77 | func (j *JSRequest) NewHash() string { |
| 78 | hash := fmt.Sprintf("%s.%s.%s.%s.%s.%s.%s.%s.%s.%s", |
| 79 | j.Client["IP"], |
| 80 | j.Method, |
| 81 | j.Version, |
| 82 | j.Scheme, |
| 83 | j.Hostname, |
| 84 | j.Port, |
| 85 | j.Path, |
| 86 | j.Query, |
| 87 | j.ContentType, |
| 88 | j.Headers) |
| 89 | hash += "." + j.Body |
| 90 | return hash |
| 91 | } |
| 92 | |
| 93 | func (j *JSRequest) UpdateHash() { |
| 94 | j.refHash = j.NewHash() |
| 95 | } |
| 96 | |
| 97 | func (j *JSRequest) WasModified() bool { |
| 98 | // body was read |
| 99 | if j.bodyRead { |
| 100 | return true |
| 101 | } |
| 102 | // check if any of the fields has been changed |
| 103 | return j.NewHash() != j.refHash |
| 104 | } |
| 105 | |
| 106 | func (j *JSRequest) GetHeader(name, deflt string) string { |
| 107 | headers := strings.Split(j.Headers, "\r\n") |
| 108 | for i := 0; i < len(headers); i++ { |
| 109 | if headers[i] != "" { |
| 110 | header_parts := header_regexp.FindAllSubmatch([]byte(headers[i]), 1) |
| 111 | if len(header_parts) != 0 && len(header_parts[0]) == 3 { |
| 112 | header_name := string(header_parts[0][1]) |
| 113 | header_value := string(header_parts[0][2]) |
| 114 | |
| 115 | if strings.ToLower(name) == strings.ToLower(header_name) { |
| 116 | return header_value |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | return deflt |
| 122 | } |
| 123 | |
| 124 | func (j *JSRequest) SetHeader(name, value string) { |
| 125 | name = strings.TrimSpace(name) |
| 126 | value = strings.TrimSpace(value) |
| 127 | |
| 128 | if strings.ToLower(name) == "content-type" { |
| 129 | j.ContentType = value |
| 130 | } |
| 131 | |
| 132 | headers := strings.Split(j.Headers, "\r\n") |
| 133 | for i := 0; i < len(headers); i++ { |
| 134 | if headers[i] != "" { |
| 135 | header_parts := header_regexp.FindAllSubmatch([]byte(headers[i]), 1) |
| 136 | if len(header_parts) != 0 && len(header_parts[0]) == 3 { |
| 137 | header_name := string(header_parts[0][1]) |
| 138 | header_value := string(header_parts[0][2]) |
| 139 | |
| 140 | if strings.ToLower(name) == strings.ToLower(header_name) { |
| 141 | old_header := header_name + ": " + header_value + "\r\n" |
| 142 | new_header := name + ": " + value + "\r\n" |
| 143 | j.Headers = strings.Replace(j.Headers, old_header, new_header, 1) |
| 144 | return |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | j.Headers += name + ": " + value + "\r\n" |
| 150 | } |
| 151 | |
| 152 | func (j *JSRequest) RemoveHeader(name string) { |
| 153 | headers := strings.Split(j.Headers, "\r\n") |
| 154 | for i := 0; i < len(headers); i++ { |
| 155 | if headers[i] != "" { |
| 156 | header_parts := header_regexp.FindAllSubmatch([]byte(headers[i]), 1) |
| 157 | if len(header_parts) != 0 && len(header_parts[0]) == 3 { |
| 158 | header_name := string(header_parts[0][1]) |
| 159 | header_value := string(header_parts[0][2]) |
| 160 | |
| 161 | if strings.ToLower(name) == strings.ToLower(header_name) { |
| 162 | removed_header := header_name + ": " + header_value + "\r\n" |
| 163 | j.Headers = strings.Replace(j.Headers, removed_header, "", 1) |
| 164 | return |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func (j *JSRequest) ReadBody() string { |
| 172 | raw, err := ioutil.ReadAll(j.req.Body) |
| 173 | if err != nil { |
| 174 | return "" |
| 175 | } |
| 176 | |
| 177 | j.Body = string(raw) |
| 178 | j.bodyRead = true |
| 179 | // reset the request body to the original unread state |
| 180 | j.req.Body = ioutil.NopCloser(bytes.NewBuffer(raw)) |
| 181 | |
| 182 | return j.Body |
| 183 | } |
| 184 | |
| 185 | func (j *JSRequest) ParseForm() map[string]string { |
| 186 | if j.Body == "" { |
| 187 | j.Body = j.ReadBody() |
| 188 | } |
| 189 | |
| 190 | form := make(map[string]string) |
| 191 | parts := strings.Split(j.Body, "&") |
| 192 | |
| 193 | for _, part := range parts { |
| 194 | nv := strings.SplitN(part, "=", 2) |
| 195 | if len(nv) == 2 { |
| 196 | unescaped, err := url.QueryUnescape(nv[1]) |
| 197 | if err == nil { |
| 198 | form[nv[0]] = unescaped |
| 199 | } else { |
| 200 | form[nv[0]] = nv[1] |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return form |
| 206 | } |
| 207 | |
| 208 | func (j *JSRequest) ToRequest() (req *http.Request) { |
| 209 | portPart := "" |
| 210 | if j.Port != "" { |
| 211 | portPart = fmt.Sprintf(":%s", j.Port) |
| 212 | } |
| 213 | |
| 214 | url := fmt.Sprintf("%s://%s%s%s?%s", j.Scheme, j.Hostname, portPart, j.Path, j.Query) |
| 215 | if j.Body == "" { |
| 216 | req, _ = http.NewRequest(j.Method, url, j.req.Body) |
| 217 | } else { |
| 218 | req, _ = http.NewRequest(j.Method, url, strings.NewReader(j.Body)) |
| 219 | } |
| 220 | |
| 221 | headers := strings.Split(j.Headers, "\r\n") |
| 222 | for i := 0; i < len(headers); i++ { |
| 223 | if headers[i] != "" { |
| 224 | header_parts := header_regexp.FindAllSubmatch([]byte(headers[i]), 1) |
| 225 | if len(header_parts) != 0 && len(header_parts[0]) == 3 { |
| 226 | header_name := string(header_parts[0][1]) |
| 227 | header_value := string(header_parts[0][2]) |
| 228 | |
| 229 | if strings.ToLower(header_name) == "content-type" { |
| 230 | if header_value != j.ContentType { |
| 231 | req.Header.Set(header_name, j.ContentType) |
| 232 | continue |
| 233 | } |
| 234 | } |
| 235 | req.Header.Set(header_name, header_value) |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | req.RemoteAddr = j.Client["IP"] |
| 241 | |
| 242 | return |
| 243 | } |