From c78d2f1d22e49868870166ead4b115731e054b4f Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Wed, 7 Jul 2021 10:46:04 -0700 Subject: added media proxy for remote links --- client.go | 20 ++++++++++++++++++++ main.go | 35 +++++++++++++++++++++++++++++++++++ static/faq.html | 4 ++-- static/index.html | 20 +++++++++----------- static/ncatalog.html | 6 +++--- static/posts.html | 32 ++++++++++++++++---------------- 6 files changed, 85 insertions(+), 32 deletions(-) diff --git a/client.go b/client.go index 39ecd29..5ac527e 100644 --- a/client.go +++ b/client.go @@ -174,6 +174,9 @@ func AllNewsGet(w http.ResponseWriter, r *http.Request, db *sql.DB) { func OutboxGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Collection){ t := template.Must(template.New("").Funcs(template.FuncMap{ + "proxy": func(url string) string { + return MediaProxy(url) + }, "sub": func (i, j int) int { return i - j }}).ParseFiles("./static/main.html", "./static/nposts.html", "./static/top.html", "./static/bottom.html", "./static/posts.html")) @@ -224,6 +227,9 @@ func OutboxGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Co func CatalogGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection Collection){ t := template.Must(template.New("").Funcs(template.FuncMap{ + "proxy": func(url string) string { + return MediaProxy(url) + }, "sub": func (i, j int) int { return i - j }}).ParseFiles("./static/main.html", "./static/ncatalog.html", "./static/top.html")) actor := collection.Actor @@ -258,6 +264,9 @@ func CatalogGet(w http.ResponseWriter, r *http.Request, db *sql.DB, collection C func PostGet(w http.ResponseWriter, r *http.Request, db *sql.DB){ t := template.Must(template.New("").Funcs(template.FuncMap{ + "proxy": func(url string) string { + return MediaProxy(url) + }, "sub": func (i, j int) int { return i - j }}).ParseFiles("./static/main.html", "./static/npost.html", "./static/top.html", "./static/bottom.html", "./static/posts.html")) path := r.URL.Path @@ -567,3 +576,14 @@ type BoardSortAsc []Board func (a BoardSortAsc) Len() int { return len(a) } func (a BoardSortAsc) Less(i, j int) bool { return a[i].Name < a[j].Name } func (a BoardSortAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +func MediaProxy(url string) string { + re := regexp.MustCompile("(.+)?" + Domain + "(.+)?") + + if re.MatchString(url) { + return url + } + + MediaHashs[HashMedia(url)] = url + return "/api/media?hash=" + HashMedia(url) +} diff --git a/main.go b/main.go index 755a0ec..a84bb7d 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,8 @@ import ( "bufio" "io" "github.com/gofrs/uuid" + "crypto/sha256" + "encoding/hex" ) var Port = ":" + GetConfigValue("instanceport") @@ -45,6 +47,8 @@ var Salt = GetConfigValue("instancesalt") var activitystreams = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" +var MediaHashs = make(map[string]string) + func main() { CreatedNeededDirectories() @@ -1203,6 +1207,10 @@ func main() { go AddInstanceToIndexDB(db, actor) }) + http.HandleFunc("/api/media", func(w http.ResponseWriter, r *http.Request) { + RouteImages(w, r.URL.Query().Get("hash")) + }) + fmt.Println("Server for " + Domain + " running on port " + Port) fmt.Println("Mod key: " + *Key) @@ -2566,3 +2574,30 @@ func GetCollectionFromReq(path string) Collection { return respCollection } +func HashMedia(media string) string { + h:= sha256.New() + h.Write([]byte(media)) + return hex.EncodeToString(h.Sum(nil)) +} + +func RouteImages(w http.ResponseWriter, media string) { + + req, err := http.NewRequest("GET", MediaHashs[media], nil) + + CheckError(err, "error with Route Images req") + + resp, err := http.DefaultClient.Do(req) + + CheckError(err, "error with Route Images resp") + + defer resp.Body.Close() + + body, _ := ioutil.ReadAll(resp.Body) + for name, values := range resp.Header { + for _, value := range values { + w.Header().Set(name, value) + } + } + + w.Write(body) +} diff --git a/static/faq.html b/static/faq.html index f98fd9c..35c07f0 100644 --- a/static/faq.html +++ b/static/faq.html @@ -10,7 +10,7 @@
fchan, short for FChannel, is a federated image board based on ActivityPub, a protocol which allows social sites like social media and image boards to be decentralized. Boards across sites which are on the opposite sides of the Globe can be connected and feeds can be shared or followed. It strives to be familiar looking to regular *chan users.
You can get the source code on https://github.com/FChannel0 which is available under AGPLv3, which means that you can modify the source code of fchan however you like as long as you share your source code with everyone else. We appreciate and encourage any positive contributions to the source code!
+fchan, short for FChannel, is a federated image board based on ActivityPub, a protocol which allows social sites like social media and image boards to be decentralized. Boards across sites which are on the opposite sides of the Globe can be connected and feeds can be shared or followed. It pulls likeness from other chans for ease of familiarity and use.
You can get the source code on https://github.com/FChannel0 which is available under AGPLv3, which means that you can modify the source code of fchan however you like as long as you share your source code with everyone else. We appreciate and encourage any positive contributions to the source code!
The "Options" field can be used for special options when posting.
@@ -56,7 +56,7 @@Soon™.
v0.6.1-dev
+v0.0.7-dev