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 "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;
100 if (0 != setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &lng, 4))
102 printf("Setsockopt Fail\n");
103 printf(">>> Error %s\n", strerror(errno));
110 res = bind(listenSocket, (struct sockaddr*)&listenAddr, sizeof(listenAddr));
114 printf("Bind failed.\n");
118 res = listen(listenSocket, 0);
121 printf("Listen failed.\n");
125 //---------------------------------------------------------------------------
126 void WEB::SetRefreshPagePeriod(int p)
129 if (refreshPeriod <= 0 || refreshPeriod > 24*3600)
132 //---------------------------------------------------------------------------
133 void WEB::SetListenAddr(uint32_t ip)
137 //---------------------------------------------------------------------------
141 char recvBuffer[4096];
144 struct sockaddr_in outerAddr;
147 socklen_t outerAddrLen = sizeof(outerAddr);
149 int outerAddrLen = sizeof(outerAddr);
152 outerSocket = accept(listenSocket, (struct sockaddr*)&outerAddr, &outerAddrLen);
153 if (outerSocket == -1)
155 printf(">>> Error %s\n", strerror(errno));
158 recv(outerSocket, recvBuffer, sizeof(recvBuffer), 0);
160 if (strncmp(recvBuffer, "GET /sgauth.css", strlen("GET /sgauth.css")) == 0)
163 //printf("(1) recvBuffer=%s\n", recvBuffer);
165 else if (strncmp(recvBuffer, "GET /disconnect", strlen("GET /disconnect")) == 0)
169 //printf("(2) recvBuffer=%s\n", recvBuffer);
171 else if (strncmp(recvBuffer, "GET /connect", strlen("GET /connect")) == 0)
175 //printf("(3) recvBuffer=%s\n", recvBuffer);
177 else if (strncmp(recvBuffer, "GET /exit", strlen("GET /exit")) == 0)
191 //printf("(4) recvBuffer=%s\n", recvBuffer);
195 closesocket(outerSocket);
201 //---------------------------------------------------------------------------
202 int WEB::Redirect(const char * url)
204 const char * redirect =
206 "Content-Type: text/html\n"
211 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;%s\">\n"
214 "</body></html>\n\n";
217 sprintf(buff, redirect, url);
218 send(outerSocket, buff, strlen(buff), 0);
222 //---------------------------------------------------------------------------
227 const char * replyHeader =
229 "Content-Type: text/html\n"
234 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"%d\">\n"
235 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1251\">\n"
236 "<title>sgauth</title>\n"
237 "<link rel=\"Stylesheet\" href=\"sgauth.css\">"
240 "<H3>Stargazer</H3><p>\n";
242 const char * replyFooter = "</body></html>\n\n";
244 char replyHeaderBuffer[2000];
245 sprintf(replyHeaderBuffer, replyHeader, refreshPeriod);
247 send(outerSocket, replyHeaderBuffer, strlen(replyHeaderBuffer), 0);
251 int st = clnp->GetAuthorized();
253 sprintf(str, "<a href=\"connect\">%s</a><p>\n", gettext("Connect"));
254 res = send(outerSocket, str, strlen(str), 0);
256 sprintf(str, "<a href=\"disconnect\">%s</a><p>\n", gettext("Disconnect"));
257 res = send(outerSocket, str, strlen(str), 0);
259 sprintf(str, "<a href=\"/\">%s</a><p>\n", gettext("Refresh"));
260 res = send(outerSocket, str, strlen(str), 0);
262 sprintf(str, "<a href=\"exit\">%s</a><p>\n", gettext("Exit"));
263 res = send(outerSocket, str, strlen(str), 0);
265 sprintf(str, "<div id=\"%s\">%s</div><p>\n" , st ? "ConnectionStateOnline":"ConnectionStateOffline", st ? "Online":"Offline");
266 res = send(outerSocket, str, strlen(str), 0);
268 sprintf(str, "<div id=\"Cash\">%s: %.3f</div><p>\n" , gettext("Cash"), ls.cash / 1000.0);
269 res = send(outerSocket, str, strlen(str), 0);
271 sprintf(str, "<div id=\"Prepaid Traffic\">%s: %s</div><p>\n" ,
272 gettext("PrepaidTraffic"),
273 ls.freeMb[0] == 'C' ? ls.freeMb + 1 : ls.freeMb);
274 res = send(outerSocket, str, strlen(str), 0);
276 sprintf(str, "<TABLE id=\"TraffTable\">\n");
277 res = send(outerSocket, str, strlen(str), 0);
278 sprintf(str, " <TR id=\"TraffTableCaptionRow\">\n");
279 res = send(outerSocket, str, strlen(str), 0);
280 sprintf(str, " <TD id=\"TraffTableCaptionCellC\"> </TD>\n");
281 res = send(outerSocket, str, strlen(str), 0);
284 for (j = 0; j < DIR_NUM; j++)
286 if (dirName[j][0] == 0)
289 KOIToWin(dirName[j], &s);// +++++++++ sigsegv ========== TODO too long dir name crashes sgauth
290 sprintf(str, " <TD id=\"TraffTableCaptionCell%d\">%s</TD>\n", rowNum++, s.c_str());
291 send(outerSocket, str, strlen(str), 0);
294 sprintf(str," </TR>\n");
295 send(outerSocket, str, strlen(str), 0);
297 sprintf(str," <TR id=\"TraffTableUMRow\">\n");
298 send(outerSocket, str, strlen(str), 0);
300 sprintf(str," <TD id=\"TraffTableUMCellC\">%s</TD>\n", gettext("Month Upload"));
301 send(outerSocket, str, strlen(str), 0);
304 for (j = 0; j < DIR_NUM; j++)
306 if (dirName[j][0] == 0)
308 sprintf(str," <TD id=\"TraffTableUMCell%d\">%s</TD>\n", rowNum++, IntToKMG(ls.mu[j], ST_F));
309 res = send(outerSocket, str, strlen(str), 0);
312 sprintf(str," </TR>\n");
313 res = send(outerSocket, str, strlen(str), 0);
314 sprintf(str," <TR id=\"TraffTableDMRow\">\n");
315 res = send(outerSocket, str, strlen(str), 0);
316 sprintf(str," <TD id=\"TraffTableDMCellC\">%s</TD>\n", gettext("Month Download"));
317 res = send(outerSocket, str, strlen(str), 0);
320 for (j = 0; j < DIR_NUM; j++)
322 if (dirName[j][0] == 0)
324 sprintf(str," <TD id=\"TraffTableDMCell%d\">%s</TD>\n", rowNum++, IntToKMG(ls.md[j], ST_F));
325 res = send(outerSocket, str, strlen(str), 0);
327 sprintf(str," </TR>\n");
328 res = send(outerSocket, str, strlen(str), 0);
331 sprintf(str," <TR id=\"TraffTableUSRow\">\n");
332 res = send(outerSocket, str, strlen(str), 0);
333 sprintf(str," <TD id=\"TraffTableUSCellC\">%s</TD>\n", gettext("Session Upload"));
334 res = send(outerSocket, str, strlen(str), 0);
337 for (j = 0; j < DIR_NUM; j++)
339 if (dirName[j][0] == 0)
341 sprintf(str," <TD id=\"TraffTableUSCell%d\">%s</TD>\n", rowNum++, IntToKMG(ls.su[j], ST_F));
342 res = send(outerSocket, str, strlen(str), 0);
345 sprintf(str," </TR>\n");
346 res = send(outerSocket, str, strlen(str), 0);
347 sprintf(str," <TR id=\"TraffTableDSRow\">\n");
348 res = send(outerSocket, str, strlen(str), 0);
349 sprintf(str," <TD id=\"TraffTableDSCellC\">%s</TD>\n", gettext("Session Download"));
350 res = send(outerSocket, str, strlen(str), 0);
353 for (j = 0; j < DIR_NUM; j++)
355 if (dirName[j][0] == 0)
357 sprintf(str," <TD id=\"TraffTableDSCell%d\">%s</TD>\n", j, IntToKMG(ls.sd[j], ST_F));
358 res = send(outerSocket, str, strlen(str), 0);
361 sprintf(str," </TR>\n");
362 res = send(outerSocket, str, strlen(str), 0);
364 sprintf(str,"</TABLE>\n");
365 res = send(outerSocket, str, strlen(str), 0);
368 if (!messages.empty())
370 sprintf(str," <TABLE id=\"MessagesTable\">\n");
371 res = send(outerSocket, str, strlen(str), 0);
373 sprintf(str," <TR id=\"MessagesTableRowC\">\n");
374 send(outerSocket, str, strlen(str), 0);
375 sprintf(str," <TD>Date</TD>\n");
376 send(outerSocket, str, strlen(str), 0);
377 sprintf(str," <TD>Text</TD>\n");
378 send(outerSocket, str, strlen(str), 0);
379 sprintf(str," </TR>\n");
380 send(outerSocket, str, strlen(str), 0);
382 list<STG_MESSAGE>::reverse_iterator it;
383 it = messages.rbegin();
384 while (it != messages.rend())
386 sprintf(str," <TR id=\"MessagesTableRow%d\">\n", rowNum);
387 send(outerSocket, str, strlen(str), 0);
388 sprintf(str," <TD>%s</TD>\n", it->recvTime.c_str());
389 send(outerSocket, str, strlen(str), 0);
390 sprintf(str," <TD>%s</TD>\n", it->msg.c_str());
391 send(outerSocket, str, strlen(str), 0);
392 sprintf(str," </TR>\n");
393 send(outerSocket, str, strlen(str), 0);
398 sprintf(str," </TABLE>\n");
399 res = send(outerSocket, str, strlen(str), 0);
402 time_t t = time(NULL);
403 sprintf(str,"Îáíîâëåíî: %s</b>" , ctime(&t));
404 res = send(outerSocket, str, strlen(str), 0);
406 send(outerSocket, replyFooter, strlen(replyFooter), 0);
410 //---------------------------------------------------------------------------
413 const char * replyHeader =
415 "Content-Type: text/css\n"
416 "Connection: close\n\n";
418 const char * replyFooter= "\n\n";
420 send(outerSocket, replyHeader, strlen(replyHeader), 0);
421 send(outerSocket, css, strlen(css), 0);
422 send(outerSocket, replyFooter, strlen(replyFooter), 0);
426 //---------------------------------------------------------------------------
427 void WEB::SetDirName(const string & dn, int n)
429 web->dirName[n] = dn;
431 //---------------------------------------------------------------------------
432 void WEB::AddMessage(const string & message, int type)
434 time_t t = time(NULL);
439 m.recvTime = ctime(&t);
441 messages.push_back(m);
443 if (messages.size() > MAX_MESSAGES)
444 messages.pop_front();
447 //---------------------------------------------------------------------------
448 void WEB::UpdateStat(const LOADSTAT & ls)
450 memcpy((void*)&(WEB::ls), &ls, sizeof(LOADSTAT));
452 //---------------------------------------------------------------------------