Skip to content

Commit 51e415d

Browse files
author
ukanth
committed
#1458 - fix hostname is not updated
1 parent 0e91025 commit 51e415d

4 files changed

Lines changed: 42 additions & 20 deletions

File tree

app/src/main/java/dev/ukanth/ufirewall/activity/LogDetailActivity.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,13 @@ private void initializeRecyclerView(final Context ctx) {
174174
menu.add(0, v.getId(), 4, R.string.ping_source);
175175
menu.add(0, v.getId(), 5, R.string.resolve_destination);
176176
menu.add(0, v.getId(), 6, R.string.resolve_source);
177-
menu.add(0, v.getId(), 9, "Block this destination permanently");
178-
menu.add(0, v.getId(), 10, "Whitelist this destination");
177+
// Only show Copy Domain if a hostname was resolved
178+
String hostname = current_selected_logData.getHostname();
179+
if (hostname != null && !hostname.trim().isEmpty() && !hostname.equals(current_selected_logData.getDst())) {
180+
menu.add(0, v.getId(), 9, R.string.copy_domain);
181+
}
182+
menu.add(0, v.getId(), 10, "Block this destination permanently");
183+
menu.add(0, v.getId(), 11, "Whitelist this destination");
179184
LogPreference logPreference = SQLite.select()
180185
.from(LogPreference.class)
181186
.where(LogPreference_Table.uid.eq(uid)).querySingle();
@@ -266,10 +271,19 @@ public boolean onContextItemSelected(MenuItem item) {
266271
case 8:
267272
G.updateLogNotification(uid, true);
268273
break;
269-
case 9: // Block destination permanently
274+
case 9: // Copy Domain
275+
String domain = current_selected_logData.getHostname();
276+
if (domain != null && !domain.trim().isEmpty() && !domain.equals(current_selected_logData.getDst())) {
277+
Api.copyToClipboard(LogDetailActivity.this, domain);
278+
Api.toast(LogDetailActivity.this, getString(R.string.domain_copied));
279+
} else {
280+
Api.toast(LogDetailActivity.this, getString(R.string.no_domain_resolved));
281+
}
282+
break;
283+
case 10: // Block destination permanently
270284
showBlockDestinationDialog();
271285
break;
272-
case 10: // Whitelist destination
286+
case 11: // Whitelist destination
273287
showWhitelistDestinationDialog();
274288
break;
275289

app/src/main/java/dev/ukanth/ufirewall/log/LogInfo.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,16 +322,8 @@ public static LogInfo parseLogs(String result, final Context ctx, String pattern
322322
address.append(":");
323323
address.append(logInfo.dpt);
324324
logInfo.type = type;
325-
if (G.showHost() && (G.isDoKey(ctx) || G.isDonate())) {
326-
try {
327-
String add = InetAddress.getByName(logInfo.dst).getHostName();
328-
if (add != null) {
329-
logInfo.host = add;
330-
address.append("(").append(add).append(") ");
331-
}
332-
} catch (Exception e) {
333-
}
334-
}
325+
// Hostname resolution is handled asynchronously in LogService.store()
326+
// to avoid blocking the log parsing callback thread
335327
address.append("\n");
336328
logInfo.timestamp = System.currentTimeMillis();
337329
logInfo.uidString = address.toString();

app/src/main/java/dev/ukanth/ufirewall/service/LogService.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -801,13 +801,26 @@ private static void store(final LogInfo logInfo, Context context) {
801801
data.setUid(logInfo.uid);
802802
data.setAppName(logInfo.appName);
803803
data.setType(logInfo.type);
804-
if (G.isDoKey(context) || G.isDonate()) {
805-
try {
806-
data.setHostname(logInfo.host != null ? logInfo.host : "");
807-
} catch (Exception e) {
808-
}
809-
}
810804
data.setType(0);
805+
806+
// Resolve hostname asynchronously if enabled
807+
if (G.showHost() && logInfo.dst != null && !logInfo.dst.isEmpty()) {
808+
final String dstIp = logInfo.dst;
809+
final LogData dataRef = data;
810+
new Thread(() -> {
811+
try {
812+
String hostname = java.net.InetAddress.getByName(dstIp).getHostName();
813+
if (hostname != null && !hostname.equals(dstIp)) {
814+
dataRef.setHostname(hostname);
815+
FlowManager.getDatabase(LogDatabase.class)
816+
.beginTransactionAsync(dw -> dataRef.save(dw))
817+
.build().execute();
818+
}
819+
} catch (Exception e) {
820+
// DNS resolution failed, hostname will remain empty
821+
}
822+
}, "LogService-DNS-" + dstIp.hashCode()).start();
823+
}
811824
FlowManager.getDatabase(LogDatabase.class).beginTransactionAsync(databaseWrapper ->
812825
data.save(databaseWrapper)).build().execute();
813826
}

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,9 @@
520520
<string name="copy_text">Copy text</string>
521521
<string name="destination_copied">Destination copied</string>
522522
<string name="source_copied">Source copied</string>
523+
<string name="copy_domain">Copy Domain</string>
524+
<string name="domain_copied">Domain copied</string>
525+
<string name="no_domain_resolved">No domain resolved for this entry</string>
523526
<string name="select_the_action">Select The Action</string>
524527
<string name="result_copied_to_clipboard">Result copied to clipboard</string>
525528
<string name="result">Result</string>

0 commit comments

Comments
 (0)