-
Notifications
You must be signed in to change notification settings - Fork 0
/
localbase.go
166 lines (137 loc) · 2.97 KB
/
localbase.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
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
159
160
161
162
163
164
165
166
package main
import (
"context"
"fmt"
"log"
"strings"
"sync"
"time"
"github.com/oleksandr/bonjour"
)
type Record struct {
service string
host string
server *bonjour.Server
}
type LocalBase struct {
records map[string]*Record
mu sync.Mutex
}
func NewLocalBase() *LocalBase {
return &LocalBase{
records: make(map[string]*Record),
}
}
func (lb *LocalBase) List() []string {
lb.mu.Lock()
defer lb.mu.Unlock()
domains := make([]string, 0, len(lb.records))
for domain := range lb.records {
domains = append(domains, domain)
}
return domains
}
func (lb *LocalBase) Add(domain string, port int) error {
lb.mu.Lock()
defer lb.mu.Unlock()
config, err := readConfig()
if err != nil {
return err
}
localIP, err := getLocalIP()
if err != nil {
log.Fatalln("Error getting local IP:", err.Error())
}
log.Println("Local IP:", localIP)
clean := strings.TrimSpace(domain)
fullDomain := fmt.Sprintf("%s.local", clean)
if _, exists := lb.records[fullDomain]; exists {
return fmt.Errorf("domain %s already registered", fullDomain)
}
fullHost := fmt.Sprintf("%s.", fullDomain)
service := fmt.Sprintf("_%s._tcp", clean)
// Register nodecrane service
s1, err := bonjour.RegisterProxy(
"localbase",
service,
"",
80,
fullHost,
localIP,
[]string{},
nil)
if err != nil {
log.Fatalln("Error registering frontend service:", err.Error())
}
lb.records[fullDomain] = &Record{
service: service,
host: fullHost,
server: s1,
}
if err := addCaddyServerBlock([]string{fullDomain}, port, config.CaddyAdmin); err != nil {
s1.Shutdown()
delete(lb.records, domain)
return fmt.Errorf("failed to add Caddy server block: %v", err)
}
return nil
}
func (lb *LocalBase) Remove(domain string) error {
lb.mu.Lock()
defer lb.mu.Unlock()
record, exists := lb.records[domain]
if !exists {
return fmt.Errorf("domain %s not registered", domain)
}
record.server.Shutdown()
delete(lb.records, domain)
log.Printf("Removed domain: %s", domain)
return nil
}
func (lb *LocalBase) Shutdown() {
lb.mu.Lock()
defer lb.mu.Unlock()
for domain, rec := range lb.records {
rec.server.Shutdown()
log.Printf("Shutting down domain: %s", domain)
}
}
func (lb *LocalBase) startBroadcast(ctx context.Context) {
ticker := time.NewTicker(15 * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
lb.broadcastAll()
case <-ctx.Done():
return
}
}
}
func (lb *LocalBase) broadcastAll() {
lb.mu.Lock()
defer lb.mu.Unlock()
localIP, err := getLocalIP()
if err != nil {
log.Fatalln("Error getting local IP:", err.Error())
}
for domain, info := range lb.records {
info.server.Shutdown()
server, err := bonjour.RegisterProxy(
"localbase",
info.service,
"",
80,
info.host,
localIP,
[]string{},
nil)
if err != nil {
log.Fatalln("Error registering frontend service:", err.Error())
}
if err != nil {
log.Printf("Error re-registering service for %s: %v", domain, err)
continue
}
info.server = server
}
}