first version
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/textproto"
|
||||
|
||||
"git.trj.tw/golang/go-ddns-svc/module/config"
|
||||
"git.trj.tw/golang/go-ddns-svc/module/context"
|
||||
)
|
||||
|
||||
// VerifyPrivate -
|
||||
func VerifyPrivate(c *context.Context) {
|
||||
privateHeader := c.Request.Header.Get(textproto.CanonicalMIMEHeaderKey("x-mtfos-key"))
|
||||
if len(privateHeader) == 0 {
|
||||
c.Forbidden(nil)
|
||||
return
|
||||
}
|
||||
conf := config.GetConfig()
|
||||
if privateHeader != conf.VerifyValue {
|
||||
c.Forbidden(nil)
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"strings"
|
||||
|
||||
"git.trj.tw/golang/go-ddns-svc/module/apis/aws"
|
||||
"git.trj.tw/golang/go-ddns-svc/module/context"
|
||||
"git.trj.tw/golang/go-ddns-svc/module/tools"
|
||||
awssdk "github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/route53"
|
||||
)
|
||||
|
||||
// RecordData -
|
||||
type RecordData struct {
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// DomainData -
|
||||
type DomainData struct {
|
||||
Name string `json:"name"`
|
||||
Records []RecordData `json:"records"`
|
||||
}
|
||||
|
||||
// UpdateDDNSBody -
|
||||
type UpdateDDNSBody struct {
|
||||
Domain []DomainData `json:"domain"`
|
||||
}
|
||||
|
||||
// UpdateDDNS -
|
||||
func UpdateDDNS(c *context.Context) {
|
||||
body := UpdateDDNSBody{}
|
||||
|
||||
err := c.BindData(&body)
|
||||
if err != nil {
|
||||
log.Println("bind data error :: ", err)
|
||||
c.DataFormat(nil)
|
||||
return
|
||||
}
|
||||
|
||||
ip := tools.RemoteIP(c.Request)
|
||||
log.Println("Client IP :: ", ip)
|
||||
|
||||
if len(body.Domain) == 0 {
|
||||
c.DataFormat(nil)
|
||||
return
|
||||
}
|
||||
|
||||
domains := make([]string, 0, len(body.Domain))
|
||||
for _, v := range body.Domain {
|
||||
domains = append(domains, v.Name)
|
||||
}
|
||||
|
||||
zones, err := aws.QueryDomain(domains)
|
||||
if err != nil {
|
||||
log.Println("get zone error :: ", err)
|
||||
c.ServerError(nil)
|
||||
return
|
||||
}
|
||||
|
||||
for _, it := range zones {
|
||||
for _, domain := range body.Domain {
|
||||
|
||||
if domain.Name[len(domain.Name)-1:] != "." {
|
||||
domain.Name += "."
|
||||
}
|
||||
|
||||
if it.Name == domain.Name {
|
||||
sets, err := getRecordSet(it, domain.Records)
|
||||
if err != nil {
|
||||
log.Println("get record error :: ", err)
|
||||
continue
|
||||
}
|
||||
// compare ip
|
||||
changeSet := make([]*route53.ResourceRecordSet, 0, len(sets))
|
||||
for _, set := range sets {
|
||||
if *set.ResourceRecords[0].Value != ip {
|
||||
set.ResourceRecords[0].Value = awssdk.String(ip)
|
||||
changeSet = append(changeSet, set)
|
||||
}
|
||||
}
|
||||
|
||||
if len(changeSet) == 0 {
|
||||
break
|
||||
}
|
||||
aws.UpdateRecord(it.ID, changeSet)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
c.Success(nil)
|
||||
}
|
||||
|
||||
func getRecordSet(zone *aws.ZoneData, records []RecordData) ([]*route53.ResourceRecordSet, error) {
|
||||
names := make([]string, 0, len(records))
|
||||
for _, v := range records {
|
||||
names = append(names, fmt.Sprintf("%s.%s", v.Name, zone.Name))
|
||||
}
|
||||
sets := make([]*route53.ResourceRecordSet, 0, len(records))
|
||||
data, err := aws.GetRecord(zone.ID, names)
|
||||
|
||||
for _, it := range data {
|
||||
for _, record := range records {
|
||||
if *it.Type == strings.ToUpper(record.Type) {
|
||||
sets = append(sets, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sets, err
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package routes
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.trj.tw/golang/go-ddns-svc/module/context"
|
||||
"git.trj.tw/golang/go-ddns-svc/route/api"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -21,4 +23,9 @@ func SetRoutes() {
|
||||
e.GET("/", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "OK")
|
||||
})
|
||||
|
||||
apiGroup := e.Group("/api", context.PatchCtx(api.VerifyPrivate))
|
||||
{
|
||||
apiGroup.POST("/ddns", context.PatchCtx(api.UpdateDDNS))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user