feat(network): integrate NetBird clients, profiles and private backups
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type NetBirdStatus struct {
|
||||
State string `json:"state"`
|
||||
Address string `json:"address"`
|
||||
ServerURL string `json:"server_url"`
|
||||
Network string `json:"network"`
|
||||
}
|
||||
|
||||
var netbirdObservation struct {
|
||||
sync.RWMutex
|
||||
value NetBirdStatus
|
||||
at time.Time
|
||||
}
|
||||
|
||||
func netbirdSnapshot() NetBirdStatus {
|
||||
netbirdObservation.RLock()
|
||||
defer netbirdObservation.RUnlock()
|
||||
if time.Since(netbirdObservation.at) > 20*time.Second {
|
||||
return NetBirdStatus{State: "unavailable"}
|
||||
}
|
||||
return netbirdObservation.value
|
||||
}
|
||||
func netbirdCall(action string, fields map[string]string) (json.RawMessage, error) {
|
||||
connection, err := net.DialTimeout("unix", "/run/mission-core-netbird/control.sock", time.Second)
|
||||
if err != nil {
|
||||
return nil, errors.New("Служба NetBird недоступна. Обновите и подготовьте окружение БК.")
|
||||
}
|
||||
defer connection.Close()
|
||||
_ = connection.SetDeadline(time.Now().Add(10 * time.Second))
|
||||
body := map[string]string{"action": action}
|
||||
for k, v := range fields {
|
||||
body[k] = v
|
||||
}
|
||||
if err = json.NewEncoder(connection).Encode(body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
raw, err := bufio.NewReaderSize(connection, 16385).ReadSlice('\n')
|
||||
var result struct {
|
||||
OK bool `json:"ok"`
|
||||
Result json.RawMessage `json:"result"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
if err != nil || len(raw) > 16384 || json.Unmarshal(raw, &result) != nil {
|
||||
return nil, errors.New("Нет подтверждения NetBird.")
|
||||
}
|
||||
if !result.OK {
|
||||
return nil, errors.New(result.Error)
|
||||
}
|
||||
if action == "status" {
|
||||
var status NetBirdStatus
|
||||
if json.Unmarshal(result.Result, &status) == nil {
|
||||
netbirdObservation.Lock()
|
||||
netbirdObservation.value = status
|
||||
netbirdObservation.at = time.Now()
|
||||
netbirdObservation.Unlock()
|
||||
}
|
||||
}
|
||||
return result.Result, nil
|
||||
}
|
||||
func monitorNetBird(ctx context.Context) {
|
||||
timer := time.NewTicker(5 * time.Second)
|
||||
defer timer.Stop()
|
||||
for {
|
||||
_, _ = netbirdCall("status", nil)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-timer.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
func (s *Server) netbirdRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/network/netbird", func(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.authorized(w, r) {
|
||||
return
|
||||
}
|
||||
out, err := netbirdCall("status", nil)
|
||||
if err != nil {
|
||||
reply(w, 200, map[string]string{"state": "unavailable", "error": err.Error()})
|
||||
return
|
||||
}
|
||||
reply(w, 200, out)
|
||||
})
|
||||
for _, action := range []string{"connect", "cancel"} {
|
||||
mux.HandleFunc("POST /api/network/netbird/"+action, func(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.authorized(w, r) {
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
ServerURL string `json:"server_url"`
|
||||
SetupKey string `json:"setup_key"`
|
||||
}
|
||||
if !decode(w, r, &body) {
|
||||
return
|
||||
}
|
||||
fields := map[string]string{}
|
||||
if action == "connect" {
|
||||
fields["server_url"] = body.ServerURL
|
||||
fields["setup_key"] = body.SetupKey
|
||||
}
|
||||
out, err := netbirdCall(action, fields)
|
||||
if err != nil {
|
||||
reply(w, 409, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
reply(w, 200, out)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Offers arrive only in an authenticated response from this board's bound Core.
|
||||
func (p *Pairing) acceptNetBirdOffer(raw json.RawMessage) {
|
||||
var offer struct {
|
||||
Endpoint string `json:"endpoint"`
|
||||
Config json.RawMessage `json:"config"`
|
||||
}
|
||||
if len(raw) > 4096 || json.Unmarshal(raw, &offer) != nil || !privateEndpoint(offer.Endpoint, CorePort) {
|
||||
return
|
||||
}
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
if p.loadNetworkLocked() != nil || p.state.Binding == nil {
|
||||
return
|
||||
}
|
||||
if p.network.NetBirdEndpoint == offer.Endpoint && string(p.network.NetBirdConfig) == string(offer.Config) {
|
||||
return
|
||||
}
|
||||
p.network.NetBirdEndpoint = offer.Endpoint
|
||||
p.network.NetBirdConfig = offer.Config
|
||||
_ = p.saveNetworkLocked()
|
||||
}
|
||||
|
||||
func (p *Pairing) acceptNetworkRequestResult(raw json.RawMessage) {
|
||||
var result struct {
|
||||
ID string `json:"id"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
if len(raw) > 4096 || json.Unmarshal(raw, &result) != nil {
|
||||
return
|
||||
}
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
if p.network == nil || p.network.LocalRequest == nil || p.network.LocalRequest.ID != result.ID {
|
||||
return
|
||||
}
|
||||
p.network.LocalRequest = nil
|
||||
p.network.LocalError = result.Error
|
||||
_ = p.saveNetworkLocked()
|
||||
}
|
||||
Reference in New Issue
Block a user