-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportfw-hook.pl
147 lines (97 loc) · 3.2 KB
/
portfw-hook.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/perl
#
# Written by Christoph Heuwieser 11.02.2015
#
# setup as /etc/libvirt/hooks/qemu and chmod +x
#
use strict;
my %domains =
(
vmname01 =>
{
ip => '192.168.122.20',
forwards =>
{
33051 => '3389'
}
},
vmname02 =>
{
ip => '192.168.122.21',
forwards =>
{
33052 => '3389',
22442 => '80',
22443 => '443'
}
}
);
my $logfile = "/var/log/cv_domaincontrol.log";
my $debug = "on";
my $iptables = "/sbin/iptables";
my @external_ifs = ("xenbrext", "virbr0");
my $external_ip = "10.1.1.1";
my $domain_name = $ARGV[0];
if ($domains{$domain_name})
{
if ($ARGV[1] eq "started")
{
&log("Domain ".$domain_name." started, adding portforwarding");
&rules_update($domain_name," -I ");
}
elsif($ARGV[1] eq "stopped")
{
&log("Domain ".$domain_name." stopped, deleting portforwarding");
&rules_update($domain_name," -D ");
}
elsif($ARGV[1] eq "reconnect")
{
&log("Domain ".$domain_name." stopped, deleting portforwarding");
&rules_update($domain_name," -D ");
&log("Domain ".$domain_name." started, adding portforwarding");
&rules_update($domain_name," -I ");
}
}
&log($ARGV[0]." ".$ARGV[1]);
exit();
sub rules_update
{
my ($domain_name,$ipt_action) = @_;
foreach my $forwardsourceport (keys %{$domains{$domain_name}{'forwards'}})
{
foreach my $externalif (@external_ifs)
{
# PREROUTING TCP
my $preroutingcmd = $iptables." -t nat ".$ipt_action." PREROUTING -d ".$external_ip." -i ".$externalif." -p tcp -m tcp --dport ".$forwardsourceport." -j DNAT --to-destination ".$domains{$domain_name}{'ip'}.":".$domains{$domain_name}{'forwards'}{$forwardsourceport};
system($preroutingcmd);
# PREROUTING UDP
my $preroutingcmd = $iptables." -t nat ".$ipt_action." PREROUTING -d ".$external_ip." -i ".$externalif." -p udp -m udp --dport ".$forwardsourceport." -j DNAT --to-destination ".$domains{$domain_name}{'ip'}.":".$domains{$domain_name}{'forwards'}{$forwardsourceport};
system($preroutingcmd);
}
# FORWARD TCP
my $forwardcmd = $iptables.$ipt_action." FORWARD -d ".$domains{$domain_name}{'ip'}." -p tcp -m state --state NEW -m tcp --dport ".$domains{$domain_name}{'forwards'}{$forwardsourceport}." -j ACCEPT";
system($forwardcmd);
# FORWARD UDP
my $forwardcmd = $iptables.$ipt_action." FORWARD -d ".$domains{$domain_name}{'ip'}." -p udp -m state --state NEW -m udp --dport ".$domains{$domain_name}{'forwards'}{$forwardsourceport}." -j ACCEPT";
system($forwardcmd);
}
}
sub log
{
my $message = shift;
open(LOG,">>$logfile") or print "WARN: can not open logfile: $!\n";
print LOG ×tamp().$message."\n";
close(LOG);
print ×tamp().$message."\n" if ($debug eq "on");
}
sub timestamp
{
my @localtime = localtime;
return(sprintf("\[%02d.%02d.%04d %02d:%02d:%02d\] ",
$localtime[3],
$localtime[4]+1,
$localtime[5]+1900,
$localtime[2],
$localtime[1],
$localtime[0]));
}