2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23 $Date: 2010/03/15 12:58:17 $
33 #include "stg/common.h"
38 extern IA_CLIENT_PROT * clnp;
40 #define LISTEN_PORT (5580)
44 //---------------------------------------------------------------------------
49 sigfillset(&signalSet);
50 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
53 unsigned long WINAPI RunWeb(void *)
60 //---------------------------------------------------------------------------
69 m_res = WSAStartup(MAKEWORD(2,0), &m_wsaData);
72 for (int i = 0; i < DIR_NUM; i++)
77 memset(&m_ls, 0, sizeof(m_ls));
79 //---------------------------------------------------------------------------
85 NULL, // pointer to thread security attributes
86 16384, // initial thread stack size, in bytes
87 RunWeb, // pointer to thread function
88 NULL, // argument for new thread
89 0, // CREATE_SUSPENDED, // creation flags
90 &pt // pointer to returned thread identifier
93 pthread_create(&m_thread, NULL, RunWeb, NULL);
96 //---------------------------------------------------------------------------
97 void WEB::PrepareNet()
99 m_listenSocket = socket(PF_INET, SOCK_STREAM, 0);
101 struct sockaddr_in listenAddr;
102 listenAddr.sin_family = AF_INET;
103 listenAddr.sin_port = htons(LISTEN_PORT);
104 listenAddr.sin_addr.s_addr = m_listenWebAddr;
108 if (0 != setsockopt(m_listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
110 printf("Setsockopt Fail\n");
111 printf(">>> Error %s\n", strerror(errno));
118 m_res = bind(m_listenSocket, reinterpret_cast<sockaddr*>(&listenAddr), sizeof(listenAddr));
122 printf("Bind failed.\n");
126 m_res = listen(m_listenSocket, 0);
129 printf("Listen failed.\n");
133 //---------------------------------------------------------------------------
134 void WEB::SetRefreshPagePeriod(int p)
137 if (m_refreshPeriod <= 0 || m_refreshPeriod > 24*3600)
140 //---------------------------------------------------------------------------
141 void WEB::SetListenAddr(uint32_t ip)
143 m_listenWebAddr = ip;
145 //---------------------------------------------------------------------------
149 char recvBuffer[4096];
152 struct sockaddr_in outerAddr;
155 socklen_t outerAddrLen = sizeof(outerAddr);
157 int outerAddrLen = sizeof(outerAddr);
160 m_outerSocket = accept(m_listenSocket, reinterpret_cast<sockaddr*>(&outerAddr), &outerAddrLen);
161 if (m_outerSocket == -1)
163 printf(">>> Error %s\n", strerror(errno));
166 recv(m_outerSocket, recvBuffer, sizeof(recvBuffer), 0);
168 if (strncmp(recvBuffer, "GET /sgauth.css", strlen("GET /sgauth.css")) == 0)
171 //printf("(1) recvBuffer=%s\n", recvBuffer);
173 else if (strncmp(recvBuffer, "GET /disconnect", strlen("GET /disconnect")) == 0)
177 //printf("(2) recvBuffer=%s\n", recvBuffer);
179 else if (strncmp(recvBuffer, "GET /connect", strlen("GET /connect")) == 0)
183 //printf("(3) recvBuffer=%s\n", recvBuffer);
185 else if (strncmp(recvBuffer, "GET /exit", strlen("GET /exit")) == 0)
192 struct timespec ts = {1, 0};
193 nanosleep(&ts, NULL);
200 //printf("(4) recvBuffer=%s\n", recvBuffer);
204 closesocket(m_outerSocket);
206 close(m_outerSocket);
210 //---------------------------------------------------------------------------
211 int WEB::Redirect(const char * url)
213 const char * redirect =
215 "Content-Type: text/html\n"
220 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;%s\">\n"
223 "</body></html>\n\n";
226 sprintf(buff, redirect, url);
227 send(m_outerSocket, buff, strlen(buff), 0);
231 //---------------------------------------------------------------------------
236 const char * replyHeader =
238 "Content-Type: text/html\n"
243 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"%d\">\n"
244 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1251\">\n"
245 "<title>sgauth</title>\n"
246 "<link rel=\"Stylesheet\" href=\"sgauth.css\">"
249 "<H3>Stargazer</H3><p>\n";
251 const char * replyFooter = "</body></html>\n\n";
253 char replyHeaderBuffer[2000];
254 sprintf(replyHeaderBuffer, replyHeader, m_refreshPeriod);
256 send(m_outerSocket, replyHeaderBuffer, strlen(replyHeaderBuffer), 0);
260 int st = clnp->GetAuthorized();
262 sprintf(str, "<a href=\"connect\">%s</a><p>\n", gettext("Connect"));
263 m_res = send(m_outerSocket, str, strlen(str), 0);
265 sprintf(str, "<a href=\"disconnect\">%s</a><p>\n", gettext("Disconnect"));
266 m_res = send(m_outerSocket, str, strlen(str), 0);
268 sprintf(str, "<a href=\"/\">%s</a><p>\n", gettext("Refresh"));
269 m_res = send(m_outerSocket, str, strlen(str), 0);
271 sprintf(str, "<a href=\"exit\">%s</a><p>\n", gettext("Exit"));
272 m_res = send(m_outerSocket, str, strlen(str), 0);
274 sprintf(str, "<div id=\"%s\">%s</div><p>\n" , st ? "ConnectionStateOnline":"ConnectionStateOffline", st ? "Online":"Offline");
275 m_res = send(m_outerSocket, str, strlen(str), 0);
277 sprintf(str, "<div id=\"Cash\">%s: %.3f</div><p>\n" , gettext("Cash"), m_ls.cash / 1000.0);
278 m_res = send(m_outerSocket, str, strlen(str), 0);
280 sprintf(str, "<div id=\"Prepaid Traffic\">%s: %s</div><p>\n" ,
281 gettext("PrepaidTraffic"),
282 m_ls.freeMb[0] == 'C' ? m_ls.freeMb + 1 : m_ls.freeMb);
283 m_res = send(m_outerSocket, str, strlen(str), 0);
285 sprintf(str, "<TABLE id=\"TraffTable\">\n");
286 m_res = send(m_outerSocket, str, strlen(str), 0);
287 sprintf(str, " <TR id=\"TraffTableCaptionRow\">\n");
288 m_res = send(m_outerSocket, str, strlen(str), 0);
289 sprintf(str, " <TD id=\"TraffTableCaptionCellC\"> </TD>\n");
290 m_res = send(m_outerSocket, str, strlen(str), 0);
293 for (j = 0; j < DIR_NUM; j++)
295 if (m_dirName[j][0] == 0)
298 KOIToWin(m_dirName[j], &s);// +++++++++ sigsegv ========== TODO too long dir name crashes sgauth
299 sprintf(str, " <TD id=\"TraffTableCaptionCell%d\">%s</TD>\n", rowNum++, s.c_str());
300 send(m_outerSocket, str, strlen(str), 0);
303 sprintf(str," </TR>\n");
304 send(m_outerSocket, str, strlen(str), 0);
306 sprintf(str," <TR id=\"TraffTableUMRow\">\n");
307 send(m_outerSocket, str, strlen(str), 0);
309 sprintf(str," <TD id=\"TraffTableUMCellC\">%s</TD>\n", gettext("Month Upload"));
310 send(m_outerSocket, str, strlen(str), 0);
313 for (j = 0; j < DIR_NUM; j++)
315 if (m_dirName[j][0] == 0)
317 sprintf(str," <TD id=\"TraffTableUMCell%d\">%s</TD>\n", rowNum++, IntToKMG(m_ls.mu[j], ST_F));
318 m_res = send(m_outerSocket, str, strlen(str), 0);
321 sprintf(str," </TR>\n");
322 m_res = send(m_outerSocket, str, strlen(str), 0);
323 sprintf(str," <TR id=\"TraffTableDMRow\">\n");
324 m_res = send(m_outerSocket, str, strlen(str), 0);
325 sprintf(str," <TD id=\"TraffTableDMCellC\">%s</TD>\n", gettext("Month Download"));
326 m_res = send(m_outerSocket, str, strlen(str), 0);
329 for (j = 0; j < DIR_NUM; j++)
331 if (m_dirName[j][0] == 0)
333 sprintf(str," <TD id=\"TraffTableDMCell%d\">%s</TD>\n", rowNum++, IntToKMG(m_ls.md[j], ST_F));
334 m_res = send(m_outerSocket, str, strlen(str), 0);
336 sprintf(str," </TR>\n");
337 m_res = send(m_outerSocket, str, strlen(str), 0);
340 sprintf(str," <TR id=\"TraffTableUSRow\">\n");
341 m_res = send(m_outerSocket, str, strlen(str), 0);
342 sprintf(str," <TD id=\"TraffTableUSCellC\">%s</TD>\n", gettext("Session Upload"));
343 m_res = send(m_outerSocket, str, strlen(str), 0);
346 for (j = 0; j < DIR_NUM; j++)
348 if (m_dirName[j][0] == 0)
350 sprintf(str," <TD id=\"TraffTableUSCell%d\">%s</TD>\n", rowNum++, IntToKMG(m_ls.su[j], ST_F));
351 m_res = send(m_outerSocket, str, strlen(str), 0);
354 sprintf(str," </TR>\n");
355 m_res = send(m_outerSocket, str, strlen(str), 0);
356 sprintf(str," <TR id=\"TraffTableDSRow\">\n");
357 m_res = send(m_outerSocket, str, strlen(str), 0);
358 sprintf(str," <TD id=\"TraffTableDSCellC\">%s</TD>\n", gettext("Session Download"));
359 m_res = send(m_outerSocket, str, strlen(str), 0);
361 for (j = 0; j < DIR_NUM; j++)
363 if (m_dirName[j][0] == 0)
365 sprintf(str," <TD id=\"TraffTableDSCell%d\">%s</TD>\n", j, IntToKMG(m_ls.sd[j], ST_F));
366 m_res = send(m_outerSocket, str, strlen(str), 0);
369 sprintf(str," </TR>\n");
370 m_res = send(m_outerSocket, str, strlen(str), 0);
372 sprintf(str,"</TABLE>\n");
373 m_res = send(m_outerSocket, str, strlen(str), 0);
376 if (!m_messages.empty())
378 sprintf(str," <TABLE id=\"MessagesTable\">\n");
379 m_res = send(m_outerSocket, str, strlen(str), 0);
381 sprintf(str," <TR id=\"MessagesTableRowC\">\n");
382 send(m_outerSocket, str, strlen(str), 0);
383 sprintf(str," <TD>Date</TD>\n");
384 send(m_outerSocket, str, strlen(str), 0);
385 sprintf(str," <TD>Text</TD>\n");
386 send(m_outerSocket, str, strlen(str), 0);
387 sprintf(str," </TR>\n");
388 send(m_outerSocket, str, strlen(str), 0);
390 std::list<STG_MESSAGE>::reverse_iterator it;
391 it = m_messages.rbegin();
392 while (it != m_messages.rend())
394 sprintf(str," <TR id=\"MessagesTableRow%d\">\n", rowNum);
395 send(m_outerSocket, str, strlen(str), 0);
396 sprintf(str," <TD>%s</TD>\n", it->recvTime.c_str());
397 send(m_outerSocket, str, strlen(str), 0);
398 sprintf(str," <TD>%s</TD>\n", it->msg.c_str());
399 send(m_outerSocket, str, strlen(str), 0);
400 sprintf(str," </TR>\n");
401 send(m_outerSocket, str, strlen(str), 0);
406 sprintf(str," </TABLE>\n");
407 m_res = send(m_outerSocket, str, strlen(str), 0);
410 time_t t = time(NULL);
411 sprintf(str,"Îáíîâëåíî: %s</b>" , ctime(&t));
412 m_res = send(m_outerSocket, str, strlen(str), 0);
414 send(m_outerSocket, replyFooter, strlen(replyFooter), 0);
418 //---------------------------------------------------------------------------
421 const char * replyHeader =
423 "Content-Type: text/css\n"
424 "Connection: close\n\n";
426 const char * replyFooter= "\n\n";
428 send(m_outerSocket, replyHeader, strlen(replyHeader), 0);
429 send(m_outerSocket, SGAuth::css, strlen(SGAuth::css), 0);
430 send(m_outerSocket, replyFooter, strlen(replyFooter), 0);
434 //---------------------------------------------------------------------------
435 void WEB::SetDirName(const std::string & dn, int n)
437 web->m_dirName[n] = dn;
439 //---------------------------------------------------------------------------
440 void WEB::AddMessage(const std::string & message, int type)
442 time_t t = time(NULL);
447 m.recvTime = ctime(&t);
449 m_messages.push_back(m);
451 if (m_messages.size() > MAX_MESSAGES)
452 m_messages.pop_front();
455 //---------------------------------------------------------------------------
456 void WEB::UpdateStat(const LOADSTAT & ls)
458 memcpy(&m_ls, &ls, sizeof(LOADSTAT));
460 //---------------------------------------------------------------------------