Last active
August 29, 2015 13:56
-
-
Save AlD/8922257 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# https-ssh-switch.pl: Turn on https → ssh redirect for the source IP of | |
# an http request. Allows you to dodge some aggressive firewalls resp. | |
# paywalls. | |
# Sample sudo rule: www-data ALL=(root) NOEXEC:NOPASSWD: /sbin/iptables -t nat -I PREROUTING -i [[\:alnum\:]]* -s [[\:digit\:].]* -p tcp --dport 443 -j REDIRECT --to-ports 22 | |
# 2014-02-10 Daniel Albers <[email protected]>, released under GPL license | |
# TODO: IPv6 support | |
use strict; | |
use warnings; | |
use CGI; | |
my $ipregex = qr([0-9.]+); | |
my $q = CGI->new; | |
my $ip = $q->remote_addr(); | |
print $q->header(); | |
my $get_interface = sub ($) { | |
my $ip = shift; | |
die unless $ip =~ /^${ipregex}$/; | |
my $route = `/usr/bin/env ip -o route get "$ip"`; | |
$route =~ m/^${ipregex}(?:\s+via\s+${ipregex})?\s+dev\s+([a-z0-9.-]+)/p; | |
my $interface = $1; | |
return $interface; | |
}; | |
my $interface = $get_interface->($ip); | |
die unless $interface; | |
print $ip; | |
print `/usr/bin/sudo /sbin/iptables -t nat -I PREROUTING -i "$interface" -s "$ip" -p tcp --dport 443 -j REDIRECT --to-ports 22`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment