plaprox

Proxy for plausible.io scripts written in Go.
git clone git://vcs.sapka.me/plaprox
Log | Files | Refs | README | LICENSE

commit bf34dc0cebd408b9f25b3fcba523a87fd40134cb
Author: Michał M. Sapka <michal@sapka.me>
Date:   Thu, 13 Jul 2023 15:51:03 +0200

feat: POC

Diffstat:
A.gitignore | 1+
Ago.mod | 9+++++++++
Amain.go | 114+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 124 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +proxy diff --git a/go.mod b/go.mod @@ -0,0 +1,9 @@ +module cvs.sapka.me/plaprox + +go 1.20 + +require ( + github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect + golang.org/x/sys v0.10.0 // indirect + gopkg.in/sevlyar/go-daemon.v0 v0.1.6 // indirect +) diff --git a/main.go b/main.go @@ -0,0 +1,114 @@ +//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || plan9 + +/* + Copyright 2023 Michał Sapka (https://michal.sapka.me) + + Permission to use, copy, modify, and/or distribute this software for + any purpose with or without fee is hereby granted. + + THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL + WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE + FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY + DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT + OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +package main + +import ( + "fmt" + "log" + "net/http" + "io" + "strings" + + "gopkg.in/sevlyar/go-daemon.v0" +) + +// Proxies requests to Plausible.io and returns the response to the client +func proxy(w http.ResponseWriter, r *http.Request) { + + client := &http.Client{} + + proxiedr := r.clone + + + /* + req, err := http.NewRequest(r.Method, "https://plausible.io" + r.URL.Path, r.Body) + + fmt.Println(r.Body) + + + + if err != nil { + log.Fatal(err) + } + + /* + for k,v := range r.Header { + value := strings.Join(v[:], " ") + req.Header.Set(string(k), value) + } + */ + + /* + resp, err := client.Do(req) + if err != nil { + log.Fatal(err) + } + + defer resp.Body.Close() + + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + log.Fatal(err) + } + + bodyString := string(bodyBytes) + for k, v := range resp.Header { + fmt.Println(string(k)) + value := strings.Join(v[:], " ") + w.Header().Set(string(k), value) + } + */ + + fmt.Fprintf(w, bodyString) +} + + +func serveProxy() { + http.HandleFunc("/js/script.js", proxy) + http.HandleFunc("/api/event", proxy) + + if err := http.ListenAndServe(":9090", nil); err != nil { + log.Fatal(err) + } +} + +func main() { + serveProxy() + return + + cntxt := &daemon.Context{ + PidFileName: "plaprox.pid", + PidFilePerm: 0644, + LogFileName: "plaprox.log", + LogFilePerm: 0640, + } + + d, err := cntxt.Reborn() + if err != nil { + log.Fatal("Unable to run: ", err) + } + if d != nil { + return + } + defer cntxt.Release() + + log.Print("- - - - - - - - - - - - - - -") + log.Print("daemon started") + + serveProxy() +}