Skip to content

Commit

Permalink
redirect instead of rewrite lemmy links, check for lemmy server
Browse files Browse the repository at this point in the history
  • Loading branch information
rystaf committed Sep 3, 2023
1 parent 5857148 commit 8ccb9d9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ func test() {
}
}
}
func RemoteAddr(r *http.Request) string {
if r.Header.Get("CF-Connecting-IP") != "" {
return r.Header.Get("CF-Connecting-IP")
}
return r.RemoteAddr
}
func middleware(n httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
//remoteAddr := r.RemoteAddr
Expand Down
31 changes: 30 additions & 1 deletion routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ var funcMap = template.FuncMap{
}
body = buf.String()
body = strings.Replace(body, `<img `, `<img loading="lazy" `, -1)
body = LemmyLinkRewrite(body, host, os.Getenv("LEMMY_DOMAIN"))
body = RegReplace(body, `href="(https:\/\/[a-zA-Z0-9\.\-]+\/(c|u|comment|post)\/[^#\?]*?)"`, `href="/`+host+`/link?url=$1"`)
body = RegReplace(body, `::: ?spoiler (.*?)\n([\S\s]*?):::`, "<details><summary>$1</summary>$2</details>")
return template.HTML(body)
},
Expand Down Expand Up @@ -1429,6 +1429,33 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
}
http.Redirect(w, r, r.URL.String(), 301)
}
func GetLink(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var dest *url.URL
m, _ := url.ParseQuery(r.URL.RawQuery)
if len(m["url"]) > 0 {
dest, _ = url.Parse(m["url"][0])
}
if dest.Host == r.Host || !IsLemmy(dest.Host, RemoteAddr(r)) {
http.Redirect(w, r, dest.String(), 302)
return
}
if host := ps.ByName("host"); host != "" {
redirect := "/" + host + dest.Path
if host != dest.Host && !strings.Contains(redirect, "@") {
redirect += ("@" + dest.Host)
}
http.Redirect(w, r, redirect, 302)
return
}
if host := os.Getenv("LEMMY_DOMAIN"); host != "" {
redirect := dest.Path
if host != dest.Host && !strings.Contains(redirect, "@") {
redirect += ("@" + dest.Host)
}
http.Redirect(w, r, redirect, 302)
return
}
}
func GetRouter() *httprouter.Router {
host := os.Getenv("LEMMY_DOMAIN")
router := httprouter.New()
Expand Down Expand Up @@ -1467,6 +1494,7 @@ func GetRouter() *httprouter.Router {
router.GET("/:host/create_community", middleware(GetCreateCommunity))
router.POST("/:host/create_community", middleware(UserOp))
router.GET("/:host/communities", middleware(GetCommunities))
router.GET("/:host/link", middleware(GetLink))
} else {
router.ServeFiles("/_/static/*filepath", http.Dir("public"))
router.GET("/", middleware(GetFrontpage))
Expand Down Expand Up @@ -1499,6 +1527,7 @@ func GetRouter() *httprouter.Router {
router.GET("/create_community", middleware(GetCreateCommunity))
router.POST("/create_community", middleware(UserOp))
router.GET("/communities", middleware(GetCommunities))
router.GET("/link", middleware(GetLink))
}
return router
}

0 comments on commit 8ccb9d9

Please sign in to comment.