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 }); }
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(); }
private static void SendNewDataVersion(IMClient imclient, DateTime st, DateTime wt) { imclient.SetupConn(); imclient.SendSignal(ServiceConn.IM_NewVersionsCheck, DoSerialize(new DateTimeVersions { ServiceDateTime = st, WhitelistDateTime = wt })); }
public void SendEvent(Events ev) { String toSend = DoSerialize(ev); IMClient im = new IMClient(); im.setConnParams(ServiceConnectionDelegator.GetService().IPAddress, 25567); im.SetupConn(); im.SendSignal(IMClient.IM_Event, toSend); }
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); } }
public static void SendKnownHost(KnownHost kh) { //Send 1 Debug message IMClient im = new IMClient(); im.setConnParams(ServiceConnectionDelegator.GetService().IPAddress, 25567); im.SetupConn(); Console.WriteLine(DoSerialize(kh)); im.SendSignal(IMClient.IM_NewIdentity, DoSerialize(kh)); }
public void SendDebugEvent() { Events ev = new Events("Debug000", "The debug message", 2, "123.123.123.123", "hhhhhhhh", DateTime.Now); String toSend = DoSerialize(ev); Console.WriteLine(toSend); //Send 1 Debug message IMClient im = new IMClient(); im.setConnParams(ServiceConnectionDelegator.GetService().IPAddress, 25567); im.SetupConn(); im.SendSignal(IMClient.IM_Event, toSend); }
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); }
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); } } }
private static List <GUIDMACVersionIP> GetActiveHosts() { MSSQL ms = new MSSQL(); List <String> ips = ms.GetIPs(); int max = ips.Count; List <GUIDMACVersionIP> gmvis = new List <GUIDMACVersionIP>(); IPPS = new List <IPPlusStatus>(); List <Thread> threads = new List <Thread>(); foreach (String ip in ips) { Thread th = new Thread(() => HostScan.run(ip, 25567)); th.Start(); threads.Add(th); } foreach (Thread th in threads) { th.Join(); } //The response of every ping for (int i = 0; i <= count; i++) { try { IPPlusStatus iss = IPPS[i]; if (iss.Status == KnownHost.STATE_ONLINE) { IMClient iMC = new IMClient(); iMC.setConnParams(iss.IP, 25567); iMC.SetupConn(); string guid = iMC.RequestParam(IMClient.IM_GetIdentity); iMC.CloseConn(); iMC.SetupConn(); string version = iMC.RequestParam(IMClient.IM_GetVersion); iMC.CloseConn(); iMC.SetupConn(); string mac = iMC.RequestParam(IMClient.IM_GetMAC); iMC.CloseConn(); iMC.SetupConn(); string hostname = iMC.RequestParam(IMClient.IM_GetHostname); GUIDMACVersionIP gmvi = new GUIDMACVersionIP { GUID = guid, IP = iss.IP, MAC = mac, Version = version, Hostname = hostname }; gmvis.Add(gmvi); } } catch (Exception) { } } return(gmvis); }
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); }
private void HandleLog(int mode) { SQLiteClass sql = new SQLiteClass(ProgramFilesx86() + "\\DuckServer\\Information.dat"); MSSQL msl; if (!Service.shouldIBeRunning) { Console.WriteLine("This server instance should not be handling requests now."); } switch (mode) { case IM_Event: if (Service.shouldIBeRunning) { String eventString = br.ReadString(); Events ev = DeserializeXMLFileToObject <Events>(eventString); sql.CloseCon(); msl = new MSSQL(); msl.AddEvent(ev); bw.Write(IM_OK); } else { bw.Write(IM_Bad_Credentials); } break; case IM_NewVersions: if (Service.shouldIBeRunning) { IMClient imc = new IMClient(); imc.setConnParams(GetOriginIP(), 25567); imc.SetupConn(); imc.SendSignal(IM_NewVersions, Service.DoSerialize(new DateTimeVersions { ServiceVersion = sql.GetServices(), WhitelistVersion = sql.GetWhitelists(), ServiceDateTime = sql.GetLastUpdated(SQLiteClass.GET_SERVICE_LIST), WhitelistDateTime = sql.GetLastUpdated(SQLiteClass.GET_WHITELIST_LIST) })); bw.Write(IM_OK); } else { bw.Write(IM_Bad_Credentials); } break; case IM_AddDatabases: sql.AddDatabase(new DatabaseObject(br.ReadString(), br.ReadInt32())); bw.Write(IM_OK); break; case IM_GetDatabases: List <DatabaseObject> dbos = sql.GetConnections(); foreach (DatabaseObject dbo in dbos) { bw.Write(dbo.ConnectionString); bw.Write(dbo.Preference); } bw.Write(IM_OK); sql.CloseCon(); break; case IM_NewDatabases: List <DatabaseObject> dbs = DeserializeXMLFileToObject <List <DatabaseObject> >(br.ReadString()); sql.NewConnections(dbs); sql.CloseCon(); break; case IM_NewServiceList: List <ServicesObject> sros = DeserializeXMLFileToObject <List <ServicesObject> >(br.ReadString()); sql.NewServices(sros); sql.CloseCon(); //Port scan //Broadcast break; case IM_NewWhitelists: List <Whitelists> wls = DeserializeXMLFileToObject <List <Whitelists> >(br.ReadString()); sql.NewWhitelists(wls); sql.CloseCon(); break; case IM_AddWhiteList: break; case IM_RemoveWhitelist: break; case IM_Debug: Console.WriteLine(br.ReadString()); break; case IM_Diagnostic: if (!Service.shouldIBeRunning) { bw.Write(IM_Bad_Credentials); } else { bw.Write(IM_OK); } break; default: break; } bw.Write(IM_OK); }
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); }