ictgj-voting/admin_clients.go

93 lines
2.2 KiB
Go
Raw Normal View History

2017-06-08 17:20:43 +00:00
package main
import (
"net"
"net/http"
"github.com/gorilla/mux"
)
func handleAdminClients(w http.ResponseWriter, req *http.Request, page *pageData) {
vars := mux.Vars(req)
page.SubTitle = "Clients"
clientId := vars["id"]
2017-10-21 22:47:01 +00:00
client, err := m.GetClient(clientId)
if err != nil {
client = NewClient(clientId)
}
2017-06-08 17:20:43 +00:00
clientIp, _, _ := net.SplitHostPort(req.RemoteAddr)
if clientId == "" {
type clientsPageData struct {
Clients []Client
}
2017-10-18 22:18:12 +00:00
page.TemplateData = clientsPageData{Clients: m.clients}
2017-06-08 17:20:43 +00:00
page.SubTitle = "Clients"
page.show("admin-clients.html", w)
} else {
switch vars["function"] {
case "add":
2017-07-28 19:36:14 +00:00
page.SubTitle = "Authenticate Client"
2017-10-18 22:18:12 +00:00
if client.IP == "" {
client.IP = clientIp
2017-07-28 19:36:14 +00:00
}
2017-06-08 17:20:43 +00:00
type actClientPageData struct {
2017-07-28 19:36:14 +00:00
Id string
Ip string
Name string
2017-06-08 17:20:43 +00:00
}
2017-10-18 22:18:12 +00:00
page.TemplateData = actClientPageData{Id: client.UUID, Ip: client.IP, Name: client.Name}
2017-06-08 17:20:43 +00:00
page.show("admin-activateclient.html", w)
case "auth":
email := req.FormValue("email")
password := req.FormValue("password")
2017-07-28 19:36:14 +00:00
clientName := req.FormValue("clientname")
// Authentication isn't required to set a client name
2017-07-28 19:36:14 +00:00
if clientName != "" {
client.Name = clientName
2017-07-28 19:36:14 +00:00
}
client.IP = clientIp
2017-10-18 22:18:12 +00:00
m.UpdateClient(client)
2017-07-28 19:36:14 +00:00
if page.LoggedIn || doLogin(email, password) == nil {
2017-06-08 17:20:43 +00:00
// Received a valid login
// Authenticate the client
client.Auth = true
2017-10-18 22:18:12 +00:00
m.UpdateClient(client)
page.session.setFlashMessage("Client Authenticated", "success")
2017-07-28 19:36:14 +00:00
if page.LoggedIn {
2017-06-08 17:20:43 +00:00
redirect("/admin/clients", w, req)
}
}
redirect("/", w, req)
case "deauth":
client.Auth = false
2017-10-18 22:18:12 +00:00
m.UpdateClient(client)
page.session.setFlashMessage("Client De-Authenticated", "success")
2017-06-08 17:20:43 +00:00
redirect("/admin/clients", w, req)
}
}
}
2017-06-15 17:35:53 +00:00
func clientIsServer(req *http.Request) bool {
clientIp, _, _ := net.SplitHostPort(req.RemoteAddr)
ifaces, err := net.Interfaces()
if err == nil {
for _, i := range ifaces {
if addrs, err := i.Addrs(); err == nil {
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if clientIp == ip.String() {
return true
}
}
}
}
}
return false
}