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

90 lines
2.0 KiB
Go
Raw Permalink Normal View History

package main
import (
"fmt"
"net"
2021-03-04 00:05:24 +00:00
"strconv"
"github.com/julienschmidt/httprouter"
2020-03-27 13:57:14 +00:00
2021-10-31 15:43:43 +00:00
"git.nemunai.re/srs/adlin/libadlin"
)
func init() {
router.GET("/api/ips", apiHandler(showIPs))
2021-03-07 11:39:38 +00:00
router.GET("/api/students/:sid/ips", apiHandler(studentHandler(func(student *adlin.Student, body []byte) (interface{}, error) {
return getStudentIPs(student), nil
})))
}
2021-03-07 11:39:38 +00:00
func IPSuffix(s *adlin.Student, network net.IPNet) net.IP {
ipshift := s.Id*4 + 7
myIP := make(net.IP, len(network.IP))
copy(myIP, network.IP)
if ipshift > 254 {
myIP[len(myIP)-2] += byte(ipshift / 254)
ipshift = ipshift % 254
}
myIP[len(myIP)-1] += byte(ipshift)
return myIP
}
func showIPs(_ httprouter.Params, body []byte) (interface{}, error) {
r := make(map[string]map[string]string)
2020-03-27 13:57:14 +00:00
students, err := adlin.GetStudents()
if err != nil {
return nil, err
}
for _, std := range students {
2019-02-27 04:49:08 +00:00
sid := fmt.Sprintf("%d", std.Id)
r[sid] = getStudentIPs(std)
2019-02-27 04:49:08 +00:00
if std.MAC == nil {
continue
}
r[sid]["mac"] = *std.MAC
}
return r, nil
}
2021-03-07 11:39:38 +00:00
func GetStudentTunnelIPs(student *adlin.Student) (ips []string) {
2021-03-04 00:05:24 +00:00
if ts, err := student.GetActivesTunnels(); err != nil || len(ts) == 0 || ts[0].SuffixIP == 0 {
ips = append(ips, adlin.StudentIP(student.Id, 0).String()+"1")
2021-03-04 00:05:24 +00:00
} else {
for _, t := range ts {
2021-03-04 00:32:09 +00:00
ips = append(ips, t.GetStudentIP())
2021-03-04 00:05:24 +00:00
}
}
return
}
2021-03-07 11:39:38 +00:00
func getStudentIPs(student *adlin.Student) (r map[string]string) {
r = make(map[string]string)
r["vlan0"] = IPSuffix(student, net.IPNet{IP: net.ParseIP("172.23.12.0"), Mask: net.CIDRMask(17, 32)}).String()
r["wg0"] = IPSuffix(student, net.IPNet{IP: net.ParseIP("172.17.12.0"), Mask: net.CIDRMask(16, 32)}).String()
2021-03-08 13:49:44 +00:00
r["vlan7"] = IPSuffix(student, net.IPNet{IP: net.ParseIP("172.23.142.0"), Mask: net.CIDRMask(23, 32)}).String()
2021-03-04 00:05:24 +00:00
for d, ip := range GetStudentTunnelIPs(student) {
key := "wg"
if d > 0 {
key += strconv.Itoa(d)
}
r[key] = ip
}
2020-03-27 13:57:14 +00:00
r["adn"] = student.MyAssociatedDomain()
r["ddn"] = student.MyDelegatedDomain()
return
}