token-validator: update to handle custom domains
This commit is contained in:
parent
efab34d551
commit
5a4650f70e
4 changed files with 307 additions and 15 deletions
|
|
@ -30,18 +30,38 @@ func init() {
|
|||
Domain string `json:"domain"`
|
||||
A string `json:"a"`
|
||||
AAAA string `json:"aaaa"`
|
||||
CNAME string `json:"cname,omitempty"`
|
||||
}{}
|
||||
|
||||
if err := json.Unmarshal(body, &ue); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var aaaa net.IP
|
||||
if ue != nil && len(ue.AAAA) > 0 {
|
||||
aaaa = net.ParseIP(ue.AAAA)
|
||||
}
|
||||
if ue.Domain != "" && ue.A == "" && ue.AAAA == "" && ue.CNAME == "" {
|
||||
student.AssociatedDomain = nil
|
||||
|
||||
return true, AddAssociatedDomains(student, aaaa)
|
||||
if _, err := student.Update(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
} else if ue.CNAME != "" {
|
||||
cname := dns.Fqdn(ue.CNAME)
|
||||
student.AssociatedDomain = &cname
|
||||
|
||||
if _, err := student.Update(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
} else {
|
||||
var aaaa net.IP
|
||||
if ue != nil && len(ue.AAAA) > 0 {
|
||||
aaaa = net.ParseIP(ue.AAAA)
|
||||
}
|
||||
|
||||
return true, AddAssociatedDomains(student, aaaa)
|
||||
}
|
||||
}))
|
||||
router.GET("/api/adomains/:dn", apiAuthHandler(func(student adlin.Student, ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
return GetAssociatedDomain(student, ps.ByName("dn"))
|
||||
|
|
@ -50,6 +70,34 @@ func init() {
|
|||
router.GET("/api/ddomains/", apiAuthHandler(func(student adlin.Student, ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
return []string{student.MyDelegatedDomain()}, nil
|
||||
}))
|
||||
router.POST("/api/ddomains/", apiAuthHandler(func(student adlin.Student, ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
ue := &struct {
|
||||
NS string `json:"ns"`
|
||||
}{}
|
||||
|
||||
if err := json.Unmarshal(body, &ue); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if ue.NS == "" {
|
||||
student.DelegatedDomain = nil
|
||||
|
||||
if _, err := student.Update(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
} else {
|
||||
ns := dns.Fqdn(ue.NS)
|
||||
student.DelegatedDomain = &ns
|
||||
|
||||
if _, err := student.Update(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
}))
|
||||
router.GET("/api/ddomains/:dn/", apiAuthHandler(func(student adlin.Student, ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
return getRRDelegatedDomain(student, ps.ByName("dn"), "")
|
||||
}))
|
||||
|
|
@ -239,7 +287,7 @@ func delAssociatedDomains(student adlin.Student, dn string) (err error) {
|
|||
}
|
||||
|
||||
func AddAssociatedDomains(student adlin.Student, aaaa net.IP) (err error) {
|
||||
err = delAssociatedDomains(student, student.MyAssociatedDomain())
|
||||
err = delAssociatedDomains(student, student.DefaultAssociatedDomain())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -257,12 +305,12 @@ func AddAssociatedDomains(student adlin.Student, aaaa net.IP) (err error) {
|
|||
m2.Question[0] = dns.Question{adlin.AssociatedDomainSuffix, dns.TypeSOA, dns.ClassINET}
|
||||
|
||||
rrA := new(dns.A)
|
||||
rrA.Hdr = dns.RR_Header{Name: student.MyAssociatedDomain(), Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 3600}
|
||||
rrA.Hdr = dns.RR_Header{Name: student.DefaultAssociatedDomain(), Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 3600}
|
||||
rrA.A = net.IPv4(82, 64, 31, 248)
|
||||
m2.Insert([]dns.RR{rrA})
|
||||
|
||||
rrAAAA := new(dns.AAAA)
|
||||
rrAAAA.Hdr = dns.RR_Header{Name: student.MyAssociatedDomain(), Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 3600}
|
||||
rrAAAA.Hdr = dns.RR_Header{Name: student.DefaultAssociatedDomain(), Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 3600}
|
||||
rrAAAA.AAAA = aaaa
|
||||
m2.Insert([]dns.RR{rrAAAA})
|
||||
|
||||
|
|
|
|||
Reference in a new issue