validator: add logout route

This commit is contained in:
nemunaire 2018-02-18 14:42:35 +01:00
parent 23ab4264f9
commit d42593715d
2 changed files with 46 additions and 0 deletions

45
validator/logout.go Normal file
View File

@ -0,0 +1,45 @@
package main
import (
"fmt"
"log"
"net"
"net/http"
"os"
"path"
"strings"
)
func logout(w http.ResponseWriter, r *http.Request) {
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
r.RemoteAddr = addr
}
log.Printf("%s \"%s %s\" [%s]\n", r.RemoteAddr, r.Method, r.URL.Path, r.UserAgent())
w.Header().Set("Content-Type", "text/plain")
// Find corresponding MAC
var fname string
spl := strings.SplitN(r.RemoteAddr, ":", 2)
if ip := net.ParseIP(spl[0]); ip == nil {
http.Error(w, "Unable to parse given IPv4: "+spl[0], http.StatusInternalServerError)
return
} else if arptable, err := ARPAnalyze(); err != nil {
http.Error(w, "Unable to logout: "+err.Error(), http.StatusInternalServerError)
return
} else if arpent := ARPContainsIP(arptable, ip); arpent == nil {
http.Error(w, "Unable to find MAC in ARP table to logout.", http.StatusInternalServerError)
return
} else {
fname = fmt.Sprintf("%02x-%02x-%02x-%02x-%02x-%02x-%02x", arpent.HWType, arpent.HWAddress[0], arpent.HWAddress[1], arpent.HWAddress[2], arpent.HWAddress[3], arpent.HWAddress[4], arpent.HWAddress[5])
}
if err := os.Remove(path.Join(tftpDir, "pxelinux.cfg", fname)); err != nil {
log.Println("Error on logout action:", err)
http.Error(w, "Unable to logout: "+err.Error(), http.StatusInternalServerError)
return
}
log.Println("Successful logout from", r.RemoteAddr)
http.Error(w, "Success", http.StatusOK)
}

View File

@ -44,6 +44,7 @@ func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", Index)
mux.Handle("/login", lc)
mux.HandleFunc("/logout", logout)
http.HandleFunc("/", mux.ServeHTTP)
log.Println("Ready, listening on port", *bind)