/* 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 ( "bytes" "fmt" "io" "log" "net/http" "strings" "sync" ) type ResponseCarrier struct { Data []byte Headers http.Header StatusCode int Error error } /* Generic site call to an upstream server */ func (c *Controller) RequestGeneric(method string, host string, path string, hdrs *http.Header, body io.Reader) ResponseCarrier { reqUrl := fmt.Sprintf("%s://%s%s", "http", host, path) req, err := http.NewRequest(method, reqUrl, body) if err != nil { return ResponseCarrier{Data: nil, Headers: nil, StatusCode: 500, Error: err} } resp, err := c.Client.Do(req) if err != nil { return ResponseCarrier{Data: nil, Headers: nil, StatusCode: 500, Error: err} } defer resp.Body.Close() b, err := io.ReadAll(resp.Body) if err != nil { return ResponseCarrier{Data: nil, Headers: resp.Header, StatusCode: resp.StatusCode, Error: err} } bytes.ReplaceAll(b, []byte(c.Config.TargetDomain), []byte(c.Config.ProxyDomain)) if c.Config.Caching { if !strings.Contains(path, "?") { if resp.StatusCode == 200 { if method == "GET" { c.CacheResource(path, NewCachedResource(b, &resp.Header, resp.StatusCode)) } } } } return ResponseCarrier{Data: b, Headers: resp.Header, StatusCode: resp.StatusCode, Error: 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() resp := c.RequestGeneric(method, host, path, hdrs, body) if resp.Error != nil { log.Fatal("Fatal Error creating request in a RequestGeneric method: ", resp.Error) } if resp.StatusCode == 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.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]) } } }