Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthPestilane committed Jan 13, 2021
2 parents e470cef + ea70c04 commit a01e648
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 64 deletions.
77 changes: 77 additions & 0 deletions app/cmd/server/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package server

import (
"fmt"
"github.com/DarthPestilane/aliddns/app"
"github.com/DarthPestilane/aliddns/app/dns"
"github.com/DarthPestilane/aliddns/app/helper"
jsoniter "github.com/json-iterator/go"
"github.com/urfave/cli"
"net/http"
)

func handler(ctx *cli.Context) {
port := ctx.Int("port")
app.Log().Infof("listening at port %d", port)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// query strings
query := r.URL.Query()

// domain name
domainName := query.Get("domain_name")
if domainName == "" {
responseFail(w, 422, "domain_name is required")
return
}

// rr
rr := query.Get("rr")
if rr == "" {
rr = "@"
}

// ip
currentIP := helper.IP(r)

// bind dns
dnsHandler := dns.New(domainName, currentIP, rr)
app.Log().Info("=====")
if err := dnsHandler.Bind(); err != nil {
responseFail(w, 400, err.Error())
return
}
responseOK(w, 200, fmt.Sprintf("set ip of '%s.%s' to %s", rr, domainName, currentIP))
})
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
panic(fmt.Errorf("start http server failed: %s", err))
}
}

type Response struct {
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
}

func responseFail(w http.ResponseWriter, code int, msg string) {
setHeader(w, code)
b, _ := jsoniter.Marshal(Response{
Success: false,
Error: msg,
})
_, _ = w.Write(b)
}

func responseOK(w http.ResponseWriter, code int, msg string) {
setHeader(w, code)
b, _ := jsoniter.Marshal(Response{
Success: true,
Message: msg,
})
_, _ = w.Write(b)
}

func setHeader(w http.ResponseWriter, code int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
}
65 changes: 1 addition & 64 deletions app/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package server

import (
"fmt"
"github.com/DarthPestilane/aliddns/app"
"github.com/DarthPestilane/aliddns/app/dns"
"github.com/DarthPestilane/aliddns/app/helper"
jsoniter "github.com/json-iterator/go"
"github.com/urfave/cli"
"net/http"
"strconv"
)

Expand All @@ -25,66 +21,7 @@ func Command() cli.Command {
Usage: "指定`监听端口`",
},
},
Action: func(ctx *cli.Context) {
port := ctx.Int("port")
app.Log().Infof("listening at port %d", port)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

// query strings
query := r.URL.Query()

// domain name
var domainName string
if domains, has := query["domain_name"]; !has || domains[0] == "" {
w.WriteHeader(422)
b, _ := jsoniter.Marshal(map[string]interface{}{
"success": false,
"errors": "domain_name is required",
})
_, _ = w.Write(b)
return
} else {
domainName = domains[0]
}

// rr
var rr = "@"
if rrs, has := query["rr"]; has && rrs[0] != "" {
rr = rrs[0]
}

// ip
currentIP := helper.IP(r)

// bind dns
dnsHandler := dns.New(domainName, currentIP, rr)
app.Log().Info("=====")
if err := dnsHandler.Bind(); err != nil {
b, _ := jsoniter.Marshal(map[string]interface{}{
"success": false,
"errors": err.Error(),
})
w.WriteHeader(400)
_, _ = w.Write(b)
return
}

w.WriteHeader(200)
b, err := jsoniter.Marshal(map[string]interface{}{
"success": true,
"message": fmt.Sprintf("set ip of '%s.%s' to %s", rr, domainName, currentIP),
})
if err != nil {
app.Log().WithError(err).Error("decode response failed")
return
}
_, _ = w.Write(b)
})
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
panic(fmt.Errorf("start http server failed: %s", err))
}
},
Action: handler,
}
return cmd
}

0 comments on commit a01e648

Please sign in to comment.