token-validator: add routes to display predefined IPs on challenge LAN
This commit is contained in:
parent
13c5a3cabd
commit
2d2617f6ec
1 changed files with 43 additions and 0 deletions
43
token-validator/ip.go
Normal file
43
token-validator/ip.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func init() {
|
||||
router.GET("/api/ips", apiHandler(showIPs))
|
||||
router.GET("/api/students/:sid/ips", apiHandler(studentHandler(showStudentIPs)))
|
||||
}
|
||||
|
||||
func (s Student) IPSuffix() int64 {
|
||||
return s.Id * 5 + 10
|
||||
}
|
||||
|
||||
func showIPs(_ httprouter.Params, body []byte) (interface{}, error) {
|
||||
r := make(map[string]map[string]string)
|
||||
|
||||
students, err := getStudents()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, std := range students {
|
||||
r[std.Login] = make(map[string]string)
|
||||
|
||||
r[std.Login]["vlan0"] = fmt.Sprintf("172.23.0.%d", std.IPSuffix())
|
||||
r[std.Login]["vlan7"] = fmt.Sprintf("172.23.142.%d", std.IPSuffix())
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func showStudentIPs(student Student, body []byte) (interface{}, error) {
|
||||
r := make(map[string]string)
|
||||
|
||||
r["vlan0"] = fmt.Sprintf("172.23.0.%d", student.IPSuffix())
|
||||
r["vlan7"] = fmt.Sprintf("172.23.142.%d", student.IPSuffix())
|
||||
|
||||
return r, nil
|
||||
}
|
Reference in a new issue