Display IP to use in login-app

This commit is contained in:
nemunaire 2019-02-26 23:48:01 +01:00
parent 8b06f62e90
commit 82f83e3d89
4 changed files with 35 additions and 34 deletions

View File

@ -21,11 +21,8 @@ func checkLogin(username, password string) (bool, error) {
if err != nil { if err != nil {
return false, err return false, err
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { cnt, _ := ioutil.ReadAll(resp.Body)
cnt, _ := ioutil.ReadAll(resp.Body) return resp.StatusCode == http.StatusOK, errors.New(string(cnt))
return false, errors.New(string(cnt))
}
return true, nil
} }

View File

@ -6,17 +6,20 @@ import (
const URLLogin = "https://auth.adlin.nemunai.re/login" const URLLogin = "https://auth.adlin.nemunai.re/login"
func goLogin(stdscr *gc.Window, in chan gc.Key) (string, bool) { func goLogin(stdscr *gc.Window, in chan gc.Key) (string, string, bool) {
username, password := login(stdscr, in) username, password := login(stdscr, in)
validator := make(chan error) validator := make(chan bool)
go func(username, password string, progress chan error) { validator_err := make(chan error)
_, err := checkLogin(username, password) go func(username, password string, progress chan bool, err chan error) {
progress <- err st, errm := checkLogin(username, password)
}(username, password, validator) progress <- st
err <- errm
}(username, password, validator, validator_err)
if connection(stdscr, in, validator) { if connection(stdscr, in, validator, validator_err) {
return username, true e := <- validator_err
return username, e.Error(), true
} else { } else {
return goLogin(stdscr, in) return goLogin(stdscr, in)
} }
@ -49,7 +52,7 @@ func main() {
}(stdscr, in) }(stdscr, in)
// Run! // Run!
if username, ok := goLogin(stdscr, in); ok { if username, ip, ok := goLogin(stdscr, in); ok {
okreboot(stdscr, username) okreboot(stdscr, username, ip)
} }
} }

View File

@ -104,7 +104,7 @@ login:
return return
} }
func connection(stdscr *gc.Window, in chan gc.Key, validator chan error) (canContinue bool) { func connection(stdscr *gc.Window, in chan gc.Key, validator chan bool, validator_err chan error) (canContinue bool) {
gc.Cursor(0) gc.Cursor(0)
stdscr.Clear() stdscr.Clear()
@ -148,11 +148,13 @@ loginloop:
if gc.Char(ch) == gc.Char('r') { if gc.Char(ch) == gc.Char('r') {
break loginloop break loginloop
} }
case e := <- validator: case st := <- validator:
if e == nil { if st {
canContinue = true canContinue = true
break loginloop break loginloop
} else { } else {
e := <- validator_err
mainwin.ColorOn(4) mainwin.ColorOn(4)
mainwin.MovePrint(4, 2, e.Error()) mainwin.MovePrint(4, 2, e.Error())
mainwin.ColorOff(4) mainwin.ColorOff(4)
@ -186,7 +188,7 @@ loginloop:
return return
} }
func okreboot(stdscr *gc.Window, login string) { func okreboot(stdscr *gc.Window, login string, ip string) {
gc.Cursor(0) gc.Cursor(0)
stdscr.Clear() stdscr.Clear()

View File

@ -9,6 +9,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -173,32 +174,29 @@ func (l loginChecker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
// Register the user remotely // Register the user remotely
if err := l.registerUser(lu.Username, r.RemoteAddr, *mac); err != nil { if ip, err := l.registerUser(lu.Username, r.RemoteAddr, *mac); err != nil {
log.Println("Error on remote registration:", err) log.Println("Error on remote registration:", err)
http.Error(w, "Internal server error. Please retry in a few minutes", http.StatusInternalServerError) http.Error(w, "Internal server error. Please retry in a few minutes", http.StatusInternalServerError)
return return
} } else if err := l.lateLoginAction(lu.Username, r.RemoteAddr, *mac); err != nil {
// Generate PXE file
if err := l.lateLoginAction(lu.Username, r.RemoteAddr, *mac); err != nil {
log.Println("Error on late login action:", err) log.Println("Error on late login action:", err)
http.Error(w, "Internal server error. Please retry in a few minutes", http.StatusInternalServerError) http.Error(w, "Internal server error. Please retry in a few minutes", http.StatusInternalServerError)
return return
} else {
log.Println("Successful login of", lu.Username, "at", r.RemoteAddr)
http.Error(w, fmt.Sprintf("Use the following IP: %s", ip), http.StatusOK)
} }
log.Println("Successful login of", lu.Username, "at", r.RemoteAddr)
http.Error(w, "Success", http.StatusOK)
} }
func (l loginChecker) registerUser(username, remoteAddr string, ent ARPEntry) error { func (l loginChecker) registerUser(username, remoteAddr string, ent ARPEntry) ([]byte, 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])}) 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 { if err != nil {
return nil return nil, nil
} }
req, err := http.NewRequest("POST", "https://adlin.nemunai.re/api/students/", bytes.NewReader(bts)) req, err := http.NewRequest("POST", "https://adlin.nemunai.re/api/students/", bytes.NewReader(bts))
if err != nil { if err != nil {
return err return nil, err
} }
req.Header.Add("X-ADLIN-Authentication", base64.StdEncoding.EncodeToString(hmac.New(sha512.New, []byte(loginSalt)).Sum([]byte(fmt.Sprintf("%d", time.Now().Unix()/10))))) req.Header.Add("X-ADLIN-Authentication", base64.StdEncoding.EncodeToString(hmac.New(sha512.New, []byte(loginSalt)).Sum([]byte(fmt.Sprintf("%d", time.Now().Unix()/10)))))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
@ -206,13 +204,14 @@ func (l loginChecker) registerUser(username, remoteAddr string, ent ARPEntry) er
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return err return nil, err
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return errors.New(resp.Status) return nil, errors.New(resp.Status)
} else { } else {
return nil return ioutil.ReadAll(resp.Body)
} }
} }