2022-12-29 12:59:48 +00:00
|
|
|
package namecheap
|
2022-08-01 00:19:30 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-12-29 12:59:48 +00:00
|
|
|
"ncddns/mod"
|
2022-08-01 00:19:30 +01:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2022-12-29 12:59:48 +00:00
|
|
|
const (
|
|
|
|
baseURL = "https://dynamicdns.park-your-domain.com/update?"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct{}
|
2022-08-01 00:19:30 +01:00
|
|
|
|
2022-12-29 12:59:48 +00:00
|
|
|
func (nc Client) IPSet(reqs []mod.IPSetRequest) error {
|
2022-08-01 00:19:30 +01:00
|
|
|
for _, req := range reqs {
|
|
|
|
if err := nc.SetSingle(req); err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", req.Domain, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-29 12:59:48 +00:00
|
|
|
func (Client) SetSingle(req mod.IPSetRequest) error {
|
2022-08-01 00:19:30 +01:00
|
|
|
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
|
|
|
|
}
|