-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
158 lines (136 loc) · 5.08 KB
/
Copy pathclient.go
File metadata and controls
158 lines (136 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// SPDX-FileCopyrightText: 2026 Adriano Sela Aviles (@adrianosela)
// SPDX-License-Identifier: MIT
package tsdmg
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"sync/atomic"
"github.com/adrianosela/tsdmg/pkg/logger"
"github.com/adrianosela/tsdmg/pkg/service"
"github.com/adrianosela/tsdmg/pkg/types"
"tailscale.com/client/local"
"tailscale.com/tsnet"
)
// Client represents a tsdmg client capable of managing DNS records
// by requesting them via the tsdmg server. Aside from arbitrary
// record creation, the client can request to "register" itself
// meaning that the tsdmg server will create A and AAAA records of
// the form ${node}.${domain} on its behalf. The ${domain}s are
// configured on the tsdmg server.
type Client interface {
// CreateRecords creates the requested DNS records via the tsdmg server.
CreateRecords(context.Context, ...types.Record) ([]types.Record, error)
// DeleteRecords deletes the requested DNS records via the tsdmg server.
DeleteRecords(context.Context, ...types.Record) ([]types.Record, error)
// Register requests the tsdmg server to create A and AAAA records for this
// node's Tailscale private IPs. Domain configuration lives server side. That
// is, the tsdmg server decides which domains to create records in, but all
// records will be of the form ${node}.${domain}.
Register(context.Context) ([]types.Record, error)
// Close closes the client gracefully.
Close() error
}
type client struct {
svcClient service.Client
isOpen atomic.Bool
closers []func() error
}
// NewClient returns a new Client for a tsdmg server at serverURL with
// the given options. Note that serverURL must include scheme (http/s).
func NewClient(ctx context.Context, serverURL string, opts ...Option) (Client, error) {
cfg := &config{
logger: logger.New(),
serverURL: serverURL,
skipTailscaleNode: false,
tailscaleClient: nil,
}
for _, opt := range opts {
opt(cfg)
}
if err := cfg.validate(); err != nil {
return nil, fmt.Errorf("invalid configuration: %w", err)
}
// Slice for functions to be called on Client's Close().
// NOTE: they will be closed in reverse order e.g. LIFO.
var closers []func() error
// Initialize tailscale client if none provided via options.
if cfg.tailscaleClient == nil && !cfg.skipTailscaleNode {
srv := new(tsnet.Server)
srv.Ephemeral = true
if err := srv.Start(); err != nil {
return nil, fmt.Errorf("failed to start Tailscale tsnet node: %v", err)
}
closers = append(closers, srv.Close)
tsClient, err := srv.LocalClient()
if err != nil {
if closeErr := srv.Close(); closeErr != nil {
cfg.logger.Error("failed to close Tailscale tsnet node")
}
return nil, fmt.Errorf("failed to initialize tailscale local client: %v", err)
}
cfg.tailscaleClient = tsClient
}
httpClient := http.DefaultClient
if cfg.tailscaleClient != nil {
httpClient = httpClientFromTsClient(cfg.tailscaleClient)
}
return &client{
svcClient: service.NewClient(httpClient, cfg.serverURL),
isOpen: atomic.Bool{},
closers: closers,
}, nil
}
// CreateRecords creates the requested DNS records via the tsdmg server.
func (c *client) CreateRecords(ctx context.Context, records ...types.Record) ([]types.Record, error) {
out, err := c.svcClient.CreateRecords(ctx, &types.CreateRecordsInput{Records: records})
if err != nil {
return nil, fmt.Errorf("failed to create records using tsdmg http client: %v", err)
}
return out.Records, nil
}
// DeleteRecords deletes the requested DNS records via the tsdmg server.
func (c *client) DeleteRecords(ctx context.Context, records ...types.Record) ([]types.Record, error) {
out, err := c.svcClient.DeleteRecords(ctx, &types.DeleteRecordsInput{Records: records})
if err != nil {
return nil, fmt.Errorf("failed to delete records using tsdmg http client: %v", err)
}
return out.Records, nil
}
// Register requests the tsdmg server to create A and AAAA records for this
// node's Tailscale private IPs. Domain configuration lives server side. That
// is, the tsdmg server decides which domains to create records in, but all
// records will be of the form ${node}.${domain}.
func (c *client) Register(ctx context.Context) ([]types.Record, error) {
out, err := c.svcClient.Register(ctx)
if err != nil {
return nil, fmt.Errorf("failed to register node using tsdmg http client: %v", err)
}
return out.Records, nil
}
// Close closes the client gracefully.
func (c *client) Close() error {
if !c.isOpen.CompareAndSwap(true, false) {
return errClientClosed
}
// Run closers in reverse order.
errs := []error{}
for i := len(c.closers) - 1; i >= 0; i-- {
errs = append(errs, c.closers[i]())
}
return errors.Join(errs...)
}
// httpClientFromTsClient returns an httpClient for which all
// requests will go over the given Tailscale local client.
func httpClientFromTsClient(tsClient *local.Client) *http.Client {
return &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return tsClient.Dial(ctx, network, addr)
},
},
// NOTE: request timeouts will be context-controller, no need to define any here...
}
}