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

View file

@ -220,9 +220,10 @@ type NodeInfo struct {
Software NodeSoftware `json:"software"` 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 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 { if err != nil {
return false return false
} }
@ -251,15 +252,21 @@ func PostRoot(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
input = "https://" + input input = "https://" + input
} }
dest, err := url.Parse(input) dest, err := url.Parse(input)
if dest.Host != "" && IsLemmy(dest.Host) { if dest.Host != "" {
redirectUrl := "/" + dest.Host + dest.Path state, _ := Initialize(dest.Host, r)
if dest.RawQuery != "" { if err := state.LemmyError(dest.Host); err != nil {
redirectUrl = redirectUrl + "?" + dest.RawQuery 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) } else {
return data["Error"] = "Invalid destination"
} }
data["Error"] = "Invalid destination"
tmpl.Execute(w, data) tmpl.Execute(w, data)
} }
func GetIcon(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { 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 = "" state.Client.Token = ""
resp, err := state.Client.Site(context.Background(), types.GetSite{}) resp, err := state.Client.Site(context.Background(), types.GetSite{})
if err != nil { if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Server Error")) w.Write([]byte("500 - Server Error"))
return 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() { func (state *State) GetCaptcha() {
resp, err := state.Client.Captcha(context.Background(), types.GetCaptcha{}) resp, err := state.Client.Captcha(context.Background(), types.GetCaptcha{})
if err != nil { if err != nil {
@ -227,8 +246,8 @@ func (state *State) GetSite() {
state.Client.Token = "" state.Client.Token = ""
resp, err := state.Client.Site(context.Background(), types.GetSite{}) resp, err := state.Client.Site(context.Background(), types.GetSite{})
if err != nil { if err != nil {
fmt.Println(err)
state.Status = http.StatusInternalServerError state.Status = http.StatusInternalServerError
state.Host = ""
return return
} }
state.Client.Token = token state.Client.Token = token
@ -420,7 +439,6 @@ func (state *State) GetCommunities() {
Limit: types.NewOptional(int64(20)), Limit: types.NewOptional(int64(20)),
}) })
if err != nil { if err != nil {
fmt.Println(err)
return return
} }
state.TopCommunities = resp.Communities state.TopCommunities = resp.Communities

View file

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