[release] v0.16.0-unstable8

This commit is contained in:
Yann Stepienik
2024-05-11 12:40:54 +01:00
parent f4ed291972
commit 4e5dae40d3
7 changed files with 99 additions and 13 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "cosmos-server",
"version": "0.16.0-unstable7",
"version": "0.16.0-unstable8",
"description": "",
"main": "test-server.js",
"bugs": {

View File

@@ -61,7 +61,25 @@ func handleDNSRequest(w dns.ResponseWriter, r *dns.Msg) {
}
if !customHandled {
// Overwrite local hostnames with Constellation IP
// Overwrite remote hostnames with Constellation IP
remoteHostnames := utils.GetAllTunnelHostnames()
for _, q := range r.Question {
for hostname, _destination := range remoteHostnames {
destination := CachedDeviceNames[_destination]
if destination != "" {
if strings.HasSuffix(q.Name, hostname + ".") && q.Qtype == dns.TypeA {
utils.Debug("DNS Overwrite " + hostname + " with " + destination)
rr, _ := dns.NewRR(q.Name + " A " + destination)
m.Answer = append(m.Answer, rr)
customHandled = true
}
}
}
}
}
if !customHandled {
// Overwrite local hostnames with their Constellation IP
for _, q := range r.Question {
utils.Debug("DNS Question " + q.Name)
for _, hostname := range hostnames {
@@ -80,8 +98,9 @@ func handleDNSRequest(w dns.ResponseWriter, r *dns.Msg) {
for _, q := range r.Question {
utils.Debug("DNS Question " + q.Name)
for deviceName, ip := range CachedDeviceNames {
if strings.HasSuffix(q.Name, deviceName + ".") && q.Qtype == dns.TypeA {
utils.Debug("DNS Overwrite " + deviceName + " with its IP")
procDeviceName := strings.ReplaceAll(deviceName, " ", "-")
if strings.HasSuffix(q.Name, procDeviceName + ".") && q.Qtype == dns.TypeA {
utils.Debug("DNS Overwrite " + procDeviceName + " with its IP")
rr, _ := dns.NewRR(q.Name + " A " + ip)
m.Answer = append(m.Answer, rr)
customHandled = true

View File

@@ -3,9 +3,7 @@ package constellation
import (
"net/http"
"encoding/json"
"fmt"
"github.com/azukaar/cosmos-server/src/utils"
)
@@ -49,8 +47,6 @@ func DeviceList(w http.ResponseWriter, req *http.Request) {
utils.HTTPError(w, "Error decoding devices", http.StatusInternalServerError, "DL002")
return
}
fmt.Println(devices)
} else {
// If not admin, get user's devices based on their nickname
nickname := req.Header.Get("x-cosmos-user")

View File

@@ -104,6 +104,14 @@ func Init() {
for _, device := range devices {
CachedDeviceNames[device.DeviceName] = device.IP
utils.Debug("Constellation: device name cached: " + device.DeviceName + " -> " + device.IP)
if device.PublicHostname != "" {
publicHostnames := strings.Split(device.PublicHostname, ",")
for _, publicHostname := range publicHostnames {
CachedDeviceNames[strings.TrimSpace(publicHostname)] = device.IP
utils.Debug("Constellation: device name cached: " + publicHostname + " -> " + device.IP)
}
}
}
utils.Log("Constellation: device names cache populated")
@@ -138,6 +146,7 @@ func Init() {
utils.Warn("Slave config has changed, restarting Nebula...")
ConstellationInitLock.Unlock()
RestartNebula()
ConstellationInitLock.Lock()
utils.RestartHTTPServer()
}
}

View File

@@ -41,6 +41,27 @@ func startNebulaInBackground() error {
return errors.New("nebula is already running")
}
// if pid file, kill the process
if _, err := os.Stat(utils.CONFIGFOLDER + "nebula.pid"); err == nil {
// read pid
pid, err := ioutil.ReadFile(utils.CONFIGFOLDER + "nebula.pid")
if err != nil {
utils.Error("Constellation: Error reading pid file", err)
} else {
// kill process
pidInt, _ := strconv.Atoi(string(pid))
processToKill, err := os.FindProcess(pidInt)
if err != nil {
utils.Error("Constellation: Error finding process", err)
} else {
err = processToKill.Kill()
if err != nil {
utils.Error("Constellation: Error killing process", err)
}
}
}
}
logBuffer = &lumberjack.Logger{
Filename: utils.CONFIGFOLDER+"nebula.log",
MaxSize: 1, // megabytes
@@ -68,6 +89,12 @@ func startNebulaInBackground() error {
NebulaStarted = true
// save PID
err := ioutil.WriteFile(utils.CONFIGFOLDER+"nebula.pid", []byte(fmt.Sprintf("%d", process.Process.Pid)), 0644)
if err != nil {
utils.Error("Constellation: Error writing PID file", err)
}
utils.Log(fmt.Sprintf("%s started with PID %d\n", binaryToRun(), process.Process.Pid))
return nil
}
@@ -83,8 +110,15 @@ func stop() error {
if err := process.Process.Kill(); err != nil {
return err
}
process = nil
utils.Log("Stopped nebula.")
// remove PID file
if _, err := os.Stat(utils.CONFIGFOLDER + "nebula.pid"); err == nil {
os.Remove(utils.CONFIGFOLDER + "nebula.pid")
}
return nil
}
@@ -203,7 +237,12 @@ func ExportConfigToYAML(overwriteConfig utils.ConstellationConfig, outputPath st
for _, l := range lh {
finalConfig.StaticHostMap[cleanIp(l.IP)] = []string{
l.PublicHostname + ":" + l.Port,
// l.PublicHostname + ":" + l.Port,
}
for _, hostname := range strings.Split(l.PublicHostname, ",") {
hostname = strings.TrimSpace(hostname)
finalConfig.StaticHostMap[cleanIp(l.IP)] = append(finalConfig.StaticHostMap[cleanIp(l.IP)], hostname + ":" + l.Port)
}
}
@@ -300,7 +339,12 @@ func getYAMLClientConfig(name, configPath, capki, cert, key, APIKey string, devi
for _, l := range lh {
staticHostMap[cleanIp(l.IP)] = []string{
l.PublicHostname + ":" + l.Port,
// l.PublicHostname + ":" + l.Port,
}
for _, hostname := range strings.Split(l.PublicHostname, ",") {
hostname = strings.TrimSpace(hostname)
staticHostMap[cleanIp(l.IP)] = append(staticHostMap[cleanIp(l.IP)].([]string), hostname + ":" + l.Port)
}
}
} else {
@@ -391,7 +435,7 @@ func getYAMLClientConfig(name, configPath, capki, cert, key, APIKey string, devi
protocol = "http://"
port = ":" + utils.GetMainConfig().HTTPConfig.HTTPPort
}
} else if route.UseHost {
} else {
// extract port from target
if strings.Contains(route.Host, ":") {
_port := strings.Split(route.Host, ":")[1]
@@ -404,6 +448,8 @@ func getYAMLClientConfig(name, configPath, capki, cert, key, APIKey string, devi
route.AcceptInsecureHTTPSTarget = true
route.UseHost = true
route.Target = protocol + "192.168.201.1" + port
route.Host = route.TunneledHost

View File

@@ -110,6 +110,7 @@ func NewProxy(targetHost string, AcceptInsecureHTTPSTarget bool, DisableHeaderHa
hostPort := ""
if route.OverwriteHostHeader != "" {
hostDest = route.OverwriteHostHeader
req.Host = hostDest
}
// split port

View File

@@ -449,7 +449,9 @@ func GetAllHostnames(applyWildCard bool, removePorts bool) []string {
mainHostname,
}
proxies := GetMainConfig().HTTPConfig.ProxyConfig.Routes
proxies := GetMainConfig().ConstellationConfig.Tunnels
proxies = append(proxies, GetMainConfig().HTTPConfig.ProxyConfig.Routes...)
for _, proxy := range proxies {
if proxy.UseHost && proxy.Host != "" && !strings.Contains(proxy.Host, ",") && !strings.Contains(proxy.Host, " ") {
if removePorts {
@@ -496,6 +498,19 @@ func GetAllHostnames(applyWildCard bool, removePorts bool) []string {
return uniqueHostnames
}
// TODO
func GetAllTunnelHostnames() map[string]string {
config := GetMainConfig()
tunnels := config.ConstellationConfig.Tunnels
results := map[string]string{}
for _, tunnel := range tunnels {
results[strings.Split(tunnel.Host, ":")[0]] = tunnel.TunnelVia
}
return results
}
func GetAvailableRAM() uint64 {
vmStat, err := mem.VirtualMemory()
if err != nil {