-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_api.go
More file actions
87 lines (79 loc) · 2.42 KB
/
main_api.go
File metadata and controls
87 lines (79 loc) · 2.42 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
// Copyright 2024-2026 George (earentir) Pantazis (https://earentir.dev)
// SPDX-License-Identifier: GPL-2.0-only
package main
import (
"log/slog"
"net"
"strings"
"dnsplane/api"
"dnsplane/commandhandler"
"dnsplane/daemon"
"dnsplane/data"
)
func stopAPIAsync(state *daemon.State) {
api.Stop(state)
}
func currentServerListeners(state *daemon.State) commandhandler.ServerListenerInfo {
listener := state.ListenerSnapshot()
dnsPort := strings.TrimSpace(listener.DNSPort)
settings := data.GetInstance().GetResolverSettings()
if dnsPort == "" {
dnsPort = strings.TrimSpace(settings.DNSPort)
}
socket := strings.TrimSpace(listener.ClientSocketPath)
tcp := strings.TrimSpace(listener.ClientTCPAddress)
apiEndpoint := strings.TrimSpace(listener.APIEndpoint)
if apiEndpoint == "" && settings.RESTPort != "" {
rest := strings.TrimSpace(settings.RESTPort)
apiBind := strings.TrimSpace(settings.APIBind)
if apiBind != "" {
apiEndpoint = normalizeTCPAddress(net.JoinHostPort(apiBind, rest))
} else {
apiEndpoint = normalizeTCPAddress(":" + rest)
}
}
dnsAddr := dnsListenAddr(dnsPort)
info := commandhandler.ServerListenerInfo{
DNSProtocol: "udp,tcp",
DNSListeners: []string{normalizeTCPAddress(dnsAddr)},
ClientSocket: socket,
ClientSocketEnabled: socket != "",
ClientTCPEndpoint: tcp,
ClientTCPEnabled: tcp != "",
ClientTCPRunning: isClientTCPListenerRunning(),
APIEndpoint: apiEndpoint,
APIEnabled: listener.APIEnabled,
APIRunning: state.APIRunning(),
}
return info
}
func startAPIAsync(state *daemon.State, port string, log *slog.Logger) {
if port == "" {
port = data.GetInstance().GetResolverSettings().RESTPort
}
trimmed := strings.TrimSpace(port)
st := data.GetInstance().GetResolverSettings()
apiBind := strings.TrimSpace(st.APIBind)
var apiEndpoint string
if apiBind != "" {
apiEndpoint = normalizeTCPAddress(net.JoinHostPort(apiBind, trimmed))
} else {
apiEndpoint = normalizeTCPAddress(":" + trimmed)
}
state.UpdateListener(func(info *daemon.ListenerSettings) {
info.APIPort = trimmed
info.APIEndpoint = apiEndpoint
info.APIEnabled = true
})
if state.APIRunning() {
return
}
opts := &api.ListenOptions{
BindIP: st.APIBind,
TLSCertFile: st.APITLSCertFile,
TLSKeyFile: st.APITLSKeyFile,
RateLimitRPS: st.APIRateLimitPerIP,
RateLimitBurst: st.APIRateLimitBurst,
}
api.Start(state, trimmed, opts, nil, log)
}