Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve code quality #17

Open
StPanning opened this issue Mar 12, 2018 · 6 comments
Open

improve code quality #17

StPanning opened this issue Mar 12, 2018 · 6 comments

Comments

@StPanning
Copy link

StPanning commented Mar 12, 2018

The current code is written in a way that makes it hard to contribute:

  • All modules share the same namespace,
  • there are global variables, that are reused in several modules
  • Almost no functions are existent

To make the code easier to maintain I suggest that:

  • the code should use use warnings and use strict
    everywhere.
  • all modules should follow the perl way to create modules with package
  • most global variables are made local

As a first step I would:

  • put the '*pl' files in modules/ in a separate namespace, like JoomScan/Check
  • transform modules to packages
  • add 'use warnings' and use 'strict'
  • make the code work

I would only change the code that needs to be changed to make the code work,
to keep the extensive changes to a minimum.

after this is done, I would proceed with the rest of the code.

some example:
modules/robots.pl orig:

dprint("Checking robots.txt existing");
$response=$ua->get("$target/robots.txt");
my $headers  = $response->headers();
my $content_type =$headers->content_type();
if ($response->status_line =~ /200/g and $content_type =~ /text\/plain/g) {
	$source=$response->decoded_content;
	my @lines = split /\n/, $source;
	$probot="";
	foreach my $line( @lines ) { 
		if($line =~ /llow:/g){
			$between=substr($line, index($line, ': ')+2, 99999);
			$probot.="$target$between\n";
		}
	}
	tprint("robots.txt is found\npath : $target/robots.txt \n\nInteresting path found from 
        robots.txt\n$probot");
}else{
	 fprint("robots.txt is not found");
}

would become JoomScan/Check/RobotsTXT.pm:

package JoomScan::Check::RobotsTXT;
use warnings;
use strict;
use JoomScan::Logging qw(tprint dprint fprint)

sub check {
  my ($ua, $target) = @_;
  dprint("Checking robots.txt existing");
  my $response = $ua->get("$target/robots.txt");
  my $headers  = $response->headers();
  my $content_type =$headers->content_type();
  my $probot="";
  if ($response->status_line =~ /200/g and 
      $content_type =~ /text\/plain/g) {
	my $source = $response->decoded_content;
	my @lines = split /\n/, $source;
	foreach my $line( @lines ) { 
	  if($line =~ /llow:/g){
	    my $between=substr($line, index($line, ': ')+2, 99999);
	    $probot.="$target$between\n";
	  }
	}
	tprint("robots.txt is found\npath : $target/robots.txt \n\nInteresting path found from robots.txt\n$probot");
      }else{
	fprint("robots.txt is not found");
      }
}

1;

I think that code could still be improved, but I would leave that for the next round of refactoring

@Ali-Razmjoo
Copy link
Collaborator

Hello,

It's actually a great idea, if we want to focus on rewriting things, we may also write a cleaner code, with self-documentation. I believe it's a great idea to use perlpod too.

despite we must have a release for tomorrow #19, let's just fix simple issues for release 0.0.5 and apply this changes for 0.0.6 release.

@rezasp please review and let us know if you agree.

Best Regards.

@rezasp
Copy link
Collaborator

rezasp commented Mar 13, 2018

Hello,

Thank you for sharing us your great idea, I agree, I am working on a few enhancements for tomorrow, and let you know when I'm finished.

Regards.

@StPanning
Copy link
Author

ok. Great. I will refactor the code. When I'm done I will send a PR.
Since I have a day job, this may last some days

@Ali-Razmjoo
Copy link
Collaborator

Hey,

thanks for updating us. be in touch (Y).

regards.

@StPanning
Copy link
Author

StPanning commented Mar 17, 2018

I have sent a PR.
The change looks huge, but the code changes are little.
all I did is:

  • put sub {} around the exisiting code
  • put that code in modules
  • made global variables private
  • modified joomla.pl, to make it work

I resisted the urge to fix/cleanup more.
This way you can easier follow what I did.

@Ali-Razmjoo
Copy link
Collaborator

Hello @StPanning,

Thanks for your contribution, I will review your code after BH Arsenal Singapore. the details you shared is very useful.

Best Regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
@StPanning @Ali-Razmjoo @rezasp and others