Check if website contains webring server hostname
This commit is contained in:
parent
9aa75639f9
commit
7f90eef911
16
funcs.go
16
funcs.go
|
@ -7,8 +7,10 @@ package main
|
|||
import (
|
||||
"html"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
@ -77,11 +79,21 @@ func (m *Ring) modify(a string) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func isEndpointOnline(site string) bool {
|
||||
func isTargetValid(my_url *url.URL, site string) bool {
|
||||
resp, err := http.Get(site)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return false
|
||||
}
|
||||
return 200 <= resp.StatusCode && resp.StatusCode < 300
|
||||
if !(200 <= resp.StatusCode && resp.StatusCode < 300) {
|
||||
return false
|
||||
}
|
||||
|
||||
bodyb, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Printf("error while reading target http body: %v", err)
|
||||
return false
|
||||
}
|
||||
body := string(bodyb)
|
||||
return strings.Contains(body, my_url.Host)
|
||||
}
|
||||
|
|
|
@ -92,7 +92,7 @@ func (m Ring) next(writer http.ResponseWriter, request *http.Request) {
|
|||
for j := i + 1; j < length+i; j++ {
|
||||
dest := m.ring[j%length].url
|
||||
log.Println("Checking '" + dest + "'")
|
||||
if isEndpointOnline(dest) {
|
||||
if isTargetValid(request.URL, dest) {
|
||||
log.Println("Redirecting visitor to '" + dest + "'")
|
||||
http.Redirect(writer, request, dest, http.StatusFound)
|
||||
success = true
|
||||
|
@ -126,7 +126,7 @@ func (m Ring) previous(writer http.ResponseWriter, request *http.Request) {
|
|||
// from here to start of list
|
||||
for i := index - 1; i > 0; i-- {
|
||||
dest := m.ring[i].url
|
||||
if isEndpointOnline(dest) {
|
||||
if isTargetValid(request.URL, dest) {
|
||||
log.Println("Redirecting visitor to '" + dest + "'")
|
||||
http.Redirect(writer, request, dest, http.StatusFound)
|
||||
return
|
||||
|
@ -135,7 +135,7 @@ func (m Ring) previous(writer http.ResponseWriter, request *http.Request) {
|
|||
// from end of list to here
|
||||
for i := length - 1; i > index; i-- {
|
||||
dest := m.ring[i].url
|
||||
if isEndpointOnline(dest) {
|
||||
if isTargetValid(request.URL, dest) {
|
||||
log.Println("Redirecting visitor to '" + dest + "'")
|
||||
http.Redirect(writer, request, dest, http.StatusFound)
|
||||
return
|
||||
|
@ -152,6 +152,8 @@ please email amolith@secluded.site and let him (me) know what's up.`, 500)
|
|||
|
||||
// Redirects the visitor to a random member
|
||||
func (m Ring) random(writer http.ResponseWriter, request *http.Request) {
|
||||
// todo: isTargetValid
|
||||
|
||||
if m.modify("ring") {
|
||||
log.Println("Ring modified, clearing field and re-parsing")
|
||||
m.parseList()
|
||||
|
|
Loading…
Reference in a new issue