GnuCashew ~ GnuCash Enabled Web
GCW
main.cpp
Go to the documentation of this file.
1 #line 2 "src/main.cpp"
2 
3 #include <dirent.h>
4 #include <fstream>
5 #include <grp.h>
6 #include <langinfo.h>
7 #include <locale.h>
8 #include <pwd.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <time.h>
14 
15 #include <Wt/Http/Request.h>
16 #include <Wt/Http/Response.h>
17 #include <Wt/WDateTime.h>
18 #include <Wt/WLayout.h>
19 #include <Wt/WResource.h>
20 #include <Wt/WServer.h>
21 
22 //#include "Dbo/Users/Auth.h"
23 #include "GnuCashew.h"
24 
25 std::string g_dbName;
26 
27 #define VAULT_ROOT std::string("/")
28 #define CLIENT_FOLDERS "/"
29 
30 void show_program_version( const std::string & message )
31 {
32 #ifdef __GRAB_DATE_STRING_FROM_PROGRAM_DATE__
33  struct stat attr;
34  stat( argv[0], &attr );
35 
36  auto tm = localtime( &attr.st_mtime );
37  char datestring[256];
38 
39  /* Get localized date string. */
40  strftime( datestring, sizeof(datestring), nl_langinfo(D_T_FMT), tm );
41 #endif
42 
43 #ifdef NEVER
44  std::string datestring = __GNUCASHEW_VERSION__ + " " + __GNUCASHEW_BUILD__;
45 
46  std::cout << __DATETIMEPIDFILELINE__
47  << " " << argv[0]
48  << " " << datestring
49  << " -> " << message
50  << std::endl;
51 #endif
52 
53 }
54 
56 : public Wt::WApplication
57 {
58  public:
59  Redirector( const Wt::WEnvironment & env )
60  : Wt::WApplication(env)
61  {
62  redirect( "/demo" );
63  }
64 };
65 
66 
67 /*
68 ** This is a simple static html file server. It is designed to serve up files
69 ** in the approot/html folder. This resource employs a little bit of access
70 ** control by way of 'valid-ip' address handling.
71 **
72 */
74 : public Wt::WResource
75 {
76  public:
77 
78  void handleRequest( const Wt::Http::Request & request, Wt::Http::Response & response );
79 
80 };
81 
82 void HtmlResource::handleRequest( const Wt::Http::Request & request, Wt::Http::Response & response )
83 {
84 #ifdef NEVER
85  std::cout << __FILE__ << ":" << __LINE__
86  << "\n headers:" << request.headers().size()
87  << "\n path:" << request.path()
88  << "\n queryString:" << request.queryString()
89  << "\n urlParams:" << request.urlParams().size()
90  << "\n pathInfo:" << request.pathInfo()
91  << "\n clientAddress:" << request.clientAddress()
92  << std::endl;
93 
94  for( auto header : request.headers() )
95  {
96  std::cout << __FILE__ << ":" << __LINE__ << " " << header.name() << " " << header.value() << std::endl;
97 
98  }
99 #endif
100 
101  auto _blocked = [&]()
102  {
103  std::vector< std::string > allowed =
104  {
105  };
106 
107  return
108  std::find
109  (
110  allowed.begin(),
111  allowed.end(),
112  request.headerValue( "X-Forwarded-For" )
113  ) == allowed.end();
114  };
115 
116  /*
117  ** this checks if the IP address is blocked.
118  ** if it is blocked, it will insert an entry
119  ** in to a local log file so we can back-trace
120  ** attempted hacks.
121  **
122  */
123  if( _blocked() )
124  {
125  std::cout << __FILE__ << ":" << __LINE__
126  << " HtmlResource::ipblocked: [" << request.headerValue( "X-Forwarded-For" ) << "]"
127  << " '" << request.path() << "'"
128  << std::endl;
129 
130  response.out() << "<html><body>oh oh, something went wrong.<br />Please contact the site administrator</body></html>";
131 
132  std::ofstream file;
133  file.open( "HtmlResource.log", std::ios_base::app );
134  if( file.is_open() )
135  {
136  file
137  << "[" << Wt::WDateTime::currentDateTime().toString().toUTF8() << "]"
138  << " [" << request.headerValue( "x-Forwarded-For" ) << "]"
139  << " " << request.path()
140  << std::endl;
141  }
142 
143  return;
144  }
145 
146  /*
147  ** The user is allowed to view these documents.
148  **
149  */
150  auto fileName = "approot/html" + request.pathInfo();
151  std::ifstream file;
152  file.open( fileName );
153 
154  if( !file.is_open() )
155  {
156  std::cout << __FILE__ << ":" << __LINE__ << " NO FILE:" << fileName << std::endl;
157  response.out() << "no file!";
158  }
159 
160  std::cout << __FILE__ << ":" << __LINE__
161  << " [" << request.headerValue( "X-Forwarded-For" ) << "]"
162  << " HtmlResource:" << fileName
163  << std::endl;
164 
165  response.out() << file.rdbuf();
166 
167 } // endvoid HtmlResource::handleRequest( const Wt::Http::Request & request, Wt::Http::Response & response )
168 
169 
171 : public Wt::WResource
172 {
173  public:
174 
175  void handleRequest( const Wt::Http::Request & request, Wt::Http::Response & response );
176 
177 };
178 
179 void MonitResource::handleRequest( const Wt::Http::Request & request, Wt::Http::Response & response )
180 {
181 #ifdef NEVER
182  std::cout << __FILE__ << ":" << __LINE__
183  << "\n headers:" << request. headers ().size()
184  << "\n path:" << request. path ()
185  << "\n queryString:" << request. queryString ()
186  << "\n urlParams:" << request. urlParams ().size()
187  << "\n pathInfo:" << request. pathInfo ()
188  << "\n clientAddress:" << request. clientAddress ()
189  << std::endl;
190 
191  for( auto header : request.headers() )
192  {
193  std::cout << __FILE__ << ":" << __LINE__ << " " << header.name() << " " << header.value() << std::endl;
194 
195  }
196 #endif
197 
198 #ifdef NEVER
199  auto _blocked = [&]()
200  {
201  std::vector< std::string > allowed =
202  {
203  };
204 
205  return
206  std::find
207  (
208  allowed.begin(),
209  allowed.end(),
210  request.headerValue( "X-Forwarded-For" )
211  )
212  == allowed.end();
213  };
214 
215  /*
216  ** this checks if the IP address is blocked.
217  ** if it is blocked, it will insert an entry
218  ** in to a local log file so we can back-trace
219  ** attempted hacks.
220  **
221  */
222  if( _blocked() )
223  {
224  std::cout << __FILE__ << ":" << __LINE__
225  << " HtmlResource::ipblocked: [" << request.headerValue( "X-Forwarded-For" ) << "]"
226  << " '" << request.path() << "'"
227  << std::endl;
228 
229  response.out() << "<html><body>oh oh, something went wrong.<br />Please contact the site administrator</body></html>";
230 
231  std::ofstream file;
232  file.open( "HtmlResource.log", std::ios_base::app );
233  if( file.is_open() )
234  {
235  file
236  << "[" << Wt::WDateTime::currentDateTime().toString().toUTF8() << "]"
237  << " [" << request.headerValue( "x-Forwarded-For" ) << "]"
238  << " " << request.path()
239  << std::endl;
240  }
241 
242  return;
243  }
244 #endif
245 
246  response.out() << "ok\n";
247 
248 } // endvoid ApiResource::handleRequest( const Wt::Http::Request & request, Wt::Http::Response & response )
249 
250 
251 
252 
253 
254 
255 
256 
257 
258 
259 
260 /*
261 ** This is a simple static html file server. It is designed to serve up files
262 ** in the approot/html folder. This resource employs a little bit of access
263 ** control by way of 'valid-ip' address handling.
264 **
265 */
267 : public Wt::WResource
268 {
269  public:
270 
271  void handleRequest( const Wt::Http::Request & request, Wt::Http::Response & response );
272 
273 };
274 
275 void ApiResource::handleRequest( const Wt::Http::Request & request, Wt::Http::Response & response )
276 {
277 #ifdef NEVER
278  std::cout << __FILE__ << ":" << __LINE__
279  << "\n headers:" << request. headers ().size()
280  << "\n path:" << request. path ()
281  << "\n queryString:" << request. queryString ()
282  << "\n urlParams:" << request. urlParams ().size()
283  << "\n pathInfo:" << request. pathInfo ()
284  << "\n clientAddress:" << request. clientAddress ()
285  << std::endl;
286 
287  for( auto header : request.headers() )
288  {
289  std::cout << __FILE__ << ":" << __LINE__ << " " << header.name() << " " << header.value() << std::endl;
290 
291  }
292 #endif
293 
294  auto _blocked = [&]()
295  {
296  std::vector< std::string > allowed =
297  {
298  };
299 
300  return
301  std::find
302  (
303  allowed.begin(),
304  allowed.end(),
305  request.headerValue( "X-Forwarded-For" )
306  )
307  == allowed.end();
308  };
309 
310  /*
311  ** this checks if the IP address is blocked.
312  ** if it is blocked, it will insert an entry
313  ** in to a local log file so we can back-trace
314  ** attempted hacks.
315  **
316  */
317  if( _blocked() )
318  {
319  std::cout << __FILE__ << ":" << __LINE__
320  << " HtmlResource::ipblocked: [" << request.headerValue( "X-Forwarded-For" ) << "]"
321  << " '" << request.path() << "'"
322  << std::endl;
323 
324  response.out() << "<html><body>oh oh, something went wrong.<br />Please contact the site administrator</body></html>";
325 
326  std::ofstream file;
327  file.open( "HtmlResource.log", std::ios_base::app );
328  if( file.is_open() )
329  {
330  file
331  << "[" << Wt::WDateTime::currentDateTime().toString().toUTF8() << "]"
332  << " [" << request.headerValue( "x-Forwarded-For" ) << "]"
333  << " " << request.path()
334  << std::endl;
335  }
336 
337  return;
338  }
339 
340  if( request.queryString() == "c=jobphotos" )
341  {
342  for( int i = 100000; i < 115000; i++ )
343  {
344  auto _path =
345  Wt::WString( VAULT_ROOT + "/jobs/" )
346  .arg( i )
347  .toUTF8()
348  ;
349 
350  std::cout << __FILE__ << ":" << __LINE__ << " " << _path << std::endl;
351 
352 
353 
354  }
355  }
356 
357  response.out() << "ok";
358 
359 } // endvoid ApiResource::handleRequest( const Wt::Http::Request & request, Wt::Http::Response & response )
360 
361 
362 
363 
364 
365 
366 
367 template <class C>
368 void addEntryPoint( const std::string & url, Wt::WServer & server )
369 {
370  server.addEntryPoint
371  (
372  Wt::EntryPointType::Application,
373  [](const Wt::WEnvironment &env)
374  {
375  return std::make_unique<C>(env);
376  },
377  url
378  );
379 }
380 
381 int main( int argc, char ** argv )
382 {
383  show_program_version( "start-up" );
384 
385  /*
386  ** Right now we're putting the 'database to open' on the command
387  ** line. We can then run a 'demo' version connected to the 'demo'
388  ** database easily. Later we'll replace this with a proper 'open'
389  ** tool.
390  **
391  */
392  g_dbName = argv[1];
393 
394  /*
395  ** Set the layout to employ the services that
396  ** are compatible with embedded table views. Right now, only
397  ** JavaScript layout implementation works properly.
398  **
399  */
400  Wt::WLayout::setDefaultImplementation( Wt::LayoutImplementation::JavaScript );
401 // Wt::WLayout::setDefaultImplementation( Wt::LayoutImplementation::Flex );
402 
403  /*
404  ** Fire up the Wt web server. If anything throws we'll catch it.
405  **
406  */
407  try
408  {
409  Wt::WServer server( argc, argv );
410 
411  /*
412  ** These resources provide access to things in a static manner
413  ** (not used yet)
414  **
415  */
416 // server.addResource( std::make_shared< HtmlResource >(), "/html" );
417 // server.addResource( std::make_shared< MonitResource >(), "/monittoken" );
418 
419  /*
420  ** These resources provide an external API interface to the project
421  ** (not used yet)
422  **
423  */
424 // auto apiResource = std::make_shared<ApiResource>();
425 // server.addResource( apiResource, "/api" );
426 // server.addResource( apiResource, "/api2" );
427 
428  /*
429  ** These are the site entry-points. There are two URL for this, but
430  ** they yeild the same 'site' service. The 'demo' is there just for
431  ** giving quick demos so the url reads gnucashew.websiteserver.demo
432  ** rather than gnucashew.websiteserver.gnucashew.
433  **
434  */
435  addEntryPoint< Redirector >( "/" , server );
436  addEntryPoint< GCW::App >( "/demo" , server );
437  addEntryPoint< GCW::App >( "/gnucashew" , server );
438 
439  server.run();
440 
441  } // endtry
442 
443  catch( Wt::WServer::Exception & e )
444  {
445  std::cerr << e.what() << " TERMINATING TERMINATING TERMINATING" << std::endl;
446  return -1;
447  }
448 
449  return 0;
450 
451 } // endint main( int argc, char ** argv )
452 
453 
void handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response)
Definition: main.cpp:275
void handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response)
Definition: main.cpp:82
void handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response)
Definition: main.cpp:179
Redirector(const Wt::WEnvironment &env)
Definition: main.cpp:59
int main(int argc, char **argv)
Definition: main.cpp:381
void show_program_version(const std::string &message)
Definition: main.cpp:30
std::string g_dbName
Definition: main.cpp:25
#define VAULT_ROOT
Definition: main.cpp:27
void addEntryPoint(const std::string &url, Wt::WServer &server)
Definition: main.cpp:368
auto currentDateTime() -> Wt::WDateTime
Current Date/Time.
Definition: Core.cpp:260
auto find(const std::string &_splitGuid) -> Item::Ptr
Find a single split.
Definition: Splits.cpp:85
App * app()
Definition: App.cpp:67
Definition: GncLock.h:6