001: /**
002: * Copyright (C) 2003 Manfred Andres
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: */
018:
019: /**
020: * The Connection-class is used to store connection-specific data
021: * only once.
022: */package freecs.content;
023:
024: import freecs.Server;
025: import java.net.InetAddress;
026: import java.net.UnknownHostException;
027: import java.nio.channels.SocketChannel;
028: import java.nio.channels.SelectionKey;
029:
030: public class Connection {
031: // this is the remote-address of the socket-connection
032: public InetAddress peerAddress = null;
033: public String peerIp = null;
034:
035: // this represents the real-client-ip if it has been identified
036: public String clientIp = null;
037: public InetAddress clientAddress = null;
038:
039: // this is the proxy forward-chain (from the xForwardedFor headerfield)
040: public String[] fwChain = null;
041:
042: // true if the client has a direct-socket-connectino
043: public boolean isDirectlyConnected = false;
044:
045: public Connection(SelectionKey sk) {
046: peerAddress = ((SocketChannel) sk.channel()).socket()
047: .getInetAddress();
048: peerIp = peerAddress.getHostAddress();
049: clientAddress = null;
050: clientIp = null;
051: if (Server.TRACE_CREATE_AND_FINALIZE)
052: Server.log(this ,
053: "++++++++++++++++++++++++++++++++++++++++CREATE",
054: Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
055: }
056:
057: public Connection(SelectionKey sk, String[] fwChain, boolean idc) {
058: isDirectlyConnected = idc;
059: peerAddress = ((SocketChannel) sk.channel()).socket()
060: .getInetAddress();
061: if (peerAddress != null)
062: peerIp = peerAddress.getHostAddress();
063: if (fwChain != null) {
064: isDirectlyConnected = false;
065: this .fwChain = fwChain;
066: if (fwChain[0].indexOf(".") > -1)
067: try {
068: clientAddress = InetAddress.getByName(fwChain[0]);
069: if (clientAddress != null)
070: clientIp = clientAddress.getHostAddress();
071: } catch (UnknownHostException uhe) {
072: Server.debug(this ,
073: "Unable to determine real IP for "
074: + fwChain[0], uhe,
075: Server.MSG_STATE, Server.LVL_MINOR);
076: }
077: return;
078: } else if (!idc) {
079: return;
080: }
081: clientAddress = peerAddress;
082: if (peerAddress != null)
083: clientIp = clientAddress.getHostAddress();
084: if (Server.TRACE_CREATE_AND_FINALIZE)
085: Server.log(this ,
086: "++++++++++++++++++++++++++++++++++++++++CREATE",
087: Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
088: }
089:
090: private volatile String toStringVal = null;
091:
092: public String toString() {
093: if (toStringVal == null) {
094: StringBuffer sb = new StringBuffer();
095: if (!isDirectlyConnected)
096: sb.append("proxy=");
097: sb.append(peerIp);
098: if (!isDirectlyConnected && clientIp != null) {
099: sb.append(" clientIp=");
100: sb.append(clientIp);
101: }
102: toStringVal = sb.toString();
103: }
104: return toStringVal;
105: }
106:
107: public boolean isBanable() {
108: if (this .clientIp != null)
109: return true;
110: if (fwChain != null && fwChain.length > 0 && fwChain[0] != null
111: && fwChain[0].length() > 0)
112: return true;
113: return false;
114: }
115:
116: public boolean equals(Object o) {
117: if (o == null)
118: return false;
119: if (!(o instanceof Connection))
120: return false;
121: Connection c = (Connection) o;
122: if (this .clientIp == null && c.clientIp != null)
123: return false;
124: if (this .clientIp != null && !this .clientIp.equals(c.clientIp))
125: return false;
126: if (this .peerIp == null && c.peerIp != null)
127: return false;
128: if (this .peerIp != null && !this .peerIp.equals(c.peerIp))
129: return false;
130: if (this .fwChain == null && c.fwChain != null)
131: return false;
132: if (this .fwChain != null && !this .fwChain.equals(c.fwChain))
133: return false;
134: return true;
135: }
136:
137: public int hashCode() {
138: if (this .clientIp != null && this .peerIp != null) {
139: return (this .clientIp + "/" + this .peerIp).hashCode();
140: } else if (this .peerIp != null) {
141: return this .peerIp.hashCode();
142: } else {
143: return this .clientIp.hashCode();
144: }
145: }
146:
147: public String getBanKey() {
148: if (this .clientIp != null)
149: return this .clientIp;
150: if (this .fwChain != null && fwChain.length > 0) {
151: StringBuffer sb = new StringBuffer();
152: sb.append(this .peerIp);
153: sb.append(":").append(fwChain[fwChain.length - 1]);
154: return sb.toString();
155: }
156: return null;
157: }
158:
159: public void finalize() {
160: if (Server.TRACE_CREATE_AND_FINALIZE)
161: Server
162: .log(
163: this ,
164: "----------------------------------------FINALIZED",
165: Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
166: }
167: }
|