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 res = WSAStartup(MAKEWORD(2,0), &wsaData);
72 for (int i = 0; i < DIR_NUM; i++)
77 memset(&ls, 0, sizeof(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(&thread, NULL, RunWeb, NULL);
96 //---------------------------------------------------------------------------
97 void WEB::PrepareNet()
99 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 = listenWebAddr;
108 if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
110 printf("Setsockopt Fail\n");
111 printf(">>> Error %s\n", strerror(errno));
118 res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
122 printf("Bind failed.\n");
126 res = listen(listenSocket, 0);
129 printf("Listen failed.\n");
133 //---------------------------------------------------------------------------
134 void WEB::SetRefreshPagePeriod(int p)
137 if (refreshPeriod <= 0 || refreshPeriod > 24*3600)
140 //---------------------------------------------------------------------------
141 void WEB::SetListenAddr(uint32_t ip)
145 //---------------------------------------------------------------------------
149 char recvBuffer[4096];
152 struct sockaddr_in outerAddr;
155 socklen_t outerAddrLen = sizeof(outerAddr);
157 int outerAddrLen = sizeof(outerAddr);
160 outerSocket = accept(listenSocket, (struct sockaddr*)&outerAddr, &outerAddrLen);
161 if (outerSocket == -1)
163 printf(">>> Error %s\n", strerror(errno));
166 recv(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(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(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, refreshPeriod);
256 send(outerSocket, replyHeaderBuffer, strlen(replyHeaderBuffer), 0);
260 int st = clnp->GetAuthorized();
262 sprintf(str, "<a href=\"connect\">%s</a><p>\n", gettext("Connect"));
263 res = send(outerSocket, str, strlen(str), 0);
265 sprintf(str, "<a href=\"disconnect\">%s</a><p>\n", gettext("Disconnect"));
266 res = send(outerSocket, str, strlen(str), 0);
268 sprintf(str, "<a href=\"/\">%s</a><p>\n", gettext("Refresh"));
269 res = send(outerSocket, str, strlen(str), 0);
271 sprintf(str, "<a href=\"exit\">%s</a><p>\n", gettext("Exit"));
272 res = send(outerSocket, str, strlen(str), 0);
274 sprintf(str, "<div id=\"%s\">%s</div><p>\n" , st ? "ConnectionStateOnline":"ConnectionStateOffline", st ? "Online":"Offline");
275 res = send(outerSocket, str, strlen(str), 0);
277 sprintf(str, "<div id=\"Cash\">%s: %.3f</div><p>\n" , gettext("Cash"), ls.cash / 1000.0);
278 res = send(outerSocket, str, strlen(str), 0);
280 sprintf(str, "<div id=\"Prepaid Traffic\">%s: %s</div><p>\n" ,
281 gettext("PrepaidTraffic"),
282 ls.freeMb[0] == 'C' ? ls.freeMb + 1 : ls.freeMb);
283 res = send(outerSocket, str, strlen(str), 0);
285 sprintf(str, "<TABLE id=\"TraffTable\">\n");
286 res = send(outerSocket, str, strlen(str), 0);
287 sprintf(str, " <TR id=\"TraffTableCaptionRow\">\n");
288 res = send(outerSocket, str, strlen(str), 0);
289 sprintf(str, " <TD id=\"TraffTableCaptionCellC\"> </TD>\n");
290 res = send(outerSocket, str, strlen(str), 0);
293 for (j = 0; j < DIR_NUM; j++)
295 if (dirName[j][0] == 0)
298 KOIToWin(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(outerSocket, str, strlen(str), 0);
303 sprintf(str," </TR>\n");
304 send(outerSocket, str, strlen(str), 0);
306 sprintf(str," <TR id=\"TraffTableUMRow\">\n");
307 send(outerSocket, str, strlen(str), 0);
309 sprintf(str," <TD id=\"TraffTableUMCellC\">%s</TD>\n", gettext("Month Upload"));
310 send(outerSocket, str, strlen(str), 0);
313 for (j = 0; j < DIR_NUM; j++)
315 if (dirName[j][0] == 0)
317 sprintf(str," <TD id=\"TraffTableUMCell%d\">%s</TD>\n", rowNum++, IntToKMG(ls.mu[j], ST_F));
318 res = send(outerSocket, str, strlen(str), 0);
321 sprintf(str," </TR>\n");
322 res = send(outerSocket, str, strlen(str), 0);
323 sprintf(str," <TR id=\"TraffTableDMRow\">\n");
324 res = send(outerSocket, str, strlen(str), 0);
325 sprintf(str," <TD id=\"TraffTableDMCellC\">%s</TD>\n", gettext("Month Download"));
326 res = send(outerSocket, str, strlen(str), 0);
329 for (j = 0; j < DIR_NUM; j++)
331 if (dirName[j][0] == 0)
333 sprintf(str," <TD id=\"TraffTableDMCell%d\">%s</TD>\n", rowNum++, IntToKMG(ls.md[j], ST_F));
334 res = send(outerSocket, str, strlen(str), 0);
336 sprintf(str," </TR>\n");
337 res = send(outerSocket, str, strlen(str), 0);
340 sprintf(str," <TR id=\"TraffTableUSRow\">\n");
341 res = send(outerSocket, str, strlen(str), 0);
342 sprintf(str," <TD id=\"TraffTableUSCellC\">%s</TD>\n", gettext("Session Upload"));
343 res = send(outerSocket, str, strlen(str), 0);
346 for (j = 0; j < DIR_NUM; j++)
348 if (dirName[j][0] == 0)
350 sprintf(str," <TD id=\"TraffTableUSCell%d\">%s</TD>\n", rowNum++, IntToKMG(ls.su[j], ST_F));
351 res = send(outerSocket, str, strlen(str), 0);
354 sprintf(str," </TR>\n");
355 res = send(outerSocket, str, strlen(str), 0);
356 sprintf(str," <TR id=\"TraffTableDSRow\">\n");
357 res = send(outerSocket, str, strlen(str), 0);
358 sprintf(str," <TD id=\"TraffTableDSCellC\">%s</TD>\n", gettext("Session Download"));
359 res = send(outerSocket, str, strlen(str), 0);
361 for (j = 0; j < DIR_NUM; j++)
363 if (dirName[j][0] == 0)
365 sprintf(str," <TD id=\"TraffTableDSCell%d\">%s</TD>\n", j, IntToKMG(ls.sd[j], ST_F));
366 res = send(outerSocket, str, strlen(str), 0);
369 sprintf(str," </TR>\n");
370 res = send(outerSocket, str, strlen(str), 0);
372 sprintf(str,"</TABLE>\n");
373 res = send(outerSocket, str, strlen(str), 0);
376 if (!messages.empty())
378 sprintf(str," <TABLE id=\"MessagesTable\">\n");
379 res = send(outerSocket, str, strlen(str), 0);
381 sprintf(str," <TR id=\"MessagesTableRowC\">\n");
382 send(outerSocket, str, strlen(str), 0);
383 sprintf(str," <TD>Date</TD>\n");
384 send(outerSocket, str, strlen(str), 0);
385 sprintf(str," <TD>Text</TD>\n");
386 send(outerSocket, str, strlen(str), 0);
387 sprintf(str," </TR>\n");
388 send(outerSocket, str, strlen(str), 0);
390 std::list<STG_MESSAGE>::reverse_iterator it;
391 it = messages.rbegin();
392 while (it != messages.rend())
394 sprintf(str," <TR id=\"MessagesTableRow%d\">\n", rowNum);
395 send(outerSocket, str, strlen(str), 0);
396 sprintf(str," <TD>%s</TD>\n", it->recvTime.c_str());
397 send(outerSocket, str, strlen(str), 0);
398 sprintf(str," <TD>%s</TD>\n", it->msg.c_str());
399 send(outerSocket, str, strlen(str), 0);
400 sprintf(str," </TR>\n");
401 send(outerSocket, str, strlen(str), 0);
406 sprintf(str," </TABLE>\n");
407 res = send(outerSocket, str, strlen(str), 0);
410 time_t t = time(NULL);
411 sprintf(str,"Îáíîâëåíî: %s</b>" , ctime(&t));
412 res = send(outerSocket, str, strlen(str), 0);
414 send(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(outerSocket, replyHeader, strlen(replyHeader), 0);
429 send(outerSocket, SGAuth::css, strlen(SGAuth::css), 0);
430 send(outerSocket, replyFooter, strlen(replyFooter), 0);
434 //---------------------------------------------------------------------------
435 void WEB::SetDirName(const std::string & dn, int n)
437 web->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 messages.push_back(m);
451 if (messages.size() > MAX_MESSAGES)
452 messages.pop_front();
455 //---------------------------------------------------------------------------
456 void WEB::UpdateStat(const LOADSTAT & ls)
458 memcpy((void*)&(WEB::ls), &ls, sizeof(LOADSTAT));
460 //---------------------------------------------------------------------------