Skip to content

Commit

Permalink
Fix incorrect "something went wrong" message + used SLF4J to log
Browse files Browse the repository at this point in the history
  • Loading branch information
bramar2 committed Aug 5, 2023
1 parent e4a21e9 commit 79964dd
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/main/java/me/bramar/undetectedselenium/DriverPatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.io.*;
Expand All @@ -21,6 +23,7 @@
// ported from Python undetected-chromedriver
@Getter
public class DriverPatcher {
private static final Logger logger = LoggerFactory.getLogger(DriverPatcher.class);
private static final String chromeLabsRepo = "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing";
private final File latestStable;
private String urlRepo = "https://chromedriver.storage.googleapis.com";
Expand Down Expand Up @@ -352,7 +355,6 @@ public String fetchReleaseNumber() throws IOException {
}
}
public void patchExe() throws IOException {
// if(true) return;
StringBuilder contentBuilder = new StringBuilder();
try(FileReader reader = new FileReader(this.executablePath, StandardCharsets.ISO_8859_1)) {
char[] buffer = new char[8192];
Expand All @@ -367,19 +369,19 @@ public void patchExe() throws IOException {
}
}
String content = contentBuilder.toString();
int oldLength = content.length();
Matcher matcher = Pattern.compile("\\{window\\.cdc.*?;\\}").matcher(content);
boolean found = false;
while(matcher.find()) {
found = true;
int len = matcher.group().length();
String toReplace = "{let a=\"undetected chromedriver\"}";
String filler = "A".repeat(len - toReplace.length());
toReplace = toReplace.replace("chromedriver", "chromedriver" + filler);
content = content.substring(0, matcher.start()) + toReplace + content.substring(matcher.end());
}
content = content.replaceAll("window\\.cdc", "window.arh");
if(content.length() == oldLength) {
System.out.println("Something went wrong patching the binary, could not find injection code block");
}
if(!found) logger.warn("Something went wrong patching the binary, could not find injection code block");

try(FileWriter writer = new FileWriter(this.executablePath, StandardCharsets.ISO_8859_1)) {
writer.write(content.toCharArray());
}
Expand Down

0 comments on commit 79964dd

Please sign in to comment.