Skip to content

Commit

Permalink
Peer port honoring work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
alukin committed Apr 24, 2019
1 parent 14f9335 commit 6c4e1f9
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 80 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -21,3 +21,4 @@ nbactions.xml
/apl-exec/apl.log
**/apl.log
**/pom.xml.versionsBackup
/apl-core/nbproject/
Expand Up @@ -24,6 +24,7 @@
import com.apollocurrency.aplwallet.apl.core.app.TransactionProcessorImpl;
import com.apollocurrency.aplwallet.apl.core.db.TransactionalDataSource;
import com.apollocurrency.aplwallet.apl.util.AplException;
import com.apollocurrency.aplwallet.apl.util.injectable.PropertiesHolder;
import javax.enterprise.inject.Vetoed;
import org.json.simple.JSONStreamAware;

Expand All @@ -40,6 +41,7 @@ public abstract class AbstractAPIRequestHandler {
private DatabaseManager databaseManager;
protected static AdminPasswordVerifier apw = CDI.current().select(AdminPasswordVerifier.class).get();
protected ElGamalEncryptor elGamal = CDI.current().select(ElGamalEncryptor.class).get();
protected static PropertiesHolder propertiesHolder = CDI.current().select(PropertiesHolder.class).get();

protected Blockchain lookupBlockchain() {
if (blockchain == null) blockchain = CDI.current().select(BlockchainImpl.class).get();
Expand Down
Expand Up @@ -44,6 +44,7 @@
import com.apollocurrency.aplwallet.apl.core.http.APITag;
import com.apollocurrency.aplwallet.apl.core.http.AbstractAPIRequestHandler;
import com.apollocurrency.aplwallet.apl.core.peer.Peers;
import com.apollocurrency.aplwallet.apl.util.Constants;
import com.apollocurrency.aplwallet.apl.util.UPnP;
import com.apollocurrency.aplwallet.apl.util.injectable.PropertiesHolder;
import org.json.simple.JSONObject;
Expand All @@ -55,7 +56,6 @@
import javax.enterprise.inject.spi.CDI;
@Vetoed
public final class GetState extends AbstractAPIRequestHandler {
private static PropertiesHolder propertiesHolder = CDI.current().select(PropertiesHolder.class).get();
private UPnP upnp = CDI.current().select(UPnP.class).get();

public GetState() {
Expand Down Expand Up @@ -105,7 +105,7 @@ public JSONStreamAware processRequest(HttpServletRequest req) {
response.put("maxMemory", Runtime.getRuntime().maxMemory());
response.put("totalMemory", Runtime.getRuntime().totalMemory());
response.put("freeMemory", Runtime.getRuntime().freeMemory());
response.put("peerPort", Peers.getDefaultPeerPort());
response.put("peerPort", Peers.myPort);
response.put("isOffline", propertiesHolder.isOffline());
response.put("needsAdminPassword", !apw.disableAdminPassword);
response.put("customLoginWarning", propertiesHolder.customLoginWarning());
Expand Down
Expand Up @@ -23,7 +23,6 @@
import com.apollocurrency.aplwallet.apl.core.app.EpochTime;
import javax.enterprise.inject.spi.CDI;

import com.apollocurrency.aplwallet.apl.core.app.Time;
import com.apollocurrency.aplwallet.apl.util.Version;
import com.apollocurrency.aplwallet.apl.crypto.Convert;

Expand Down Expand Up @@ -59,12 +58,13 @@ JSONStreamAware processRequest(JSONObject request, Peer peer) {
peerImpl.setLastUpdated(timeService.getEpochTime());
long origServices = peerImpl.getServices();
String servicesString = (String)request.get("services");
String announcedAddress = null;
peerImpl.setServices(servicesString != null ? Long.parseUnsignedLong(servicesString) : 0);
peerImpl.analyzeHallmark((String)request.get("hallmark"));
if (!Peers.ignorePeerAnnouncedAddress) {
String announcedAddress = Convert.emptyToNull((String) request.get("announcedAddress"));
announcedAddress = Convert.emptyToNull((String) request.get("announcedAddress"));
if (announcedAddress != null) {
announcedAddress = Peers.addressWithPort(announcedAddress.toLowerCase());
announcedAddress = announcedAddress.toLowerCase();
if (announcedAddress != null) {
if (!peerImpl.verifyAnnouncedAddress(announcedAddress)) {
LOG.debug("GetInfo: ignoring invalid announced address for " + peerImpl.getHost());
Expand Down Expand Up @@ -104,7 +104,7 @@ JSONStreamAware processRequest(JSONObject request, Peer peer) {
version = new Version(1, 0, 0);
}
if (LOG.isDebugEnabled()) {
LOG.debug("PEER-GetINFO: version {}", version);
LOG.debug("PEER-GetINFO: Addr: {}, version {}",announcedAddress, version);
}
peerImpl.setVersion(version);

Expand Down
Expand Up @@ -21,7 +21,6 @@
package com.apollocurrency.aplwallet.apl.core.peer;

import javax.enterprise.inject.spi.CDI;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand All @@ -31,9 +30,14 @@
import com.apollocurrency.aplwallet.apl.core.chainid.BlockchainConfig;
import com.apollocurrency.aplwallet.apl.crypto.Convert;
import com.apollocurrency.aplwallet.apl.crypto.Crypto;
import com.apollocurrency.aplwallet.apl.util.injectable.PropertiesHolder;

public final class Hallmark {

private static PropertiesHolder propertiesHolder= CDI.current().select(PropertiesHolder.class).get();;

private static BlockchainConfig blockchainConfig = CDI.current().select(BlockchainConfig.class).get();

public static int parseDate(String dateValue) {
return Integer.parseInt(dateValue.substring(0, 4)) * 10000
+ Integer.parseInt(dateValue.substring(5, 7)) * 100
Expand Down Expand Up @@ -129,9 +133,10 @@ public static Hallmark parseHallmark(String hallmarkString) {
private Hallmark(String hallmarkString, byte[] publicKey, byte[] signature, String host, int weight, int date, boolean isValid)
throws URISyntaxException {
this.hallmarkString = hallmarkString;
URI uri = new URI("http://" + host);
this.host = uri.getHost();
this.port = uri.getPort() == -1 ? Peers.getDefaultPeerPort() : uri.getPort();
PeerAddress pa = new PeerAddress(propertiesHolder);
pa.fromString(host);
this.host = pa.getHost();
this.port = pa.getPort();
this.publicKey = publicKey;
this.accountId = Account.getId(publicKey);
this.signature = signature;
Expand Down
Expand Up @@ -76,7 +76,9 @@ public long getCode() {
String getHost();

int getPort();


String getHostWithPort();

String getAnnouncedAddress();

State getState();
Expand Down
@@ -0,0 +1,84 @@
/*
* Copyright © 2018-2019 Apollo Foundation
*/
package com.apollocurrency.aplwallet.apl.core.peer;

import com.apollocurrency.aplwallet.apl.util.Constants;
import com.apollocurrency.aplwallet.apl.util.injectable.PropertiesHolder;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

/**
*
* @author alukin@gmail.com
*/
public class PeerAddress {
private InetAddress host;
private String hostName;
private Integer port;
private final PropertiesHolder propertiesHolder;


public PeerAddress(PropertiesHolder propertiesHolder) {
this.propertiesHolder = propertiesHolder;
try {
host = InetAddress.getByName("127.0.0.1");
} catch (UnknownHostException ex) {
}
port = getDefaultPeerPort();
}

public final Integer getDefaultPeerPort() {
return propertiesHolder.getIntProperty("apl.networkPeerServerPort", Constants.DEFAULT_PEER_PORT);
}

public void fromString(String addr){
try {
String a=addr.toLowerCase();
if(!a.startsWith("http")){
a="http://"+a;
}
URL u = new URL(a);
host=InetAddress.getByName(u.getHost());
port=u.getPort();
if(port==-1){
port=getDefaultPeerPort();
}
} catch (MalformedURLException | UnknownHostException ex) {
}
}

public String getHost() {
return host.getHostAddress();
}

public void setHost(String host){
hostName=host;
try {
this.host = InetAddress.getByName(host);
} catch (UnknownHostException ex) {
}
}

public Integer getPort() {
return port;
}

public void setPort(Integer port) {
this.port = port;
}

public String getAddrWithPort(){
if (host instanceof Inet6Address){
return "["+host.getHostAddress()+"]:"+port.toString();
} else{
return host.getHostAddress()+":"+port.toString();
}
}
public InetAddress getInetAddress(){
return host;
}
}
Expand Up @@ -56,6 +56,7 @@
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URI;
Expand Down Expand Up @@ -111,8 +112,8 @@ public final class PeerImpl implements Peer {
private final BlockchainConfig blockchainConfig;
private final Blockchain blockchain;
private volatile EpochTime timeService;


private PropertiesHolder propertiesHolder;
PeerImpl(String host,
String announcedAddress,
BlockchainConfig blockchainConfig,
Expand All @@ -121,6 +122,7 @@ public final class PeerImpl implements Peer {
PropertiesHolder propertiesHolder
) {
this.host = host;
this.propertiesHolder=propertiesHolder;
this.announcedAddress = announcedAddress;
try {
this.port = getURI(false).getPort();
Expand All @@ -142,7 +144,15 @@ public final class PeerImpl implements Peer {
public String getHost() {
return host;
}


@Override
public String getHostWithPort(){
PeerAddress pa = new PeerAddress(propertiesHolder);
pa.setHost(host);
pa.setPort(port);
return pa.getAddrWithPort();
}

@Override
public State getState() {
return state;
Expand Down Expand Up @@ -332,11 +342,14 @@ public String getAnnouncedAddress() {
return announcedAddress;
}

void setAnnouncedAddress(String announcedAddress) {
void setAnnouncedAddress(String announcedAddress) throws MalformedURLException, UnknownHostException {
if (announcedAddress != null && announcedAddress.length() > Peers.MAX_ANNOUNCED_ADDRESS_LENGTH) {
throw new IllegalArgumentException("Announced address too long: " + announcedAddress.length());
}
this.announcedAddress = announcedAddress;
PeerAddress pa = new PeerAddress(propertiesHolder);
pa.fromString(announcedAddress);
this.announcedAddress = pa.getAddrWithPort();
this.port=pa.getPort();
if (announcedAddress != null) {
try {
this.port = getURI(false).getPort();
Expand All @@ -350,7 +363,7 @@ void setAnnouncedAddress(String announcedAddress) {

@Override
public int getPort() {
return port <= 0 ? Peers.getDefaultPeerPort() : port;
return port;
}

@Override
Expand Down Expand Up @@ -573,7 +586,7 @@ public JSONObject send(final JSONStreamAware request, UUID targetChainId, int ma
//
// Send the request using HTTP
//
String urlString = "http://" + host + ":" + getPort() + "/apl";
String urlString = "http://" + getHostWithPort() + "/apl";
URL url = new URL(urlString);
LOG.debug("Connecting to URL = {}...", urlString);
if (communicationLoggingMask != 0)
Expand Down Expand Up @@ -752,8 +765,6 @@ public void handshake(UUID targetChainId) {
if (!Peers.ignorePeerAnnouncedAddress) {
String newAnnouncedAddress = Convert.emptyToNull((String) response.get("announcedAddress"));
if (newAnnouncedAddress != null) {
newAnnouncedAddress = Peers.addressWithPort(newAnnouncedAddress.toLowerCase());
if (newAnnouncedAddress != null) {
if (!verifyAnnouncedAddress(newAnnouncedAddress)) {
LOG.debug("Connect: new announced address for " + host + " not accepted");
if (!verifyAnnouncedAddress(announcedAddress)) {
Expand All @@ -773,14 +784,13 @@ public void handshake(UUID targetChainId) {
return;
}
}
}
} else {
Peers.setAnnouncedAddress(this, host);
}
}

if (announcedAddress == null) {
if (hallmark == null || hallmark.getPort() == Peers.getDefaultPeerPort()) {
if (hallmark == null){// || hallmark.getPort() == Peers.getDefaultPeerPort()) {
Peers.setAnnouncedAddress(this, host);
LOG.debug("Connected to peer without announced address, setting to " + host);
} else {
Expand Down Expand Up @@ -812,7 +822,8 @@ boolean verifyAnnouncedAddress(String newAnnouncedAddress) {
}
try {
URI uri = getURI(false);
int announcedPort = uri.getPort() == -1 ? Peers.getDefaultPeerPort() : uri.getPort();
PeerAddress pa = new PeerAddress(propertiesHolder);
int announcedPort = pa.getPort();
if (hallmark != null && announcedPort != hallmark.getPort()) {
LOG.debug("Announced port " + announcedPort + " does not match hallmark " + hallmark.getPort() + ", ignoring hallmark for " + host);
unsetHallmark();
Expand Down

0 comments on commit 6c4e1f9

Please sign in to comment.