login-validator: add IP

This commit is contained in:
nemunaire 2019-02-28 02:27:33 +01:00
commit 0b05e8539d
4 changed files with 31 additions and 13 deletions

View file

@ -9,7 +9,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
@ -171,6 +170,7 @@ func (l loginChecker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if mac == nil {
log.Printf("Unable to find MAC address for given IP (%s)\n", ip)
http.Error(w, "Internal server error. Please retry in a few minutes", http.StatusInternalServerError)
return
}
// Register the user remotely
@ -178,7 +178,7 @@ func (l loginChecker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println("Error on remote registration:", err)
http.Error(w, "Internal server error. Please retry in a few minutes", http.StatusInternalServerError)
return
} else if err := l.lateLoginAction(lu.Username, r.RemoteAddr, *mac); err != nil {
} else if err := l.lateLoginAction(lu.Username, r.RemoteAddr, *mac, ip); err != nil {
log.Println("Error on late login action:", err)
http.Error(w, "Internal server error. Please retry in a few minutes", http.StatusInternalServerError)
return
@ -188,7 +188,13 @@ func (l loginChecker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
func (l loginChecker) registerUser(username, remoteAddr string, ent ARPEntry) ([]byte, error) {
type myIP struct {
Id int64 `json:"id"`
Login string `json:"login"`
IP string `json:"ip"`
}
func (l loginChecker) registerUser(username, remoteAddr string, ent ARPEntry) (net.IP, error) {
bts, err := json.Marshal(map[string]interface{}{"login": username, "ip": remoteAddr, "mac": fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", ent.HWAddress[0], ent.HWAddress[1], ent.HWAddress[2], ent.HWAddress[3], ent.HWAddress[4], ent.HWAddress[5])})
if err != nil {
return nil, nil
@ -211,10 +217,15 @@ func (l loginChecker) registerUser(username, remoteAddr string, ent ARPEntry) ([
if resp.StatusCode != http.StatusOK {
return nil, errors.New(resp.Status)
} else {
return ioutil.ReadAll(resp.Body)
dec := json.NewDecoder(resp.Body)
var myip myIP
if err := dec.Decode(&myip); err != nil {
return nil, err
}
return net.ParseIP(myip.IP), nil
}
}
func (l loginChecker) lateLoginAction(username, remoteAddr string, mac ARPEntry) error {
return RegisterUserMAC(mac, username)
func (l loginChecker) lateLoginAction(username, remoteAddr string, mac ARPEntry, ip net.IP) error {
return RegisterUserMAC(mac, ip, username)
}