From ced35c4190a4f11d531b0b2c46aa1cf95d797633 Mon Sep 17 00:00:00 2001
From: Arya Kiran
Date: Sat, 8 Apr 2023 13:04:11 +0530
Subject: [PATCH] add support for disabling proxying
---
cmd/gothub/env.go | 2 ++
cmd/gothub/setup.go | 12 ++++++++++++
compose.yml | 1 +
pages/about.go | 1 +
serve/serve.go | 39 +++++++++++++++++++++++++++++++--------
views/about.html | 1 +
6 files changed, 48 insertions(+), 8 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..4fc7763 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 a64c0db..d34f91e 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 962e0a6..7b89e51 100644
--- a/pages/about.go
+++ b/pages/about.go
@@ -24,6 +24,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 3c9cc18..12f47f6 100644
--- a/serve/serve.go
+++ b/serve/serve.go
@@ -84,13 +84,19 @@ 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';")
+ }
c.Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
return c.Next()
@@ -105,20 +111,37 @@ 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
+ log.Println(proxying)
+ if proxying == "true" {
+ utils.ProxyRequest(c, "https://github.com/"+c.Params("user")+"/"+c.Params("repo")+"/archive/"+c.Params("branch")+".zip")
+ return nil
+ } else {
+ c.Redirect("https://github.com/" + c.Params("user") + "/" + c.Params("repo") + "/archive/" + c.Params("branch") + ".zip")
+ log.Println("no proxi")
+ 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
+ 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
+ }
})
api := app.Group("/api")
diff --git a/views/about.html b/views/about.html
index abf80a0..392a788 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}}