Skip to content

Commit

Permalink
[release] v0.16.3-unstable2
Browse files Browse the repository at this point in the history
  • Loading branch information
azukaar committed Sep 24, 2024
1 parent d534b79 commit fa4fbcd
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 71 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Version 0.16.3
- Local domains now produce services instead of CNAME for better compatibility
- DNS Lookup is now a warning
- DNS Lookup ignores local domains

## Version 0.16.2
- Only propose cosmos.local as default to setup using local domains

Expand Down
4 changes: 1 addition & 3 deletions client/src/pages/config/routes/routeman.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ const RouteManagement = ({ routeConfig, routeNames, config, TargetContainer, noC

fullValues = sanitizeRoute(fullValues);

console.log(fullValues)

let op;
if(newRoute) {
op = API.config.newRoute(routeConfig.Name, fullValues)
Expand Down Expand Up @@ -254,7 +252,7 @@ const RouteManagement = ({ routeConfig, routeNames, config, TargetContainer, noC
}}
/>
{hostError && <Grid item xs={12}>
<Alert color='error'>{hostError}</Alert>
<Alert color='warning'>{hostError}</Alert>
</Grid>}
</>
)}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cosmos-server",
"version": "0.16.3-unstable1",
"version": "0.16.3-unstable2",
"description": "",
"main": "test-server.js",
"bugs": {
Expand Down
14 changes: 9 additions & 5 deletions src/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ func CheckDNSRoute(w http.ResponseWriter, req *http.Request) {
return
}

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

if errDNS != nil {
utils.Error("CheckDNS", errDNS)
utils.HTTPError(w, "DNS Check error: " + errDNS.Error(), http.StatusInternalServerError, "DNS002")
return
errDNS := utils.CheckDNS(url)

if errDNS != nil {
utils.Error("CheckDNS", errDNS)
utils.HTTPError(w, "DNS Check error: " + errDNS.Error(), http.StatusInternalServerError, "DNS002")
return
}
}

json.NewEncoder(w).Encode(map[string]interface{}{
Expand Down
193 changes: 131 additions & 62 deletions src/proxy/avahi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"sync"
"time"
"net"
"strconv"

"github.com/godbus/dbus/v5"
"github.com/holoplot/go-avahi"
Expand All @@ -26,6 +28,7 @@ type Publisher struct {
rdataField []byte
mu sync.Mutex
cnames []string
published bool
}

func NewPublisher() (*Publisher, error) {
Expand Down Expand Up @@ -74,30 +77,65 @@ func NewPublisher() (*Publisher, error) {
func (p *Publisher) Fqdn() string {
return p.fqdn
}

func (p *Publisher) UpdateCNAMES(cnames []string, ttl uint32) error {
p.mu.Lock()
defer p.mu.Unlock()

// If the CNAMEs haven't changed and we've already published, do nothing
if p.published && stringSlicesEqual(p.cnames, cnames) {
return nil
}

if err := p.avahiEntryGroup.Reset(); err != nil {
utils.Error("[mDNS] failed to reset entry group", err)
return err
}

for _, cname := range cnames {
err := p.avahiEntryGroup.AddRecord(
avahi.InterfaceUnspec,
avahi.ProtoUnspec,
uint32(0),
cname,
AVAHI_DNS_CLASS_IN,
AVAHI_DNS_TYPE_CNAME,
ttl,
p.rdataField,
)
if err != nil {
utils.Error("[mDNS] failed to add record to entry group", err)
return err
ifaces, err := net.Interfaces()
if err != nil {
utils.Error("[mDNS] failed to get network interfaces", err)
return err
}

for _, iface := range ifaces {
if iface.Flags&net.FlagLoopback != 0 {
continue
}

portStr, _ := strconv.Atoi(utils.GetServerPort())

for _, cname := range cnames {
utils.Log(fmt.Sprintf("[mDNS] Adding service for: %s on interface %s", cname, iface.Name))
err := p.avahiEntryGroup.AddService(
int32(iface.Index),
avahi.ProtoUnspec,
0,
cname,
"_http._tcp",
"",
"",
uint16(portStr),
nil,
)
if err != nil {
utils.Error(fmt.Sprintf("[mDNS] failed to add service to entry group for interface %s", iface.Name), err)
continue
}

err = p.avahiEntryGroup.AddRecord(
int32(iface.Index),
avahi.ProtoUnspec,
0,
cname,
AVAHI_DNS_CLASS_IN,
AVAHI_DNS_TYPE_CNAME,
ttl,
p.rdataField,
)
if err != nil {
utils.Error(fmt.Sprintf("[mDNS] failed to add CNAME record to entry group for interface %s", iface.Name), err)
continue
}
}
}

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

p.cnames = cnames
p.published = true
return nil
}

func (p *Publisher) Close() {
p.avahiServer.Close()
}

var publisher *Publisher
var publisherMu sync.Mutex

func publishing(ctx context.Context, publisher *Publisher, ttl, interval uint32) error {
resendDuration := time.Duration(interval) * time.Second
ticker := time.NewTicker(resendDuration)
defer ticker.Stop()

publishFunc := func() error {
publisherMu.Lock()
cnamesToPublish := publisher.cnames
publisherMu.Unlock()

err := publisher.UpdateCNAMES(cnamesToPublish, ttl)
p.mu.Lock()
defer p.mu.Unlock()
if p.avahiEntryGroup != nil {
err := p.avahiEntryGroup.Reset()
if err != nil {
utils.Error("[mDNS] failed to update CNAMEs", err)
} else {
utils.Debug("[mDNS] Successfully published CNAMEs: " + fmt.Sprint(cnamesToPublish))
utils.Error("[mDNS] failed to reset entry group during close", err)
}
return err
p.avahiEntryGroup = nil
}

// Publish immediately
if err := publishFunc(); err != nil {
return err
if p.avahiServer != nil {
p.avahiServer.Close()
}
if p.dbusConn != nil {
p.dbusConn.Close()
}
p.published = false
}

for {
select {
case <-ticker.C:
if err := publishFunc(); err != nil {
utils.Error("[mDNS] Failed to publish CNAMEs in ticker", err)
// Continue the loop instead of returning, to keep trying
continue
}
case <-ctx.Done():
utils.Log("[mDNS] context is done, closing publisher")
publisher.Close()
return nil
func stringSlicesEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}

func PublishAllMDNSFromConfig() {
Expand All @@ -171,18 +194,10 @@ func PublishAllMDNSFromConfig() {
utils.Error("[mDNS] failed to start mDNS (*.local domains). Install Avahi to solve this issue.", err)
return
}

go func() {
err := publishing(context.Background(), publisher, 60, 5)
if err != nil {
utils.Error("[mDNS] mDNS publishing loop failed", err)
}
}()
}

routes := utils.GetAllHostnames(false, true)

// only keep .local domains
localRoutes := []string{}

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

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

// if empty
if len(localRoutes) == 0 {
utils.Log("[mDNS] No .local domains to publish")
return
Expand All @@ -209,4 +223,59 @@ func PublishAllMDNSFromConfig() {
if err != nil {
utils.Error("[mDNS] failed to update CNAMEs", err)
}
}
}

func RestartMDNS() {
publisherMu.Lock()
defer publisherMu.Unlock()

if publisher != nil {
publisher.Close()
publisher = nil
}

PublishAllMDNSFromConfig()
}

var publisher *Publisher
var publisherMu sync.Mutex

func publishing(ctx context.Context, publisher *Publisher, ttl, interval uint32) error {
resendDuration := time.Duration(interval) * time.Second
ticker := time.NewTicker(resendDuration)
defer ticker.Stop()

publishFunc := func() error {
publisherMu.Lock()
cnamesToPublish := publisher.cnames
publisherMu.Unlock()

err := publisher.UpdateCNAMES(cnamesToPublish, ttl)
if err != nil {
utils.Error("[mDNS] failed to update CNAMEs", err)
} else {
utils.Debug("[mDNS] Successfully published CNAMEs: " + fmt.Sprint(cnamesToPublish))
}
return err
}

// Publish immediately
if err := publishFunc(); err != nil {
return err
}

for {
select {
case <-ticker.C:
if err := publishFunc(); err != nil {
utils.Error("[mDNS] Failed to publish CNAMEs in ticker", err)
// Continue the loop instead of returning, to keep trying
continue
}
case <-ctx.Done():
utils.Log("[mDNS] context is done, closing publisher")
publisher.Close()
return nil
}
}
}
7 changes: 7 additions & 0 deletions src/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,13 @@ func GetServerURL(overwriteHostname string) string {
return ServerURL + "/"
}

func GetServerPort() string {
if IsHTTPS {
return MainConfig.HTTPConfig.HTTPSPort
}
return MainConfig.HTTPConfig.HTTPPort
}

func ImageToBase64(path string) (string, error) {
imageFile, err := os.Open(path)
if err != nil {
Expand Down

0 comments on commit fa4fbcd

Please sign in to comment.