1- /*
2- * Copyright (c) [2016] [ <ether.camp> ]
3- * This file is part of the ethereumJ library.
4- *
5- * The ethereumJ library is free software: you can redistribute it and/or modify
6- * it under the terms of the GNU Lesser General Public License as published by
7- * the Free Software Foundation, either version 3 of the License, or
8- * (at your option) any later version.
9- *
10- * The ethereumJ library is distributed in the hope that it will be useful,
11- * but WITHOUT ANY WARRANTY; without even the implied warranty of
12- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13- * GNU Lesser General Public License for more details.
14- *
15- * You should have received a copy of the GNU Lesser General Public License
16- * along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
17- */
181package org .tron .common .overlay .server ;
192
203import static org .tron .protos .Protocol .ReasonCode .DUPLICATE_PEER ;
214import static org .tron .protos .Protocol .ReasonCode .TOO_MANY_PEERS ;
5+ import static org .tron .protos .Protocol .ReasonCode .TOO_MANY_PEERS_WITH_SAME_IP ;
226import static org .tron .protos .Protocol .ReasonCode .UNKNOWN ;
237
248import com .google .common .cache .Cache ;
259import com .google .common .cache .CacheBuilder ;
26- import io .netty .util .internal .ConcurrentSet ;
2710import java .net .InetAddress ;
2811import java .net .InetSocketAddress ;
2912import java .util .Collection ;
30- import java .util .List ;
3113import java .util .Map ;
32- import java .util .Set ;
3314import java .util .concurrent .ConcurrentHashMap ;
3415import java .util .concurrent .TimeUnit ;
16+ import lombok .Getter ;
3517import org .slf4j .Logger ;
3618import org .slf4j .LoggerFactory ;
3719import org .springframework .beans .factory .annotation .Autowired ;
3820import org .springframework .stereotype .Component ;
3921import org .tron .common .overlay .client .PeerClient ;
4022import org .tron .common .overlay .discover .node .Node ;
41- import org .tron .common .overlay .discover .node .NodeManager ;
4223import org .tron .core .config .args .Args ;
4324import org .tron .core .db .ByteArrayWrapper ;
4425import org .tron .protos .Protocol .ReasonCode ;
@@ -59,11 +40,14 @@ public class ChannelManager {
5940 private Cache <InetAddress , ReasonCode > recentlyDisconnected = CacheBuilder .newBuilder ().maximumSize (1000 )
6041 .expireAfterWrite (30 , TimeUnit .SECONDS ).recordStats ().build ();
6142
43+ @ Getter
6244 private Map <InetAddress , Node > trustPeers = new ConcurrentHashMap ();
6345
6446 private Args args = Args .getInstance ();
6547
66- private int maxActivePeers = args .getNodeMaxActiveNodes () > 0 ? args .getNodeMaxActiveNodes () : 30 ;
48+ private int maxActivePeers = args .getNodeMaxActiveNodes ();
49+
50+ private int getMaxActivePeersWithSameIp = args .getNodeMaxActiveNodesWithSameIp ();
6751
6852 private PeerServer peerServer ;
6953
@@ -77,12 +61,12 @@ private ChannelManager(final PeerServer peerServer, final PeerClient peerClient)
7761 this .peerServer = peerServer ;
7862 this .peerClient = peerClient ;
7963
80- if (args .getNodeListenPort () > 0 ) {
64+ if (this . args .getNodeListenPort () > 0 ) {
8165 new Thread (() -> peerServer .start (Args .getInstance ().getNodeListenPort ()),
8266 "PeerServerThread" ).start ();
8367 }
8468
85- for (Node node : args .getTrustNodes ()){
69+ for (Node node : args .getPassiveNodes ()){
8670 trustPeers .put (new InetSocketAddress (node .getHost (), node .getPort ()).getAddress () , node );
8771 }
8872 logger .info ("Trust peer size {}" , trustPeers .size ());
@@ -138,6 +122,11 @@ public synchronized boolean processPeer(Channel peer) {
138122 peer .disconnect (TOO_MANY_PEERS );
139123 return false ;
140124 }
125+
126+ if (getConnectionNum (peer .getInetAddress ()) >= getMaxActivePeersWithSameIp ){
127+ peer .disconnect (TOO_MANY_PEERS_WITH_SAME_IP );
128+ return false ;
129+ }
141130 }
142131
143132 if (activePeers .containsKey (peer .getNodeIdWrapper ())) {
@@ -155,6 +144,16 @@ public synchronized boolean processPeer(Channel peer) {
155144 return true ;
156145 }
157146
147+ public int getConnectionNum (InetAddress inetAddress ){
148+ int cnt = 0 ;
149+ for (Channel channel : activePeers .values ()){
150+ if (channel .getInetAddress ().equals (inetAddress )){
151+ cnt ++;
152+ }
153+ }
154+ return cnt ;
155+ }
156+
158157 public Collection <Channel > getActivePeers () {
159158 return activePeers .values ();
160159 }
@@ -167,10 +166,6 @@ public Cache<InetAddress, ReasonCode> getBadPeers(){
167166 return this .badPeers ;
168167 }
169168
170- public Map <InetAddress , Node > getTrustPeers (){
171- return trustPeers ;
172- }
173-
174169 public void close () {
175170 peerServer .close ();
176171 peerClient .close ();
0 commit comments