This commit is contained in:
Jay
2019-05-09 16:13:25 +08:00
parent e6f22deb49
commit 4fa99f16bd
11 changed files with 306 additions and 9 deletions
+28 -1
View File
@@ -71,4 +71,31 @@ func LoadConfig(p ...string) error {
func GetConfig() *Config { return conf }
// GetV4Data -
func (p DomainData) GetV4Data() {}
func (p DomainData) GetV4Data() DomainData {
domain := DomainData{}
domain.Name = p.Name
if len(p.Records) == 0 {
return domain
}
for _, it := range p.Records {
if it.Type == "A" {
domain.Records = append(domain.Records, it)
}
}
return domain
}
// GetV6Data -
func (p DomainData) GetV6Data() DomainData {
domain := DomainData{}
domain.Name = p.Name
if len(p.Records) == 0 {
return domain
}
for _, it := range p.Records {
if it.Type == "AAAA" {
domain.Records = append(domain.Records, it)
}
}
return domain
}
+78
View File
@@ -0,0 +1,78 @@
package config
import (
"os"
"path"
"reflect"
"testing"
)
func TestLoadConfig(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
p := path.Join(wd, "../..", "config.default.yml")
err = LoadConfig(p)
if err != nil {
t.Fatal(err)
}
}
func TestGetConfig(t *testing.T) {
conf := GetConfig()
if conf == nil {
t.Fail()
}
}
func TestDomainData_GetV6Data(t *testing.T) {
type fields struct {
Name string
Records []RecordData
}
tests := []struct {
name string
fields fields
want DomainData
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := DomainData{
Name: tt.fields.Name,
Records: tt.fields.Records,
}
if got := p.GetV6Data(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("DomainData.GetV6Data() = %v, want %v", got, tt.want)
}
})
}
}
func TestDomainData_GetV4Data(t *testing.T) {
type fields struct {
Name string
Records []RecordData
}
tests := []struct {
name string
fields fields
want DomainData
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := DomainData{
Name: tt.fields.Name,
Records: tt.fields.Records,
}
if got := p.GetV4Data(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("DomainData.GetV4Data() = %v, want %v", got, tt.want)
}
})
}
}
+100 -7
View File
@@ -2,24 +2,117 @@ package ddns
import (
"errors"
"io/ioutil"
"log"
"net/http"
"net/textproto"
"net/url"
"bytes"
"git.trj.tw/golang/go-ddns-client/module/config"
jsoniter "github.com/json-iterator/go"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// Errors
var (
ErrNoDomain = errors.New("No Domain Setting")
ErrAPIResp = errors.New("api call not success")
)
// SendDNSSetting -
func SendDNSSetting() error {
// DNSSetting -
type DNSSetting struct {
V4 []config.DomainData
V6 []config.DomainData
}
// GetDNSSetting -
func GetDNSSetting() (DNSSetting, error) {
setting := DNSSetting{}
conf := config.GetConfig()
if len(conf.Domains) == 0 {
return ErrNoDomain
return setting, ErrNoDomain
}
v4Data := make([]config.DomainData, 0)
v6Data := make([]config.DomainData, 0)
for _, it := range conf.Domains {
v4 := it.GetV4Data()
if len(v4.Records) > 0 {
v4Data = append(v4Data, v4)
}
v6 := it.GetV6Data()
if len(v6.Records) > 0 {
v6Data = append(v6Data, v6)
}
}
setting.V4 = v4Data
setting.V6 = v6Data
return setting, nil
}
// SendRequest -
func SendRequest(v4 bool, data []config.DomainData) error {
conf := config.GetConfig()
var baseURL string
if v4 {
baseURL = conf.URL.V4
} else {
baseURL = conf.URL.V6
}
token := conf.VerifyValue
if len(baseURL) == 0 || len(token) == 0 {
return errors.New("server setting error")
}
body := struct {
Domains []config.DomainData `json:"domains"`
}{}
body.Domains = data
byteData, err := json.Marshal(body)
if err != nil {
return err
}
log.Println("send data :: ", string(byteData))
apiURL, err := url.Parse(baseURL)
if err != nil {
return err
}
apiURL, err = apiURL.Parse("/api/ddns")
if err != nil {
return err
}
reader := bytes.NewReader(byteData)
req, err := http.NewRequest("POST", apiURL.String(), reader)
if err != nil {
return err
}
req.Header.Set(textproto.CanonicalMIMEHeaderKey("X-Mtfos-Key"), token)
req.Header.Set(textproto.CanonicalMIMEHeaderKey("Content-Type"), "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
bodyByte, err := ioutil.ReadAll(resp.Body)
if err == nil {
log.Println("body ::: ", string(bodyByte))
}
if resp.StatusCode != 200 {
return ErrAPIResp
}
return nil
}
func parseConf(domains []config.DomainData) {
}
+28
View File
@@ -0,0 +1,28 @@
package ddns
import (
"reflect"
"testing"
)
func TestGetDNSSetting(t *testing.T) {
tests := []struct {
name string
want DNSSetting
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetDNSSetting()
if (err != nil) != tt.wantErr {
t.Errorf("GetDNSSetting() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetDNSSetting() = %v, want %v", got, tt.want)
}
})
}
}
-1
View File
@@ -17,7 +17,6 @@ func RegOptions() {
flag.StringVar(&opts.Config, "config", "", "config file path - default: `pwd/config.yml`")
flag.StringVar(&opts.Config, "f", "", "config file path - default: `pwd/config.yml`")
flag.BoolVar(&opts.Help, "help", false, "show help")
flag.BoolVar(&opts.Once, "once", false, "run once")
flag.Parse()
}
+35
View File
@@ -0,0 +1,35 @@
package option
import (
"reflect"
"testing"
)
func TestRegOptions(t *testing.T) {
tests := []struct {
name string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
RegOptions()
})
}
}
func TestGetOptions(t *testing.T) {
tests := []struct {
name string
want *Options
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetOptions(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetOptions() = %v, want %v", got, tt.want)
}
})
}
}