Browse Source

Basics of port knocking features complete

spesk1 4 years ago
parent
commit
e572394f8e
4 changed files with 98 additions and 22 deletions
  1. 19 1
      README.md
  2. 17 2
      lib/SimplyGit/Git.pm
  3. 5 2
      lib/SimplyGit/Shellex.pm
  4. 57 17
      sg

+ 19 - 1
README.md

@@ -13,7 +13,7 @@ Usage:
         Dump .git/config to STDOUT. Not really useful but exposed for testing of reading config into internal data structure
 
         --push-all [--commit-msg]
-        Push all untracked and modified files 
+        Push all untracked and modified files
                 * Can be used with interactive mode
                 * Can provide a commit msg with --commit-msg (otherwise a generic will be provided)
 
@@ -31,8 +31,26 @@ Usage:
         --configure-local-user [--user,--email]
         Configure local git user
                 * Can be used with interactive mode
+
+        --config-file
+        Default is ~/.sg/sg.config, can use this opt to use another file
+                * See example.config
+
+        --knock
+        Will try and knock the defined git server at the defined ports before any operation
+                * See example.config
+
+        --knock-clone
+        Will try and knock the defined git server and clone the provided repo
+                * Will not check if you're in a git dir
 ```
 
+## Deps
+* perl, Log4Perl, which, git
+
+## Installation
+Can use provided install.sh to install
+
 ## TODO - Features:
 * Warn about upstream updates
 

+ 17 - 2
lib/SimplyGit/Git.pm

@@ -3,9 +3,14 @@ use strict;
 use warnings;
 use Log::Log4perl qw(:easy);
 use lib ".";
-use SimplyGit::Shellex qw(shellex findBin);
+use SimplyGit::Shellex qw(shellex findBin knocker);
 use Exporter qw(import);
-our @EXPORT_OK = qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset resetFromUpstream updateGitIgnore appendRepoUserConfig parseSGConfig warnOnUser);
+our @EXPORT_OK = qw(
+	readConfig getStatus returnState addFiles 
+	commitChanges pushChanges stashAndReset resetFromUpstream 
+	updateGitIgnore appendRepoUserConfig parseSGConfig 
+	warnOnUser basicClone
+);
 
 # TODO: Add info/debug logging for all subroutines
 
@@ -322,3 +327,13 @@ sub appendRepoUserConfig($$$) {
 	}
 
 }
+
+sub basicClone($$) {
+
+	my $cloneTarget = shift;
+	my $logger = shift;
+	my $gitCmd = findBin("git",$logger);
+	shellex("$gitCmd clone $cloneTarget",$logger);
+	print "Successfully cloned $cloneTarget\n";
+
+}

+ 5 - 2
lib/SimplyGit/Shellex.pm

@@ -5,13 +5,16 @@ use Log::Log4perl qw(:easy);
 use Exporter qw(import);
 our @EXPORT_OK = qw(shellex findBin knocker);
 
-sub knocker($$) {
+sub knocker($$$) {
 
+	my $target = shift;
 	my $portRef = shift;
 	my $logger = shift;
+	my $nmapCmd = findBin("nmap",$logger);
 	foreach my $port (@$portRef) {
-		print "Would knock on $portRef";
+		shellex("$nmapCmd -Pn --host_timeout 201 --max-retries 0 -p $port $target > /dev/null",$logger);
 	}
+
 }
 
 sub shellex {

+ 57 - 17
sg

@@ -7,14 +7,13 @@ use Getopt::Long qw(GetOptions);
 use Log::Log4perl qw(:easy);
 # TODO: This needs to be scoped properly
 use lib "/usr/local/lib";
-use SimplyGit::Shellex qw(shellex findBin);
-use SimplyGit::Git qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset resetFromUpstream updateGitIgnore appendRepoUserConfig parseSGConfig warnOnUser);
-
-# TODO: This should maybe be more robust?
-if ( ! -d ".git" ) {
-	print "Not a git dir, exiting...\n";
-	exit 1;
-}
+use SimplyGit::Shellex qw(shellex findBin knocker);
+use SimplyGit::Git qw(
+	readConfig getStatus returnState addFiles 
+	commitChanges pushChanges stashAndReset resetFromUpstream 
+	updateGitIgnore appendRepoUserConfig parseSGConfig 
+	warnOnUser basicClone
+);
 
 sub initSG($) {
 
@@ -35,8 +34,8 @@ sub initSG($) {
 	}
 
 	if ( ! -f $configFile ) {
-		print "Creating $logFile\n";
-		shellex("touch $logFile");
+		print "Creating $configFile\n";
+		shellex("touch $configFile");
 	}
 
 	return ( $path, $logFile, $configFile );
@@ -80,8 +79,17 @@ GetOptions(
 	'user=s',
 	'email=s',
 	'config-file=s',
+	'knock',
+	'knock-clone=s',
+	'help',
 );
 
+# TODO: This should maybe be more robust?
+if ( ! -d ".git" && ! defined $args{'knock-clone'} && ! defined $args{'help'} ) {
+	print "Not a git dir, exiting...\n";
+	exit 1;
+}
+
 sub printHelp {
 
 	my $help = <<EOF
@@ -115,6 +123,15 @@ Usage:
 
 	--config-file
 	Default is ~/.sg/sg.config, can use this opt to use another file
+		* See example.config
+
+	--knock
+	Will try and knock the defined git server at the defined ports before any operation
+		* See example.config
+	
+	--knock-clone 
+	Will try and knock the defined git server and clone the provided repo
+		* Will not check if you're in a git dir
 
 EOF
 ;
@@ -134,6 +151,11 @@ if ( scalar keys %args < 1 ) {
 
 sub parseArgs {
 
+	if ( defined $args{'help'} ) {
+		printHelp();
+		exit 0;
+	}
+
 	if ( defined $args{'view'} && scalar keys %args > 1 ) {
 		print "Can't pass other args with --view\n";
 		exit 1;
@@ -151,7 +173,7 @@ sub parseArgs {
 
 	if ( defined $args{'push-all'} ) {
 		foreach my $arg ( keys %args ) {
-			if ( $arg eq "interactive" || $arg eq "commit-msg" || $arg eq "push-all" ) {
+			if ( $arg eq "interactive" || $arg eq "commit-msg" || $arg eq "push-all" || $arg eq "knock" ) {
 				next;
 			} else {
 				print "Can only pass --interactive and --commit-msg with --push-all\n";
@@ -201,6 +223,12 @@ sub parseArgs {
 		$args{'config-file'} = $sgConfigFile;
 	}
 
+	if ( defined $args{'knock-clone'} ) {
+		if ( scalar keys %args > 2 ) {
+			print "Knock clone accepts no other args\n";
+		}
+	}
+
 }
 
 parseArgs();
@@ -208,9 +236,11 @@ my %sgConfig = parseSGConfig($args{'config-file'},$logger);
 if ( defined $sgConfig{'UserWarn'} ) {
 	warnOnUser($sgConfig{'user.name'},$sgConfig{'user.email'},$logger);
 }
-if ( defined $sgConfig{'Knock'} ) {
-	foreach my $port ( @{$sgConfig{'ports'}} ) {
-		print "Would knock $port\n";
+
+sub knock() {
+	if ( defined $sgConfig{'Knock'} && ( defined $args{'knock'} || defined $args{'knock-clone'} ) ) {
+		print "Knocking...\n";
+		knocker($sgConfig{'knock.target'},$sgConfig{'ports'},$logger);
 	}
 }
 
@@ -332,12 +362,16 @@ if ( defined $args{'push-all'} ) {
 			print "Canceling...\n";
 			exit 1;
 		}
-
+		
+		knock();
 		my $gitOutput = pushChanges($logger);
 		print "Git returned:\n$gitOutput\n";
 
-	} else {
+	} 
+	
+	else {
 
+		knock();
 		pushChanges($logger);
 		my $gitOutput = pushChanges($logger);
 		print "Git returned:\n$gitOutput\n";
@@ -349,6 +383,7 @@ if ( defined $args{'push-all'} ) {
 
 if ( defined $args{'reset-from-master'} ) {
 
+	knock();
 	stashAndReset($logger);
 
 }
@@ -360,8 +395,10 @@ if ( defined $args{'reset-from-upstream'} ) {
 		chomp $args{'upstream-url'};
 		shellex("$gitCmd remote add upstream $args{'upstream-url'}",$logger);
 		shellex("$gitCmd fetch upstream",$logger);
+		knock();
 		resetFromUpstream($logger);
 	} else {
+		knock();
 		resetFromUpstream($logger);
 	}
 }
@@ -399,4 +436,7 @@ if ( defined $args{'configure-local-user'} ) {
 
 }
 
-
+if ( defined $args{'knock-clone'} ) {
+	knock();
+	basicClone($args{'knock-clone'},$logger);
+}