1 |
tdb |
1.2 |
import java.io.*; |
2 |
|
|
import java.net.*; |
3 |
|
|
|
4 |
tdb |
1.1 |
class ACLTest { |
5 |
|
|
|
6 |
|
|
public static void main(String args[]) { |
7 |
tdb |
1.2 |
// setup ACL defaulting to DENY |
8 |
tdb |
1.1 |
ACL acl = new ACL(ACL.DENY); |
9 |
|
|
|
10 |
tdb |
1.2 |
// add some test rules |
11 |
tdb |
1.1 |
acl.add(ACL.ALLOW, "*.ukc.ac.uk"); |
12 |
tdb |
1.5 |
acl.add(ACL.ALLOW, "129.12.*.*"); |
13 |
tdb |
1.2 |
acl.add(ACL.ALLOW, "*.bishnet.net"); |
14 |
|
|
acl.add(ACL.ALLOW, "192.168.*"); |
15 |
tdb |
1.5 |
acl.add(ACL.ALLOW, "192.168.*.*"); |
16 |
|
|
acl.add(ACL.ALLOW, "one.two.three.four.five.six.*"); |
17 |
|
|
acl.add(ACL.ALLOW, "192.129.12.*.*"); |
18 |
|
|
acl.add(ACL.ALLOW, "129.12.*"); |
19 |
|
|
acl.add(ACL.ALLOW, "129.12.4"); |
20 |
|
|
acl.add(ACL.ALLOW, "129.*"); |
21 |
|
|
acl.add(ACL.ALLOW, "129*"); |
22 |
tdb |
1.7 |
acl.add(ACL.ALLOW, "*.*.3.1"); |
23 |
|
|
acl.add(ACL.ALLOW, "*.3.1"); |
24 |
|
|
acl.add(ACL.ALLOW, "*.3.*"); |
25 |
|
|
acl.add(ACL.ALLOW, "*3.1"); |
26 |
tdb |
1.5 |
acl.add(ACL.ALLOW, "*"); |
27 |
|
|
acl.add(ACL.ALLOW, ""); |
28 |
tdb |
1.1 |
// not really needed if we default to deny :) |
29 |
tdb |
1.2 |
//acl.add(ACL.DENY, "*"); |
30 |
tdb |
1.1 |
|
31 |
tdb |
1.3 |
// dump our ACL to the console before serialization |
32 |
tdb |
1.4 |
System.out.println("BEFORE: " + acl); |
33 |
tdb |
1.3 |
|
34 |
|
|
// name for our serialized ACL |
35 |
|
|
String aclFilename = "aclfile"; |
36 |
|
|
|
37 |
|
|
// write out our ACL to a file |
38 |
|
|
try { |
39 |
|
|
ObjectOutputStream ostream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(aclFilename))); |
40 |
|
|
ostream.writeObject(acl); |
41 |
|
|
ostream.flush(); |
42 |
|
|
ostream.close(); |
43 |
|
|
} |
44 |
|
|
catch (Exception e) { |
45 |
|
|
System.out.println("exception: " + e); |
46 |
|
|
e.printStackTrace(); |
47 |
|
|
System.exit(1); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
// and read it back in :) |
51 |
|
|
try { |
52 |
|
|
ObjectInputStream istream = new ObjectInputStream(new BufferedInputStream(new FileInputStream(aclFilename))); |
53 |
|
|
acl = (ACL) istream.readObject(); |
54 |
|
|
istream.close(); |
55 |
|
|
} |
56 |
|
|
catch (Exception e) { |
57 |
|
|
System.out.println("exception: " + e); |
58 |
|
|
e.printStackTrace(); |
59 |
|
|
System.exit(1); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
// dump our ACL to the console after serialization |
63 |
tdb |
1.4 |
System.out.println("AFTER: " + acl); |
64 |
tdb |
1.1 |
|
65 |
tdb |
1.2 |
// run a few tests |
66 |
tdb |
1.1 |
System.out.println("killigrew.ukc.ac.uk: " + acl.check("killigrew.ukc.ac.uk")); |
67 |
|
|
System.out.println("129.12.41.13: " + acl.check("129.12.41.13")); |
68 |
|
|
System.out.println("kruskal.18hp.net: " + acl.check("kruskal.18hp.net")); |
69 |
|
|
System.out.println("192.168.1.1: " + acl.check("192.168.1.1")); |
70 |
tdb |
1.2 |
|
71 |
|
|
// setup an ACLServerSocket putting our ACL in |
72 |
tdb |
1.6 |
// and an ACLDatagramSocket (which we'll test better later) |
73 |
tdb |
1.2 |
ACLServerSocket ss = null; |
74 |
tdb |
1.6 |
ACLDatagramSocket ds = null; |
75 |
tdb |
1.2 |
try { |
76 |
|
|
ss = new ACLServerSocket(acl, 1337); |
77 |
tdb |
1.6 |
ds = new ACLDatagramSocket(acl, 1337); |
78 |
tdb |
1.2 |
} |
79 |
|
|
catch(IOException e) { |
80 |
|
|
System.out.println("exception: " + e); |
81 |
|
|
e.printStackTrace(); |
82 |
|
|
System.exit(1); |
83 |
|
|
} |
84 |
tdb |
1.6 |
|
85 |
|
|
System.out.println(ss); |
86 |
|
|
System.out.println(ds); |
87 |
tdb |
1.2 |
|
88 |
|
|
// start listening |
89 |
|
|
// valid connections (allowed by ACL) will get a message sent back |
90 |
|
|
while(true) { |
91 |
|
|
try { |
92 |
|
|
Socket s = ss.accept(); |
93 |
|
|
PrintWriter writer = new PrintWriter(s.getOutputStream(), true); |
94 |
|
|
writer.println("Connection suceeded from: " + s.getInetAddress().getHostName()); |
95 |
|
|
writer.println("Closing in 5 seconds"); |
96 |
|
|
try { Thread.sleep(5000); } catch(Exception ee) {} |
97 |
|
|
writer.close(); |
98 |
|
|
s.close(); |
99 |
|
|
} |
100 |
|
|
catch(IOException e) { |
101 |
|
|
System.out.println("exception: " + e); |
102 |
|
|
e.printStackTrace(); |
103 |
|
|
System.exit(1); |
104 |
|
|
} |
105 |
|
|
} |
106 |
tdb |
1.1 |
} |
107 |
|
|
|
108 |
|
|
} |