mobile_nebula/nebula/control.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

156 lines
3.2 KiB
Go
Raw Permalink Normal View History

2020-07-27 20:43:58 +00:00
package mobileNebula
import (
"encoding/json"
"errors"
"fmt"
"net/netip"
2020-07-27 20:43:58 +00:00
"os"
2021-04-23 21:23:06 +00:00
"runtime"
2020-07-27 20:43:58 +00:00
"runtime/debug"
"github.com/sirupsen/logrus"
"github.com/slackhq/nebula"
nc "github.com/slackhq/nebula/config"
"github.com/slackhq/nebula/overlay"
"github.com/slackhq/nebula/util"
2020-07-27 20:43:58 +00:00
)
type Nebula struct {
c *nebula.Control
l *logrus.Logger
config *nc.C
2020-07-27 20:43:58 +00:00
}
2021-04-23 21:23:06 +00:00
func init() {
// Reduces memory utilization according to https://twitter.com/felixge/status/1355846360562589696?s=20
runtime.MemProfileRate = 0
}
2020-07-27 20:43:58 +00:00
func NewNebula(configData string, key string, logFile string, tunFd int) (*Nebula, error) {
// GC more often, largely for iOS due to extension 15mb limit
debug.SetGCPercent(20)
yamlConfig, err := RenderConfig(configData, key)
if err != nil {
return nil, err
}
l := logrus.New()
f, err := os.OpenFile(logFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return nil, err
}
l.SetOutput(f)
c := nc.NewC(l)
err = c.LoadString(yamlConfig)
2021-04-23 21:23:06 +00:00
if err != nil {
return nil, fmt.Errorf("failed to load config: %s", err)
}
2020-07-27 20:43:58 +00:00
//TODO: inject our version
ctrl, err := nebula.Main(c, false, "", l, overlay.NewFdDeviceFromConfig(&tunFd))
2020-07-27 20:43:58 +00:00
if err != nil {
switch v := err.(type) {
case *util.ContextualError:
v.Log(l)
return nil, v.Unwrap()
2020-07-27 20:43:58 +00:00
default:
l.WithError(err).Error("Failed to start")
2020-07-27 20:43:58 +00:00
return nil, err
}
}
return &Nebula{ctrl, l, c}, nil
2020-07-27 20:43:58 +00:00
}
2021-04-23 21:23:06 +00:00
func (n *Nebula) Log(v string) {
n.l.Println(v)
}
2020-07-27 20:43:58 +00:00
func (n *Nebula) Start() {
n.c.Start()
}
func (n *Nebula) ShutdownBlock() {
n.c.ShutdownBlock()
}
func (n *Nebula) Stop() {
n.c.Stop()
}
2021-04-23 21:23:06 +00:00
func (n *Nebula) Rebind(reason string) {
n.l.Debugf("Rebinding UDP listener and updating lighthouses due to %s", reason)
2020-07-27 20:43:58 +00:00
n.c.RebindUDPServer()
}
func (n *Nebula) Reload(configData string, key string) error {
n.l.Info("Reloading Nebula")
yamlConfig, err := RenderConfig(configData, key)
if err != nil {
return err
}
return n.config.ReloadConfigString(yamlConfig)
}
2020-07-27 20:43:58 +00:00
func (n *Nebula) ListHostmap(pending bool) (string, error) {
hosts := n.c.ListHostmapHosts(pending)
2020-07-27 20:43:58 +00:00
b, err := json.Marshal(hosts)
if err != nil {
return "", err
}
return string(b), nil
}
func (n *Nebula) GetHostInfoByVpnIp(vpnIp string, pending bool) (string, error) {
netVpnIp, err := netip.ParseAddr(vpnIp)
if err != nil {
return "", err
}
b, err := json.Marshal(n.c.GetHostInfoByVpnIp(netVpnIp, pending))
2020-07-27 20:43:58 +00:00
if err != nil {
return "", err
}
return string(b), nil
}
func (n *Nebula) CloseTunnel(vpnIp string) bool {
netVpnIp, err := netip.ParseAddr(vpnIp)
if err != nil {
return false
}
return n.c.CloseTunnel(netVpnIp, false)
2020-07-27 20:43:58 +00:00
}
func (n *Nebula) SetRemoteForTunnel(vpnIp string, addr string) (string, error) {
udpAddr, err := netip.ParseAddrPort(addr)
if err != nil {
2020-07-27 20:43:58 +00:00
return "", errors.New("could not parse udp address")
}
netVpnIp, err := netip.ParseAddr(vpnIp)
if err != nil {
return "", errors.New("could not parse vpnIp")
}
b, err := json.Marshal(n.c.SetRemoteForTunnel(netVpnIp, udpAddr))
2020-07-27 20:43:58 +00:00
if err != nil {
return "", err
}
return string(b), nil
}
2021-04-23 21:23:06 +00:00
func (n *Nebula) Sleep() {
if closed := n.c.CloseAllTunnels(true); closed > 0 {
n.l.WithField("tunnels", closed).Info("Sleep called, closed non lighthouse tunnels")
}
}