Ejemplo n.º 1
0
        public AssetDiscoveryModule()
        {
            Get["/discovery", true] = async(x, ct) => {
                var devices = await Antd.ServiceDiscovery.Rssdp.Discover();

                var model = new PageAssetDiscoveryModel {
                    List = devices
                };
                return(JsonConvert.SerializeObject(model));
            };

            Get["/device/description"] = x => {
                var model = ServiceDiscovery.Rssdp.GetDeviceDescription();
                return(JsonConvert.SerializeObject(model));
            };

            Get["/device/services"] = x => {
                var model = ServiceDiscovery.Rssdp.GetServices();
                return(JsonConvert.SerializeObject(model));
            };

            Get["/device/filesystem"] = x => {
                var model = ServiceDiscovery.Rssdp.GetFileSystem();
                return(JsonConvert.SerializeObject(model));
            };
        }
Ejemplo n.º 2
0
        public AssetDiscoveryModule()
        {
            Get["/discovery"] = x => {
                var avahiBrowse = new AvahiBrowse();
                avahiBrowse.DiscoverService("antd");
                var localServices = avahiBrowse.Locals;
                var launcher      = new CommandLauncher();
                var list          = new List <AvahiServiceViewModel>();
                var kh            = new SshKnownHosts();
                foreach (var ls in localServices)
                {
                    var arr = ls.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                    var mo  = new AvahiServiceViewModel {
                        HostName   = arr[0].Trim(),
                        Ip         = arr[1].Trim(),
                        Port       = arr[2].Trim(),
                        MacAddress = ""
                    };
                    launcher.Launch("ping-c", new Dictionary <string, string> {
                        { "$ip", arr[1].Trim() }
                    });
                    var result = launcher.Launch("arp", new Dictionary <string, string> {
                        { "$ip", arr[1].Trim() }
                    }).ToList();
                    if (result.Any())
                    {
                        var mac = result.LastOrDefault().Print(3, " ");
                        mo.MacAddress = mac;
                    }
                    mo.IsKnown = kh.Hosts.Contains(arr[1].Trim());
                    list.Add(mo);
                }
                var model = new PageAssetDiscoveryModel {
                    AntdAvahiServices = list
                };
                return(JsonConvert.SerializeObject(model));
            };

            Post["/asset/handshake/start"] = x => {
                var          hostIp           = Request.Form.Host;
                var          hostPort         = Request.Form.Port;
                const string pathToPrivateKey = "/root/.ssh/id_rsa";
                const string pathToPublicKey  = "/root/.ssh/id_rsa.pub";
                if (!File.Exists(pathToPublicKey))
                {
                    var bash = new Bash();
                    var k    = bash.Execute($"ssh-keygen -t rsa -N '' -f {pathToPrivateKey}");
                    ConsoleLogger.Log(k);
                }
                var key = File.ReadAllText(pathToPublicKey);
                if (string.IsNullOrEmpty(key))
                {
                    return(HttpStatusCode.InternalServerError);
                }
                var dict = new Dictionary <string, string> {
                    { "ApplePie", key }
                };
                var r  = new ApiConsumer().Post($"http://{hostIp}:{hostPort}/asset/handshake", dict);
                var kh = new SshKnownHosts();
                kh.Add(hostIp);
                return(r);
            };

            Post["/asset/handshake"] = x => {
                string apple = Request.Form.ApplePie;
                var    info  = apple.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                if (info.Length < 2)
                {
                    return(HttpStatusCode.InternalServerError);
                }
                var          key        = info[0];
                var          remoteUser = info[1];
                const string user       = "******";
                var          model      = new AuthorizedKeyModel {
                    RemoteUser = remoteUser,
                    User       = user,
                    KeyValue   = key
                };
                var authorizedKeysConfiguration = new AuthorizedKeysConfiguration();
                authorizedKeysConfiguration.AddKey(model);
                try {
                    Directory.CreateDirectory("/root/.ssh");
                    const string authorizedKeysPath = "/root/.ssh/authorized_keys";
                    if (File.Exists(authorizedKeysPath))
                    {
                        var f = File.ReadAllText(authorizedKeysPath);
                        if (!f.Contains(apple))
                        {
                            File.AppendAllLines(authorizedKeysPath, new List <string> {
                                apple
                            });
                        }
                    }
                    else
                    {
                        File.WriteAllLines(authorizedKeysPath, new List <string> {
                            apple
                        });
                    }
                    var bash = new Bash();
                    bash.Execute($"chmod 600 {authorizedKeysPath}", false);
                    bash.Execute($"chown {user}:{user} {authorizedKeysPath}", false);
                    return(HttpStatusCode.OK);
                }
                catch (Exception ex) {
                    ConsoleLogger.Log(ex);
                    return(HttpStatusCode.InternalServerError);
                }
            };

            Post["/asset/wol"] = x => {
                string mac      = Request.Form.MacAddress;
                var    launcher = new CommandLauncher();
                launcher.Launch("wol", new Dictionary <string, string> {
                    { "$mac", mac }
                });
                return(HttpStatusCode.OK);
            };

            Get["/asset/nmap/{ip}"] = x => {
                string ip       = x.ip;
                var    launcher = new CommandLauncher();
                var    result   = launcher.Launch("nmap-ip-fast", new Dictionary <string, string> {
                    { "$ip", ip }
                }).Where(_ => !_.Contains("MAC Address")).Skip(5).Reverse().Skip(1).Reverse();
                var list = new List <NmapScanStatus>();
                foreach (var r in result)
                {
                    var a  = r.SplitToList(" ").ToArray();
                    var mo = new NmapScanStatus {
                        Protocol = a[0],
                        Status   = a[1],
                        Type     = a[2]
                    };
                    list.Add(mo);
                }
                list = list.OrderBy(_ => _.Protocol).ToList();
                return(JsonConvert.SerializeObject(list));
            };
        }