-
Notifications
You must be signed in to change notification settings - Fork 4
/
nanny.pl
executable file
·106 lines (77 loc) · 2.68 KB
/
nanny.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env perl
=head1 NAME
nanny.pl - Limit the websites that can be browsed
=head1 SYNOPSIS
nanny.pl [URI]
-h, --help print this help message
-s, --super use "super" mode and block all resources (js & css)
Simple usage:
nanny.pl http://www.google.com/
=head1 DESCRIPTION
Don't let the user escape from the current site. This is a very basic parental
control implementation.
=cut
use strict;
use warnings;
use Glib ':constants';
use Gtk3 -init;
use Gtk3::WebKit;
use Getopt::Long qw(:config auto_help);
use Pod::Usage;
use Data::Dumper;
use URI;
sub main {
GetOptions(
's|super' => \my $super,
) or pod2usage(1);
my ($url) = @ARGV;
$url ||= 'http://localhost:3001/';
my $allowed_host_port = get_host_port($url);
my $window = Gtk3::Window->new('toplevel');
$window->set_default_size(800, 600);
$window->signal_connect(destroy => sub { Gtk3->main_quit() });
my $view = Gtk3::WebKit::WebView->new();
if ($super) {
print "Super nanny activated\n";
$view->signal_connect("resource-request-starting" => sub {
my ($view, $frame, $resource, $request, $response) = @_;
my $host_port = get_host_port($request->get_uri);
return if $host_port eq $allowed_host_port;
# Block the request if it goes outside, we block by setting the URI
# to 'about:blank'
print "Block access to $host_port\n";
$request->set_uri('about:blank');
});
}
else {
print "Nanny activated\n";
# Add a callback to monitor where each URI will go and reject the URI if the
# location differs from the original website.
# This only blocks, iframes and clicked links. Javascript and CSS are not blocked.
$view->signal_connect("navigation-policy-decision-requested" => sub {
my ($view, $frame, $request, $action, $decision) = @_;
my $host_port = get_host_port($request->get_uri);
# We allow browsing the same site
return FALSE if $host_port eq $allowed_host_port;
# We block the access to an external site
print "Block access to $host_port\n";
$decision->ignore();
return TRUE;
});
}
# Pack the widgets together
my $scrolls = Gtk3::ScrolledWindow->new();
$scrolls->add($view);
$window->add($scrolls);
$window->show_all();
$view->load_uri($url);
Gtk3->main;
return 0;
}
sub get_host_port {
my ($url) = @_;
my $uri = URI->new($url);
# Not all URI have a host/port (e.g. "mailto:[email protected]")
return $uri->can('host_port') ? $uri->host_port : '';
}
exit main() unless caller;