Browse Source

WIP: Adding port knocking features for git servers that use port knocking

spesk1 4 years ago
parent
commit
68883bd92e
4 changed files with 38 additions and 5 deletions
  1. 4 0
      example.config
  2. 14 3
      lib/SimplyGit/Git.pm
  3. 10 1
      lib/SimplyGit/Shellex.pm
  4. 10 1
      sg

+ 4 - 0
example.config

@@ -2,3 +2,7 @@
 UserWarn = "true"
 user.name = "spesk1"
 user.email = "spesk@pm.me"
+
+# Use if your git server requires port knocking
+Knock = "true"
+ports = [ "1111", "2222" ]

+ 14 - 3
lib/SimplyGit/Git.pm

@@ -39,6 +39,9 @@ sub warnOnUser($$$) {
 
 }
 
+# https://perlmaven.com/trim
+sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
+
 sub parseSGConfig($$) {
 
 	my $config = shift;
@@ -55,12 +58,20 @@ sub parseSGConfig($$) {
 		if ( $line =~ m/^(.*)\ =\ "(.*)"$/ ) {
 			$configHash{$1} = $2;
 		}
-	}
 
-	if ( defined $configHash{'UserWarn'} ) {
-		warnOnUser($configHash{'user.name'},$configHash{'user.email'},$logger);
+		if ( $line =~ m/^(.*)\ =\ \[(.*)\]/ ) {
+			my @trimmedPorts;
+			my @ports = split(",",$2);
+			foreach my $port (@ports) {
+				$port =~ /(\d{1,5})/;
+				push(@trimmedPorts,trim($1));
+			}
+			$configHash{$1} = \@trimmedPorts;
+		}
 	}
 
+	return %configHash;
+
 }
 
 sub returnConfigPath($$) {

+ 10 - 1
lib/SimplyGit/Shellex.pm

@@ -3,7 +3,16 @@ use strict;
 use warnings;
 use Log::Log4perl qw(:easy);
 use Exporter qw(import);
-our @EXPORT_OK = qw(shellex findBin);
+our @EXPORT_OK = qw(shellex findBin knocker);
+
+sub knocker($$) {
+
+	my $portRef = shift;
+	my $logger = shift;
+	foreach my $port (@$portRef) {
+		print "Would knock on $portRef";
+	}
+}
 
 sub shellex {
 

+ 10 - 1
sg

@@ -204,7 +204,16 @@ sub parseArgs {
 }
 
 parseArgs();
-parseSGConfig($args{'config-file'},$logger);
+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";
+	}
+}
+
 
 # TODO: This sub could be more concise with a sub to print array refs
 if ( defined $args{'view'} ) {