/*
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc.
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
http-wokou, An HTTP Proxying framework for bypassing DNS Security
Copyright (C) 2024 Russell Hrubesky, ChiralWorks Software LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
package httpserver
import (
"fmt"
"io"
"log"
"net/http"
"strings"
"sync"
)
/*
Generic site call to an upstream server
*/
func (c *Controller) RequestGeneric(method string, host string, path string, hdrs *http.Header, body io.Reader) ([]byte, *http.Header, int, error) {
reqUrl := fmt.Sprintf("https://%s%s", host, path)
if method == "POST" {
req, err := http.NewRequest(method, reqUrl, c.requestBodyRewrites(body))
if err != nil {
return nil, nil, 500, err
}
c.setHeaders(req, hdrs)
resp, err := c.Client.Do(req)
if err != nil {
return nil, nil, 500, err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, 500, err
}
return c.pageMod(b), &resp.Header, resp.StatusCode, nil
}
req, err := http.NewRequest(method, reqUrl, body)
if err != nil {
return nil, nil, 500, err
}
c.setHeaders(req, hdrs)
resp, err := c.Client.Do(req)
if err != nil {
return nil, nil, 500, err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, 500, err
}
var data []byte
_, ok := c.PageMods.Bypass[path]
if ok {
data = b
} else {
data = c.pageMod(b)
}
if c.Config.Caching {
if !strings.Contains(path, "?") {
if resp.StatusCode == 200 {
if method == "GET" {
c.CacheResource(path, NewCachedResource(data, &resp.Header, resp.StatusCode))
}
}
}
}
return data, &resp.Header, resp.StatusCode, nil
}
func (c *Controller) TryHosts(method string, path string, hdrs *http.Header, body io.Reader, hosts []string) {
var wg sync.WaitGroup
for idx := range hosts {
wg.Add(1)
go func(method string, host string, path string, hdrs *http.Header, body io.Reader) {
defer wg.Done()
_, _, rcode, err := c.RequestGeneric(method, host, path, hdrs, body)
if err != nil {
log.Fatal("Fatal Error creating request in a RequestGeneric method: ", err)
}
if rcode == 200 {
basePath := strings.Split(path, "?")[0]
c.RouteMaps.MapUriToDomain(basePath, host)
}
}(method, hosts[idx], path, hdrs, body)
}
wg.Wait()
}
/*
Sets the request headers to whatever is defined in this private method
:param req: a pointer to an HTTP request
*/
func (c *Controller) setHeaders(req *http.Request, hdrs *http.Header) {
req.AddCookie(c.Config.PhpSession)
req.AddCookie(c.Config.SsoToken)
req.Header.Set("User-Agent", c.Config.UserAgent)
req.Header.Set("Referer", c.Config.FullDomain)
req.Header.Set("Origin", c.Config.FullDomain)
for k, v := range *hdrs {
_, ok := NonmutableHeaders[k]
if !ok {
req.Header.Add(k, v[0])
}
}
}