clean up more

now I really need to clean my eyes
This commit is contained in:
Locria Cyber 2023-12-04 13:18:36 +00:00
parent 1b1dac635d
commit f7f8fe5c0e
Signed by: iacore
GPG key ID: F8C16E5157A63006
3 changed files with 33 additions and 28 deletions

View file

@ -19,7 +19,7 @@ func link(l string) string {
}
// parseIndex parses the index template and returns a template struct.
func (m *model) parseIndex() {
func (m *Ring) parseIndex() {
m.index = nil
tmpl, err := template.ParseFiles(*flagIndex)
if err != nil {
@ -35,7 +35,7 @@ func (m *model) parseIndex() {
// List parses the list of members, appends the data to a slice of type list,
// then returns the slice
func (m *model) parseList() {
func (m *Ring) parseList() {
m.ring = nil
file, err := ioutil.ReadFile(*flagMembers)
if err != nil {
@ -44,7 +44,7 @@ func (m *model) parseList() {
lines := strings.Split(string(file), "\n")
for _, line := range lines[:len(lines)-1] {
fields := strings.Fields(line)
m.ring = append(m.ring, ring{handle: fields[0], url: fields[1]})
m.ring = append(m.ring, RingMember{handle: fields[0], url: fields[1]})
}
fileStat, err := os.Stat(*flagMembers)
if err != nil {
@ -55,7 +55,7 @@ func (m *model) parseList() {
// Modify takes arguments "index" or "ring" and returns true if either have been
// modified since last read
func (m *model) modify(a string) bool {
func (m *Ring) modify(a string) bool {
if a == "ring" {
ringStat, err := os.Stat(*flagMembers)
if err != nil {
@ -76,7 +76,7 @@ func (m *model) modify(a string) bool {
return true
}
func isStatusOk(site string) bool {
func isEndpointOnline(site string) bool {
resp, err := http.Get(site)
if err != nil {
log.Println(err)

View file

@ -9,11 +9,12 @@ import (
"log"
"math/rand"
"net/http"
"net/url"
"time"
)
// Serves the webpage created by createRoot()
func (m model) root(writer http.ResponseWriter, request *http.Request) {
func (m Ring) root(writer http.ResponseWriter, request *http.Request) {
if m.modify("ring") {
log.Println("Ring modified, clearing field and re-parsing")
m.parseList()
@ -32,23 +33,28 @@ func (m model) root(writer http.ResponseWriter, request *http.Request) {
m.index.Execute(writer, template.HTML(table))
}
func (m Ring) match(query url.Values, item RingMember) bool {
host := query.Get("host")
return item.url == host
}
// Redirects the visitor to the next member, wrapping around the list if the
// next would be out-of-bounds, and ensuring the destination returns a 200 OK
// status before performing the redirect.
func (m model) next(writer http.ResponseWriter, request *http.Request) {
func (m Ring) next(writer http.ResponseWriter, request *http.Request) {
if m.modify("ring") {
log.Println("Ring modified, clearing field and re-parsing")
m.parseList()
}
host := request.URL.Query().Get("host")
scheme, success := "https://", false
success := false
query := request.URL.Query()
length := len(m.ring)
for i, item := range m.ring {
if item.url == host {
if m.match(query, item) {
for j := i + 1; j < length+i; j++ {
dest := scheme + m.ring[j%length].url
dest := m.ring[j%length].url
log.Println("Checking '" + dest + "'")
if isStatusOk(dest) {
if isEndpointOnline(dest) {
log.Println("Redirecting visitor to '" + dest + "'")
http.Redirect(writer, request, dest, http.StatusFound)
success = true
@ -59,27 +65,26 @@ func (m model) next(writer http.ResponseWriter, request *http.Request) {
}
}
if !success {
http.Error(writer, "Ring member '"+host+"' not found.", 404)
http.Error(writer, "Ring member '"+query.Encode()+"' not found.", 404)
}
}
// Redirects the visitor to the previous member, wrapping around the list if the
// next would be out-of-bounds, and ensuring the destination returns a 200 OK
// status before performing the redirect.
func (m model) previous(writer http.ResponseWriter, request *http.Request) {
func (m Ring) previous(writer http.ResponseWriter, request *http.Request) {
if m.modify("ring") {
log.Println("Ring modified, clearing field and re-parsing")
m.parseList()
}
host := request.URL.Query().Get("host")
scheme := "https://"
query := request.URL.Query()
length := len(m.ring)
for index, item := range m.ring {
if item.url == host {
if m.match(query, item) {
// from here to start of list
for i := index - 1; i > 0; i-- {
dest := scheme + m.ring[i].url
if isStatusOk(dest) {
dest := m.ring[i].url
if isEndpointOnline(dest) {
log.Println("Redirecting visitor to '" + dest + "'")
http.Redirect(writer, request, dest, http.StatusFound)
return
@ -87,8 +92,8 @@ func (m model) previous(writer http.ResponseWriter, request *http.Request) {
}
// from end of list to here
for i := length - 1; i > index; i-- {
dest := scheme + m.ring[i].url
if isStatusOk(dest) {
dest := m.ring[i].url
if isEndpointOnline(dest) {
log.Println("Redirecting visitor to '" + dest + "'")
http.Redirect(writer, request, dest, http.StatusFound)
return
@ -100,11 +105,11 @@ please email amolith@secluded.site and let him (me) know what's up.`, 500)
return
}
}
http.Error(writer, "Ring member '"+host+"' not found.", 404)
http.Error(writer, "Ring member '"+query.Encode()+"' not found.", 404)
}
// Redirects the visitor to a random member
func (m model) random(writer http.ResponseWriter, request *http.Request) {
func (m Ring) random(writer http.ResponseWriter, request *http.Request) {
if m.modify("ring") {
log.Println("Ring modified, clearing field and re-parsing")
m.parseList()

10
main.go
View file

@ -15,13 +15,13 @@ import (
flag "github.com/spf13/pflag"
)
type ring struct {
type RingMember struct {
handle string
url string
}
type model struct {
ring []ring
type Ring struct {
ring []RingMember
index *template.Template
ringModTime int64
indexModTime int64
@ -54,7 +54,7 @@ func main() {
panic("socket addr is empty; please specify -l")
}
m := model{}
m := Ring{}
m.init()
mux := http.NewServeMux()
@ -82,7 +82,7 @@ func main() {
}
}
func (m *model) init() {
func (m *Ring) init() {
flag.Parse()
log.Println("Listening on", *flagListen)
log.Println("Looking for members in", *flagMembers)