Skip to content

Commit a01e648

Browse files
Merge branch 'develop'
2 parents e470cef + ea70c04 commit a01e648

2 files changed

Lines changed: 78 additions & 64 deletions

File tree

app/cmd/server/handler.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package server
2+
3+
import (
4+
"fmt"
5+
"github.com/DarthPestilane/aliddns/app"
6+
"github.com/DarthPestilane/aliddns/app/dns"
7+
"github.com/DarthPestilane/aliddns/app/helper"
8+
jsoniter "github.com/json-iterator/go"
9+
"github.com/urfave/cli"
10+
"net/http"
11+
)
12+
13+
func handler(ctx *cli.Context) {
14+
port := ctx.Int("port")
15+
app.Log().Infof("listening at port %d", port)
16+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
17+
// query strings
18+
query := r.URL.Query()
19+
20+
// domain name
21+
domainName := query.Get("domain_name")
22+
if domainName == "" {
23+
responseFail(w, 422, "domain_name is required")
24+
return
25+
}
26+
27+
// rr
28+
rr := query.Get("rr")
29+
if rr == "" {
30+
rr = "@"
31+
}
32+
33+
// ip
34+
currentIP := helper.IP(r)
35+
36+
// bind dns
37+
dnsHandler := dns.New(domainName, currentIP, rr)
38+
app.Log().Info("=====")
39+
if err := dnsHandler.Bind(); err != nil {
40+
responseFail(w, 400, err.Error())
41+
return
42+
}
43+
responseOK(w, 200, fmt.Sprintf("set ip of '%s.%s' to %s", rr, domainName, currentIP))
44+
})
45+
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
46+
panic(fmt.Errorf("start http server failed: %s", err))
47+
}
48+
}
49+
50+
type Response struct {
51+
Success bool `json:"success"`
52+
Message string `json:"message,omitempty"`
53+
Error string `json:"error,omitempty"`
54+
}
55+
56+
func responseFail(w http.ResponseWriter, code int, msg string) {
57+
setHeader(w, code)
58+
b, _ := jsoniter.Marshal(Response{
59+
Success: false,
60+
Error: msg,
61+
})
62+
_, _ = w.Write(b)
63+
}
64+
65+
func responseOK(w http.ResponseWriter, code int, msg string) {
66+
setHeader(w, code)
67+
b, _ := jsoniter.Marshal(Response{
68+
Success: true,
69+
Message: msg,
70+
})
71+
_, _ = w.Write(b)
72+
}
73+
74+
func setHeader(w http.ResponseWriter, code int) {
75+
w.Header().Set("Content-Type", "application/json")
76+
w.WriteHeader(code)
77+
}

app/cmd/server/server.go

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ package server
22

33
import (
44
"fmt"
5-
"github.com/DarthPestilane/aliddns/app"
6-
"github.com/DarthPestilane/aliddns/app/dns"
75
"github.com/DarthPestilane/aliddns/app/helper"
8-
jsoniter "github.com/json-iterator/go"
96
"github.com/urfave/cli"
10-
"net/http"
117
"strconv"
128
)
139

@@ -25,66 +21,7 @@ func Command() cli.Command {
2521
Usage: "指定`监听端口`",
2622
},
2723
},
28-
Action: func(ctx *cli.Context) {
29-
port := ctx.Int("port")
30-
app.Log().Infof("listening at port %d", port)
31-
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
32-
w.Header().Set("Content-Type", "application/json")
33-
34-
// query strings
35-
query := r.URL.Query()
36-
37-
// domain name
38-
var domainName string
39-
if domains, has := query["domain_name"]; !has || domains[0] == "" {
40-
w.WriteHeader(422)
41-
b, _ := jsoniter.Marshal(map[string]interface{}{
42-
"success": false,
43-
"errors": "domain_name is required",
44-
})
45-
_, _ = w.Write(b)
46-
return
47-
} else {
48-
domainName = domains[0]
49-
}
50-
51-
// rr
52-
var rr = "@"
53-
if rrs, has := query["rr"]; has && rrs[0] != "" {
54-
rr = rrs[0]
55-
}
56-
57-
// ip
58-
currentIP := helper.IP(r)
59-
60-
// bind dns
61-
dnsHandler := dns.New(domainName, currentIP, rr)
62-
app.Log().Info("=====")
63-
if err := dnsHandler.Bind(); err != nil {
64-
b, _ := jsoniter.Marshal(map[string]interface{}{
65-
"success": false,
66-
"errors": err.Error(),
67-
})
68-
w.WriteHeader(400)
69-
_, _ = w.Write(b)
70-
return
71-
}
72-
73-
w.WriteHeader(200)
74-
b, err := jsoniter.Marshal(map[string]interface{}{
75-
"success": true,
76-
"message": fmt.Sprintf("set ip of '%s.%s' to %s", rr, domainName, currentIP),
77-
})
78-
if err != nil {
79-
app.Log().WithError(err).Error("decode response failed")
80-
return
81-
}
82-
_, _ = w.Write(b)
83-
})
84-
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
85-
panic(fmt.Errorf("start http server failed: %s", err))
86-
}
87-
},
24+
Action: handler,
8825
}
8926
return cmd
9027
}

0 commit comments

Comments
 (0)