lemmy check

This commit is contained in:
Ryan Stafford 2023-07-01 10:57:04 -04:00
parent 4787f209c6
commit 8de7bbfa9c
4 changed files with 51 additions and 22 deletions

16
main.go
View file

@ -67,10 +67,14 @@ func init() {
}
func middleware(n httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if ps.ByName("host") != "" && !IsLemmy(ps.ByName("host")) {
http.Redirect(w, r, "/", 301)
return
}
//remoteAddr := r.RemoteAddr
//if r.Header.Get("CF-Connecting-IP") != "" {
// remoteAddr = r.Header.Get("CF-Connecting-IP")
//}
//if ps.ByName("host") != "" && !IsLemmy(ps.ByName("host"), remoteAddr) {
// http.Redirect(w, r, "/", 301)
// return
//}
n(w, r, ps)
}
}
@ -79,8 +83,8 @@ func main() {
log.Println("serve", *addr)
router := httprouter.New()
router.ServeFiles("/:host/static/*filepath", http.Dir("public"))
router.GET("/", middleware(GetRoot))
router.POST("/", middleware(PostRoot))
router.GET("/", GetRoot)
router.POST("/", PostRoot)
router.GET("/:host/", middleware(GetFrontpage))
router.GET("/:host/search", middleware(Search))
router.POST("/:host/search", middleware(UserOp))

View file

@ -220,9 +220,10 @@ type NodeInfo struct {
Software NodeSoftware `json:"software"`
}
func IsLemmy(domain string) bool {
func IsLemmy(domain string, remoteAddr string) bool {
client := http.Client{Transport: NewAddHeaderTransport(remoteAddr)}
var nodeInfo NodeInfo
res, err := http.Get("https://" + domain + "/nodeinfo/2.0.json")
res, err := client.Get("https://" + domain + "/nodeinfo/2.0.json")
if err != nil {
return false
}
@ -251,15 +252,21 @@ func PostRoot(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
input = "https://" + input
}
dest, err := url.Parse(input)
if dest.Host != "" && IsLemmy(dest.Host) {
redirectUrl := "/" + dest.Host + dest.Path
if dest.RawQuery != "" {
redirectUrl = redirectUrl + "?" + dest.RawQuery
if dest.Host != "" {
state, _ := Initialize(dest.Host, r)
if err := state.LemmyError(dest.Host); err != nil {
data["Error"] = err
} else {
redirectUrl := "/" + dest.Host + dest.Path
if dest.RawQuery != "" {
redirectUrl = redirectUrl + "?" + dest.RawQuery
}
http.Redirect(w, r, redirectUrl, 302)
return
}
http.Redirect(w, r, redirectUrl, 302)
return
} else {
data["Error"] = "Invalid destination"
}
data["Error"] = "Invalid destination"
tmpl.Execute(w, data)
}
func GetIcon(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@ -271,7 +278,6 @@ func GetIcon(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
state.Client.Token = ""
resp, err := state.Client.Site(context.Background(), types.GetSite{})
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Server Error"))
return

View file

@ -211,6 +211,25 @@ func (state *State) ParseQuery(RawQuery string) {
//
//}
func (state *State) LemmyError(domain string) error {
var nodeInfo NodeInfo
res, err := state.HTTPClient.Get("https://" + domain + "/nodeinfo/2.0.json")
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("Status Code: %v", res.StatusCode)
}
err = json.NewDecoder(res.Body).Decode(&nodeInfo)
if err != nil {
return err
}
if nodeInfo.Software.Name == "lemmy" {
return nil
}
return errors.New("Not a lemmy instance")
}
func (state *State) GetCaptcha() {
resp, err := state.Client.Captcha(context.Background(), types.GetCaptcha{})
if err != nil {
@ -227,8 +246,8 @@ func (state *State) GetSite() {
state.Client.Token = ""
resp, err := state.Client.Site(context.Background(), types.GetSite{})
if err != nil {
fmt.Println(err)
state.Status = http.StatusInternalServerError
state.Host = ""
return
}
state.Client.Token = token
@ -420,7 +439,6 @@ func (state *State) GetCommunities() {
Limit: types.NewOptional(int64(20)),
})
if err != nil {
fmt.Println(err)
return
}
state.TopCommunities = resp.Communities

View file

@ -11,11 +11,12 @@
<span>-</span>
<a {{ if eq .Sort "TopAll"}}class="selected"{{end}} href="{{ .SortBy "TopAll"}}">all time</a>
{{ end }}
{{ if and (not .SearchType ) (not (and .Session (not .Posts) (not .Community) (and .Listing "Subscribed"))) }}
popular in:
<a href="{{ .ListBy "All"}}" {{if eq .Listing "All"}}class="selected"{{end}}>Everywhere</a>
<a href="{{ .ListBy "All"}}" {{if eq .Listing "All"}}class="selected"{{end}}>everywhere</a>
<span>-</span>
<a href="{{ .ListBy "Local"}}" {{if eq .Listing "Local"}}class="selected"{{end}}>Local</a>
<a href="{{ .ListBy "Local"}}" {{if eq .Listing "Local"}}class="selected"{{end}}>local</a>
{{ if .Session }}
<span>-</span>
<a href="{{ .ListBy "Subscribed"}}" {{if eq .Listing "Subscribed"}}class="selected"{{end}}>subscribed</a>
{{ end }}
</div>