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 {
return false, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
cnt, _ := ioutil.ReadAll(resp.Body)
return false, errors.New(string(cnt))
}
return true, nil
cnt, _ := ioutil.ReadAll(resp.Body)
return resp.StatusCode == http.StatusOK, errors.New(string(cnt))
}

View File

@ -6,17 +6,20 @@ import (
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)
validator := make(chan error)
go func(username, password string, progress chan error) {
_, err := checkLogin(username, password)
progress <- err
}(username, password, validator)
validator := make(chan bool)
validator_err := make(chan error)
go func(username, password string, progress chan bool, err chan error) {
st, errm := checkLogin(username, password)
progress <- st
err <- errm
}(username, password, validator, validator_err)
if connection(stdscr, in, validator) {
return username, true
if connection(stdscr, in, validator, validator_err) {
e := <- validator_err
return username, e.Error(), true
} else {
return goLogin(stdscr, in)
}
@ -49,7 +52,7 @@ func main() {
}(stdscr, in)
// Run!
if username, ok := goLogin(stdscr, in); ok {
okreboot(stdscr, username)
if username, ip, ok := goLogin(stdscr, in); ok {
okreboot(stdscr, username, ip)
}
}

View File

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

View File

@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
@ -173,32 +174,29 @@ func (l loginChecker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
// 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)
http.Error(w, "Internal server error. Please retry in a few minutes", http.StatusInternalServerError)
return
}
// Generate PXE file
if err := l.lateLoginAction(lu.Username, r.RemoteAddr, *mac); err != nil {
} else if err := l.lateLoginAction(lu.Username, r.RemoteAddr, *mac); 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
} 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])})
if err != nil {
return nil
return nil, nil
}
req, err := http.NewRequest("POST", "https://adlin.nemunai.re/api/students/", bytes.NewReader(bts))
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.Set("Content-Type", "application/json")
@ -206,13 +204,14 @@ func (l loginChecker) registerUser(username, remoteAddr string, ent ARPEntry) er
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.New(resp.Status)
return nil, errors.New(resp.Status)
} else {
return nil
return ioutil.ReadAll(resp.Body)
}
}