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"
32 #include "stg/ia_auth_c.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 //---------------------------------------------------------------------------
76 NULL, // pointer to thread security attributes
77 16384, // initial thread stack size, in bytes
78 RunWeb, // pointer to thread function
79 NULL, // argument for new thread
80 0, // CREATE_SUSPENDED, // creation flags
81 &pt // pointer to returned thread identifier
84 pthread_create(&thread, NULL, RunWeb, NULL);
87 //---------------------------------------------------------------------------
88 void WEB::PrepareNet()
90 listenSocket = socket(PF_INET, SOCK_STREAM, 0);
92 struct sockaddr_in listenAddr;
93 listenAddr.sin_family = AF_INET;
94 listenAddr.sin_port = htons(LISTEN_PORT);
95 listenAddr.sin_addr.s_addr = listenWebAddr;
99 if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
101 printf("Setsockopt Fail\n");
102 printf(">>> Error %s\n", strerror(errno));
109 res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
113 printf("Bind failed.\n");
117 res = listen(listenSocket, 0);
120 printf("Listen failed.\n");
124 //---------------------------------------------------------------------------
125 void WEB::SetRefreshPagePeriod(int p)
128 if (refreshPeriod <= 0 || refreshPeriod > 24*3600)
131 //---------------------------------------------------------------------------
132 void WEB::SetListenAddr(uint32_t ip)
136 //---------------------------------------------------------------------------
140 char recvBuffer[4096];
143 struct sockaddr_in outerAddr;
146 socklen_t outerAddrLen = sizeof(outerAddr);
148 int outerAddrLen = sizeof(outerAddr);
151 outerSocket = accept(listenSocket, (struct sockaddr*)&outerAddr, &outerAddrLen);
152 if (outerSocket == -1)
154 printf(">>> Error %s\n", strerror(errno));
157 recv(outerSocket, recvBuffer, sizeof(recvBuffer), 0);
159 if (strncmp(recvBuffer, "GET /sgauth.css", strlen("GET /sgauth.css")) == 0)
162 //printf("(1) recvBuffer=%s\n", recvBuffer);
164 else if (strncmp(recvBuffer, "GET /disconnect", strlen("GET /disconnect")) == 0)
168 //printf("(2) recvBuffer=%s\n", recvBuffer);
170 else if (strncmp(recvBuffer, "GET /connect", strlen("GET /connect")) == 0)
174 //printf("(3) recvBuffer=%s\n", recvBuffer);
176 else if (strncmp(recvBuffer, "GET /exit", strlen("GET /exit")) == 0)
190 //printf("(4) recvBuffer=%s\n", recvBuffer);
194 closesocket(outerSocket);
200 //---------------------------------------------------------------------------
201 int WEB::Redirect(const char * url)
203 const char * redirect =
205 "Content-Type: text/html\n"
210 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;%s\">\n"
213 "</body></html>\n\n";
216 sprintf(buff, redirect, url);
217 send(outerSocket, buff, strlen(buff), 0);
221 //---------------------------------------------------------------------------
226 const char * replyHeader =
228 "Content-Type: text/html\n"
233 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"%d\">\n"
234 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1251\">\n"
235 "<title>sgauth</title>\n"
236 "<link rel=\"Stylesheet\" href=\"sgauth.css\">"
239 "<H3>Stargazer</H3><p>\n";
241 const char * replyFooter = "</body></html>\n\n";
243 char replyHeaderBuffer[2000];
244 sprintf(replyHeaderBuffer, replyHeader, refreshPeriod);
246 send(outerSocket, replyHeaderBuffer, strlen(replyHeaderBuffer), 0);
250 int st = clnp->GetAuthorized();
252 sprintf(str, "<a href=\"connect\">%s</a><p>\n", gettext("Connect"));
253 res = send(outerSocket, str, strlen(str), 0);
255 sprintf(str, "<a href=\"disconnect\">%s</a><p>\n", gettext("Disconnect"));
256 res = send(outerSocket, str, strlen(str), 0);
258 sprintf(str, "<a href=\"/\">%s</a><p>\n", gettext("Refresh"));
259 res = send(outerSocket, str, strlen(str), 0);
261 sprintf(str, "<a href=\"exit\">%s</a><p>\n", gettext("Exit"));
262 res = send(outerSocket, str, strlen(str), 0);
264 sprintf(str, "<div id=\"%s\">%s</div><p>\n" , st ? "ConnectionStateOnline":"ConnectionStateOffline", st ? "Online":"Offline");
265 res = send(outerSocket, str, strlen(str), 0);
267 sprintf(str, "<div id=\"Cash\">%s: %.3f</div><p>\n" , gettext("Cash"), ls.cash / 1000.0);
268 res = send(outerSocket, str, strlen(str), 0);
270 sprintf(str, "<div id=\"Prepaid Traffic\">%s: %s</div><p>\n" ,
271 gettext("PrepaidTraffic"),
272 ls.freeMb[0] == 'C' ? ls.freeMb + 1 : ls.freeMb);
273 res = send(outerSocket, str, strlen(str), 0);
275 sprintf(str, "<TABLE id=\"TraffTable\">\n");
276 res = send(outerSocket, str, strlen(str), 0);
277 sprintf(str, " <TR id=\"TraffTableCaptionRow\">\n");
278 res = send(outerSocket, str, strlen(str), 0);
279 sprintf(str, " <TD id=\"TraffTableCaptionCellC\"> </TD>\n");
280 res = send(outerSocket, str, strlen(str), 0);
283 for (j = 0; j < DIR_NUM; j++)
285 if (dirName[j][0] == 0)
288 KOIToWin(dirName[j], &s);// +++++++++ sigsegv ========== TODO too long dir name crashes sgauth
289 sprintf(str, " <TD id=\"TraffTableCaptionCell%d\">%s</TD>\n", rowNum++, s.c_str());
290 send(outerSocket, str, strlen(str), 0);
293 sprintf(str," </TR>\n");
294 send(outerSocket, str, strlen(str), 0);
296 sprintf(str," <TR id=\"TraffTableUMRow\">\n");
297 send(outerSocket, str, strlen(str), 0);
299 sprintf(str," <TD id=\"TraffTableUMCellC\">%s</TD>\n", gettext("Month Upload"));
300 send(outerSocket, str, strlen(str), 0);
303 for (j = 0; j < DIR_NUM; j++)
305 if (dirName[j][0] == 0)
307 sprintf(str," <TD id=\"TraffTableUMCell%d\">%s</TD>\n", rowNum++, IntToKMG(ls.mu[j], ST_F));
308 res = send(outerSocket, str, strlen(str), 0);
311 sprintf(str," </TR>\n");
312 res = send(outerSocket, str, strlen(str), 0);
313 sprintf(str," <TR id=\"TraffTableDMRow\">\n");
314 res = send(outerSocket, str, strlen(str), 0);
315 sprintf(str," <TD id=\"TraffTableDMCellC\">%s</TD>\n", gettext("Month Download"));
316 res = send(outerSocket, str, strlen(str), 0);
319 for (j = 0; j < DIR_NUM; j++)
321 if (dirName[j][0] == 0)
323 sprintf(str," <TD id=\"TraffTableDMCell%d\">%s</TD>\n", rowNum++, IntToKMG(ls.md[j], ST_F));
324 res = send(outerSocket, str, strlen(str), 0);
326 sprintf(str," </TR>\n");
327 res = send(outerSocket, str, strlen(str), 0);
330 sprintf(str," <TR id=\"TraffTableUSRow\">\n");
331 res = send(outerSocket, str, strlen(str), 0);
332 sprintf(str," <TD id=\"TraffTableUSCellC\">%s</TD>\n", gettext("Session Upload"));
333 res = send(outerSocket, str, strlen(str), 0);
336 for (j = 0; j < DIR_NUM; j++)
338 if (dirName[j][0] == 0)
340 sprintf(str," <TD id=\"TraffTableUSCell%d\">%s</TD>\n", rowNum++, IntToKMG(ls.su[j], ST_F));
341 res = send(outerSocket, str, strlen(str), 0);
344 sprintf(str," </TR>\n");
345 res = send(outerSocket, str, strlen(str), 0);
346 sprintf(str," <TR id=\"TraffTableDSRow\">\n");
347 res = send(outerSocket, str, strlen(str), 0);
348 sprintf(str," <TD id=\"TraffTableDSCellC\">%s</TD>\n", gettext("Session Download"));
349 res = send(outerSocket, str, strlen(str), 0);
352 for (j = 0; j < DIR_NUM; j++)
354 if (dirName[j][0] == 0)
356 sprintf(str," <TD id=\"TraffTableDSCell%d\">%s</TD>\n", j, IntToKMG(ls.sd[j], ST_F));
357 res = send(outerSocket, str, strlen(str), 0);
360 sprintf(str," </TR>\n");
361 res = send(outerSocket, str, strlen(str), 0);
363 sprintf(str,"</TABLE>\n");
364 res = send(outerSocket, str, strlen(str), 0);
367 if (!messages.empty())
369 sprintf(str," <TABLE id=\"MessagesTable\">\n");
370 res = send(outerSocket, str, strlen(str), 0);
372 sprintf(str," <TR id=\"MessagesTableRowC\">\n");
373 send(outerSocket, str, strlen(str), 0);
374 sprintf(str," <TD>Date</TD>\n");
375 send(outerSocket, str, strlen(str), 0);
376 sprintf(str," <TD>Text</TD>\n");
377 send(outerSocket, str, strlen(str), 0);
378 sprintf(str," </TR>\n");
379 send(outerSocket, str, strlen(str), 0);
381 list<STG_MESSAGE>::reverse_iterator it;
382 it = messages.rbegin();
383 while (it != messages.rend())
385 sprintf(str," <TR id=\"MessagesTableRow%d\">\n", rowNum);
386 send(outerSocket, str, strlen(str), 0);
387 sprintf(str," <TD>%s</TD>\n", it->recvTime.c_str());
388 send(outerSocket, str, strlen(str), 0);
389 sprintf(str," <TD>%s</TD>\n", it->msg.c_str());
390 send(outerSocket, str, strlen(str), 0);
391 sprintf(str," </TR>\n");
392 send(outerSocket, str, strlen(str), 0);
397 sprintf(str," </TABLE>\n");
398 res = send(outerSocket, str, strlen(str), 0);
401 time_t t = time(NULL);
402 sprintf(str,"Îáíîâëåíî: %s</b>" , ctime(&t));
403 res = send(outerSocket, str, strlen(str), 0);
405 send(outerSocket, replyFooter, strlen(replyFooter), 0);
409 //---------------------------------------------------------------------------
412 const char * replyHeader =
414 "Content-Type: text/css\n"
415 "Connection: close\n\n";
417 const char * replyFooter= "\n\n";
419 send(outerSocket, replyHeader, strlen(replyHeader), 0);
420 send(outerSocket, css, strlen(css), 0);
421 send(outerSocket, replyFooter, strlen(replyFooter), 0);
425 //---------------------------------------------------------------------------
426 void WEB::SetDirName(const string & dn, int n)
428 web->dirName[n] = dn;
430 //---------------------------------------------------------------------------
431 void WEB::AddMessage(const string & message, int type)
433 time_t t = time(NULL);
438 m.recvTime = ctime(&t);
440 messages.push_back(m);
442 if (messages.size() > MAX_MESSAGES)
443 messages.pop_front();
446 //---------------------------------------------------------------------------
447 void WEB::UpdateStat(const LOADSTAT & ls)
449 memcpy((void*)&(WEB::ls), &ls, sizeof(LOADSTAT));
451 //---------------------------------------------------------------------------