f-86: hostsvc: relais DNS et NTP par hôte, socket de contrôle #86
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
da6295baf1
commit
aa1cc58ae3
20 changed files with 2235 additions and 0 deletions
82
internal/client/hostsvc/client.go
Normal file
82
internal/client/hostsvc/client.go
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
package hostsvcclient
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
hostsvcapi "git.g3e.fr/syonad/two/internal/api/hostsvc"
|
||||
)
|
||||
|
||||
const DefaultTimeout = 5 * time.Second
|
||||
|
||||
type Client struct {
|
||||
path string
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
func New(path string) *Client {
|
||||
return &Client{path: path, timeout: DefaultTimeout}
|
||||
}
|
||||
|
||||
func (c *Client) WithTimeout(d time.Duration) *Client {
|
||||
return &Client{path: c.path, timeout: d}
|
||||
}
|
||||
|
||||
func (c *Client) call(req hostsvcapi.Request) (hostsvcapi.Response, error) {
|
||||
conn, err := net.DialTimeout("unix", c.path, c.timeout)
|
||||
if err != nil {
|
||||
return hostsvcapi.Response{}, fmt.Errorf("dial %s: %w", c.path, err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
if err := conn.SetDeadline(time.Now().Add(c.timeout)); err != nil {
|
||||
return hostsvcapi.Response{}, fmt.Errorf("set deadline on %s: %w", c.path, err)
|
||||
}
|
||||
|
||||
raw, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return hostsvcapi.Response{}, fmt.Errorf("encode %s: %w", req.Verb, err)
|
||||
}
|
||||
if _, err := conn.Write(append(raw, '\n')); err != nil {
|
||||
return hostsvcapi.Response{}, fmt.Errorf("send %s: %w", req.Verb, err)
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(conn)
|
||||
scanner.Buffer(make([]byte, 0, 4096), hostsvcapi.MaxMessageBytes)
|
||||
if !scanner.Scan() {
|
||||
if err := scanner.Err(); err != nil {
|
||||
return hostsvcapi.Response{}, fmt.Errorf("read reply to %s: %w", req.Verb, err)
|
||||
}
|
||||
return hostsvcapi.Response{}, fmt.Errorf("no reply to %s", req.Verb)
|
||||
}
|
||||
|
||||
var resp hostsvcapi.Response
|
||||
if err := json.Unmarshal(scanner.Bytes(), &resp); err != nil {
|
||||
return hostsvcapi.Response{}, fmt.Errorf("parse reply to %s: %w", req.Verb, err)
|
||||
}
|
||||
if !resp.OK {
|
||||
return resp, fmt.Errorf("%s refused: %s", req.Verb, resp.Error)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) Open(vpc string) error {
|
||||
_, err := c.call(hostsvcapi.Request{Verb: hostsvcapi.VerbOpen, VPC: vpc})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Client) Close(vpc string) error {
|
||||
_, err := c.call(hostsvcapi.Request{Verb: hostsvcapi.VerbClose, VPC: vpc})
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Client) List() ([]string, error) {
|
||||
resp, err := c.call(hostsvcapi.Request{Verb: hostsvcapi.VerbList})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resp.VPCs, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue