token-validator: Implement token collector
This commit is contained in:
parent
ae3b2e6f3b
commit
3e3a0e9e37
3 changed files with 60 additions and 3 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package adlin
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"crypto/rand"
|
||||
"crypto/sha512"
|
||||
"encoding/base64"
|
||||
|
|
@ -14,6 +15,18 @@ import (
|
|||
|
||||
const StdNetmask = 80
|
||||
|
||||
var (
|
||||
collector_secret ed25519.PrivateKey
|
||||
)
|
||||
|
||||
func SetCollectorSecret(b []byte) {
|
||||
collector_secret = ed25519.NewKeyFromSeed(b)
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
|
@ -36,6 +49,10 @@ type WGDump struct {
|
|||
KeepAlive string
|
||||
}
|
||||
|
||||
func (d *WGDump) GetPubKey() ([]byte, error) {
|
||||
return base64.StdEncoding.DecodeString(d.PubKey)
|
||||
}
|
||||
|
||||
var (
|
||||
wgDumpCache_data map[string]*WGDump = nil
|
||||
wgDumpCache_time time.Time
|
||||
|
|
@ -111,6 +128,14 @@ func (tt *TunnelToken) GetStudentIP() string {
|
|||
}
|
||||
}
|
||||
|
||||
func (tt *TunnelToken) GenKeySign() []byte {
|
||||
stdprivkey := ed25519.NewKeyFromSeed(tt.token[:ed25519.SeedSize])
|
||||
|
||||
stdpublic := []byte(stdprivkey.Public().(ed25519.PublicKey))
|
||||
|
||||
return ed25519.Sign(collector_secret, stdpublic)
|
||||
}
|
||||
|
||||
func TokenFromText(token string) []byte {
|
||||
sha := sha512.Sum512([]byte(token))
|
||||
return sha[:]
|
||||
|
|
@ -200,6 +225,23 @@ func (student *Student) GetActivesTunnels() (ts []*TunnelToken, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (student *Student) GetActivesTunnelsPubKey() (ts []ed25519.PublicKey, err error) {
|
||||
var activeTuns []*TunnelToken
|
||||
activeTuns, err = student.GetActivesTunnels()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, tun := range activeTuns {
|
||||
if tun.Dump != nil {
|
||||
pk := ed25519.NewKeyFromSeed(tun.token[:ed25519.SeedSize])
|
||||
ts = append(ts, pk.Public().(ed25519.PublicKey))
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
|
|
|
|||
Reference in a new issue