90 lines
1.5 KiB
Go
90 lines
1.5 KiB
Go
package directadmin
|
|
|
|
import (
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/gorilla/schema"
|
|
)
|
|
|
|
type daClient struct {
|
|
encoder *schema.Encoder
|
|
decoder *schema.Decoder
|
|
|
|
addr string
|
|
username string
|
|
token string
|
|
}
|
|
|
|
// New creates new DirectAdmin client.
|
|
func New(addr, username, token string) DirectAdminClient {
|
|
encoder := schema.NewEncoder()
|
|
encoder.RegisterEncoder(net.IP{}, func(v reflect.Value) string {
|
|
ip := net.IP(v.Bytes())
|
|
return ip.String()
|
|
})
|
|
|
|
decoder := schema.NewDecoder()
|
|
decoder.IgnoreUnknownKeys(true)
|
|
|
|
return &daClient{
|
|
encoder: encoder,
|
|
decoder: decoder,
|
|
addr: addr,
|
|
username: username,
|
|
token: token,
|
|
}
|
|
}
|
|
|
|
// DNSControl implements DirectAdminClient.
|
|
func (c *daClient) DNSControl(params *DNSControlParams) (*DNSControlResponse, error) {
|
|
var (
|
|
formReq = url.Values{}
|
|
respObj = &DNSControlResponse{}
|
|
)
|
|
|
|
if err := c.encoder.Encode(params, formReq); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req, err := http.NewRequest(
|
|
http.MethodPost,
|
|
c.addr+"/CMD_API_DNS_CONTROL",
|
|
strings.NewReader(formReq.Encode()),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.SetBasicAuth(c.username, c.token)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
respData, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
formResp, err := resolveParams(string(respData))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := c.decoder.Decode(respObj, formResp); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return respObj, nil
|
|
}
|
|
|
|
var (
|
|
_ DirectAdminClient = &daClient{}
|
|
)
|