done 2020
This commit is contained in:
parent
7682cae26c
commit
1d2199aaef
6 changed files with 90 additions and 64 deletions
|
|
@ -9,19 +9,24 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
const (
|
||||
AssociatedDomainSuffix = "adlin2020.p0m.fr"
|
||||
DelegatedDomainSuffix = "srs.p0m.fr"
|
||||
AssociatedDomainSuffix = "adlin2020.p0m.fr."
|
||||
DelegatedDomainSuffix = "srs.p0m.fr."
|
||||
)
|
||||
|
||||
var tsigSecret = map[string]string{"ddns.": ""}
|
||||
|
||||
func init() {
|
||||
router.GET("/api/adomains/", apiAuthHandler(func (student Student, ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
return student.GetAssociatedDomains(), nil
|
||||
|
|
@ -47,21 +52,21 @@ func init() {
|
|||
if err := json.Unmarshal(body, &ue); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return true, student.AddNSDelegatedDomain(ps.ByName("dn"), ue.TTL, ue.Values[0])
|
||||
return true, student.AddNSDelegatedDomain(ps.ByName("dn"), ue.TTL, ue.Values)
|
||||
}))
|
||||
router.PATCH("/api/ddomains/:dn/NS", apiAuthHandler(func (student Student, ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
var ue Entry
|
||||
if err := json.Unmarshal(body, &ue); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return true, student.UpdateNSDelegatedDomain(ps.ByName("dn"), ue.TTL, ue.ValuesFrom[0], ue.Values[0])
|
||||
return true, student.UpdateNSDelegatedDomain(ps.ByName("dn"), ue.TTL, ue.ValuesFrom, ue.Values)
|
||||
}))
|
||||
router.DELETE("/api/ddomains/:dn/NS", apiAuthHandler(func (student Student, ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
var ue Entry
|
||||
if err := json.Unmarshal(body, &ue); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return true, student.DeleteRRDelegatedDomain(ps.ByName("dn"), "NS", ue.Values...)
|
||||
return true, student.DeleteRRDelegatedDomain(ps.ByName("dn"), "NS", ue.Values)
|
||||
}))
|
||||
router.GET("/api/ddomains/:dn/GLUE", apiAuthHandler(func (student Student, ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
return student.getRRDelegatedDomain(ps.ByName("dn"), "AAAA")
|
||||
|
|
@ -71,28 +76,28 @@ func init() {
|
|||
if err := json.Unmarshal(body, &ue); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return true, student.AddGLUEDelegatedDomain(ps.ByName("dn"), ue.TTL, ue.Values[0])
|
||||
return true, student.AddGLUEDelegatedDomain(ps.ByName("dn"), ue.TTL, ue.Values)
|
||||
}))
|
||||
router.PATCH("/api/ddomains/:dn/AAAA", apiAuthHandler(func (student Student, ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
var ue Entry
|
||||
if err := json.Unmarshal(body, &ue); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return true, student.UpdateGLUEDelegatedDomain(ps.ByName("dn"), ue.TTL, ue.ValuesFrom[0], ue.Values[0])
|
||||
return true, student.UpdateGLUEDelegatedDomain(ps.ByName("dn"), ue.TTL, ue.ValuesFrom, ue.Values)
|
||||
}))
|
||||
router.POST("/api/ddomains/:dn/GLUE", apiAuthHandler(func (student Student, ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
var ue Entry
|
||||
if err := json.Unmarshal(body, &ue); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return true, student.UpdateGLUEDelegatedDomain(ps.ByName("dn"), ue.TTL, ue.ValuesFrom[0], ue.Values[0])
|
||||
return true, student.UpdateGLUEDelegatedDomain(ps.ByName("dn"), ue.TTL, ue.ValuesFrom, ue.Values)
|
||||
}))
|
||||
router.DELETE("/api/ddomains/:dn/AAAA", apiAuthHandler(func (student Student, ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
var ue Entry
|
||||
if err := json.Unmarshal(body, &ue); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return true, student.DeleteRRDelegatedDomain(ps.ByName("dn"), "AAAA", ue.Values...)
|
||||
return true, student.DeleteRRDelegatedDomain(ps.ByName("dn"), "AAAA", ue.Values)
|
||||
}))
|
||||
router.GET("/api/ddomains/:dn/DS", apiAuthHandler(func (student Student, ps httprouter.Params, body []byte) (interface{}, error) {
|
||||
return student.getRRDelegatedDomain(ps.ByName("dn"), "DS")
|
||||
|
|
@ -116,16 +121,16 @@ func init() {
|
|||
if err := json.Unmarshal(body, &ue); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return true, student.DeleteRRDelegatedDomain(ps.ByName("dn"), "DS", ue.Values...)
|
||||
return true, student.DeleteRRDelegatedDomain(ps.ByName("dn"), "DS", ue.Values)
|
||||
}))
|
||||
}
|
||||
|
||||
type Entry struct {
|
||||
Domain string `json:"domain"`
|
||||
TTL uint64 `json:"ttl"`
|
||||
TTL uint32 `json:"ttl"`
|
||||
RR string `json:"rr"`
|
||||
ValuesFrom []string `json:"valuesfrom,omitempty"`
|
||||
Values []string `json:"values"`
|
||||
ValuesFrom string `json:"valuesfrom,omitempty"`
|
||||
Values string `json:"values"`
|
||||
}
|
||||
|
||||
func runKnotc(args ...string) (out []byte, err error) {
|
||||
|
|
@ -137,21 +142,27 @@ func runKnotc(args ...string) (out []byte, err error) {
|
|||
return cmd.CombinedOutput()
|
||||
}
|
||||
|
||||
func parseKnotZoneRead(args ...string) (rr []Entry, err error) {
|
||||
var out []byte
|
||||
args = append([]string{"zone-read"}, args...)
|
||||
out, err = runKnotc(args...)
|
||||
func parseKnotZoneRead(globalDomain string, domain string) (rr []Entry, err error) {
|
||||
t := new(dns.Transfer)
|
||||
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
cols := strings.Fields(line)
|
||||
if len(cols) >= 5 {
|
||||
var ttl uint64
|
||||
ttl, err = strconv.ParseUint(cols[2], 10, 64)
|
||||
if err != nil {
|
||||
return
|
||||
m := new(dns.Msg)
|
||||
t.TsigSecret = tsigSecret
|
||||
m.SetAxfr(globalDomain)
|
||||
m.SetTsig("ddns.", dns.HmacSHA256, 300, time.Now().Unix())
|
||||
|
||||
c, err := t.In(m, "127.0.0.1:53")
|
||||
if err != nil {
|
||||
log.Println(globalDomain, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for l := range c {
|
||||
for _, r := range l.RR {
|
||||
var z dns.RFC3597
|
||||
z.ToRFC3597(r)
|
||||
if strings.HasSuffix(r.Header().Name, domain) {
|
||||
rr = append(rr, Entry{r.Header().Name, r.Header().Ttl, dns.TypeToString[r.Header().Rrtype], "", z.Rdata})
|
||||
}
|
||||
|
||||
rr = append(rr, Entry{cols[1], ttl, cols[3], nil, cols[4:]})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -159,15 +170,13 @@ func parseKnotZoneRead(args ...string) (rr []Entry, err error) {
|
|||
}
|
||||
|
||||
func (student Student) myAssociatedDomain() (string) {
|
||||
return fmt.Sprintf("%s.%s.", strings.Trim(strings.Replace(student.Login, "_", "-", -1), "-_"), AssociatedDomainSuffix)
|
||||
return fmt.Sprintf("%s.%s", strings.Trim(strings.Replace(student.Login, "_", "-", -1), "-_"), AssociatedDomainSuffix)
|
||||
}
|
||||
|
||||
func (student Student) GetAssociatedDomains() (ds []string) {
|
||||
studentDomain := student.myAssociatedDomain()
|
||||
|
||||
if _, err := parseKnotZoneRead(AssociatedDomainSuffix, studentDomain); err == nil {
|
||||
ds = append(ds, studentDomain)
|
||||
}
|
||||
ds = append(ds, studentDomain)
|
||||
|
||||
return
|
||||
}
|
||||
|
|
@ -199,30 +208,43 @@ func (student Student) GetAssociatedDomain(dn string) (rrs []Entry, err error) {
|
|||
}
|
||||
|
||||
func (student Student) AddAssociatedDomains() (err error) {
|
||||
for _, d := range []string{student.myAssociatedDomain()} {
|
||||
for _, cmd := range [][]string{
|
||||
[]string{"zone-begin", AssociatedDomainSuffix},
|
||||
[]string{"zone-set", AssociatedDomainSuffix, d, "900", "A", "82.64.31.248"},
|
||||
[]string{"zone-set", AssociatedDomainSuffix, d, "900", "AAAA", studentIP(student.Id) + "1"},
|
||||
[]string{"zone-commit", AssociatedDomainSuffix},
|
||||
} {
|
||||
var out []byte
|
||||
out, err = runKnotc(cmd...)
|
||||
if err != nil {
|
||||
log.Printf("An error occurs on command '%s': %s", strings.Join(cmd, " "), err.Error())
|
||||
err = errors.New(string(out))
|
||||
runKnotc("zone-abort", AssociatedDomainSuffix)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
m1 := new(dns.Msg)
|
||||
m1.Id = dns.Id()
|
||||
m1.Opcode = dns.OpcodeUpdate
|
||||
m1.Question = make([]dns.Question, 1)
|
||||
m1.Question[0] = dns.Question{AssociatedDomainSuffix, dns.TypeSOA, dns.ClassINET}
|
||||
|
||||
rrAd := new(dns.A)
|
||||
rrAd.Hdr = dns.RR_Header{Name: student.myAssociatedDomain(), Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 0}
|
||||
m1.Remove([]dns.RR{rrAd})
|
||||
|
||||
rrA := new(dns.A)
|
||||
rrA.Hdr = dns.RR_Header{Name: student.myAssociatedDomain(), Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 3600}
|
||||
rrA.A = net.IPv4(82, 64, 31, 248)
|
||||
m1.Insert([]dns.RR{rrA})
|
||||
|
||||
rrAAAAd := new(dns.AAAA)
|
||||
rrAAAAd.Hdr = dns.RR_Header{Name: student.myAssociatedDomain(), Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 0}
|
||||
m1.Remove([]dns.RR{rrAAAAd})
|
||||
|
||||
rrAAAA := new(dns.AAAA)
|
||||
rrAAAA.Hdr = dns.RR_Header{Name: student.myAssociatedDomain(), Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: 3600}
|
||||
rrAAAA.AAAA = studentIP(student.Id)
|
||||
rrAAAA.AAAA[15] = 1
|
||||
m1.Insert([]dns.RR{rrAAAA})
|
||||
|
||||
c := new(dns.Client)
|
||||
c.TsigSecret = tsigSecret
|
||||
m1.SetTsig("ddns.", dns.HmacSHA256, 300, time.Now().Unix())
|
||||
|
||||
_, _, err = c.Exchange(m1, "127.0.0.1:53")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
func (student Student) MyDelegatedDomain() (string) {
|
||||
return fmt.Sprintf("%s.%s.", strings.Trim(strings.Replace(student.Login, "_", "-", -1), "-_"), DelegatedDomainSuffix)
|
||||
return fmt.Sprintf("%s.%s", strings.Trim(strings.Replace(student.Login, "_", "-", -1), "-_"), DelegatedDomainSuffix)
|
||||
}
|
||||
|
||||
func (student Student) getRRDelegatedDomain(dn string, rr string) (rrs []Entry, err error) {
|
||||
|
|
@ -238,7 +260,7 @@ func (student Student) getRRDelegatedDomain(dn string, rr string) (rrs []Entry,
|
|||
err = errors.New(fmt.Sprintf("Unable to find domain %q.", dn))
|
||||
}
|
||||
|
||||
if entries, errr := parseKnotZoneRead(DelegatedDomainSuffix); err != nil {
|
||||
if entries, errr := parseKnotZoneRead(DelegatedDomainSuffix, dn); err != nil {
|
||||
return nil, errr
|
||||
} else {
|
||||
for _, e := range entries {
|
||||
|
|
@ -251,7 +273,7 @@ func (student Student) getRRDelegatedDomain(dn string, rr string) (rrs []Entry,
|
|||
return
|
||||
}
|
||||
|
||||
func (student Student) AddNSDelegatedDomain(dn string, ttl uint64, ns string) (err error) {
|
||||
func (student Student) AddNSDelegatedDomain(dn string, ttl uint32, ns string) (err error) {
|
||||
for _, d := range []string{student.MyDelegatedDomain()} {
|
||||
for _, cmd := range [][]string{
|
||||
[]string{"zone-begin", DelegatedDomainSuffix},
|
||||
|
|
@ -272,7 +294,7 @@ func (student Student) AddNSDelegatedDomain(dn string, ttl uint64, ns string) (e
|
|||
return
|
||||
}
|
||||
|
||||
func (student Student) UpdateNSDelegatedDomain(dn string, ttl uint64, oldns string, ns string) (err error) {
|
||||
func (student Student) UpdateNSDelegatedDomain(dn string, ttl uint32, oldns string, ns string) (err error) {
|
||||
for _, d := range []string{student.MyDelegatedDomain()} {
|
||||
for _, cmd := range [][]string{
|
||||
[]string{"zone-begin", DelegatedDomainSuffix},
|
||||
|
|
@ -295,7 +317,7 @@ func (student Student) UpdateNSDelegatedDomain(dn string, ttl uint64, oldns stri
|
|||
}
|
||||
|
||||
|
||||
func (student Student) AddGLUEDelegatedDomain(dn string, ttl uint64, aaaa string) (err error) {
|
||||
func (student Student) AddGLUEDelegatedDomain(dn string, ttl uint32, aaaa string) (err error) {
|
||||
domains := []string{student.MyDelegatedDomain()}
|
||||
found := false
|
||||
for _, d := range domains {
|
||||
|
|
@ -327,7 +349,7 @@ func (student Student) AddGLUEDelegatedDomain(dn string, ttl uint64, aaaa string
|
|||
return
|
||||
}
|
||||
|
||||
func (student Student) UpdateGLUEDelegatedDomain(dn string, ttl uint64, oldaaaa string, aaaa string) (err error) {
|
||||
func (student Student) UpdateGLUEDelegatedDomain(dn string, ttl uint32, oldaaaa string, aaaa string) (err error) {
|
||||
domains := []string{student.MyDelegatedDomain()}
|
||||
found := false
|
||||
for _, d := range domains {
|
||||
|
|
@ -362,7 +384,9 @@ func (student Student) UpdateGLUEDelegatedDomain(dn string, ttl uint64, oldaaaa
|
|||
}
|
||||
|
||||
|
||||
func (student Student) AddDSDelegatedDomain(dn string, ttl uint64, dnskey []string) (err error) {
|
||||
func (student Student) AddDSDelegatedDomain(dn string, ttl uint32, rdata string) (err error) {
|
||||
dnskey := strings.Split(rdata, " ")
|
||||
|
||||
if len(dnskey) != 4 {
|
||||
return errors.New("Wrong number of value for this record")
|
||||
}
|
||||
|
|
@ -411,12 +435,12 @@ func (student Student) AddDSDelegatedDomain(dn string, ttl uint64, dnskey []stri
|
|||
return
|
||||
}
|
||||
|
||||
func (student Student) UpdateDSDelegatedDomain(dn string, ttl uint64, oldds []string, ds []string) (err error) {
|
||||
func (student Student) UpdateDSDelegatedDomain(dn string, ttl uint32, oldds string, ds string) (err error) {
|
||||
for _, d := range []string{student.MyDelegatedDomain()} {
|
||||
for _, cmd := range [][]string{
|
||||
[]string{"zone-begin", DelegatedDomainSuffix},
|
||||
[]string{"zone-unset", DelegatedDomainSuffix, d, "DS", strings.Join(oldds, " ")},
|
||||
[]string{"zone-set", DelegatedDomainSuffix, d, fmt.Sprintf("%d", ttl), "DS", strings.Join(ds, " ")},
|
||||
[]string{"zone-unset", DelegatedDomainSuffix, d, "DS", oldds},
|
||||
[]string{"zone-set", DelegatedDomainSuffix, d, fmt.Sprintf("%d", ttl), "DS", ds},
|
||||
[]string{"zone-commit", DelegatedDomainSuffix},
|
||||
} {
|
||||
var out []byte
|
||||
|
|
|
|||
|
|
@ -304,6 +304,7 @@ angular.module("AdLinApp")
|
|||
}
|
||||
|
||||
$scope.saveNSRR = function(nsrr) {
|
||||
nsrr.values = nsrr.values.join(" ");
|
||||
$http({
|
||||
method: (nsrr.valuesfrom !== undefined)?'PATCH':'POST',
|
||||
url: "/api/ddomains/" + nsrr.domain + "/" + nsrr.rr,
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ func getStudentIPs(student Student) (r map[string]string) {
|
|||
|
||||
r["vlan0"] = fmt.Sprintf("172.23.0.%d", student.IPSuffix())
|
||||
r["vlan7"] = fmt.Sprintf("172.23.142.%d", student.IPSuffix())
|
||||
r["wg"] = studentIP(student.Id)
|
||||
r["wg"] = studentIP(student.Id).String()
|
||||
r["adn"] = student.myAssociatedDomain()
|
||||
r["ddn"] = student.MyDelegatedDomain()
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
|
@ -49,7 +50,7 @@ type TunnelInfo struct {
|
|||
Status string `json:"status"`
|
||||
SrvPubKey []byte `json:"srv_pubkey"`
|
||||
SrvPort uint16 `json:"srv_port"`
|
||||
CltIPv6 string `json:"clt_ipv6"`
|
||||
CltIPv6 net.IP `json:"clt_ipv6"`
|
||||
CltRange uint8 `json:"clt_range"`
|
||||
SrvGW6 string `json:"srv_gw6"`
|
||||
}
|
||||
|
|
@ -241,8 +242,8 @@ func GetStudentsTunnels() (ts []TunnelToken, err error) {
|
|||
}
|
||||
}
|
||||
|
||||
func studentIP(idstd int64) string {
|
||||
return fmt.Sprintf("2a01:e0a:2b:2252:%x::", idstd)
|
||||
func studentIP(idstd int64) net.IP {
|
||||
return net.ParseIP(fmt.Sprintf("2a01:e0a:2b:2252:%x::", idstd))
|
||||
}
|
||||
|
||||
func GenWGConfig(w io.Writer) (error) {
|
||||
|
|
|
|||
Reference in a new issue