Ejemplo n.º 1
0
        private static bool ServiceConcurrencyCheck()
        {
            SQLiteClass           sql  = new SQLiteClass(SQLiteClass.ProgramFilesx86() + "\\DuckServer\\Information.dat");
            List <ServicesObject> sros = sql.GetServices();
            String ip         = GetIPFromConfig();
            int    myPriority = Int32.MaxValue;
            bool   found      = false;

            foreach (ServicesObject sro in sros)
            {
                if (sro.IPAddress.Trim().Equals(ip.Trim()))
                {
                    myPriority = sro.Preference;
                    if (myPriority - 1 == 0)
                    {
                        return(true);
                    }
                    found = true;
                }
            }

            if (!found)
            {
                Console.WriteLine("Could not find " + ip + " in service list");
            }

            foreach (ServicesObject sro in sros)
            {
                //If not me
                if (!sro.IPAddress.Equals(ip) && sro.Preference == (myPriority - 1))
                {
                    try
                    {
                        IMClient imc = new IMClient();
                        imc.setConnParams(sro.IPAddress.Trim(), sro.port);
                        imc.SetupConn();
                        imc.Disconnect();
                        return(false);
                    }
                    catch
                    {
                        MSSQL ms = new MSSQL();
                        ms.AddEvent(new Events("HOST200", "Server at " + ip + " could not connect to service with preference " + (myPriority - 1) + " ," + sro.IPAddress.Trim(), 3, ip, sql.GetGUID(), DateTime.Now));
                        return(true);
                    }
                }
            }
            Console.WriteLine("No server list available");
            return(false);
        }
Ejemplo n.º 2
0
        static int Main(string[] args)
        {
            SQLiteClass sql = new SQLiteClass(SQLiteClass.ProgramFilesx86() + "\\DuckClient\\Information.dat");

            var startTimeSpan  = TimeSpan.Zero;
            var periodTimeSpan = TimeSpan.FromSeconds(20);

            var timer = new Timer((e) =>
            {
                RoutineCheck();
            }, null, startTimeSpan, periodTimeSpan);

            Service s = new Service();

            Console.ReadLine();
            return(0);
        }
Ejemplo n.º 3
0
        public Service()
        {
            SQLiteClass sql   = new SQLiteClass(SQLiteClass.ProgramFilesx86() + "\\DuckServer\\Information.dat");
            MSSQL       mSSQL = new MSSQL();

            sql.NewServices(mSSQL.GetServices());
            Console.Title = "DuckServer";
            Console.WriteLine("----- DuckServer -----");
            Console.WriteLine("[{0}] Starting server...", DateTime.Now);
            ip = IPAddress.Parse(GetIPFromConfig());
            Console.WriteLine(ip.ToString());
            server = new TcpListener(ip, port);
            server.Start();
            Console.WriteLine("[{0}] Server is running properly on " + ip + ":" + port, DateTime.Now);

            Listen();
        }
Ejemplo n.º 4
0
        static String GetIPFromConfig()
        {
            string path = SQLiteClass.ProgramFilesx86() + "\\DuckServer\\config.cfg";

            if (!File.Exists(path) || new FileInfo(path).Length == 0)
            {
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine("127.0.0.1");
                }
            }

            string[] lines = File.ReadAllLines(path);
            foreach (string line in lines)
            {
                return(line);
            }
            return("127.0.0.1");
        }
Ejemplo n.º 5
0
        private static void RoutineCheck()
        {
            SQLiteClass sql = new SQLiteClass(SQLiteClass.ProgramFilesx86() + "\\DuckClient\\Information.dat");
            List <SQLiteClass.CachedMessage> cms = sql.GetCachedMessages();

            Console.WriteLine("Loading servers");
            ServiceConnectionDelegator.LoadServices();
            ServicesObject so = ServiceConnectionDelegator.GetService();

            if (so == null)
            {
                Console.WriteLine("Could not find a server");
            }
            else
            {
                Console.WriteLine("Using server: " + so.IPAddress);
            }
            IMClient.SendEvent(new Events("DEBUG000", "Debug message 0", 0, Service.GetIPFromConfig(), sql.GetGUID(), DateTime.Now));

            foreach (SQLiteClass.CachedMessage cm in cms)
            {
                try
                {
                    IMClient imc = new IMClient();
                    imc.setConnParams(so.IPAddress.Trim(), so.port);
                    Console.WriteLine("Sending a cached event to " + so.IPAddress);
                    imc.SetupConn();
                    imc.SendSignal((byte)cm.code, cm.message);
                    imc.Disconnect();
                    sql.RemoveCachedMessage(cm);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.Source);
                    Console.WriteLine(e.StackTrace);
                }
            }
        }
Ejemplo n.º 6
0
        public static String GetIPFromConfig()
        {
            SQLiteClass           sql  = new SQLiteClass(SQLiteClass.ProgramFilesx86() + "\\DuckClient\\information.dat");
            List <ServicesObject> sros = sql.GetServices();

            using (RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(@"SOFTWARE\DuckClient\Address", true))
            {
                if (key != null)
                {
                    Object o = key.GetValue("PrefIP");
                    if (o != null)
                    {
                        return(o.ToString());
                    }
                    else
                    {
                        Console.WriteLine("Registry entry was null");
                    }
                }
                else
                {
                    WriteKey("127.0.0.1");
                }
            }

            ServicesObject inUse = ServiceConnectionDelegator.GetService();

            Console.WriteLine(inUse);
            try
            {
                return(GetIPAddressLike(inUse.IPAddress));
            }
            catch
            {
            }

            Console.WriteLine("Returning default IP");
            return("127.0.0.1");
        }
Ejemplo n.º 7
0
 public static void SendEvent(Events ev)
 {
     try
     {
         ServicesObject so  = ServiceConnectionDelegator.GetService();
         IMClient       imc = new IMClient();
         imc.setConnParams(so.IPAddress.Trim(), so.port);
         imc.SetupConn();
         imc.SendSignal(IM_Event, Program.DoSerialize(ev));
         imc.Disconnect();
     }
     catch (SocketException e)
     {
         Console.WriteLine(e.Message);
         Console.WriteLine(e.Source);
         Console.WriteLine(e.StackTrace);
         SQLiteClass sql = new SQLiteClass(SQLiteClass.ProgramFilesx86() + "\\DuckClient\\Information.dat");
         sql.CacheMessage(IM_Event, Program.DoSerialize(ev));
     }
     catch
     {
     }
 }
Ejemplo n.º 8
0
        public static void LoadServices()
        {
            start = true;
            SQLiteClass sql = new SQLiteClass(SQLiteClass.ProgramFilesx86() + "\\DuckClient\\Information.dat");

            rawServices = sql.GetServices();

            spps = new List <ServicePlusStatus>();

            List <Thread> threads = new List <Thread>();

            foreach (ServicesObject so in rawServices)
            {
                Thread th = new Thread(() => HostScan.run(so));
                th.Start();
                threads.Add(th);
            }

            foreach (Thread th in threads)
            {
                th.Join();
            }
        }
Ejemplo n.º 9
0
        private static void RoutineCheck()
        {
            if (!MSSQL.ConnectionsExist())
            {
                Console.WriteLine("No database connections configured");
                return;
            }
            if (shouldIBeRunning && !runningScan)
            {
                runningScan = true;
                Console.WriteLine("Getting active hosts");
                List <GUIDMACVersionIP> gmvis = GetActiveHosts();
                Console.WriteLine(gmvis.Count + " running clients");
                MSSQL            ms  = new MSSQL();
                List <KnownHost> khs = ms.GetKnownHosts();

                foreach (GUIDMACVersionIP gmvi in gmvis)
                {
                    bool found = false;
                    foreach (KnownHost kh in khs)
                    {
                        bool change = false;
                        if (kh.GUID.Trim().ToUpper().Equals(gmvi.GUID.Trim().ToUpper()))
                        {
                            found = true;
                            if (!kh.hostMAC.Trim().ToUpper().Equals(gmvi.MAC.Trim().ToUpper()))
                            {
                                ms.AddEvent(new Events("HOST010", "MAC Address changed to " + gmvi.MAC, 2, gmvi.IP, gmvi.GUID, DateTime.Now));
                                kh.hostMAC = gmvi.MAC;

                                change = true;
                            }
                            if (!kh.version.Trim().ToUpper().Equals(gmvi.Version.Trim().ToUpper()))
                            {
                                ms.AddEvent(new Events("HOST020", "Version changed to " + gmvi.Version, 1, gmvi.IP, gmvi.GUID, DateTime.Now));
                                kh.version = gmvi.Version;

                                change = true;
                            }
                            if (!kh.hostIP.Trim().ToUpper().Equals(gmvi.IP.Trim().ToUpper()))
                            {
                                Console.WriteLine(kh.hostIP.Trim().ToUpper() + " " + gmvi.IP.Trim().ToUpper());
                                ms.AddEvent(new Events("HOST030", "IP Changed to " + gmvi.IP, 1, gmvi.IP, gmvi.GUID, DateTime.Now));
                                kh.hostIP = gmvi.IP;

                                change = true;
                            }
                            if (change)
                            {
                                ms.UpdateHost(kh);
                            }
                            break;
                        }
                    }
                    if (!found)
                    {
                        try
                        {
                            Console.WriteLine("New host " + gmvi.GUID);
                            IMClient imc = new IMClient();
                            imc.setConnParams(gmvi.IP, 25567);
                            imc.SetupConn();
                            imc.SendSignal(ServiceConn.IM_RegistrationDone, gmvi.GUID);
                            imc.Disconnect();
                            ms.AddEvent(new Events("HOST100", "New host added", 2, gmvi.IP, gmvi.GUID, DateTime.Now));
                            ms.AddKnownHost(new KnownHost(gmvi.MAC, gmvi.IP, gmvi.Version, DateTime.Now, gmvi.GUID, gmvi.Hostname));
                        }
                        catch
                        {
                        }
                    }
                }
                runningScan = false;
                //Version broadcast

                ms.GetWhitelists();

                SQLiteClass sql = new SQLiteClass(SQLiteClass.ProgramFilesx86() + "\\DuckServer\\Information.dat");
                sql.NewServices(ms.GetServices());
                sql.SetLastUpdated(SQLiteClass.GET_SERVICE_LIST, ms.GetLastUpdated(SQLiteClass.GET_SERVICE_LIST));
                sql.NewWhitelists(ms.GetWhitelists());
                sql.SetLastUpdated(SQLiteClass.GET_WHITELIST_LIST, ms.GetLastUpdated(SQLiteClass.GET_WHITELIST_LIST));

                DateTime serviceTime   = sql.GetLastUpdated(SQLiteClass.GET_SERVICE_LIST);
                DateTime whitelistTime = sql.GetLastUpdated(SQLiteClass.GET_WHITELIST_LIST);

                foreach (GUIDMACVersionIP gmvi in gmvis)
                {
                    IMClient imclient = new IMClient();
                    imclient.setConnParams(gmvi.IP, 25567);
                    Thread th = new Thread(() => SendNewDataVersion(imclient, serviceTime, whitelistTime));
                    th.Start();
                }
            }

            shouldIBeRunning = ServiceConcurrencyCheck();
            Console.WriteLine("The service concurrency check returns that I should be running: " + shouldIBeRunning);
        }