direct io copy response body rather than into threads memory; also gofmt

This commit is contained in:
Dan Ballard 2025-03-05 09:45:19 -08:00
parent 8e7bdd6eb9
commit 4386c07113
1 changed files with 17 additions and 12 deletions

29
main.go
View File

@ -3,6 +3,8 @@ package main
import (
"encoding/json"
"flag"
"io"
//"fmt"
"git.openprivacy.ca/openprivacy/log"
"io/ioutil"
@ -26,7 +28,6 @@ var cookiesLock sync.Mutex
var blocklistedIps map[string]bool
var blocklistLock sync.Mutex
var counter *Counter
func main() {
@ -76,7 +77,7 @@ func load() {
func save() {
for {
time.Sleep(1*time.Minute)
time.Sleep(1 * time.Minute)
blocklistLock.Lock()
bytes, err := json.Marshal(blocklistedIps)
blocklistLock.Unlock()
@ -90,13 +91,17 @@ func save() {
func logger() {
for {
time.Sleep(5*time.Second)
time.Sleep(5 * time.Second)
log.Infof("Thread count: %v\n", counter.String())
}
}
func listen(listenPort, proxyPort int) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { counter.Add(); filter(w, r, listenPort, proxyPort); counter.Sub()})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
counter.Add()
filter(w, r, listenPort, proxyPort)
counter.Sub()
})
addr := "127.0.0.1:" + strconv.Itoa(listenPort)
err := http.ListenAndServe(addr, nil)
if err != nil {
@ -178,8 +183,8 @@ func filter(res http.ResponseWriter, req *http.Request, listenPort, proxyPort in
}
blocklistLock.Unlock()
http.Redirect(res, req, "http://gitopcybr57ris5iuivfz62gdwe2qk5pinnt2wplpwzicaybw73stjqd.onion", http.StatusSeeOther)
// res.WriteHeader(http.StatusNotFound)
//fmt.Fprint(res, "404 - suspected botnet")
// res.WriteHeader(http.StatusNotFound)
//fmt.Fprint(res, "404 - suspected botnet")
}
// https://medium.com/@mlowicki/http-s-proxy-in-golang-in-less-than-100-lines-of-code-6a51c2f2c38c
@ -191,8 +196,8 @@ func filter(res http.ResponseWriter, req *http.Request, listenPort, proxyPort in
}
}*/
//https://stackoverflow.com/questions/23164547/golang-reverseproxy-not-working
func copyHeader(source http.Header, dest *http.Header){
// https://stackoverflow.com/questions/23164547/golang-reverseproxy-not-working
func copyHeader(source http.Header, dest *http.Header) {
for n, v := range source {
for _, vv := range v {
dest.Add(n, vv)
@ -203,7 +208,6 @@ func copyHeader(source http.Header, dest *http.Header){
func pass(res http.ResponseWriter, req *http.Request, listenPort, proxyPort int) {
//log.Debugf("Request pass to proxy")
//log.Infof("orig: %v\n", req.Host)
req.Host = strings.Replace(req.Host, strconv.Itoa(listenPort), strconv.Itoa(proxyPort), 1)
//req.Host = "git.danballard.com"
@ -223,18 +227,19 @@ func pass(res http.ResponseWriter, req *http.Request, listenPort, proxyPort int)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
/*body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error(err)
return
}
}*/
dH := res.Header()
copyHeader(resp.Header, &dH)
dH.Add("Requested-Host", req.Host)
res.WriteHeader(resp.StatusCode)
/*n, err := */ res.Write(body)
/*n, err := */ //res.Write(body)
io.Copy(res, resp.Body)
//log.Infof("res.write n: %v err: %v\n", n, err)
}