|
|
|
@ -28,13 +28,13 @@ func GetCollectorPublic() ed25519.PublicKey {
|
|
|
|
|
return collector_secret.Public().(ed25519.PublicKey)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func StudentIP(idstd int64) net.IP {
|
|
|
|
|
return net.ParseIP(fmt.Sprintf("2a01:e0a:2b:2252:%x::", idstd))
|
|
|
|
|
func StudentIP(idstd int64, overrideid int) net.IP {
|
|
|
|
|
return net.ParseIP(fmt.Sprintf("2a01:e0a:2b:2252:%x%x::", overrideid, idstd))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func StudentNet(idstd int64) *net.IPNet {
|
|
|
|
|
func StudentNet(idstd int64, overrideid int) *net.IPNet {
|
|
|
|
|
return &net.IPNet{
|
|
|
|
|
IP: StudentIP(idstd),
|
|
|
|
|
IP: StudentIP(idstd, overrideid),
|
|
|
|
|
Mask: net.CIDRMask(StdNetmask, 128),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -113,12 +113,13 @@ func readWgDump() (wgd map[string]*WGDump, err error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type TunnelToken struct {
|
|
|
|
|
token []byte
|
|
|
|
|
TokenText string
|
|
|
|
|
IdStudent int64
|
|
|
|
|
PubKey []byte
|
|
|
|
|
Time time.Time
|
|
|
|
|
SuffixIP int
|
|
|
|
|
token []byte
|
|
|
|
|
TokenText string
|
|
|
|
|
IdStudent int64
|
|
|
|
|
PubKey []byte
|
|
|
|
|
Time time.Time
|
|
|
|
|
SuffixIP int
|
|
|
|
|
OverrideID int
|
|
|
|
|
// Version stores the TP number where the token is used
|
|
|
|
|
Version int
|
|
|
|
|
Dump *WGDump
|
|
|
|
@ -126,14 +127,14 @@ type TunnelToken struct {
|
|
|
|
|
|
|
|
|
|
func (tt *TunnelToken) GetStudentIP() string {
|
|
|
|
|
if tt.SuffixIP == 0 {
|
|
|
|
|
return fmt.Sprintf("%s%x", StudentIP(tt.IdStudent).String(), 1)
|
|
|
|
|
return fmt.Sprintf("%s%x", StudentIP(tt.IdStudent, tt.OverrideID).String(), 1)
|
|
|
|
|
} else {
|
|
|
|
|
return fmt.Sprintf("%s%x", StudentIP(tt.IdStudent).String(), tt.SuffixIP)
|
|
|
|
|
return fmt.Sprintf("%s%x", StudentIP(tt.IdStudent, tt.OverrideID).String(), tt.SuffixIP)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (tt *TunnelToken) GetServerIP(suffix int) string {
|
|
|
|
|
return fmt.Sprintf("%s%x", StudentIP(tt.IdStudent).String(), suffix)
|
|
|
|
|
return fmt.Sprintf("%s%x", StudentIP(tt.IdStudent, tt.OverrideID).String(), suffix)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (tt *TunnelToken) GenKeySign() []byte {
|
|
|
|
@ -151,7 +152,7 @@ func TokenFromText(token string) []byte {
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
err = DBQueryRow("SELECT token, token_text, id_student, pubkey, time, suffixip, idoverride, 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.OverrideID, &t.Version)
|
|
|
|
|
if err == nil && t.PubKey != nil {
|
|
|
|
|
if wgd, errr := readWgDump(); errr == nil {
|
|
|
|
|
if v, ok := wgd[base64.StdEncoding.EncodeToString(t.PubKey)]; ok {
|
|
|
|
@ -178,7 +179,7 @@ func (student *Student) NewTunnelToken(suffixip int) (t *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 {
|
|
|
|
|
if rows, errr := DBQuery("SELECT token, token_text, id_student, pubkey, time, suffixip, idoverride, 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 {
|
|
|
|
|
return nil, errr
|
|
|
|
@ -187,7 +188,7 @@ func (student *Student) GetTunnelTokens() (ts []*TunnelToken, err error) {
|
|
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
t := &TunnelToken{}
|
|
|
|
|
if err = rows.Scan(&t.token, &t.TokenText, &t.IdStudent, &t.PubKey, &t.Time, &t.SuffixIP, &t.Version); err != nil {
|
|
|
|
|
if err = rows.Scan(&t.token, &t.TokenText, &t.IdStudent, &t.PubKey, &t.Time, &t.SuffixIP, &t.OverrideID, &t.Version); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if t.PubKey != nil {
|
|
|
|
@ -206,7 +207,7 @@ func (student *Student) GetTunnelTokens() (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 {
|
|
|
|
|
if rows, errr := DBQuery("SELECT token, token_text, id_student, pubkey, time, suffixip, idoverride, 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 {
|
|
|
|
|
return nil, errr
|
|
|
|
@ -215,7 +216,7 @@ func (student *Student) GetActivesTunnels() (ts []*TunnelToken, err error) {
|
|
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
t := &TunnelToken{}
|
|
|
|
|
if err = rows.Scan(&t.token, &t.TokenText, &t.IdStudent, &t.PubKey, &t.Time, &t.SuffixIP, &t.Version); err != nil {
|
|
|
|
|
if err = rows.Scan(&t.token, &t.TokenText, &t.IdStudent, &t.PubKey, &t.Time, &t.SuffixIP, &t.OverrideID, &t.Version); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if t.PubKey != nil {
|
|
|
|
@ -259,7 +260,7 @@ func (student *Student) GetActivesTunnelsPubKey() (ts []ed25519.PublicKey, err e
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
err = DBQueryRow("SELECT token, token_text, id_student, pubkey, time, suffixip, idoverride, 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.OverrideID, &t.Version)
|
|
|
|
|
if err == nil && t.PubKey != nil {
|
|
|
|
|
if wgd, errr := readWgDump(); errr == nil {
|
|
|
|
|
if v, ok := wgd[base64.StdEncoding.EncodeToString(t.PubKey)]; ok {
|
|
|
|
@ -296,14 +297,14 @@ func (t *TunnelToken) Delete() (int64, 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 {
|
|
|
|
|
if rows, errr := DBQuery("SELECT T.token, T.token_text, T.id_student, T.pubkey, T.time, T.suffixip, T.idoverride, T.version FROM student_tunnel_tokens T INNER JOIN (SELECT B.id_student, MAX(B.time) AS time, B.idoverride FROM student_tunnel_tokens B WHERE B.pubkey IS NOT NULL GROUP BY id_student, idoverride) 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() {
|
|
|
|
|
t := &TunnelToken{}
|
|
|
|
|
if err = rows.Scan(&t.token, &t.TokenText, &t.IdStudent, &t.PubKey, &t.Time, &t.SuffixIP, &t.Version); err != nil {
|
|
|
|
|
if err = rows.Scan(&t.token, &t.TokenText, &t.IdStudent, &t.PubKey, &t.Time, &t.SuffixIP, &t.OverrideID, &t.Version); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ts = append(ts, t)
|
|
|
|
|