Updated init script.
[dtbartle/bnbt.git] / tracker_admin.cpp
1 /***\r
2 *\r
3 * BNBT Beta 8.0 - A C++ BitTorrent Tracker\r
4 * Copyright (C) 2003-2004 Trevor Hogan\r
5 *\r
6 * CBTT variations (C) 2003-2005 Harold Feit\r
7 *\r
8 * This library is free software; you can redistribute it and/or\r
9 * modify it under the terms of the GNU Lesser General Public\r
10 * License as published by the Free Software Foundation; either\r
11 * version 2.1 of the License, or (at your option) any later version.\r
12 *\r
13 * This library is distributed in the hope that it will be useful,\r
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
16 * Lesser General Public License for more details.\r
17 *\r
18 * You should have received a copy of the GNU Lesser General Public\r
19 * License along with this library; if not, write to the Free Software\r
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
21 *\r
22 ***/\r
23 \r
24 #include "bnbt.h"\r
25 #include "atom.h"\r
26 #include "link.h"\r
27 #include "tracker.h"\r
28 #include "server.h"\r
29 #include "util.h"\r
30 \r
31 void CTracker :: serverResponseAdmin( struct request_t *pRequest, struct response_t *pResponse, user_t user )\r
32 {\r
33         pResponse->strCode = "200 OK";\r
34 \r
35         pResponse->mapHeaders.insert( pair<string, string>( "Content-Type", string( "text/html; charset=" ) + gstrCharSet ) );\r
36 \r
37         pResponse->strContent += "<html>\n";\r
38         pResponse->strContent += "<head>\n";\r
39 \r
40         if ( !gstrTrackerTitle.empty( ) )\r
41                 pResponse->strContent += "<title>" + gstrTrackerTitle + " - Tracker Admin Panel</title>\n";\r
42         else\r
43                 pResponse->strContent += "<title>BNBT Admin Panel</title>\n"; \r
44 \r
45         if( !gstrStyle.empty( ) )\r
46                 pResponse->strContent += "<link rel=stylesheet type=\"text/css\" href=\"" + gstrStyle + "\">\n";\r
47 \r
48         pResponse->strContent += "</head>\n";\r
49         pResponse->strContent += "<body>\n";\r
50 \r
51         if( !m_bDisableLogon )\r
52         {\r
53                 if( user.strLogin.empty( ) )\r
54                         pResponse->strContent += "<p class=\"login1_index\">You are not logged in. Click <a href=\"/login.html\">here</a> to login.</p>\n";\r
55                 else\r
56                         pResponse->strContent += "<p class=\"login2_index\">You are logged in as <span class=\"username\">" + UTIL_RemoveHTML( user.strLogin ) + "</span>. Click <a href=\"/login.html?logout=1\">here</a> to logout.</p>\n";\r
57         }\r
58 \r
59         if ( !gstrTrackerTitle.empty( ) )\r
60                 pResponse->strContent += "<title>" + gstrTrackerTitle + " - Tracker Admin Panel</title>\n";\r
61         else\r
62                 pResponse->strContent += "<title>BNBT Tracker Admin Panel</title>\n";\r
63 \r
64         if( user.iAccess & ACCESS_ADMIN )\r
65         {\r
66                 //\r
67                 // kill tracker\r
68                 //\r
69 \r
70                 if( pRequest->mapParams["ap_kill"] == "1" )\r
71                 {\r
72                         gpServer->Kill( );\r
73 \r
74                         return;\r
75                 }\r
76 \r
77                 if( pRequest->mapParams["ap_recount"] == "1" )\r
78                 {\r
79                         gpServer->getTracker( )->CountUniquePeers( );\r
80 \r
81                         pResponse->strContent += "<p>Counting unique peers. Click <a href=\"/admin.html\">here</a> to return to the admin page.</p>\n";\r
82                         pResponse->strContent += "</body>\n";\r
83                         pResponse->strContent += "</html>\n";\r
84 \r
85                         return;\r
86                 }\r
87                 //RSS Support - code by labarks\r
88                 if( pRequest->mapParams["ap_rss"] == "1" )\r
89                 {\r
90                         runSaveRSS( );\r
91 \r
92                         pResponse->strContent += "<p>Updated RSS file(s). Click <a href=\"/admin.html\">here</a> to return to the admin page.</p>\n";\r
93                         pResponse->strContent += "</body>\n";\r
94                         pResponse->strContent += "</html>\n";\r
95 \r
96                         return;\r
97                 }\r
98                 //end addition\r
99 \r
100                 //\r
101                 // reset tracker link\r
102                 //\r
103 \r
104                 if( pRequest->mapParams["ap_relink"] == "1" )\r
105                 {\r
106                         if( gpLink )\r
107                         {\r
108                                 gpLink->Kill( );\r
109                                 delete gpLink;\r
110                                 gpLink = NULL;\r
111 \r
112                                 gpLink = new CLink;\r
113 \r
114                                 if( gbDebug )\r
115                                         UTIL_LogPrint("Resetting Tracker Network Link At Admin Panel Request\n");\r
116                                 \r
117                                 pResponse->strContent += "<p>Resetting tracker link. Click <a href=\"/admin.html\">here</a> to return to the admin page.</p>\n";\r
118                         }\r
119                         else if( gpLinkServer )\r
120                         {\r
121                                 //Kill server\r
122                                 delete gpLinkServer;\r
123                                 gpLinkServer = NULL;\r
124 \r
125                                 //Restart server\r
126 \r
127                                 gpLinkServer = new CLinkServer( );\r
128 \r
129                                 if( gbDebug )\r
130                                         UTIL_LogPrint("Resetting Tracker Network Hub At Admin Panel Request\n");\r
131 \r
132                                 pResponse->strContent += "<p>Resetting link server. Click <a href=\"/admin.html\">here</a> to return to the admin page.</p>\n";\r
133                         }\r
134                         else\r
135                                 pResponse->strContent += "<p>This tracker does not own a tracker link. Click <a href=\"/admin.html\">here</a> to return to the admin page.</p>\n";\r
136 \r
137                         pResponse->strContent += "</body>\n";\r
138                         pResponse->strContent += "</html>\n";\r
139 \r
140                         return;\r
141                 }\r
142 \r
143                 //\r
144                 // clients\r
145                 //\r
146 \r
147                 pResponse->strContent += "<p>Currently serving ";\r
148                 pResponse->strContent += CAtomInt( gpServer->m_vecClients.size( ) ).toString( );\r
149                 pResponse->strContent += " clients (including you)!</p>\n";\r
150 \r
151                 //\r
152                 // tracker links\r
153                 //\r
154 \r
155                 pResponse->strContent += "<table summary=\"tlink\">\n";\r
156                 pResponse->strContent += "<tr><th colspan=2>Tracker Links</th></tr>\n";\r
157                 pResponse->strContent += "<tr><td>Type</td>";\r
158 \r
159                 if( gpLinkServer )\r
160                 {\r
161                         pResponse->strContent += "<td>Primary Tracker</td></tr>\n";\r
162                         pResponse->strContent += "<tr><td>Connections</td><td>";\r
163 \r
164                         gpLinkServer->m_mtxLinks.Claim( );\r
165 \r
166                         for( vector<CLinkClient *> :: iterator i = gpLinkServer->m_vecLinks.begin( ); i != gpLinkServer->m_vecLinks.end( ); i++ )\r
167                         {\r
168                                 pResponse->strContent += (*i)->getName( );\r
169 \r
170                                 if( (*i)->m_bActive )\r
171                                         pResponse->strContent += " (ACTIVE)";\r
172                                 else\r
173                                         pResponse->strContent += " (NOT ACTIVE)";\r
174 \r
175                                 if( i + 1 != gpLinkServer->m_vecLinks.end( ) )\r
176                                         pResponse->strContent += "<br>";\r
177                         }\r
178 \r
179                         gpLinkServer->m_mtxLinks.Release( );\r
180 \r
181                         pResponse->strContent += "</td></tr>\n";\r
182                         pResponse->strContent += "<tr><td colspan=2><a href=\"/admin.html?ap_relink=1\">Reset Link Server</a>\n";\r
183                         pResponse->strContent += "</td></tr>\n";\r
184                 }\r
185                 else if( gpLink )\r
186                 {\r
187                         pResponse->strContent += "<td>Secondary Tracker</td></tr>\n";\r
188                         pResponse->strContent += "<tr><td>Connection</td><td>" + gpLink->getName( ) + "</td></tr>\n";\r
189                         pResponse->strContent += "<tr><td colspan=2><a href=\"/admin.html?ap_relink=1\">Reset Tracker Link</a>\n";\r
190                         pResponse->strContent += "</td></tr>\n";\r
191                 }\r
192                 else\r
193                         pResponse->strContent += "<td>No Link</td></tr>\n";\r
194 \r
195                 pResponse->strContent += "</table>\n";\r
196 \r
197                 //\r
198                 // kill tracker\r
199                 //\r
200 \r
201                 pResponse->strContent += "<p><a href=\"/admin.html?ap_kill=1\">Kill Tracker</a></p>\n";\r
202                 pResponse->strContent += "<p>If you kill the tracker your connection will be dropped and no response will be sent to your browser.</p>\n";\r
203 \r
204                 //\r
205                 // count unique peers\r
206                 //\r
207 \r
208                 //RSS Support - Code by labarks - no need for this if it isn't enabled\r
209                 if( m_bCountUniquePeers )\r
210                         pResponse->strContent += "<p><a href=\"/admin.html?ap_recount=1\">Count Unique Peers</a></p>\n";\r
211                 /*Original Source Code:\r
212                 pResponse->strContent += "<p><a href=\"/admin.html?ap_recount=1\">Count Unique Peers</a></p>\n";\r
213                 */\r
214                 if( m_strDumpRSSFile != "" )\r
215                         pResponse->strContent += "<p><a href=\"/admin.html?ap_rss=1\">Update RSS file(s)</a></p>\n";\r
216                 //end addition\r
217 \r
218                 // The Trinity Edition - Addition Begins\r
219                 // The following adds an RTT link when viewing the ADMIN PAGE\r
220 \r
221                 pResponse->strContent += "<p><a href=\"/index.html\">Return to Tracker</a></p>\n";\r
222 \r
223                 // ------------------------------------------------- End of Addition\r
224         }\r
225         else\r
226                 pResponse->strContent += "<p class=\"denied\">You are not authorized to view this page.</p>\n";\r
227 \r
228         pResponse->strContent += "</body>\n";\r
229         pResponse->strContent += "</html>\n";\r
230 }\r