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);
362 for (j = 0; j < DIR_NUM; j++)
364 if (dirName[j][0] == 0)
366 sprintf(str," <TD id=\"TraffTableDSCell%d\">%s</TD>\n", j, IntToKMG(ls.sd[j], ST_F));
367 res = send(outerSocket, str, strlen(str), 0);
370 sprintf(str," </TR>\n");
371 res = send(outerSocket, str, strlen(str), 0);
373 sprintf(str,"</TABLE>\n");
374 res = send(outerSocket, str, strlen(str), 0);
377 if (!messages.empty())
379 sprintf(str," <TABLE id=\"MessagesTable\">\n");
380 res = send(outerSocket, str, strlen(str), 0);
382 sprintf(str," <TR id=\"MessagesTableRowC\">\n");
383 send(outerSocket, str, strlen(str), 0);
384 sprintf(str," <TD>Date</TD>\n");
385 send(outerSocket, str, strlen(str), 0);
386 sprintf(str," <TD>Text</TD>\n");
387 send(outerSocket, str, strlen(str), 0);
388 sprintf(str," </TR>\n");
389 send(outerSocket, str, strlen(str), 0);
391 list<STG_MESSAGE>::reverse_iterator it;
392 it = messages.rbegin();
393 while (it != messages.rend())
395 sprintf(str," <TR id=\"MessagesTableRow%d\">\n", rowNum);
396 send(outerSocket, str, strlen(str), 0);
397 sprintf(str," <TD>%s</TD>\n", it->recvTime.c_str());
398 send(outerSocket, str, strlen(str), 0);
399 sprintf(str," <TD>%s</TD>\n", it->msg.c_str());
400 send(outerSocket, str, strlen(str), 0);
401 sprintf(str," </TR>\n");
402 send(outerSocket, str, strlen(str), 0);
407 sprintf(str," </TABLE>\n");
408 res = send(outerSocket, str, strlen(str), 0);
411 time_t t = time(NULL);
412 sprintf(str,"Îáíîâëåíî: %s</b>" , ctime(&t));
413 res = send(outerSocket, str, strlen(str), 0);
415 send(outerSocket, replyFooter, strlen(replyFooter), 0);
419 //---------------------------------------------------------------------------
422 const char * replyHeader =
424 "Content-Type: text/css\n"
425 "Connection: close\n\n";
427 const char * replyFooter= "\n\n";
429 send(outerSocket, replyHeader, strlen(replyHeader), 0);
430 send(outerSocket, css, strlen(css), 0);
431 send(outerSocket, replyFooter, strlen(replyFooter), 0);
435 //---------------------------------------------------------------------------
436 void WEB::SetDirName(const string & dn, int n)
438 web->dirName[n] = dn;
440 //---------------------------------------------------------------------------
441 void WEB::AddMessage(const string & message, int type)
443 time_t t = time(NULL);
448 m.recvTime = ctime(&t);
450 messages.push_back(m);
452 if (messages.size() > MAX_MESSAGES)
453 messages.pop_front();
456 //---------------------------------------------------------------------------
457 void WEB::UpdateStat(const LOADSTAT & ls)
459 memcpy((void*)&(WEB::ls), &ls, sizeof(LOADSTAT));
461 //---------------------------------------------------------------------------