cloud-dns.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/perl
  2. # mod_cloudflare installer for cPanel's EasyApache build system
  3. # Created by Tyler Larson @tltech.com
  4. # Run using "build" or "install" to either build a tar.gz module,
  5. # or alternately to simply install it locally.
  6. use Archive::Tar;
  7. use LWP::UserAgent;
  8. use LWP::Protocol::https;
  9. use IO::Compress::Gzip qw(gzip) ;
  10. # Location from where to download the current version of mod_cloudflare
  11. $DOWNLOAD_URL="https://raw.githubusercontent.com/cloudflare/mod_cloudflare/master/mod_cloudflare.c";
  12. # Location where to install on cpanel servers
  13. $CPANEL_DIR="/var/cpanel/easy/apache/custom_opt_mods";
  14. sub usage() {
  15. print STDERR <<END;
  16. $0 ( build | install )
  17. - build -- builds a tar.gz installation package
  18. - install -- installs or updates the easyapache module locally
  19. END
  20. exit(1);
  21. }
  22. $MODE=shift;
  23. $MODE eq "build" or $MODE eq "install" or usage();
  24. $MODE eq "install" and not ( -d $CPANEL_DIR ) and do {
  25. print STDERR "Install directory does not exist: $CPANEL_DIR\n";
  26. print STDERR "Is this a cpanel server? If not, then build, not install.\n";
  27. exit 1;
  28. };
  29. ######
  30. # Download latest version of the code
  31. $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
  32. $resp = $ua->get($DOWNLOAD_URL);
  33. $resp and $resp->is_success or die "Failed to download: [$DOWNLOAD_URL]";
  34. $mod = $resp->decoded_content;
  35. ######
  36. # Create the source code tar.gz
  37. $pkg_tar = Archive::Tar->new;
  38. $pkg_tar->add_data("mod_cloudflare/mod_cloudflare.c", $mod);
  39. $pkg_data = $pkg_tar->write();
  40. $pkg_gzip = "";
  41. gzip \$pkg_data => \$pkg_gzip or die;
  42. #######
  43. # Create the installation tar.gz
  44. # Note: http://goo.gl/Tu7hY redirects to https://support.cloudflare.com/entries/22055786-How-do-I-restore-original-visitor-IP-to-Apache-Web-Servers-
  45. $out_tar = Archive::Tar->new;
  46. $out_tar->add_data("Cpanel/Easy/ModCloudflare.pm.tar.gz",$pkg_gzip);
  47. $out_tar->add_data("Cpanel/Easy/ModCloudflare.pm",<<'END');
  48. package Cpanel::Easy::ModCloudflare;
  49. # Created by Tyler Larson based on easyapache module for mod_rpaf
  50. our $easyconfig = {
  51. 'version' => '$Rev: 1 $',
  52. 'name' => 'Mod CloudFlare',
  53. 'note' => 'CloudFlare reverse proxy support',
  54. 'url' => 'http://goo.gl/Tu7hY',
  55. 'src_cd2' => 'mod_cloudflare',
  56. 'hastargz' => 1,
  57. 'step' => {
  58. '0' => {
  59. 'name' => 'Compiling, installing, and activating',
  60. 'command' => sub {
  61. my ($self) = @_;
  62. my ($rc, @msg) = $self->run_system_cmd_returnable( [ $self->_get_main_apxs_bin(), qw(-i -a -c mod_cloudflare.c)] );
  63. if (!$rc) { $self->print_alert_color('red', q{apxs mod_cloudflare.c failed}); }
  64. return ($rc, @msg);
  65. },
  66. },
  67. },
  68. };
  69. 1;
  70. END
  71. if ( $MODE eq "build" ) {
  72. # we're just building the archive, so output it right here.
  73. $out_tar->write("custom_opt_mod-mod_cloudflare.tar.gz",COMPRESS_GZIP);
  74. print "Built as: custom_opt_mod-mod_cloudflare.tar.gz\n";
  75. } else {
  76. # We're installing the module, so extract our archive to the appropriate dir
  77. chdir $CPANEL_DIR or die ("Failed chdir to $CPANEL_DIR");
  78. $out_tar->extract() or die "Failed to install.";
  79. print "Installed successfully.\n";
  80. }