Beispiel #1
0
	void HttpServer::slotSocketReadyToRead()
	{
		QSocket* client = (QSocket*)sender();
		HttpClientHandler* handler = clients.find(client);
		if (!handler)
		{
			client->deleteLater();
			return;
		}
		
		handler->readyToRead();
	}
void BServerSocket::onConnectionClosed()
{
	QSocket *s = (QSocket *)sender();
	qDebug("Closed here");

//	system("echo testing > /tmp/serverini");
	QSqlQuery query(QString("SELECT computer_logout('") + s->name() + QString("');"), db);
	
	s->close();
	s->deleteLater();
	
}
void BServerSocket::onReadyRead()
{
	QSocket *s = (QSocket *)sender();
	if (s->canReadLine())
	{
		QString line = s->readLine();
		qDebug(line);
		line.remove("\n");
		line.remove("\r");
		if (line == "ping")
			writeLineToSocket("pong",s);

		QSqlQuery query(db);
		QString sql;
		QString ipaddress;
		QString username;
		QStringList tokens = QStringList::split(" ",line);
		QString iptablescmd = "iptables ";
		switch (convert(tokens[0]))
		{
		case SN_1 :
			// tokens[1] -> ip / user@ip, tokens[2]-> menit (15,30,60,90,120,99999999), tokens[3]-> tipe (INT,KTK)
			//masukkan ke database karena ada yang login
			//cek disini validitas data klo sala kembalikan pesan error ER_1 / "ER01"
			//send ke client OK_1 / "OK01"
			
			if (tokens[1].contains("@"))
			{
				username = tokens[1].section("@",0,0);
				ipaddress = tokens[1].section("@",1,1); 
				if (tokens[3]=="INT")
				{	
					//iptablescmd += "-D -o $FREEBILLING_IFACE -p tcp -m owner --uid-owner=";
					//iptablescmd += username; /* ganti ke uid */ 
					//iptablescms += " -j DROP";
					//printf("%s\n",iptablescmd.ascii());
					//system (iptablescmd);
				} else
				{
					
					//iptablescmd += "-A -o $FREEBILLING_IFACE -p tcp -m owner --uid-owner=";
					//iptablescmd += username; /* ganti ke uid */ 
					//iptablescmd += " -j DROP";	
					//printf("%s\n",iptablescmd.ascii());
					//system (iptablescmd);
				}


			} else
			{
				
				ipaddress = tokens[1];
				if (tokens[3]=="INT")
				{	
					iptablescmd += "-A POSTROUTING -t nat -s ";
					iptablescmd += ipaddress;
					iptablescmd += " -o $FREEBILLING_IFACE -j MASQUERADE";
					printf("%s\n",iptablescmd.ascii());
					system (iptablescmd);
				} else
				{
					iptablescmd += "-D POSTROUTING -t nat -s ";
					iptablescmd += ipaddress;
					iptablescmd += " -o $FREEBILLING_IFACE -j MASQUERADE";	
					printf("%s\n",iptablescmd.ascii());
					system (iptablescmd);
				}
			}
			
			s->setName(ipaddress);
			
			sql = "SELECT computer_login('" + ipaddress + "','" + tokens[3] + "'," + tokens[2] + ");";

			query.exec(sql);	
			
			writeLineToSocket("OK01",s);
		break;
		case SN_4 :
			query.exec(QString("SELECT computer_logout('") + s->name() + QString("');"));
			s->close();
			s->deleteLater();
		break;
		case CS_1 :
			//resend this message to client
			this->writeLineToSocket("CS01",(QSocket *)this->child(tokens[1]));
			s->close();
			s->deleteLater();
		break;
		}
	}
}