Files
EdgeAPI/internal/dnsclients/provider_custom_http.go

171 lines
3.8 KiB
Go
Raw Normal View History

2021-01-28 11:29:57 +08:00
package dnsclients
import (
"bytes"
"crypto/sha1"
"crypto/tls"
"encoding/json"
"fmt"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/iwind/TeaGo/maps"
"io/ioutil"
"net/http"
"strconv"
"time"
)
var customHTTPClient = &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
2021-04-15 17:02:01 +08:00
// CustomHTTPProvider HTTP自定义DNS
2021-01-28 11:29:57 +08:00
type CustomHTTPProvider struct {
url string
secret string
}
2021-04-15 17:02:01 +08:00
// Auth 认证
2021-01-28 11:29:57 +08:00
// 参数:
// - url
// - secret
func (this *CustomHTTPProvider) Auth(params maps.Map) error {
this.url = params.GetString("url")
if len(this.url) == 0 {
return errors.New("'url' should not be empty")
}
this.secret = params.GetString("secret")
if len(this.secret) == 0 {
return errors.New("'secret' should not be empty")
}
return nil
}
2021-04-15 17:02:01 +08:00
// GetRecords 获取域名解析记录列表
2021-01-28 11:29:57 +08:00
func (this *CustomHTTPProvider) GetRecords(domain string) (records []*Record, err error) {
resp, err := this.post(maps.Map{
"action": "GetRecords",
"domain": domain,
})
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &records)
return
}
2021-04-15 17:02:01 +08:00
// GetRoutes 读取域名支持的线路数据
2021-01-28 11:29:57 +08:00
func (this *CustomHTTPProvider) GetRoutes(domain string) (routes []*Route, err error) {
resp, err := this.post(maps.Map{
"action": "GetRoutes",
"domain": domain,
})
if err != nil {
return nil, err
}
err = json.Unmarshal(resp, &routes)
return
}
2021-04-15 17:02:01 +08:00
// QueryRecord 查询单个记录
2021-01-28 11:29:57 +08:00
func (this *CustomHTTPProvider) QueryRecord(domain string, name string, recordType RecordType) (*Record, error) {
resp, err := this.post(maps.Map{
"action": "QueryRecord",
"domain": domain,
"name": name,
"recordType": recordType,
})
if err != nil {
return nil, err
}
if len(resp) == 0 {
return nil, nil
}
record := &Record{}
err = json.Unmarshal(resp, record)
if err != nil {
return nil, err
}
if len(record.Value) == 0 {
return nil, nil
}
return record, nil
}
2021-04-15 17:02:01 +08:00
// AddRecord 设置记录
2021-01-28 11:29:57 +08:00
func (this *CustomHTTPProvider) AddRecord(domain string, newRecord *Record) error {
_, err := this.post(maps.Map{
"action": "AddRecord",
"domain": domain,
"newRecord": newRecord,
})
return err
}
2021-04-15 17:02:01 +08:00
// UpdateRecord 修改记录
2021-01-28 11:29:57 +08:00
func (this *CustomHTTPProvider) UpdateRecord(domain string, record *Record, newRecord *Record) error {
_, err := this.post(maps.Map{
"action": "UpdateRecord",
"domain": domain,
"record": record,
"newRecord": newRecord,
})
return err
}
2021-04-15 17:02:01 +08:00
// DeleteRecord 删除记录
2021-01-28 11:29:57 +08:00
func (this *CustomHTTPProvider) DeleteRecord(domain string, record *Record) error {
_, err := this.post(maps.Map{
"action": "DeleteRecord",
"domain": domain,
"record": record,
})
return err
}
2021-04-15 17:02:01 +08:00
// DefaultRoute 默认线路
2021-01-28 11:29:57 +08:00
func (this *CustomHTTPProvider) DefaultRoute() string {
resp, err := this.post(maps.Map{
"action": "DefaultRoute",
})
if err != nil {
return ""
}
return string(resp)
}
// 执行操作
func (this *CustomHTTPProvider) post(params maps.Map) (respData []byte, err error) {
data, err := json.Marshal(params)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, this.url, bytes.NewReader(data))
if err != nil {
return nil, err
}
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
req.Header.Set("Timestamp", timestamp)
req.Header.Set("Token", fmt.Sprintf("%x", sha1.Sum([]byte(this.secret+"@"+timestamp))))
req.Header.Set("User-Agent", "GoEdge/"+teaconst.Version)
resp, err := customHTTPClient.Do(req)
if err != nil {
return nil, err
}
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode != 200 {
return nil, errors.New("status should be 200, but got '" + strconv.Itoa(resp.StatusCode) + "'")
}
return ioutil.ReadAll(resp.Body)
}