Skip to content

Commit fa4fbcd

Browse files
committed
[release] v0.16.3-unstable2
1 parent d534b79 commit fa4fbcd

File tree

6 files changed

+154
-71
lines changed

6 files changed

+154
-71
lines changed

changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Version 0.16.3
2+
- Local domains now produce services instead of CNAME for better compatibility
3+
- DNS Lookup is now a warning
4+
- DNS Lookup ignores local domains
5+
16
## Version 0.16.2
27
- Only propose cosmos.local as default to setup using local domains
38

client/src/pages/config/routes/routeman.jsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ const RouteManagement = ({ routeConfig, routeNames, config, TargetContainer, noC
112112

113113
fullValues = sanitizeRoute(fullValues);
114114

115-
console.log(fullValues)
116-
117115
let op;
118116
if(newRoute) {
119117
op = API.config.newRoute(routeConfig.Name, fullValues)
@@ -254,7 +252,7 @@ const RouteManagement = ({ routeConfig, routeNames, config, TargetContainer, noC
254252
}}
255253
/>
256254
{hostError && <Grid item xs={12}>
257-
<Alert color='error'>{hostError}</Alert>
255+
<Alert color='warning'>{hostError}</Alert>
258256
</Grid>}
259257
</>
260258
)}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cosmos-server",
3-
"version": "0.16.3-unstable1",
3+
"version": "0.16.3-unstable2",
44
"description": "",
55
"main": "test-server.js",
66
"bugs": {

src/dns.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ func CheckDNSRoute(w http.ResponseWriter, req *http.Request) {
2424
return
2525
}
2626

27-
errDNS := utils.CheckDNS(url)
27+
// if ends with .local
28+
if !strings.HasSuffix(url, ".local") {
2829

29-
if errDNS != nil {
30-
utils.Error("CheckDNS", errDNS)
31-
utils.HTTPError(w, "DNS Check error: " + errDNS.Error(), http.StatusInternalServerError, "DNS002")
32-
return
30+
errDNS := utils.CheckDNS(url)
31+
32+
if errDNS != nil {
33+
utils.Error("CheckDNS", errDNS)
34+
utils.HTTPError(w, "DNS Check error: " + errDNS.Error(), http.StatusInternalServerError, "DNS002")
35+
return
36+
}
3337
}
3438

3539
json.NewEncoder(w).Encode(map[string]interface{}{

src/proxy/avahi.go

+131-62
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"sync"
77
"time"
8+
"net"
9+
"strconv"
810

911
"github.com/godbus/dbus/v5"
1012
"github.com/holoplot/go-avahi"
@@ -26,6 +28,7 @@ type Publisher struct {
2628
rdataField []byte
2729
mu sync.Mutex
2830
cnames []string
31+
published bool
2932
}
3033

3134
func NewPublisher() (*Publisher, error) {
@@ -74,30 +77,65 @@ func NewPublisher() (*Publisher, error) {
7477
func (p *Publisher) Fqdn() string {
7578
return p.fqdn
7679
}
77-
7880
func (p *Publisher) UpdateCNAMES(cnames []string, ttl uint32) error {
7981
p.mu.Lock()
8082
defer p.mu.Unlock()
8183

84+
// If the CNAMEs haven't changed and we've already published, do nothing
85+
if p.published && stringSlicesEqual(p.cnames, cnames) {
86+
return nil
87+
}
88+
8289
if err := p.avahiEntryGroup.Reset(); err != nil {
8390
utils.Error("[mDNS] failed to reset entry group", err)
8491
return err
8592
}
8693

87-
for _, cname := range cnames {
88-
err := p.avahiEntryGroup.AddRecord(
89-
avahi.InterfaceUnspec,
90-
avahi.ProtoUnspec,
91-
uint32(0),
92-
cname,
93-
AVAHI_DNS_CLASS_IN,
94-
AVAHI_DNS_TYPE_CNAME,
95-
ttl,
96-
p.rdataField,
97-
)
98-
if err != nil {
99-
utils.Error("[mDNS] failed to add record to entry group", err)
100-
return err
94+
ifaces, err := net.Interfaces()
95+
if err != nil {
96+
utils.Error("[mDNS] failed to get network interfaces", err)
97+
return err
98+
}
99+
100+
for _, iface := range ifaces {
101+
if iface.Flags&net.FlagLoopback != 0 {
102+
continue
103+
}
104+
105+
portStr, _ := strconv.Atoi(utils.GetServerPort())
106+
107+
for _, cname := range cnames {
108+
utils.Log(fmt.Sprintf("[mDNS] Adding service for: %s on interface %s", cname, iface.Name))
109+
err := p.avahiEntryGroup.AddService(
110+
int32(iface.Index),
111+
avahi.ProtoUnspec,
112+
0,
113+
cname,
114+
"_http._tcp",
115+
"",
116+
"",
117+
uint16(portStr),
118+
nil,
119+
)
120+
if err != nil {
121+
utils.Error(fmt.Sprintf("[mDNS] failed to add service to entry group for interface %s", iface.Name), err)
122+
continue
123+
}
124+
125+
err = p.avahiEntryGroup.AddRecord(
126+
int32(iface.Index),
127+
avahi.ProtoUnspec,
128+
0,
129+
cname,
130+
AVAHI_DNS_CLASS_IN,
131+
AVAHI_DNS_TYPE_CNAME,
132+
ttl,
133+
p.rdataField,
134+
)
135+
if err != nil {
136+
utils.Error(fmt.Sprintf("[mDNS] failed to add CNAME record to entry group for interface %s", iface.Name), err)
137+
continue
138+
}
101139
}
102140
}
103141

@@ -107,54 +145,39 @@ func (p *Publisher) UpdateCNAMES(cnames []string, ttl uint32) error {
107145
}
108146

109147
p.cnames = cnames
148+
p.published = true
110149
return nil
111150
}
112151

113152
func (p *Publisher) Close() {
114-
p.avahiServer.Close()
115-
}
116-
117-
var publisher *Publisher
118-
var publisherMu sync.Mutex
119-
120-
func publishing(ctx context.Context, publisher *Publisher, ttl, interval uint32) error {
121-
resendDuration := time.Duration(interval) * time.Second
122-
ticker := time.NewTicker(resendDuration)
123-
defer ticker.Stop()
124-
125-
publishFunc := func() error {
126-
publisherMu.Lock()
127-
cnamesToPublish := publisher.cnames
128-
publisherMu.Unlock()
129-
130-
err := publisher.UpdateCNAMES(cnamesToPublish, ttl)
153+
p.mu.Lock()
154+
defer p.mu.Unlock()
155+
if p.avahiEntryGroup != nil {
156+
err := p.avahiEntryGroup.Reset()
131157
if err != nil {
132-
utils.Error("[mDNS] failed to update CNAMEs", err)
133-
} else {
134-
utils.Debug("[mDNS] Successfully published CNAMEs: " + fmt.Sprint(cnamesToPublish))
158+
utils.Error("[mDNS] failed to reset entry group during close", err)
135159
}
136-
return err
160+
p.avahiEntryGroup = nil
137161
}
138-
139-
// Publish immediately
140-
if err := publishFunc(); err != nil {
141-
return err
162+
if p.avahiServer != nil {
163+
p.avahiServer.Close()
164+
}
165+
if p.dbusConn != nil {
166+
p.dbusConn.Close()
142167
}
168+
p.published = false
169+
}
143170

144-
for {
145-
select {
146-
case <-ticker.C:
147-
if err := publishFunc(); err != nil {
148-
utils.Error("[mDNS] Failed to publish CNAMEs in ticker", err)
149-
// Continue the loop instead of returning, to keep trying
150-
continue
151-
}
152-
case <-ctx.Done():
153-
utils.Log("[mDNS] context is done, closing publisher")
154-
publisher.Close()
155-
return nil
171+
func stringSlicesEqual(a, b []string) bool {
172+
if len(a) != len(b) {
173+
return false
174+
}
175+
for i, v := range a {
176+
if v != b[i] {
177+
return false
156178
}
157179
}
180+
return true
158181
}
159182

160183
func PublishAllMDNSFromConfig() {
@@ -171,18 +194,10 @@ func PublishAllMDNSFromConfig() {
171194
utils.Error("[mDNS] failed to start mDNS (*.local domains). Install Avahi to solve this issue.", err)
172195
return
173196
}
174-
175-
go func() {
176-
err := publishing(context.Background(), publisher, 60, 5)
177-
if err != nil {
178-
utils.Error("[mDNS] mDNS publishing loop failed", err)
179-
}
180-
}()
181197
}
182198

183199
routes := utils.GetAllHostnames(false, true)
184200

185-
// only keep .local domains
186201
localRoutes := []string{}
187202

188203
if config.NewInstall {
@@ -199,7 +214,6 @@ func PublishAllMDNSFromConfig() {
199214

200215
utils.Log("[mDNS] Publishing the following routes to mDNS: " + fmt.Sprint(localRoutes))
201216

202-
// if empty
203217
if len(localRoutes) == 0 {
204218
utils.Log("[mDNS] No .local domains to publish")
205219
return
@@ -209,4 +223,59 @@ func PublishAllMDNSFromConfig() {
209223
if err != nil {
210224
utils.Error("[mDNS] failed to update CNAMEs", err)
211225
}
212-
}
226+
}
227+
228+
func RestartMDNS() {
229+
publisherMu.Lock()
230+
defer publisherMu.Unlock()
231+
232+
if publisher != nil {
233+
publisher.Close()
234+
publisher = nil
235+
}
236+
237+
PublishAllMDNSFromConfig()
238+
}
239+
240+
var publisher *Publisher
241+
var publisherMu sync.Mutex
242+
243+
func publishing(ctx context.Context, publisher *Publisher, ttl, interval uint32) error {
244+
resendDuration := time.Duration(interval) * time.Second
245+
ticker := time.NewTicker(resendDuration)
246+
defer ticker.Stop()
247+
248+
publishFunc := func() error {
249+
publisherMu.Lock()
250+
cnamesToPublish := publisher.cnames
251+
publisherMu.Unlock()
252+
253+
err := publisher.UpdateCNAMES(cnamesToPublish, ttl)
254+
if err != nil {
255+
utils.Error("[mDNS] failed to update CNAMEs", err)
256+
} else {
257+
utils.Debug("[mDNS] Successfully published CNAMEs: " + fmt.Sprint(cnamesToPublish))
258+
}
259+
return err
260+
}
261+
262+
// Publish immediately
263+
if err := publishFunc(); err != nil {
264+
return err
265+
}
266+
267+
for {
268+
select {
269+
case <-ticker.C:
270+
if err := publishFunc(); err != nil {
271+
utils.Error("[mDNS] Failed to publish CNAMEs in ticker", err)
272+
// Continue the loop instead of returning, to keep trying
273+
continue
274+
}
275+
case <-ctx.Done():
276+
utils.Log("[mDNS] context is done, closing publisher")
277+
publisher.Close()
278+
return nil
279+
}
280+
}
281+
}

src/utils/utils.go

+7
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,13 @@ func GetServerURL(overwriteHostname string) string {
643643
return ServerURL + "/"
644644
}
645645

646+
func GetServerPort() string {
647+
if IsHTTPS {
648+
return MainConfig.HTTPConfig.HTTPSPort
649+
}
650+
return MainConfig.HTTPConfig.HTTPPort
651+
}
652+
646653
func ImageToBase64(path string) (string, error) {
647654
imageFile, err := os.Open(path)
648655
if err != nil {

0 commit comments

Comments
 (0)