ncddns/providers/namecheap/namecheap.go

50 lines
978 B
Go

package namecheap
import (
"fmt"
"io"
"ncddns/mod"
"net/http"
"net/url"
"os"
)
const (
baseURL = "https://dynamicdns.park-your-domain.com/update?"
)
type Client struct{}
func (nc Client) IPSet(reqs []mod.IPSetRequest) error {
for _, req := range reqs {
if err := nc.SetSingle(req); err != nil {
return fmt.Errorf("%s: %w", req.Domain, err)
}
}
return nil
}
func (Client) SetSingle(req mod.IPSetRequest) error {
params := url.Values{}
params.Add("host", req.Host)
params.Add("domain", req.Domain)
params.Add("password", req.Password)
params.Add("ip", req.IP)
resp, err := http.Get(fmt.Sprintf("%s%s", baseURL, params.Encode()))
if err != nil {
return fmt.Errorf("request: %w", err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("read body: %w", err)
}
if resp.StatusCode != http.StatusOK {
fmt.Fprintf(os.Stderr, "got status [%d]: %s\n", resp.StatusCode, string(data))
}
return nil
}