From aa94dcc60a565b7f974fa7daf80d8d70a5042105 Mon Sep 17 00:00:00 2001
From: Arya K
Date: Tue, 16 May 2023 12:42:17 +0000
Subject: [PATCH] add support for disabling proxying (#94)
fixes #45
Co-authored-by: Arya Kiran
Reviewed-on: https://codeberg.org/gothub/gothub/pulls/94
---
cmd/gothub/env.go | 2 ++
cmd/gothub/setup.go | 12 +++++++++
compose.yml | 1 +
pages/about.go | 2 ++
serve/serve.go | 62 ++++++++++++++++++++++++++++++++++-----------
views/about.html | 1 +
views/repo.html | 2 +-
7 files changed, 66 insertions(+), 16 deletions(-)
diff --git a/cmd/gothub/env.go b/cmd/gothub/env.go
index c286fb3..8c37e6b 100644
--- a/cmd/gothub/env.go
+++ b/cmd/gothub/env.go
@@ -29,6 +29,8 @@ func env() {
} else {
fmt.Println("Docker: false")
}
+ // Proxying
+ fmt.Println("Proxying: " + os.Getenv("GOTHUB_PROXYING_ENABLED"))
// IP logging
fmt.Println("IP logging: " + os.Getenv("GOTHUB_IP_LOGGED"))
// URL request logging
diff --git a/cmd/gothub/setup.go b/cmd/gothub/setup.go
index 08b35e5..b755de9 100644
--- a/cmd/gothub/setup.go
+++ b/cmd/gothub/setup.go
@@ -29,6 +29,7 @@ type Vars struct {
InstanceCountry string
InstanceProvider string
CloudflareStatus string
+ Proxying string
}
func setup() {
@@ -130,6 +131,16 @@ func setup() {
vars.CloudflareStatus = "false"
}
+ // ask user whether they want proxying enabled
+ fmt.Print("Do you want proxying enabled? (y/n) ")
+ var proxying string
+ fmt.Scanln(&proxying)
+ if proxying == "y" {
+ vars.Proxying = "true"
+ } else {
+ vars.Proxying = "false"
+ }
+
// save to .env file
fmt.Println("Saving environment variables to .env file...")
f, err := os.Create(".env")
@@ -151,6 +162,7 @@ func setup() {
f.WriteString("GOTHUB_INSTANCE_COUNTRY=" + vars.InstanceCountry + "\n")
f.WriteString("GOTHUB_INSTANCE_PROVIDER=" + vars.InstanceProvider + "\n")
f.WriteString("GOTHUB_INSTANCE_CLOUDFLARE=" + vars.CloudflareStatus)
+ f.WriteString("GOTHUB_PROXYING_ENABLED=" + vars.Proxying)
println("All done! You can now start your GotHub instance with 'gothub serve'.")
println("You can review the environment variables with 'gothub env'.")
diff --git a/compose.yml b/compose.yml
index e01b09b..ae3ec24 100644
--- a/compose.yml
+++ b/compose.yml
@@ -9,6 +9,7 @@ services:
environment:
- DOCKER=true
- GOTHUB_SETUP_COMPLETE=false
+ - GOTHUB_PROXYING_ENABLED=true/false
- GOTHUB_IP_LOGGED=true/false
- GOTHUB_REQUEST_URL_LOGGED=true/false
- GOTHUB_USER_AGENT_LOGGED=true/false
diff --git a/pages/about.go b/pages/about.go
index e7ce9bd..fde51c4 100644
--- a/pages/about.go
+++ b/pages/about.go
@@ -8,6 +8,7 @@ import (
)
type PrivacyInfo struct {
+ ProxyEnabled string
IPAddr string
ReqURL string
UserAgent string
@@ -24,6 +25,7 @@ type PrivacyInfo struct {
func HandleAbout(c *fiber.Ctx) error {
var privacyInfoArray []PrivacyInfo
privacyInfoArray = append(privacyInfoArray, PrivacyInfo{
+ ProxyEnabled: os.Getenv("GOTHUB_PROXYING_ENABLED"),
IPAddr: os.Getenv("GOTHUB_IP_LOGGED"),
ReqURL: os.Getenv("GOTHUB_REQUEST_URL_LOGGED"),
UserAgent: os.Getenv("GOTHUB_USER_AGENT_LOGGED"),
diff --git a/serve/serve.go b/serve/serve.go
index 3c0fcdc..58dd360 100644
--- a/serve/serve.go
+++ b/serve/serve.go
@@ -86,13 +86,21 @@ func Serve(port string) {
MaxAge: 31536000,
}
+ proxying, ok := os.LookupEnv("GOTHUB_PROXYING_ENABLED")
+ if !ok {
+ proxying = "true"
+ }
// add global headers
app.Use(func(c *fiber.Ctx) error {
c.Set("X-Frame-Options", "SAMEORIGIN")
c.Set("X-XSS-Protection", "1; mode=block")
c.Set("X-Content-Type-Options", "nosniff")
c.Set("Referrer-Policy", "no-referrer")
- 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';")
+ 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("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
return c.Next()
@@ -107,30 +115,54 @@ func Serve(port string) {
app.Get("/explore", ratelimiter, pages.HandleExplore)
app.Get("/:user", pages.HandleUser)
app.Get("/avatar/:id", func(c *fiber.Ctx) error {
- utils.ProxyRequest(c, "https://avatars.githubusercontent.com/u/"+c.Params("id")+"?v=4")
- return nil
+ if proxying == "true" {
+ utils.ProxyRequest(c, "https://avatars.githubusercontent.com/u/"+c.Params("id")+"?v=4")
+ return nil
+ } else {
+ c.Redirect("https://avatars.githubusercontent.com/u/" + c.Params("id") + "?v=4")
+ return nil
+ }
})
app.Get("/:user/:repo", pages.HandleRepo)
app.Get("/:user/:repo/blob/:branch/+", pages.FileView)
app.Get("/:user/:repo/tree/:branch/+", pages.DirView)
app.Get("/:user/:repo/tree/:branch", pages.HandleRepo)
- app.Get("/download/:user/:repo/:branch", func(c *fiber.Ctx) error {
- utils.ProxyRequest(c, "https://github.com/"+c.Params("user")+"/"+c.Params("repo")+"/archive/"+c.Params("branch")+".zip")
- return nil
+ app.Get("/:user/:repo/archive/:branch", func(c *fiber.Ctx) error {
+ if proxying == "true" {
+ utils.ProxyRequest(c, "https://github.com/"+c.Params("user")+"/"+c.Params("repo")+"/archive/"+c.Params("branch"))
+ return nil
+ } else {
+ c.Redirect("https://github.com/" + c.Params("user") + "/" + c.Params("repo") + "/archive/" + c.Params("branch"))
+ return nil
+ }
+ })
+ app.Get("/raw/:user/:repo/:branch/+", func(c *fiber.Ctx) error {
+ if proxying == "true" {
+ utils.ProxyRequest(c, "https://raw.githubusercontent.com/"+c.Params("user")+"/"+c.Params("repo")+"/"+c.Params("branch")+"/"+c.Params("+"))
+ return nil
+ } else {
+ c.Redirect("https://raw.githubusercontent.com/" + c.Params("user") + "/" + c.Params("repo") + "/" + c.Params("branch") + "/" + c.Params("+"))
+ return nil
+ }
})
app.Get("/camo/:p1/:p2", func(c *fiber.Ctx) error {
- utils.ProxyRequest(c, "https://camo.githubusercontent.com/"+c.Params("p1")+"/"+c.Params("p2"))
- return nil
+ if proxying == "true" {
+ utils.ProxyRequest(c, "https://camo.githubusercontent.com/"+c.Params("p1")+"/"+c.Params("p2"))
+ return nil
+ } else {
+ c.Redirect("https://camo.githubusercontent.com/"+c.Params("p1")+"/"+c.Params("p2"))
+ return nil
+ }
})
app.Get("/gist/:user/:gistID", pages.HandleGist)
app.Get("/download/gist/:user/:gistID/:revision", func(c *fiber.Ctx) error {
- utils.ProxyRequest(c, "https://gist.github.com/"+c.Params("user")+"/"+c.Params("gistID")+"/archive/"+c.Params("revision")+".zip")
- return nil
- })
-
- app.Get("/raw/:user/:repo/:branch/:file", func(c *fiber.Ctx) error {
- utils.ProxyRequest(c, "https://raw.githubusercontent.com/"+c.Params("user")+"/"+c.Params("repo")+"/"+c.Params("branch")+"/"+c.Params("file"))
- return nil
+ if proxying == "true" {
+ utils.ProxyRequest(c, "https://gist.github.com/"+c.Params("user")+"/"+c.Params("gistID")+"/archive/"+c.Params("revision")+".zip")
+ return nil
+ } else {
+ c.Redirect("https://gist.github.com/"+c.Params("user")+"/"+c.Params("gistID")+"/archive/"+c.Params("revision")+".zip")
+ return nil
+ }
})
api := app.Group("/api")
diff --git a/views/about.html b/views/about.html
index abf80a0..fe30323 100644
--- a/views/about.html
+++ b/views/about.html
@@ -10,6 +10,7 @@
If this page isn't filled, please contact your instance's maintainer.
+ - Proxying enabled: {{.ProxyEnabled}}
- IP Address logged: {{.IPAddr}}
- Request URL logged: {{.ReqURL}}
- User Agent logged: {{.UserAgent}}
diff --git a/views/repo.html b/views/repo.html
index 0fb36ae..e5816de 100644
--- a/views/repo.html
+++ b/views/repo.html
@@ -3,7 +3,7 @@
{{ if .repo }} {{ range $key, $value := .repo}}