| 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 |
|
|
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.1 |
// not really needed if we default to deny :) |
| 16 |
tdb |
1.2 |
//acl.add(ACL.DENY, "*"); |
| 17 |
tdb |
1.1 |
|
| 18 |
tdb |
1.2 |
// dump our ACL to the console |
| 19 |
|
|
System.out.println(acl.getStringACL()); |
| 20 |
tdb |
1.1 |
|
| 21 |
tdb |
1.2 |
// run a few tests |
| 22 |
tdb |
1.1 |
System.out.println("killigrew.ukc.ac.uk: " + acl.check("killigrew.ukc.ac.uk")); |
| 23 |
|
|
System.out.println("129.12.41.13: " + acl.check("129.12.41.13")); |
| 24 |
|
|
System.out.println("kruskal.18hp.net: " + acl.check("kruskal.18hp.net")); |
| 25 |
|
|
System.out.println("192.168.1.1: " + acl.check("192.168.1.1")); |
| 26 |
tdb |
1.2 |
|
| 27 |
|
|
// setup an ACLServerSocket putting our ACL in |
| 28 |
|
|
ACLServerSocket ss = null; |
| 29 |
|
|
try { |
| 30 |
|
|
ss = new ACLServerSocket(acl, 1337); |
| 31 |
|
|
} |
| 32 |
|
|
catch(IOException e) { |
| 33 |
|
|
System.out.println("exception: " + e); |
| 34 |
|
|
e.printStackTrace(); |
| 35 |
|
|
System.exit(1); |
| 36 |
|
|
} |
| 37 |
|
|
|
| 38 |
|
|
// start listening |
| 39 |
|
|
// valid connections (allowed by ACL) will get a message sent back |
| 40 |
|
|
while(true) { |
| 41 |
|
|
try { |
| 42 |
|
|
Socket s = ss.accept(); |
| 43 |
|
|
PrintWriter writer = new PrintWriter(s.getOutputStream(), true); |
| 44 |
|
|
writer.println("Connection suceeded from: " + s.getInetAddress().getHostName()); |
| 45 |
|
|
writer.println("Closing in 5 seconds"); |
| 46 |
|
|
try { Thread.sleep(5000); } catch(Exception ee) {} |
| 47 |
|
|
writer.close(); |
| 48 |
|
|
s.close(); |
| 49 |
|
|
} |
| 50 |
|
|
catch(IOException e) { |
| 51 |
|
|
System.out.println("exception: " + e); |
| 52 |
|
|
e.printStackTrace(); |
| 53 |
|
|
System.exit(1); |
| 54 |
|
|
} |
| 55 |
|
|
} |
| 56 |
tdb |
1.1 |
} |
| 57 |
|
|
|
| 58 |
|
|
} |