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

57 lines
1.2 KiB
Go

package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"git.nemunai.re/lectures/adlin/libadlin"
)
func init() {
router.GET("/api/ips", apiHandler(showIPs))
router.GET("/api/students/:sid/ips", apiHandler(studentHandler(func(student adlin.Student, body []byte) (interface{}, error) {
return getStudentIPs(student), nil
})))
}
func IPSuffix(s adlin.Student) int64 {
return s.Id*4 + 10
}
func showIPs(_ httprouter.Params, body []byte) (interface{}, error) {
r := make(map[string]map[string]string)
students, err := adlin.GetStudents()
if err != nil {
return nil, err
}
for _, std := range students {
sid := fmt.Sprintf("%d", std.Id)
r[sid] = getStudentIPs(std)
if std.MAC == nil {
continue
}
r[sid]["mac"] = *std.MAC
}
return r, nil
}
func getStudentIPs(student adlin.Student) (r map[string]string) {
r = make(map[string]string)
r["vlan0"] = fmt.Sprintf("172.23.0.%d", IPSuffix(student))
r["wg0"] = fmt.Sprintf("172.17.0.%d", IPSuffix(student))
r["vlan7"] = fmt.Sprintf("172.23.142.%d", IPSuffix(student))
r["wg"] = adlin.StudentIP(student.Id).String()
r["adn"] = student.MyAssociatedDomain()
r["ddn"] = student.MyDelegatedDomain()
return
}