This repository has been archived on 2024-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
adlin/token-validator/ip.go

51 lines
1.0 KiB
Go

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 {
sid := fmt.Sprintf("%d", std.Id)
r[sid] = make(map[string]string)
if std.MAC == nil {
continue
}
r[sid]["mac"] = *std.MAC
r[sid]["vlan0"] = fmt.Sprintf("172.23.0.%d", std.IPSuffix())
r[sid]["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
}