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 //---------------------------------------------------------------------------
63 res = WSAStartup(MAKEWORD(2,0), &wsaData);
66 for (int i = 0; i < DIR_NUM; i++)
71 //---------------------------------------------------------------------------
77 NULL, // pointer to thread security attributes
78 16384, // initial thread stack size, in bytes
79 RunWeb, // pointer to thread function
80 NULL, // argument for new thread
81 0, // CREATE_SUSPENDED, // creation flags
82 &pt // pointer to returned thread identifier
85 pthread_create(&thread, NULL, RunWeb, NULL);
88 //---------------------------------------------------------------------------
89 void WEB::PrepareNet()
91 listenSocket = socket(PF_INET, SOCK_STREAM, 0);
92 listenAddr.sin_family = AF_INET;
93 listenAddr.sin_port = htons(LISTEN_PORT);
94 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 outerAddrLen = sizeof(outerAddr);
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 outerSocket = accept(listenSocket, (struct sockaddr*)&outerAddr, &outerAddrLen);
146 if (outerSocket == -1)
148 printf(">>> Error %s\n", strerror(errno));
151 recv(outerSocket, recvBuffer, sizeof(recvBuffer), 0);
153 if (strncmp(recvBuffer, "GET /sgauth.css", strlen("GET /sgauth.css")) == 0)
156 //printf("(1) recvBuffer=%s\n", recvBuffer);
158 else if (strncmp(recvBuffer, "GET /disconnect", strlen("GET /disconnect")) == 0)
162 //printf("(2) recvBuffer=%s\n", recvBuffer);
164 else if (strncmp(recvBuffer, "GET /connect", strlen("GET /connect")) == 0)
168 //printf("(3) recvBuffer=%s\n", recvBuffer);
170 else if (strncmp(recvBuffer, "GET /exit", strlen("GET /exit")) == 0)
184 //printf("(4) recvBuffer=%s\n", recvBuffer);
188 closesocket(outerSocket);
194 //---------------------------------------------------------------------------
195 int WEB::Redirect(const char * url)
197 const char * redirect =
199 "Content-Type: text/html\n"
204 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;%s\">\n"
207 "</body></html>\n\n";
210 sprintf(buff, redirect, url);
211 send(outerSocket, buff, strlen(buff), 0);
215 //---------------------------------------------------------------------------
220 const char * replyHeader =
222 "Content-Type: text/html\n"
227 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"%d\">\n"
228 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1251\">\n"
229 "<title>sgauth</title>\n"
230 "<link rel=\"Stylesheet\" href=\"sgauth.css\">"
233 "<H3>Stargazer</H3><p>\n";
235 const char * replyFooter = "</body></html>\n\n";
237 char replyHeaderBuffer[2000];
238 sprintf(replyHeaderBuffer, replyHeader, refreshPeriod);
240 send(outerSocket, replyHeaderBuffer, strlen(replyHeaderBuffer), 0);
244 int st = clnp->GetAuthorized();
246 sprintf(str, "<a href=\"connect\">%s</a><p>\n", gettext("Connect"));
247 res = send(outerSocket, str, strlen(str), 0);
249 sprintf(str, "<a href=\"disconnect\">%s</a><p>\n", gettext("Disconnect"));
250 res = send(outerSocket, str, strlen(str), 0);
252 sprintf(str, "<a href=\"/\">%s</a><p>\n", gettext("Refresh"));
253 res = send(outerSocket, str, strlen(str), 0);
255 sprintf(str, "<a href=\"exit\">%s</a><p>\n", gettext("Exit"));
256 res = send(outerSocket, str, strlen(str), 0);
258 sprintf(str, "<div id=\"%s\">%s</div><p>\n" , st ? "ConnectionStateOnline":"ConnectionStateOffline", st ? "Online":"Offline");
259 res = send(outerSocket, str, strlen(str), 0);
261 sprintf(str, "<div id=\"Cash\">%s: %.3f</div><p>\n" , gettext("Cash"), ls.cash / 1000.0);
262 res = send(outerSocket, str, strlen(str), 0);
264 sprintf(str, "<div id=\"Prepaid Traffic\">%s: %s</div><p>\n" ,
265 gettext("PrepaidTraffic"),
266 ls.freeMb[0] == 'C' ? ls.freeMb + 1 : ls.freeMb);
267 res = send(outerSocket, str, strlen(str), 0);
269 sprintf(str, "<TABLE id=\"TraffTable\">\n");
270 res = send(outerSocket, str, strlen(str), 0);
271 sprintf(str, " <TR id=\"TraffTableCaptionRow\">\n");
272 res = send(outerSocket, str, strlen(str), 0);
273 sprintf(str, " <TD id=\"TraffTableCaptionCellC\"> </TD>\n");
274 res = send(outerSocket, str, strlen(str), 0);
277 for (j = 0; j < DIR_NUM; j++)
279 if (dirName[j][0] == 0)
282 KOIToWin(dirName[j], &s);// +++++++++ sigsegv ========== TODO too long dir name crashes sgauth
283 sprintf(str, " <TD id=\"TraffTableCaptionCell%d\">%s</TD>\n", rowNum++, s.c_str());
284 send(outerSocket, str, strlen(str), 0);
287 sprintf(str," </TR>\n");
288 send(outerSocket, str, strlen(str), 0);
290 sprintf(str," <TR id=\"TraffTableUMRow\">\n");
291 send(outerSocket, str, strlen(str), 0);
293 sprintf(str," <TD id=\"TraffTableUMCellC\">%s</TD>\n", gettext("Month Upload"));
294 send(outerSocket, str, strlen(str), 0);
297 for (j = 0; j < DIR_NUM; j++)
299 if (dirName[j][0] == 0)
301 sprintf(str," <TD id=\"TraffTableUMCell%d\">%s</TD>\n", rowNum++, IntToKMG(ls.mu[j], ST_F));
302 res = send(outerSocket, str, strlen(str), 0);
305 sprintf(str," </TR>\n");
306 res = send(outerSocket, str, strlen(str), 0);
307 sprintf(str," <TR id=\"TraffTableDMRow\">\n");
308 res = send(outerSocket, str, strlen(str), 0);
309 sprintf(str," <TD id=\"TraffTableDMCellC\">%s</TD>\n", gettext("Month Download"));
310 res = 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=\"TraffTableDMCell%d\">%s</TD>\n", rowNum++, IntToKMG(ls.md[j], ST_F));
318 res = send(outerSocket, str, strlen(str), 0);
320 sprintf(str," </TR>\n");
321 res = send(outerSocket, str, strlen(str), 0);
324 sprintf(str," <TR id=\"TraffTableUSRow\">\n");
325 res = send(outerSocket, str, strlen(str), 0);
326 sprintf(str," <TD id=\"TraffTableUSCellC\">%s</TD>\n", gettext("Session Upload"));
327 res = send(outerSocket, str, strlen(str), 0);
330 for (j = 0; j < DIR_NUM; j++)
332 if (dirName[j][0] == 0)
334 sprintf(str," <TD id=\"TraffTableUSCell%d\">%s</TD>\n", rowNum++, IntToKMG(ls.su[j], ST_F));
335 res = send(outerSocket, str, strlen(str), 0);
338 sprintf(str," </TR>\n");
339 res = send(outerSocket, str, strlen(str), 0);
340 sprintf(str," <TR id=\"TraffTableDSRow\">\n");
341 res = send(outerSocket, str, strlen(str), 0);
342 sprintf(str," <TD id=\"TraffTableDSCellC\">%s</TD>\n", gettext("Session Download"));
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=\"TraffTableDSCell%d\">%s</TD>\n", j, IntToKMG(ls.sd[j], ST_F));
351 res = send(outerSocket, str, strlen(str), 0);
354 sprintf(str," </TR>\n");
355 res = send(outerSocket, str, strlen(str), 0);
357 sprintf(str,"</TABLE>\n");
358 res = send(outerSocket, str, strlen(str), 0);
361 if (!messages.empty())
363 sprintf(str," <TABLE id=\"MessagesTable\">\n");
364 res = send(outerSocket, str, strlen(str), 0);
366 sprintf(str," <TR id=\"MessagesTableRowC\">\n");
367 send(outerSocket, str, strlen(str), 0);
368 sprintf(str," <TD>Date</TD>\n");
369 send(outerSocket, str, strlen(str), 0);
370 sprintf(str," <TD>Text</TD>\n");
371 send(outerSocket, str, strlen(str), 0);
372 sprintf(str," </TR>\n");
373 send(outerSocket, str, strlen(str), 0);
375 list<STG_MESSAGE>::reverse_iterator it;
376 it = messages.rbegin();
377 while (it != messages.rend())
379 sprintf(str," <TR id=\"MessagesTableRow%d\">\n", rowNum);
380 send(outerSocket, str, strlen(str), 0);
381 sprintf(str," <TD>%s</TD>\n", it->recvTime.c_str());
382 send(outerSocket, str, strlen(str), 0);
383 sprintf(str," <TD>%s</TD>\n", it->msg.c_str());
384 send(outerSocket, str, strlen(str), 0);
385 sprintf(str," </TR>\n");
386 send(outerSocket, str, strlen(str), 0);
391 sprintf(str," </TABLE>\n");
392 res = send(outerSocket, str, strlen(str), 0);
395 time_t t = time(NULL);
396 sprintf(str,"Îáíîâëåíî: %s</b>" , ctime(&t));
397 res = send(outerSocket, str, strlen(str), 0);
399 send(outerSocket, replyFooter, strlen(replyFooter), 0);
403 //---------------------------------------------------------------------------
406 const char * replyHeader =
408 "Content-Type: text/css\n"
409 "Connection: close\n\n";
411 const char * replyFooter= "\n\n";
413 send(outerSocket, replyHeader, strlen(replyHeader), 0);
414 send(outerSocket, css, strlen(css), 0);
415 send(outerSocket, replyFooter, strlen(replyFooter), 0);
419 //---------------------------------------------------------------------------
420 void WEB::SetDirName(const string & dn, int n)
422 web->dirName[n] = dn;
424 //---------------------------------------------------------------------------
425 void WEB::AddMessage(const string & message, int type)
427 time_t t = time(NULL);
432 m.recvTime = ctime(&t);
434 messages.push_back(m);
436 if (messages.size() > MAX_MESSAGES)
437 messages.pop_front();
440 //---------------------------------------------------------------------------
441 void WEB::UpdateStat(const LOADSTAT & ls)
443 memcpy((void*)&(WEB::ls), &ls, sizeof(LOADSTAT));
445 //---------------------------------------------------------------------------