From 4e5dae40d3a1bdf929e492f2201f3b1517016018 Mon Sep 17 00:00:00 2001 From: Yann Stepienik Date: Sat, 11 May 2024 12:40:54 +0100 Subject: [PATCH] [release] v0.16.0-unstable8 --- package.json | 2 +- src/constellation/DNS.go | 25 +++++++++++-- src/constellation/api_devices_list.go | 6 +--- src/constellation/index.go | 9 +++++ src/constellation/nebula.go | 52 +++++++++++++++++++++++++-- src/proxy/routeTo.go | 1 + src/utils/utils.go | 17 ++++++++- 7 files changed, 99 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 10cc834..e6fb447 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cosmos-server", - "version": "0.16.0-unstable7", + "version": "0.16.0-unstable8", "description": "", "main": "test-server.js", "bugs": { diff --git a/src/constellation/DNS.go b/src/constellation/DNS.go index 3c15f6f..99cc130 100644 --- a/src/constellation/DNS.go +++ b/src/constellation/DNS.go @@ -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 diff --git a/src/constellation/api_devices_list.go b/src/constellation/api_devices_list.go index b8aea47..c55819d 100644 --- a/src/constellation/api_devices_list.go +++ b/src/constellation/api_devices_list.go @@ -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") diff --git a/src/constellation/index.go b/src/constellation/index.go index fb6b0d3..170747f 100644 --- a/src/constellation/index.go +++ b/src/constellation/index.go @@ -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() } } diff --git a/src/constellation/nebula.go b/src/constellation/nebula.go index e4f6b81..56f036d 100644 --- a/src/constellation/nebula.go +++ b/src/constellation/nebula.go @@ -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 diff --git a/src/proxy/routeTo.go b/src/proxy/routeTo.go index 5581eb3..763a4df 100644 --- a/src/proxy/routeTo.go +++ b/src/proxy/routeTo.go @@ -110,6 +110,7 @@ func NewProxy(targetHost string, AcceptInsecureHTTPSTarget bool, DisableHeaderHa hostPort := "" if route.OverwriteHostHeader != "" { hostDest = route.OverwriteHostHeader + req.Host = hostDest } // split port diff --git a/src/utils/utils.go b/src/utils/utils.go index 8c9ea6e..1c3fb2a 100644 --- a/src/utils/utils.go +++ b/src/utils/utils.go @@ -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 {