/
dns.go
57 lines (43 loc) · 962 Bytes
/
dns.go
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
package instance
import (
"fmt"
"sync"
"github.com/txn2/txeh"
)
type DNS struct {
hosts *txeh.Hosts
mu sync.Mutex
}
func NewDNS() (*DNS, error) {
hosts, err := txeh.NewHostsDefault()
if err != nil {
return nil, fmt.Errorf("error initializing etc hosts handler: %w", err)
}
reloadErr := hosts.Reload()
if reloadErr != nil {
return nil, fmt.Errorf("error reloading etc hosts: %w", reloadErr)
}
return &DNS{
hosts: hosts,
}, nil
}
func (d *DNS) Add(ips *IPSlot) error {
d.mu.Lock()
defer d.mu.Unlock()
d.hosts.AddHost(ips.HostSnapshotIP(), ips.InstanceID)
err := d.hosts.Save()
if err != nil {
return fmt.Errorf("error adding env instance to etc hosts: %w", err)
}
return nil
}
func (d *DNS) Remove(ips *IPSlot) error {
d.mu.Lock()
defer d.mu.Unlock()
d.hosts.RemoveHost(ips.InstanceID)
err := d.hosts.Save()
if err != nil {
return fmt.Errorf("error removing env instance to etc hosts: %w", err)
}
return nil
}