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 $
31 #include "stg/common.h"
36 extern IA_CLIENT_PROT * clnp;
38 #define LISTEN_PORT (5580)
42 //---------------------------------------------------------------------------
46 unsigned long WINAPI RunWeb(void *)
53 //---------------------------------------------------------------------------
62 res = WSAStartup(MAKEWORD(2,0), &wsaData);
65 for (int i = 0; i < DIR_NUM; i++)
70 memset(&ls, 0, sizeof(ls));
72 //---------------------------------------------------------------------------
78 NULL, // pointer to thread security attributes
79 16384, // initial thread stack size, in bytes
80 RunWeb, // pointer to thread function
81 NULL, // argument for new thread
82 0, // CREATE_SUSPENDED, // creation flags
83 &pt // pointer to returned thread identifier
86 pthread_create(&thread, NULL, RunWeb, NULL);
89 //---------------------------------------------------------------------------
90 void WEB::PrepareNet()
92 listenSocket = socket(PF_INET, SOCK_STREAM, 0);
94 struct sockaddr_in listenAddr;
95 listenAddr.sin_family = AF_INET;
96 listenAddr.sin_port = htons(LISTEN_PORT);
97 listenAddr.sin_addr.s_addr = listenWebAddr;
101 if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
103 printf("Setsockopt Fail\n");
104 printf(">>> Error %s\n", strerror(errno));
111 res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
115 printf("Bind failed.\n");
119 res = listen(listenSocket, 0);
122 printf("Listen failed.\n");
126 //---------------------------------------------------------------------------
127 void WEB::SetRefreshPagePeriod(int p)
130 if (refreshPeriod <= 0 || refreshPeriod > 24*3600)
133 //---------------------------------------------------------------------------
134 void WEB::SetListenAddr(uint32_t ip)
138 //---------------------------------------------------------------------------
142 char recvBuffer[4096];
145 struct sockaddr_in outerAddr;
148 socklen_t outerAddrLen = sizeof(outerAddr);
150 int outerAddrLen = sizeof(outerAddr);
153 outerSocket = accept(listenSocket, (struct sockaddr*)&outerAddr, &outerAddrLen);
154 if (outerSocket == -1)
156 printf(">>> Error %s\n", strerror(errno));
159 recv(outerSocket, recvBuffer, sizeof(recvBuffer), 0);
161 if (strncmp(recvBuffer, "GET /sgauth.css", strlen("GET /sgauth.css")) == 0)
164 //printf("(1) recvBuffer=%s\n", recvBuffer);
166 else if (strncmp(recvBuffer, "GET /disconnect", strlen("GET /disconnect")) == 0)
170 //printf("(2) recvBuffer=%s\n", recvBuffer);
172 else if (strncmp(recvBuffer, "GET /connect", strlen("GET /connect")) == 0)
176 //printf("(3) recvBuffer=%s\n", recvBuffer);
178 else if (strncmp(recvBuffer, "GET /exit", strlen("GET /exit")) == 0)
192 //printf("(4) recvBuffer=%s\n", recvBuffer);
196 closesocket(outerSocket);
202 //---------------------------------------------------------------------------
203 int WEB::Redirect(const char * url)
205 const char * redirect =
207 "Content-Type: text/html\n"
212 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;%s\">\n"
215 "</body></html>\n\n";
218 sprintf(buff, redirect, url);
219 send(outerSocket, buff, strlen(buff), 0);
223 //---------------------------------------------------------------------------
228 const char * replyHeader =
230 "Content-Type: text/html\n"
235 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"%d\">\n"
236 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1251\">\n"
237 "<title>sgauth</title>\n"
238 "<link rel=\"Stylesheet\" href=\"sgauth.css\">"
241 "<H3>Stargazer</H3><p>\n";
243 const char * replyFooter = "</body></html>\n\n";
245 char replyHeaderBuffer[2000];
246 sprintf(replyHeaderBuffer, replyHeader, refreshPeriod);
248 send(outerSocket, replyHeaderBuffer, strlen(replyHeaderBuffer), 0);
252 int st = clnp->GetAuthorized();
254 sprintf(str, "<a href=\"connect\">%s</a><p>\n", gettext("Connect"));
255 res = send(outerSocket, str, strlen(str), 0);
257 sprintf(str, "<a href=\"disconnect\">%s</a><p>\n", gettext("Disconnect"));
258 res = send(outerSocket, str, strlen(str), 0);
260 sprintf(str, "<a href=\"/\">%s</a><p>\n", gettext("Refresh"));
261 res = send(outerSocket, str, strlen(str), 0);
263 sprintf(str, "<a href=\"exit\">%s</a><p>\n", gettext("Exit"));
264 res = send(outerSocket, str, strlen(str), 0);
266 sprintf(str, "<div id=\"%s\">%s</div><p>\n" , st ? "ConnectionStateOnline":"ConnectionStateOffline", st ? "Online":"Offline");
267 res = send(outerSocket, str, strlen(str), 0);
269 sprintf(str, "<div id=\"Cash\">%s: %.3f</div><p>\n" , gettext("Cash"), ls.cash / 1000.0);
270 res = send(outerSocket, str, strlen(str), 0);
272 sprintf(str, "<div id=\"Prepaid Traffic\">%s: %s</div><p>\n" ,
273 gettext("PrepaidTraffic"),
274 ls.freeMb[0] == 'C' ? ls.freeMb + 1 : ls.freeMb);
275 res = send(outerSocket, str, strlen(str), 0);
277 sprintf(str, "<TABLE id=\"TraffTable\">\n");
278 res = send(outerSocket, str, strlen(str), 0);
279 sprintf(str, " <TR id=\"TraffTableCaptionRow\">\n");
280 res = send(outerSocket, str, strlen(str), 0);
281 sprintf(str, " <TD id=\"TraffTableCaptionCellC\"> </TD>\n");
282 res = send(outerSocket, str, strlen(str), 0);
285 for (j = 0; j < DIR_NUM; j++)
287 if (dirName[j][0] == 0)
290 KOIToWin(dirName[j], &s);// +++++++++ sigsegv ========== TODO too long dir name crashes sgauth
291 sprintf(str, " <TD id=\"TraffTableCaptionCell%d\">%s</TD>\n", rowNum++, s.c_str());
292 send(outerSocket, str, strlen(str), 0);
295 sprintf(str," </TR>\n");
296 send(outerSocket, str, strlen(str), 0);
298 sprintf(str," <TR id=\"TraffTableUMRow\">\n");
299 send(outerSocket, str, strlen(str), 0);
301 sprintf(str," <TD id=\"TraffTableUMCellC\">%s</TD>\n", gettext("Month Upload"));
302 send(outerSocket, str, strlen(str), 0);
305 for (j = 0; j < DIR_NUM; j++)
307 if (dirName[j][0] == 0)
309 sprintf(str," <TD id=\"TraffTableUMCell%d\">%s</TD>\n", rowNum++, IntToKMG(ls.mu[j], ST_F));
310 res = send(outerSocket, str, strlen(str), 0);
313 sprintf(str," </TR>\n");
314 res = send(outerSocket, str, strlen(str), 0);
315 sprintf(str," <TR id=\"TraffTableDMRow\">\n");
316 res = send(outerSocket, str, strlen(str), 0);
317 sprintf(str," <TD id=\"TraffTableDMCellC\">%s</TD>\n", gettext("Month Download"));
318 res = send(outerSocket, str, strlen(str), 0);
321 for (j = 0; j < DIR_NUM; j++)
323 if (dirName[j][0] == 0)
325 sprintf(str," <TD id=\"TraffTableDMCell%d\">%s</TD>\n", rowNum++, IntToKMG(ls.md[j], ST_F));
326 res = send(outerSocket, str, strlen(str), 0);
328 sprintf(str," </TR>\n");
329 res = send(outerSocket, str, strlen(str), 0);
332 sprintf(str," <TR id=\"TraffTableUSRow\">\n");
333 res = send(outerSocket, str, strlen(str), 0);
334 sprintf(str," <TD id=\"TraffTableUSCellC\">%s</TD>\n", gettext("Session Upload"));
335 res = send(outerSocket, str, strlen(str), 0);
338 for (j = 0; j < DIR_NUM; j++)
340 if (dirName[j][0] == 0)
342 sprintf(str," <TD id=\"TraffTableUSCell%d\">%s</TD>\n", rowNum++, IntToKMG(ls.su[j], ST_F));
343 res = send(outerSocket, str, strlen(str), 0);
346 sprintf(str," </TR>\n");
347 res = send(outerSocket, str, strlen(str), 0);
348 sprintf(str," <TR id=\"TraffTableDSRow\">\n");
349 res = send(outerSocket, str, strlen(str), 0);
350 sprintf(str," <TD id=\"TraffTableDSCellC\">%s</TD>\n", gettext("Session Download"));
351 res = send(outerSocket, str, strlen(str), 0);
354 for (j = 0; j < DIR_NUM; j++)
356 if (dirName[j][0] == 0)
358 sprintf(str," <TD id=\"TraffTableDSCell%d\">%s</TD>\n", j, IntToKMG(ls.sd[j], ST_F));
359 res = send(outerSocket, str, strlen(str), 0);
362 sprintf(str," </TR>\n");
363 res = send(outerSocket, str, strlen(str), 0);
365 sprintf(str,"</TABLE>\n");
366 res = send(outerSocket, str, strlen(str), 0);
369 if (!messages.empty())
371 sprintf(str," <TABLE id=\"MessagesTable\">\n");
372 res = send(outerSocket, str, strlen(str), 0);
374 sprintf(str," <TR id=\"MessagesTableRowC\">\n");
375 send(outerSocket, str, strlen(str), 0);
376 sprintf(str," <TD>Date</TD>\n");
377 send(outerSocket, str, strlen(str), 0);
378 sprintf(str," <TD>Text</TD>\n");
379 send(outerSocket, str, strlen(str), 0);
380 sprintf(str," </TR>\n");
381 send(outerSocket, str, strlen(str), 0);
383 list<STG_MESSAGE>::reverse_iterator it;
384 it = messages.rbegin();
385 while (it != messages.rend())
387 sprintf(str," <TR id=\"MessagesTableRow%d\">\n", rowNum);
388 send(outerSocket, str, strlen(str), 0);
389 sprintf(str," <TD>%s</TD>\n", it->recvTime.c_str());
390 send(outerSocket, str, strlen(str), 0);
391 sprintf(str," <TD>%s</TD>\n", it->msg.c_str());
392 send(outerSocket, str, strlen(str), 0);
393 sprintf(str," </TR>\n");
394 send(outerSocket, str, strlen(str), 0);
399 sprintf(str," </TABLE>\n");
400 res = send(outerSocket, str, strlen(str), 0);
403 time_t t = time(NULL);
404 sprintf(str,"Îáíîâëåíî: %s</b>" , ctime(&t));
405 res = send(outerSocket, str, strlen(str), 0);
407 send(outerSocket, replyFooter, strlen(replyFooter), 0);
411 //---------------------------------------------------------------------------
414 const char * replyHeader =
416 "Content-Type: text/css\n"
417 "Connection: close\n\n";
419 const char * replyFooter= "\n\n";
421 send(outerSocket, replyHeader, strlen(replyHeader), 0);
422 send(outerSocket, css, strlen(css), 0);
423 send(outerSocket, replyFooter, strlen(replyFooter), 0);
427 //---------------------------------------------------------------------------
428 void WEB::SetDirName(const string & dn, int n)
430 web->dirName[n] = dn;
432 //---------------------------------------------------------------------------
433 void WEB::AddMessage(const string & message, int type)
435 time_t t = time(NULL);
440 m.recvTime = ctime(&t);
442 messages.push_back(m);
444 if (messages.size() > MAX_MESSAGES)
445 messages.pop_front();
448 //---------------------------------------------------------------------------
449 void WEB::UpdateStat(const LOADSTAT & ls)
451 memcpy((void*)&(WEB::ls), &ls, sizeof(LOADSTAT));
453 //---------------------------------------------------------------------------