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