KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > content > Connection


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */

18
19 /**
20  * The Connection-class is used to store connection-specific data
21  * only once.
22  */

23 package freecs.content;
24
25 import freecs.Server;
26 import java.net.InetAddress JavaDoc;
27 import java.net.UnknownHostException JavaDoc;
28 import java.nio.channels.SocketChannel JavaDoc;
29 import java.nio.channels.SelectionKey JavaDoc;
30
31 public class Connection {
32     // this is the remote-address of the socket-connection
33
public InetAddress JavaDoc peerAddress = null;
34     public String JavaDoc peerIp = null;
35     
36     // this represents the real-client-ip if it has been identified
37
public String JavaDoc clientIp=null;
38     public InetAddress JavaDoc clientAddress=null;
39     
40     // this is the proxy forward-chain (from the xForwardedFor headerfield)
41
public String JavaDoc[] fwChain = null;
42     
43     // true if the client has a direct-socket-connectino
44
public boolean isDirectlyConnected=false;
45     
46     public Connection (SelectionKey JavaDoc sk) {
47         peerAddress = ((SocketChannel JavaDoc) sk.channel()).socket().getInetAddress();
48         peerIp = peerAddress.getHostAddress();
49         clientAddress = null;
50         clientIp = null;
51         if (Server.TRACE_CREATE_AND_FINALIZE)
52             Server.log (this, "++++++++++++++++++++++++++++++++++++++++CREATE", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
53     }
54     
55     public Connection (SelectionKey JavaDoc sk, String JavaDoc[] fwChain, boolean idc) {
56         isDirectlyConnected = idc;
57         peerAddress = ((SocketChannel JavaDoc) sk.channel()).socket().getInetAddress();
58         if (peerAddress != null)
59             peerIp = peerAddress.getHostAddress();
60         if (fwChain != null) {
61             isDirectlyConnected = false;
62             this.fwChain = fwChain;
63             if (fwChain[0].indexOf(".") > -1) try {
64                 clientAddress = InetAddress.getByName(fwChain[0]);
65                 if (clientAddress != null)
66                     clientIp = clientAddress.getHostAddress();
67             } catch (UnknownHostException JavaDoc uhe) {
68                 Server.debug (this, "Unable to determine real IP for " + fwChain[0], uhe, Server.MSG_STATE, Server.LVL_MINOR);
69             }
70             return;
71         } else if (!idc) {
72             return;
73         }
74         clientAddress = peerAddress;
75         if (peerAddress != null)
76             clientIp = clientAddress.getHostAddress();
77         if (Server.TRACE_CREATE_AND_FINALIZE)
78             Server.log (this, "++++++++++++++++++++++++++++++++++++++++CREATE", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
79     }
80
81     private volatile String JavaDoc toStringVal = null;
82     public String JavaDoc toString () {
83         if (toStringVal == null) {
84             StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
85             if (!isDirectlyConnected)
86                 sb.append ("proxy=");
87             sb.append (peerIp);
88             if (!isDirectlyConnected && clientIp != null) {
89                 sb.append (" clientIp=");
90                 sb.append (clientIp);
91             }
92             toStringVal=sb.toString();
93         }
94         return toStringVal;
95     }
96     
97     public boolean isBanable () {
98         if (this.clientIp != null)
99             return true;
100         if (fwChain!=null && fwChain.length > 0
101                 && fwChain[0]!=null && fwChain[0].length() > 0)
102             return true;
103         return false;
104     }
105     
106     public boolean equals (Object JavaDoc o) {
107         if (o == null)
108             return false;
109         if (!(o instanceof Connection))
110             return false;
111         Connection c = (Connection) o;
112         if (this.clientIp == null && c.clientIp != null)
113             return false;
114         if (this.clientIp != null && !this.clientIp.equals(c.clientIp))
115             return false;
116         if (this.peerIp == null && c.peerIp != null)
117             return false;
118         if (this.peerIp != null && !this.peerIp.equals(c.peerIp))
119             return false;
120         if (this.fwChain == null && c.fwChain != null)
121             return false;
122         if (this.fwChain != null && !this.fwChain.equals(c.fwChain))
123             return false;
124         return true;
125     }
126
127     public int hashCode () {
128         if (this.clientIp!=null && this.peerIp != null) {
129             return (this.clientIp + "/" + this.peerIp).hashCode();
130         } else if (this.peerIp!=null) {
131             return this.peerIp.hashCode();
132         } else {
133             return this.clientIp.hashCode();
134         }
135     }
136     
137     public String JavaDoc getBanKey() {
138         if (this.clientIp!=null)
139             return this.clientIp;
140         if (this.fwChain!=null && fwChain.length > 0) {
141             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
142             sb.append (this.peerIp);
143             sb.append (":").append(fwChain[fwChain.length -1]);
144             return sb.toString();
145         }
146         return null;
147     }
148
149     public void finalize() {
150         if (Server.TRACE_CREATE_AND_FINALIZE)
151             Server.log(this, "----------------------------------------FINALIZED", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
152     }
153 }
Popular Tags