1 18 19 23 package freecs.content; 24 25 import freecs.Server; 26 import java.net.InetAddress ; 27 import java.net.UnknownHostException ; 28 import java.nio.channels.SocketChannel ; 29 import java.nio.channels.SelectionKey ; 30 31 public class Connection { 32 public InetAddress peerAddress = null; 34 public String peerIp = null; 35 36 public String clientIp=null; 38 public InetAddress clientAddress=null; 39 40 public String [] fwChain = null; 42 43 public boolean isDirectlyConnected=false; 45 46 public Connection (SelectionKey sk) { 47 peerAddress = ((SocketChannel ) 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 sk, String [] fwChain, boolean idc) { 56 isDirectlyConnected = idc; 57 peerAddress = ((SocketChannel ) 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 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 toStringVal = null; 82 public String toString () { 83 if (toStringVal == null) { 84 StringBuffer sb = new StringBuffer (); 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 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 getBanKey() { 138 if (this.clientIp!=null) 139 return this.clientIp; 140 if (this.fwChain!=null && fwChain.length > 0) { 141 StringBuffer sb = new StringBuffer (); 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 |