Ejemplo n.º 1
0
        public void Commit()
        {
            //Maybe make a receipt?
            SQLiteClass sql = new SQLiteClass();

            sql.NewConnections(clean);
            //Get all services and ports
            List <ServicesObject> svcs = sql.GetServices();

            foreach (ServicesObject so in svcs)
            {
                try
                {
                    IMClient im = new IMClient();
                    im.setConnParams(so.IPAddress, so.port);
                    im.SetupConn();

                    im.SendSignal((byte)IMClient.IM_NewDatabases, DoSerialize(clean));
                    im.Disconnect();
                }
                catch (System.Net.Sockets.SocketException)
                {
                }
            }

            sql.CloseCon();
        }
Ejemplo n.º 2
0
        public static void run(ServicesObject so)
        {
            List <int> toRet = new List <int>();

            try
            {
                using (TcpClient client = new TcpClient())
                {
                    var result = client.BeginConnect(so.IPAddress.Trim(), so.port, null, null);

                    var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(4));

                    if (success)
                    {
                        IMClient imc = new IMClient();
                        imc.setConnParams(so.IPAddress.Trim(), so.port);
                        try
                        {
                            imc.SetupConn();
                            imc.SendSignal(ServiceConn.IM_Diagnostic, "yes");
                            imc.Disconnect();
                            ServiceConnectionDelegator.spps.Add(new ServicePlusStatus {
                                service = so, Status = ServicesObject.STATUS_ONLINE
                            });
                        }
                        catch (Exception)
                        {
                            ServiceConnectionDelegator.spps.Add(new ServicePlusStatus {
                                service = so, Status = ServicesObject.STATUS_OFFLINE
                            });
                        }
                    }
                    else
                    {
                        ServiceConnectionDelegator.spps.Add(new ServicePlusStatus {
                            service = so, Status = ServicesObject.STATUS_OFFLINE
                        });
                    }
                }
            }
            catch (SocketException s)
            {
                ServiceConnectionDelegator.spps.Add(new ServicePlusStatus {
                    service = so, Status = ServicesObject.STATUS_OFFLINE
                });
            }
            catch (Exception e)
            {
                ServiceConnectionDelegator.spps.Add(new ServicePlusStatus {
                    service = so, Status = ServicesObject.STATUS_OFFLINE
                });
                Console.WriteLine(e.Message);
                Console.WriteLine(e.Source);
                Console.WriteLine(e.StackTrace);
            }
        }
Ejemplo n.º 3
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.º 4
0
        public static VersionGUIDPair GetVersionAndGUID()
        {
            IMClient iM = new IMClient();

            iM.setConnParams(ServiceConnectionDelegator.GetService().IPAddress, 25567);
            iM.SetupConn();
            String aguid = iM.RequestParam(IMClient.IM_GetIdentity);

            iM.Disconnect();
            iM.SetupConn();
            String aversion = iM.RequestParam(IMClient.IM_GetVersion);

            return(new VersionGUIDPair {
                Version = aversion, GUID = aguid
            });
        }
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
        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);
        }
Ejemplo n.º 7
0
        private void HandleLog(int mode)
        {
            SQLiteClass sql = new SQLiteClass(ProgramFilesx86() + "\\DuckClient\\Information.dat");

            switch (mode)
            {
            case IM_GetIdentity:
                bw.Write(sql.GetGUID());
                break;

            case IM_GetVersion:
                bw.Write(sql.GetVersion());
                break;

            case IM_GetMAC:
                var s = MACFinder.getMacByIp(GetIPAddress());
                bw.Write(s);
                break;

            case IM_GetHostname:
                bw.Write(Dns.GetHostName());
                break;

            case IM_RegistrationDone:
                if (br.ReadString().Equals(sql.GetGUID()))
                {
                    sql.SetRegistered(true);
                }
                break;

            case IM_NewVersionsCheck:
                DateTimeVersions dtv = DeserializeXMLFileToObject <DateTimeVersions>(br.ReadString());
                if (DateTime.Compare(dtv.ServiceDateTime, sql.GetLastUpdated(SQLiteClass.GET_SERVICE_LIST)) > 0 || DateTime.Compare(dtv.WhitelistDateTime, sql.GetLastUpdated(SQLiteClass.GET_WHITELIST_LIST)) > 0)
                {
                    IMClient imc = new IMClient();
                    imc.setConnParams(GetOriginIP(), 25568);
                    imc.SetupConn();
                    imc.SendSignal(IM_NewVersions, "2");
                    imc.Disconnect();
                }
                break;

            case IM_NewVersions:
                DateTimeVersions dtv1 = DeserializeXMLFileToObject <DateTimeVersions>(br.ReadString());
                UpdateStuff(dtv1);
                break;

            case IM_NewServiceList:
                List <ServicesObject> sros = DeserializeXMLFileToObject <List <ServicesObject> >(br.ReadString());
                sql.NewServices(sros);
                break;

            case IM_NewWhitelists:
                List <Whitelists> wls = DeserializeXMLFileToObject <List <Whitelists> >(br.ReadString());
                sql.NewWhitelists(wls);
                break;

            case IM_AddWhiteList:
                break;

            case IM_RemoveWhitelist:
                break;

            case IM_Debug:
                Console.WriteLine(br.ReadString());
                bw.Write(IM_OK);
                break;

            default:
                break;
            }
            bw.Write(IM_OK);
        }