2020-07-27 20:43:58 +00:00
|
|
|
package mobileNebula
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"runtime/debug"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/slackhq/nebula"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Nebula struct {
|
|
|
|
c *nebula.Control
|
|
|
|
l *logrus.Logger
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:00:40 +00:00
|
|
|
// NewNebula assembles config and certificates to return a Nebula Client
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
config := nebula.NewConfig()
|
|
|
|
err = config.LoadString(yamlConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to load config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
l := logrus.New()
|
2020-12-16 15:00:40 +00:00
|
|
|
|
|
|
|
// Set logrus output to write to logfile
|
2020-07-27 20:43:58 +00:00
|
|
|
f, err := os.OpenFile(logFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
l.SetOutput(f)
|
|
|
|
|
|
|
|
//TODO: inject our version
|
|
|
|
c, err := nebula.Main(config, false, "", l, &tunFd)
|
|
|
|
if err != nil {
|
|
|
|
switch v := err.(type) {
|
|
|
|
case nebula.ContextualError:
|
2020-08-17 16:56:15 +00:00
|
|
|
v.Log(l)
|
|
|
|
return nil, v.Unwrap()
|
2020-07-27 20:43:58 +00:00
|
|
|
default:
|
2020-08-17 16:56:15 +00:00
|
|
|
l.WithError(err).Error("Failed to start")
|
2020-07-27 20:43:58 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Nebula{c, l}, nil
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:00:40 +00:00
|
|
|
// Start is a handler function for downstream libries to manage the Nebula service
|
2020-07-27 20:43:58 +00:00
|
|
|
func (n *Nebula) Start() {
|
|
|
|
n.c.Start()
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:00:40 +00:00
|
|
|
// ShutdownBlock is a handler function for downstream libries to manage the Nebula service
|
2020-07-27 20:43:58 +00:00
|
|
|
func (n *Nebula) ShutdownBlock() {
|
|
|
|
n.c.ShutdownBlock()
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:00:40 +00:00
|
|
|
// Stop is a handler function for downstream libries to manage the Nebula service
|
2020-07-27 20:43:58 +00:00
|
|
|
func (n *Nebula) Stop() {
|
|
|
|
n.c.Stop()
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:00:40 +00:00
|
|
|
// Rebind is a handler function for downstream libries to manage the Nebula service
|
2020-07-27 20:43:58 +00:00
|
|
|
func (n *Nebula) Rebind() {
|
|
|
|
n.c.RebindUDPServer()
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:00:40 +00:00
|
|
|
// ListHostmap is a handler function for downstream libries to manage the Nebula service
|
2020-07-27 20:43:58 +00:00
|
|
|
func (n *Nebula) ListHostmap(pending bool) (string, error) {
|
|
|
|
hosts := n.c.ListHostmap(pending)
|
|
|
|
b, err := json.Marshal(hosts)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(b), nil
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:00:40 +00:00
|
|
|
// GetHostInfoByVpnIp is a handler function for downstream libries to manage the Nebula service
|
2020-07-27 20:43:58 +00:00
|
|
|
func (n *Nebula) GetHostInfoByVpnIp(vpnIp string, pending bool) (string, error) {
|
2020-08-17 16:56:15 +00:00
|
|
|
b, err := json.Marshal(n.c.GetHostInfoByVpnIP(stringIpToInt(vpnIp), pending))
|
2020-07-27 20:43:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(b), nil
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:00:40 +00:00
|
|
|
// CloseTunnel takes a VPN IP and closes the corresponding tunnel
|
2020-07-27 20:43:58 +00:00
|
|
|
func (n *Nebula) CloseTunnel(vpnIp string) bool {
|
|
|
|
return n.c.CloseTunnel(stringIpToInt(vpnIp), false)
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:00:40 +00:00
|
|
|
// SetRemoteForTunnel is a wrapper function for Nebula's SetRemoteForTunnel
|
2020-07-27 20:43:58 +00:00
|
|
|
func (n *Nebula) SetRemoteForTunnel(vpnIp string, addr string) (string, error) {
|
|
|
|
udpAddr := nebula.NewUDPAddrFromString(addr)
|
|
|
|
if udpAddr == nil {
|
|
|
|
return "", errors.New("could not parse udp address")
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(n.c.SetRemoteForTunnel(stringIpToInt(vpnIp), *udpAddr))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func stringIpToInt(ip string) uint32 {
|
|
|
|
n := net.ParseIP(ip)
|
|
|
|
if len(n) == 16 {
|
|
|
|
return binary.BigEndian.Uint32(n[12:16])
|
|
|
|
}
|
|
|
|
return binary.BigEndian.Uint32(n)
|
2020-08-17 16:56:15 +00:00
|
|
|
}
|