-
Notifications
You must be signed in to change notification settings - Fork 0
/
nameserver.go
58 lines (49 loc) · 1.18 KB
/
nameserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"fmt"
"math/rand"
"net/http"
"time"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
type DNSRecord struct {
Subdomain string
IP string
CreatedAt time.Time
}
func insertDNSRecord(subdomain, ip string) error {
dnsRecord := DNSRecord{
Subdomain: subdomain,
IP: ip,
CreatedAt: time.Now(),
}
result := db.Create(&dnsRecord)
return result.Error
}
func generateRandomSubdomain() string {
const letters = "abcdefghijklmnopqrstuvwxyz"
b := make([]byte, 10)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func createNsEntry(c *gin.Context) {
ip := c.Query("ip")
if ip == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "IP address is required"})
return
}
subdomain := generateRandomSubdomain()
err := setAsNameserver(subdomain, ip)
if err != nil {
log.Errorf("Failed to set %s as nameserver for %s: %v", ip, subdomain, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to set as nameserver"})
return
}
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("Set %s as nameserver for %s", ip, subdomain)})
}
func setAsNameserver(subdomain, ip string) error {
return nil
}