Use pointer instead of struct
This commit is contained in:
parent
853477e54a
commit
6d8f38d749
18 changed files with 187 additions and 142 deletions
|
|
@ -10,7 +10,7 @@ const (
|
|||
DelegatedDomainSuffix = "srs.p0m.fr."
|
||||
)
|
||||
|
||||
func (student Student) MyDelegatedDomain() string {
|
||||
func (student *Student) MyDelegatedDomain() string {
|
||||
if student.DelegatedDomain != nil {
|
||||
return *student.DelegatedDomain
|
||||
} else {
|
||||
|
|
@ -18,11 +18,11 @@ func (student Student) MyDelegatedDomain() string {
|
|||
}
|
||||
}
|
||||
|
||||
func (student Student) DefaultAssociatedDomain() string {
|
||||
func (student *Student) DefaultAssociatedDomain() string {
|
||||
return fmt.Sprintf("%s.%s", strings.Trim(strings.Replace(student.Login, "_", "-", -1), "-_"), AssociatedDomainSuffix)
|
||||
}
|
||||
|
||||
func (student Student) MyAssociatedDomain() string {
|
||||
func (student *Student) MyAssociatedDomain() string {
|
||||
if student.AssociatedDomain != nil {
|
||||
return *student.AssociatedDomain
|
||||
} else {
|
||||
|
|
@ -30,7 +30,7 @@ func (student Student) MyAssociatedDomain() string {
|
|||
}
|
||||
}
|
||||
|
||||
func (student Student) GetAssociatedDomains() (ds []string) {
|
||||
func (student *Student) GetAssociatedDomains() (ds []string) {
|
||||
defdn := student.DefaultAssociatedDomain()
|
||||
ds = append(ds, defdn)
|
||||
|
||||
|
|
|
|||
|
|
@ -9,14 +9,14 @@ type Pong struct {
|
|||
State bool
|
||||
}
|
||||
|
||||
func (s Student) LastPongs() (pongs []Pong, err error) {
|
||||
func (s *Student) LastPongs() (pongs []*Pong, err error) {
|
||||
if rows, errr := DBQuery("SELECT time, state FROM student_pong WHERE id_student = ? ORDER BY time DESC", s.Id); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var p Pong
|
||||
p := &Pong{}
|
||||
if err = rows.Scan(&p.Date, &p.State); err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,40 +11,41 @@ type Session struct {
|
|||
Time time.Time `json:"time"`
|
||||
}
|
||||
|
||||
func GetSession(id []byte) (s Session, err error) {
|
||||
func GetSession(id []byte) (s *Session, err error) {
|
||||
s = new(Session)
|
||||
err = DBQueryRow("SELECT id_session, id_student, time FROM student_sessions WHERE id_session=?", id).Scan(&s.Id, &s.IdStudent, &s.Time)
|
||||
return
|
||||
}
|
||||
|
||||
func NewSession() (Session, error) {
|
||||
func NewSession() (*Session, error) {
|
||||
session_id := make([]byte, 255)
|
||||
if _, err := rand.Read(session_id); err != nil {
|
||||
return Session{}, err
|
||||
return nil, err
|
||||
} else if _, err := DBExec("INSERT INTO student_sessions (id_session, time) VALUES (?, ?)", session_id, time.Now()); err != nil {
|
||||
return Session{}, err
|
||||
return nil, err
|
||||
} else {
|
||||
return Session{session_id, nil, time.Now()}, nil
|
||||
return &Session{session_id, nil, time.Now()}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (student Student) NewSession() (Session, error) {
|
||||
func (student *Student) NewSession() (*Session, error) {
|
||||
session_id := make([]byte, 255)
|
||||
if _, err := rand.Read(session_id); err != nil {
|
||||
return Session{}, err
|
||||
return nil, err
|
||||
} else if _, err := DBExec("INSERT INTO student_sessions (id_session, id_student, time) VALUES (?, ?, ?)", session_id, student.Id, time.Now()); err != nil {
|
||||
return Session{}, err
|
||||
return nil, err
|
||||
} else {
|
||||
return Session{session_id, &student.Id, time.Now()}, nil
|
||||
return &Session{session_id, &student.Id, time.Now()}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s Session) SetStudent(student Student) (Session, error) {
|
||||
func (s *Session) SetStudent(student *Student) (*Session, error) {
|
||||
s.IdStudent = &student.Id
|
||||
_, err := s.Update()
|
||||
return s, err
|
||||
}
|
||||
|
||||
func (s Session) Update() (int64, error) {
|
||||
func (s *Session) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE student_sessions SET id_student = ?, time = ? WHERE id_session = ?", s.IdStudent, s.Time, s.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
|
|
@ -54,7 +55,7 @@ func (s Session) Update() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s Session) Delete() (int64, error) {
|
||||
func (s *Session) Delete() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM student_sessions WHERE id_session = ?", s.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
|
|
|
|||
|
|
@ -16,14 +16,14 @@ type StudentKey struct {
|
|||
Time time.Time `json:"time"`
|
||||
}
|
||||
|
||||
func GetStudentKeys() (keys []StudentKey, err error) {
|
||||
func GetStudentKeys() (keys []*StudentKey, err error) {
|
||||
if rows, errr := DBQuery("SELECT id_key, id_student, sshkey, time FROM student_keys"); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var k StudentKey
|
||||
k := &StudentKey{}
|
||||
if err = rows.Scan(&k.Id, &k.IdStudent, &k.Key, &k.Time); err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -37,14 +37,14 @@ func GetStudentKeys() (keys []StudentKey, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s Student) GetKeys() (keys []StudentKey, err error) {
|
||||
func (s *Student) GetKeys() (keys []*StudentKey, err error) {
|
||||
if rows, errr := DBQuery("SELECT id_key, id_student, sshkey, time FROM student_keys WHERE id_student = ?", s.Id); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var k StudentKey
|
||||
k := &StudentKey{}
|
||||
if err = rows.Scan(&k.Id, &k.IdStudent, &k.Key, &k.Time); err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -58,12 +58,13 @@ func (s Student) GetKeys() (keys []StudentKey, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func getStudentKey(id int) (k StudentKey, err error) {
|
||||
func getStudentKey(id int) (k *StudentKey, err error) {
|
||||
k = new(StudentKey)
|
||||
err = DBQueryRow("SELECT id_key, id_student, sshkey, time FROM student_keys WHERE id_key=?", id).Scan(&k.Id, &k.IdStudent, &k.Key, &k.Time)
|
||||
return
|
||||
}
|
||||
|
||||
func (s Student) NewKey(key string) (k StudentKey, err error) {
|
||||
func (s *Student) NewKey(key string) (k *StudentKey, err error) {
|
||||
// Check key before importing it
|
||||
cmd := exec.Command("ssh-keygen", "-l", "-f", "-")
|
||||
cmd.Stdin = strings.NewReader(key)
|
||||
|
|
@ -101,20 +102,20 @@ func (s Student) NewKey(key string) (k StudentKey, err error) {
|
|||
key = keyf[0] + " " + keyf[1]
|
||||
|
||||
if res, err := DBExec("INSERT INTO student_keys (id_student, sshkey, time) VALUES (?, ?, ?)", s.Id, key, time.Now()); err != nil {
|
||||
return StudentKey{}, err
|
||||
return nil, err
|
||||
} else if kid, err := res.LastInsertId(); err != nil {
|
||||
return StudentKey{}, err
|
||||
return nil, err
|
||||
} else {
|
||||
s.UnlockNewChallenge(11, "")
|
||||
return StudentKey{kid, s.Id, key, time.Now()}, nil
|
||||
return &StudentKey{kid, s.Id, key, time.Now()}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (k StudentKey) GetStudent() (Student, error) {
|
||||
func (k *StudentKey) GetStudent() (*Student, error) {
|
||||
return GetStudent(int(k.IdStudent))
|
||||
}
|
||||
|
||||
func (k StudentKey) Update() (int64, error) {
|
||||
func (k *StudentKey) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE student_keys SET id_student = ?, sshkey = ?, time = ? WHERE id_key = ?", k.IdStudent, k.Key, k.Time, k.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
|
|
@ -124,7 +125,7 @@ func (k StudentKey) Update() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (k StudentKey) Delete() (int64, error) {
|
||||
func (k *StudentKey) Delete() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM student_keys WHERE id_key = ?", k.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
|
|
|
|||
|
|
@ -17,14 +17,14 @@ type Student struct {
|
|||
DelegatedDomain *string `json:"delegated_domain,omitempty"`
|
||||
}
|
||||
|
||||
func GetStudents() (students []Student, err error) {
|
||||
func GetStudents() (students []*Student, err error) {
|
||||
if rows, errr := DBQuery("SELECT S.id_student, S.login, MAX(L.time), L.ip, L.mac, S.associatedDomain, S.delegatedDomain FROM students S INNER JOIN (SELECT a.id_student, a.time, a.ip, a.mac FROM student_login a INNER JOIN (SELECT id_student, MAX(time) AS time FROM student_login GROUP BY id_student) b ON a.id_student = b.id_student AND a.time = b.time) L ON S.id_student = L.id_student GROUP BY id_student"); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var s Student
|
||||
s := &Student{}
|
||||
if err = rows.Scan(&s.Id, &s.Login, &s.Time, &s.IP, &s.MAC, &s.AssociatedDomain, &s.DelegatedDomain); err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -38,12 +38,14 @@ func GetStudents() (students []Student, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func GetStudent(id int) (s Student, err error) {
|
||||
func GetStudent(id int) (s *Student, err error) {
|
||||
s = new(Student)
|
||||
err = DBQueryRow("SELECT S.id_student, S.login, MAX(L.time), L.ip, L.mac, S.associatedDomain, S.delegatedDomain FROM students S INNER JOIN (SELECT a.id_student, a.time, a.ip, a.mac FROM student_login a INNER JOIN (SELECT id_student, MAX(time) AS time FROM student_login GROUP BY id_student) b ON a.id_student = b.id_student AND a.time = b.time) L ON S.id_student = L.id_student WHERE S.id_student=?", id).Scan(&s.Id, &s.Login, &s.Time, &s.IP, &s.MAC, &s.AssociatedDomain, &s.DelegatedDomain)
|
||||
return
|
||||
}
|
||||
|
||||
func GetStudentByLogin(login string) (s Student, err error) {
|
||||
func GetStudentByLogin(login string) (s *Student, err error) {
|
||||
s = new(Student)
|
||||
err = DBQueryRow("SELECT S.id_student, S.login, MAX(L.time), L.ip, L.mac, S.associatedDomain, S.delegatedDomain FROM students S INNER JOIN (SELECT a.id_student, a.time, a.ip, a.mac FROM student_login a INNER JOIN (SELECT id_student, MAX(time) AS time FROM student_login GROUP BY id_student) b ON a.id_student = b.id_student AND a.time = b.time) L ON S.id_student = L.id_student WHERE login=?", login).Scan(&s.Id, &s.Login, &s.Time, &s.IP, &s.MAC, &s.AssociatedDomain, &s.DelegatedDomain)
|
||||
return
|
||||
}
|
||||
|
|
@ -54,22 +56,22 @@ func StudentExists(login string) bool {
|
|||
return err == nil && z == 1
|
||||
}
|
||||
|
||||
func NewStudent(login string) (Student, error) {
|
||||
func NewStudent(login string) (*Student, error) {
|
||||
t := time.Now()
|
||||
if res, err := DBExec("INSERT INTO students (login, time) VALUES (?, ?)", login, t); err != nil {
|
||||
return Student{}, err
|
||||
return nil, err
|
||||
} else if sid, err := res.LastInsertId(); err != nil {
|
||||
return Student{}, err
|
||||
return nil, err
|
||||
} else {
|
||||
return Student{sid, login, &t, nil, nil, nil, nil}, nil
|
||||
return &Student{sid, login, &t, nil, nil, nil, nil}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (s Student) GetPKey() []byte {
|
||||
func (s *Student) GetPKey() []byte {
|
||||
return hmac.New(sha512.New512_224, []byte(SharedSecret)).Sum([]byte(s.Login))
|
||||
}
|
||||
|
||||
func (s Student) Update() (int64, error) {
|
||||
func (s *Student) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE students SET login = ?, time = ?, associatedDomain = ?, delegatedDomain = ? WHERE id_student = ?", s.Login, s.Time, s.AssociatedDomain, s.DelegatedDomain, s.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
|
|
@ -79,7 +81,7 @@ func (s Student) Update() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s Student) Delete() (int64, error) {
|
||||
func (s *Student) Delete() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM students WHERE id_student = ?", s.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
|
|
@ -103,18 +105,20 @@ type UnlockedChallenge struct {
|
|||
Id int64 `json:"id,omitempty"`
|
||||
IdStudent int64 `json:"id_student"`
|
||||
Challenge int `json:"challenge,omitempty"`
|
||||
Time time.Time `json:"time"`
|
||||
Time *time.Time `json:"time,omitempty"`
|
||||
Value interface{} `json:"value,omitempty"`
|
||||
LastCheck *time.Time `json:"last_check,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (s Student) GetStates() (ucs []UnlockedChallenge, err error) {
|
||||
func (s *Student) GetStates() (ucs []*UnlockedChallenge, err error) {
|
||||
if rows, errr := DBQuery("SELECT id_st, challenge, time FROM student_challenges WHERE id_student = ?", s.Id); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var u UnlockedChallenge
|
||||
u := &UnlockedChallenge{}
|
||||
u.IdStudent = s.Id
|
||||
if err = rows.Scan(&u.Id, &u.Challenge, &u.Time); err != nil {
|
||||
return
|
||||
|
|
@ -129,14 +133,36 @@ func (s Student) GetStates() (ucs []UnlockedChallenge, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s Student) GetStatesByChallenge() (ucs []UnlockedChallenge, err error) {
|
||||
func (s *Student) GetChallengeErrors() (ucs []*ErroredChallenge, err error) {
|
||||
if rows, errr := DBQuery("SELECT id_st, challenge, time, error FROM student_challenge_errors WHERE id_student = ?", s.Id); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
u := &ErroredChallenge{}
|
||||
u.IdStudent = s.Id
|
||||
if err = rows.Scan(&u.Id, &u.Challenge, &u.Time, &u.Error); err != nil {
|
||||
return
|
||||
}
|
||||
ucs = append(ucs, u)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Student) GetStatesByChallenge() (ucs []*UnlockedChallenge, err error) {
|
||||
if rows, errr := DBQuery("SELECT id_st, challenge, MIN(time), value FROM student_challenges WHERE id_student = ? GROUP BY challenge, id_student", s.Id); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var u UnlockedChallenge
|
||||
u := &UnlockedChallenge{}
|
||||
u.IdStudent = s.Id
|
||||
if err = rows.Scan(&u.Id, &u.Challenge, &u.Time, &u.Value); err != nil {
|
||||
return
|
||||
|
|
@ -151,7 +177,7 @@ func (s Student) GetStatesByChallenge() (ucs []UnlockedChallenge, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s Student) UnlockChallenge(challenge int, value string) (uc UnlockedChallenge, err error) {
|
||||
func (s *Student) UnlockChallenge(challenge int, value string) (uc *UnlockedChallenge, err error) {
|
||||
if uc, err = s.UnlockNewChallenge(challenge, value); err != nil {
|
||||
if uc, err = s.UpdateUnlockedChallenge(challenge, value); err != nil {
|
||||
return
|
||||
|
|
@ -163,21 +189,23 @@ func (s Student) UnlockChallenge(challenge int, value string) (uc UnlockedChalle
|
|||
return
|
||||
}
|
||||
|
||||
func (s Student) UnlockNewChallenge(challenge int, value string) (UnlockedChallenge, error) {
|
||||
func (s *Student) UnlockNewChallenge(challenge int, value string) (*UnlockedChallenge, error) {
|
||||
if res, err := DBExec("INSERT INTO student_challenges (id_student, challenge, time, value) VALUES (?, ?, ?, ?)", s.Id, challenge, time.Now(), value); err != nil {
|
||||
return UnlockedChallenge{}, err
|
||||
return nil, err
|
||||
} else if utid, err := res.LastInsertId(); err != nil {
|
||||
return UnlockedChallenge{}, err
|
||||
return nil, err
|
||||
} else {
|
||||
return UnlockedChallenge{utid, s.Id, challenge, time.Now(), value}, err
|
||||
now := time.Now()
|
||||
return &UnlockedChallenge{utid, s.Id, challenge, &now, value, nil, ""}, err
|
||||
}
|
||||
}
|
||||
|
||||
func (s Student) UpdateUnlockedChallenge(challenge int, value string) (UnlockedChallenge, error) {
|
||||
func (s *Student) UpdateUnlockedChallenge(challenge int, value string) (*UnlockedChallenge, error) {
|
||||
if _, err := DBExec("UPDATE student_challenges SET time = ?, value = ? WHERE id_student = ? AND challenge = ?", time.Now(), value, s.Id, challenge); err != nil {
|
||||
return UnlockedChallenge{}, err
|
||||
return nil, err
|
||||
} else {
|
||||
return UnlockedChallenge{0, s.Id, challenge, time.Now(), value}, err
|
||||
now := time.Now()
|
||||
return &UnlockedChallenge{0, s.Id, challenge, &now, value, nil, ""}, err
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -189,7 +217,7 @@ type ErroredChallenge struct {
|
|||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (s Student) RegisterChallengeError(challenge int, err error) error {
|
||||
func (s *Student) RegisterChallengeError(challenge int, err error) error {
|
||||
if _, errr := DBExec("INSERT INTO student_challenge_errors (id_student, challenge, time, error) VALUES (?, ?, ?, ?)", s.Id, challenge, time.Now(), err.Error()); errr == nil {
|
||||
return nil
|
||||
} else if _, errr := DBExec("UPDATE student_challenge_errors SET time = ?, error = ? WHERE id_student = ? AND challenge = ?", time.Now(), err.Error(), s.Id, challenge); errr != nil {
|
||||
|
|
@ -199,7 +227,7 @@ func (s Student) RegisterChallengeError(challenge int, err error) error {
|
|||
}
|
||||
}
|
||||
|
||||
func (s Student) RegisterAccess(ip, mac string) error {
|
||||
func (s *Student) RegisterAccess(ip, mac string) error {
|
||||
if res, err := DBExec("INSERT INTO student_login (id_student, ip, mac, time) VALUES (?, ?, ?, ?)", s.Id, ip, mac, time.Now()); err != nil {
|
||||
return err
|
||||
} else if _, err := res.LastInsertId(); err != nil {
|
||||
|
|
|
|||
|
|
@ -37,32 +37,32 @@ type WGDump struct {
|
|||
}
|
||||
|
||||
var (
|
||||
wgDumpCache_data map[string]WGDump = nil
|
||||
wgDumpCache_data map[string]*WGDump = nil
|
||||
wgDumpCache_time time.Time
|
||||
wgDumpCache_mutex sync.RWMutex
|
||||
)
|
||||
|
||||
func _readWgDump() (wgd map[string]WGDump, err error) {
|
||||
func _readWgDump() (wgd map[string]*WGDump, err error) {
|
||||
out, errr := exec.Command("wg", "show", "wg-adlin", "dump").Output()
|
||||
|
||||
if errr != nil {
|
||||
return nil, errr
|
||||
}
|
||||
|
||||
wgd = map[string]WGDump{}
|
||||
wgd = map[string]*WGDump{}
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
cols := strings.Fields(line)
|
||||
if len(cols) != 8 {
|
||||
continue
|
||||
}
|
||||
|
||||
wgd[cols[0]] = WGDump{cols[0], cols[1], cols[2], cols[3], cols[4], cols[5], cols[6], cols[7]}
|
||||
wgd[cols[0]] = &WGDump{cols[0], cols[1], cols[2], cols[3], cols[4], cols[5], cols[6], cols[7]}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func readWgDump() (wgd map[string]WGDump, err error) {
|
||||
func readWgDump() (wgd map[string]*WGDump, err error) {
|
||||
wgDumpCache_mutex.RLock()
|
||||
defer wgDumpCache_mutex.RUnlock()
|
||||
|
||||
|
|
@ -116,24 +116,26 @@ func TokenFromText(token string) []byte {
|
|||
return sha[:]
|
||||
}
|
||||
|
||||
func GetTunnelToken(token []byte) (t TunnelToken, err error) {
|
||||
func GetTunnelToken(token []byte) (t *TunnelToken, err error) {
|
||||
t = new(TunnelToken)
|
||||
err = DBQueryRow("SELECT token, token_text, id_student, pubkey, time, suffixip, version FROM student_tunnel_tokens WHERE token=? ORDER BY time DESC", token).Scan(&t.token, &t.TokenText, &t.IdStudent, &t.PubKey, &t.Time, &t.SuffixIP, &t.Version)
|
||||
if err == nil && t.PubKey != nil {
|
||||
if wgd, errr := readWgDump(); errr == nil {
|
||||
if v, ok := wgd[base64.StdEncoding.EncodeToString(t.PubKey)]; ok {
|
||||
t.Dump = &v
|
||||
t.Dump = v
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (student Student) NewTunnelToken(suffixip int) (t TunnelToken, err error) {
|
||||
func (student *Student) NewTunnelToken(suffixip int) (t *TunnelToken, err error) {
|
||||
tok := make([]byte, 7)
|
||||
if _, err = rand.Read(tok); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
t = new(TunnelToken)
|
||||
t.TokenText = strings.Replace(strings.Replace(strings.Replace(strings.Replace(strings.Replace(base64.RawStdEncoding.EncodeToString(tok), "/", ".", -1), "+", "_", -1), "O", "<", -1), "l", "$", -1), "I", ">", -1)
|
||||
t.token = TokenFromText(t.TokenText)
|
||||
t.IdStudent = student.Id
|
||||
|
|
@ -142,7 +144,7 @@ func (student Student) NewTunnelToken(suffixip int) (t TunnelToken, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func (student Student) GetTunnelTokens() (ts []TunnelToken, err error) {
|
||||
func (student *Student) GetTunnelTokens() (ts []*TunnelToken, err error) {
|
||||
if rows, errr := DBQuery("SELECT token, token_text, id_student, pubkey, time, suffixip, version FROM student_tunnel_tokens WHERE id_student = ? ORDER BY time DESC", student.Id); errr != nil {
|
||||
return nil, errr
|
||||
} else if wgd, errr := readWgDump(); errr != nil {
|
||||
|
|
@ -151,13 +153,13 @@ func (student Student) GetTunnelTokens() (ts []TunnelToken, err error) {
|
|||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var t TunnelToken
|
||||
t := &TunnelToken{}
|
||||
if err = rows.Scan(&t.token, &t.TokenText, &t.IdStudent, &t.PubKey, &t.Time, &t.SuffixIP, &t.Version); err != nil {
|
||||
return
|
||||
}
|
||||
if t.PubKey != nil {
|
||||
if v, ok := wgd[base64.StdEncoding.EncodeToString(t.PubKey)]; ok {
|
||||
t.Dump = &v
|
||||
t.Dump = v
|
||||
}
|
||||
}
|
||||
ts = append(ts, t)
|
||||
|
|
@ -170,7 +172,7 @@ func (student Student) GetTunnelTokens() (ts []TunnelToken, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (student Student) GetActivesTunnels() (ts []TunnelToken, err error) {
|
||||
func (student *Student) GetActivesTunnels() (ts []*TunnelToken, err error) {
|
||||
if rows, errr := DBQuery("SELECT token, token_text, id_student, pubkey, time, suffixip, version FROM student_tunnel_tokens WHERE id_student = ? ORDER BY time DESC", student.Id); errr != nil {
|
||||
return nil, errr
|
||||
} else if wgd, errr := readWgDump(); errr != nil {
|
||||
|
|
@ -179,13 +181,13 @@ func (student Student) GetActivesTunnels() (ts []TunnelToken, err error) {
|
|||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var t TunnelToken
|
||||
t := &TunnelToken{}
|
||||
if err = rows.Scan(&t.token, &t.TokenText, &t.IdStudent, &t.PubKey, &t.Time, &t.SuffixIP, &t.Version); err != nil {
|
||||
return
|
||||
}
|
||||
if t.PubKey != nil {
|
||||
if v, ok := wgd[base64.StdEncoding.EncodeToString(t.PubKey)]; ok {
|
||||
t.Dump = &v
|
||||
t.Dump = v
|
||||
ts = append(ts, t)
|
||||
}
|
||||
}
|
||||
|
|
@ -198,12 +200,13 @@ func (student Student) GetActivesTunnels() (ts []TunnelToken, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (student Student) GetTunnelToken(token []byte) (t TunnelToken, err error) {
|
||||
func (student *Student) GetTunnelToken(token []byte) (t *TunnelToken, err error) {
|
||||
t = new(TunnelToken)
|
||||
err = DBQueryRow("SELECT token, token_text, id_student, pubkey, time, suffixip, version FROM student_tunnel_tokens WHERE token = ? AND id_student = ? ORDER BY time DESC", token, student.Id).Scan(&t.token, &t.TokenText, &t.IdStudent, &t.PubKey, &t.Time, &t.SuffixIP, &t.Version)
|
||||
if err == nil && t.PubKey != nil {
|
||||
if wgd, errr := readWgDump(); errr == nil {
|
||||
if v, ok := wgd[base64.StdEncoding.EncodeToString(t.PubKey)]; ok {
|
||||
t.Dump = &v
|
||||
t.Dump = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -235,14 +238,14 @@ func (t *TunnelToken) Delete() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func GetStudentsTunnels() (ts []TunnelToken, err error) {
|
||||
func GetStudentsTunnels() (ts []*TunnelToken, err error) {
|
||||
if rows, errr := DBQuery("SELECT T.token, T.token_text, T.id_student, T.pubkey, T.time, T.suffixip, T.version FROM student_tunnel_tokens T INNER JOIN (SELECT B.id_student, MAX(B.time) AS time FROM student_tunnel_tokens B WHERE B.pubkey IS NOT NULL GROUP BY id_student) L ON T.id_student = L.id_student AND T.time = L.time"); errr != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var t TunnelToken
|
||||
t := &TunnelToken{}
|
||||
if err = rows.Scan(&t.token, &t.TokenText, &t.IdStudent, &t.PubKey, &t.Time, &t.SuffixIP, &t.Version); err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue