2022-06-11 12:38:03 +00:00
|
|
|
package dns_api
|
|
|
|
|
|
|
|
import (
|
2022-06-12 00:15:08 +00:00
|
|
|
"git.gammaspectra.live/givna.me/dns-api/ed25519"
|
2022-06-11 12:38:03 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
privateKey ed25519.PrivateKey
|
|
|
|
client *http.Client
|
|
|
|
RequestExpirationTime time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewClient Creates an API client with either a specified http Client, or nil
|
|
|
|
func NewClient(privateKey ed25519.PrivateKey, client *http.Client) *Client {
|
|
|
|
if client == nil {
|
|
|
|
client = http.DefaultClient
|
|
|
|
}
|
|
|
|
return &Client{
|
|
|
|
privateKey: privateKey,
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
|
|
|
var expirationTime time.Time
|
|
|
|
if c.RequestExpirationTime != 0 {
|
|
|
|
expirationTime = time.Now().Add(c.RequestExpirationTime)
|
|
|
|
}
|
|
|
|
|
2022-06-11 12:45:31 +00:00
|
|
|
host := req.Host
|
|
|
|
if host == "" {
|
|
|
|
host = req.URL.Host
|
|
|
|
}
|
|
|
|
|
2022-06-11 12:43:49 +00:00
|
|
|
newUrl, err := CreateSignatureMessage(req.Method, req.Host, req.URL, c.privateKey, expirationTime)
|
2022-06-11 12:38:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: WithContext?
|
|
|
|
return c.client.Do(&http.Request{
|
|
|
|
Method: req.Method,
|
|
|
|
URL: newUrl,
|
|
|
|
Header: req.Header,
|
|
|
|
Body: req.Body,
|
|
|
|
GetBody: req.GetBody,
|
|
|
|
ContentLength: req.ContentLength,
|
|
|
|
TransferEncoding: req.TransferEncoding,
|
|
|
|
Close: req.Close,
|
|
|
|
Host: req.Host,
|
|
|
|
Form: req.Form,
|
|
|
|
PostForm: req.PostForm,
|
|
|
|
MultipartForm: req.MultipartForm,
|
|
|
|
Trailer: req.Trailer,
|
|
|
|
Response: req.Response,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: server call methods
|