Merge branch 'Qortal:master' into master

This commit is contained in:
Jürg Schulthess 2023-07-10 20:53:24 +02:00 committed by GitHub
commit ff7a87ab18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 106 additions and 77 deletions

View File

@ -16,7 +16,9 @@ import javax.ws.rs.core.Context;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Enumeration;
@ -44,7 +46,26 @@ public class DevProxyServerResource {
try {
String source = DevProxyManager.getInstance().getSourceHostAndPort();
// Convert localhost / 127.0.0.1 to IPv6 [::1]
if (!inPath.startsWith("/")) {
inPath = "/" + inPath;
}
String queryString = request.getQueryString() != null ? "?" + request.getQueryString() : "";
// Open URL
URL url = new URL(String.format("http://%s%s%s", source, inPath, queryString));
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// Proxy the request data
this.proxyRequestToConnection(request, con);
try {
// Make the request and proxy the response code
response.setStatus(con.getResponseCode());
}
catch (ConnectException e) {
// Tey converting localhost / 127.0.0.1 to IPv6 [::1]
if (source.startsWith("localhost") || source.startsWith("127.0.0.1")) {
int port = 80;
String[] parts = source.split(":");
@ -54,16 +75,25 @@ public class DevProxyServerResource {
source = String.format("[::1]:%d", port);
}
if (!inPath.startsWith("/")) {
inPath = "/" + inPath;
// Retry connection
url = new URL(String.format("http://%s%s%s", source, inPath, queryString));
con = (HttpURLConnection) url.openConnection();
this.proxyRequestToConnection(request, con);
response.setStatus(con.getResponseCode());
}
String queryString = request.getQueryString() != null ? "?" + request.getQueryString() : "";
// Proxy the response data back to the caller
this.proxyConnectionToResponse(con, response, inPath);
// Open URL
String urlString = String.format("http://%s%s%s", source, inPath, queryString);
URL url = new URL(urlString);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
throw ApiExceptionFactory.INSTANCE.createCustomException(request, ApiError.INVALID_CRITERIA, e.getMessage());
}
return response;
}
private void proxyRequestToConnection(HttpServletRequest request, HttpURLConnection con) throws ProtocolException {
// Proxy the request method
con.setRequestMethod(request.getMethod());
// Proxy the request headers
@ -75,11 +105,9 @@ public class DevProxyServerResource {
}
// TODO: proxy any POST parameters from "request" to "con"
}
// Proxy the response code
int responseCode = con.getResponseCode();
response.setStatus(responseCode);
private void proxyConnectionToResponse(HttpURLConnection con, HttpServletResponse response, String inPath) throws IOException {
// Proxy the response headers
for (int i = 0; ; i++) {
String headerKey = con.getHeaderFieldKey(i);
@ -131,12 +159,6 @@ public class DevProxyServerResource {
response.setContentLength(data.length);
response.getOutputStream().write(data);
}
} catch (IOException e) {
throw ApiExceptionFactory.INSTANCE.createCustomException(request, ApiError.INVALID_CRITERIA, e.getMessage());
}
return response;
}
}

View File

@ -743,8 +743,14 @@ public class OnlineAccountsManager {
if (onlineAccounts == null)
onlineAccounts = this.latestBlocksOnlineAccounts.get(timestamp);
if (onlineAccounts != null)
blocksOnlineAccounts.removeAll(onlineAccounts);
if (onlineAccounts != null) {
// Remove accounts with matching timestamp, nonce, and public key
final Set<OnlineAccountData> finalOnlineAccounts = onlineAccounts;
blocksOnlineAccounts.removeIf(a1 -> finalOnlineAccounts.stream()
.anyMatch(a2 -> a2.getTimestamp() == a1.getTimestamp() &&
Objects.equals(a2.getNonce(), a1.getNonce()) &&
Arrays.equals(a2.getPublicKey(), a1.getPublicKey())));
}
}
/**

View File

@ -25,7 +25,8 @@ public class NamesDatabaseIntegrityCheck {
TransactionType.REGISTER_NAME,
TransactionType.UPDATE_NAME,
TransactionType.BUY_NAME,
TransactionType.SELL_NAME
TransactionType.SELL_NAME,
TransactionType.CANCEL_SELL_NAME
);
private List<TransactionData> nameTransactions = new ArrayList<>();