From d29dc3b10aaef03cfbfb3229476dadd3f192a6d6 Mon Sep 17 00:00:00 2001
From: Arya Kiran
Date: Sat, 24 Jun 2023 22:47:08 +0530
Subject: [PATCH] proxy user-images + link to wiki in repo view
---
pages/dirview.go | 2 +-
pages/repo.go | 2 +-
pages/user.go | 1 +
pages/wiki.go | 2 +-
serve/serve.go | 11 ++++++++++-
views/repo.html | 1 +
6 files changed, 15 insertions(+), 4 deletions(-)
diff --git a/pages/dirview.go b/pages/dirview.go
index baf4c7f..118ea28 100644
--- a/pages/dirview.go
+++ b/pages/dirview.go
@@ -61,7 +61,7 @@ func DirView(c *fiber.Ctx) error {
})
sc.OnHTML("div#readme div.Box-body", func(e *colly.HTMLElement) {
Content, _ := e.DOM.Html()
- readmeOutput = strings.Replace(strings.Replace(strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1), "user-content-", "", -1), "https://camo.githubusercontent.com", "/camo", -1), "https://raw.githubusercontent.com", "/raw", -1)
+ readmeOutput = strings.Replace(strings.Replace(strings.Replace(strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1), "user-content-", "", -1), "https://camo.githubusercontent.com", "/camo", -1), "https://raw.githubusercontent.com", "/raw", -1), "https://user-images.githubusercontent.com", "/user-images", -1)
})
sc.OnHTML("div.js-details-container div.Details-content--hidden-not-important", func(e *colly.HTMLElement) {
e.ForEach("div.js-navigation-item", func(i int, el *colly.HTMLElement) {
diff --git a/pages/repo.go b/pages/repo.go
index 8d6e844..6a361ce 100644
--- a/pages/repo.go
+++ b/pages/repo.go
@@ -94,7 +94,7 @@ func HandleRepo(c *fiber.Ctx) error {
})
sc.OnHTML("div#readme div.Box-body", func(e *colly.HTMLElement) {
Content, _ := e.DOM.Html()
- readmeOutput = strings.Replace(strings.Replace(strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1), "user-content-", "", -1), "https://camo.githubusercontent.com", "/camo", -1), "https://raw.githubusercontent.com", "/raw", -1)
+ readmeOutput = strings.Replace(strings.Replace(strings.Replace(strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1), "user-content-", "", -1), "https://camo.githubusercontent.com", "/camo", -1), "https://raw.githubusercontent.com", "/raw", -1), "https://user-images.githubusercontent.com", "/user-images", -1)
})
sc.OnHTML("div.BorderGrid-cell ul.list-style-none", func(e *colly.HTMLElement) {
e.ForEach("li.d-inline .d-inline-flex", func(i int, el *colly.HTMLElement) {
diff --git a/pages/user.go b/pages/user.go
index a96b2ce..562f1db 100644
--- a/pages/user.go
+++ b/pages/user.go
@@ -158,6 +158,7 @@ func HandleUser(c *fiber.Ctx) error {
sc.OnHTML("article.markdown-body", func(e *colly.HTMLElement) {
Content, _ := e.DOM.Html()
Scrape.Readme = strings.Replace(strings.Replace(strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1), "user-content-", "", -1), "https://camo.githubusercontent.com", "/camo", -1), "https://raw.githubusercontent.com", "/raw", -1)
+
})
sc.OnHTML("div.text-mono", func(e *colly.HTMLElement) {
Scrape.ReadmeUrl = strings.Replace(e.ChildAttr("a", "href"), "tree", "blob", -1)
diff --git a/pages/wiki.go b/pages/wiki.go
index 509e2ba..543b500 100644
--- a/pages/wiki.go
+++ b/pages/wiki.go
@@ -73,7 +73,7 @@ func WikiView(c *fiber.Ctx) error {
})
sc.OnHTML("div#wiki-body > div.markdown-body", func(e *colly.HTMLElement) {
Content, _ := e.DOM.Html()
- Scrape.PageContent = strings.Replace(strings.Replace(strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1), "user-content-", "", -1), "https://camo.githubusercontent.com", "/camo", -1), "https://raw.githubusercontent.com", "/raw", -1)
+ Scrape.PageContent = strings.Replace(strings.Replace(strings.Replace(strings.Replace(strings.Replace(string(utils.UGCPolicy().SanitizeBytes([]byte(Content))), "https://github.com", "", -1), "user-content-", "", -1), "https://camo.githubusercontent.com", "/camo", -1), "https://raw.githubusercontent.com", "/raw", -1), "https://user-images.githubusercontent.com", "/user-images", -1)
})
sc.Visit("https://github.com/" + c.Params("user") + "/" + c.Params("repo") + "/wiki/" + c.Params("page"))
diff --git a/serve/serve.go b/serve/serve.go
index 92309fc..655c7db 100644
--- a/serve/serve.go
+++ b/serve/serve.go
@@ -105,7 +105,7 @@ func Serve(port string) {
if proxying == "true" {
c.Set("Content-Security-Policy", "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; script-src 'self'; frame-ancestors 'self'; form-action 'self'; base-uri 'self'; connect-src 'self';")
} else {
- c.Set("Content-Security-Policy", "default-src 'self' https://camo.githubusercontent.com https://avatars.githubusercontent.com https://github.com https://raw.githubusercontent.com https://gist.github.com; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https://camo.githubusercontent.com https://avatars.githubusercontent.com https://gist.github.com; font-src 'self'; connect-src 'self' https://camo.githubusercontent.com https://avatars.githubusercontent.com https://github.com https://raw.githubusercontent.com https://gist.github.com; frame-ancestors 'self'; form-action 'self'; base-uri 'self'")
+ c.Set("Content-Security-Policy", "default-src 'self' https://camo.githubusercontent.com https://avatars.githubusercontent.com https://user-images.githubusercontent.com https://github.com https://raw.githubusercontent.com https://gist.github.com; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https://user-images.githubusercontent.com https://camo.githubusercontent.com https://avatars.githubusercontent.com https://gist.github.com; font-src 'self'; connect-src 'self' https://user-images.githubusercontent.com https://camo.githubusercontent.com https://avatars.githubusercontent.com https://github.com https://raw.githubusercontent.com https://gist.github.com; frame-ancestors 'self'; form-action 'self'; base-uri 'self'")
}
c.Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
@@ -129,6 +129,15 @@ func Serve(port string) {
return nil
}
})
+ app.Get("/user-images/:id/:img", func(c *fiber.Ctx) error {
+ if proxying == "true" {
+ utils.ProxyRequest(c, "https://user-images.githubusercontent.com/"+c.Params("id")+"/"+c.Params("img")+"?v=4")
+ return nil
+ } else {
+ c.Redirect("https://user-images.githubusercontent.com/" + c.Params("id") + "/" + c.Params("img") + "?v=4")
+ return nil
+ }
+ })
app.Get("/:user/:repo", pages.HandleRepo)
app.Get("/:user/:repo/info/+", func(c *fiber.Ctx) error {
// This is needed but it looks like git doesn't send it. Or maybe i just shouldn't program at midnight
diff --git a/views/repo.html b/views/repo.html
index fbcef1c..339bd91 100644
--- a/views/repo.html
+++ b/views/repo.html
@@ -30,6 +30,7 @@
⭐ {{.Stars}} 🍴 {{.Forks}} 👀 {{.Watchers}} ⚖️ No license 🌿
{{.DefaultBranch}}
+ Wiki
{{ end }} {{ if .Language }} {{range $o := .Language}}{{$o.Name}}
{{$o.Percent}}, {{end}}