Skip to content

Commit

Permalink
Merge pull request #4 from Nomi/debug
Browse files Browse the repository at this point in the history
Version 2
  • Loading branch information
Nomi authored Dec 11, 2023
2 parents 82f2e64 + 4740ec2 commit 0c5944e
Show file tree
Hide file tree
Showing 39 changed files with 1,870 additions and 517 deletions.
146 changes: 146 additions & 0 deletions Auxiliary/BrowserDiscoveryUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
using AdvancedSharpAdbClient;
using ChromeDroid_TabMan.DTOs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static ChromeDroid_TabMan.Auxiliary.ImportUtils;
using AdvancedSharpAdbClient.DeviceCommands;
using ChromeDroid_TabMan.Connection_and_Import;
using System.Net.Http;
using System.Net.Http.Json;
using System.Security.Cryptography.X509Certificates;

namespace ChromeDroid_TabMan.Auxiliary
{
public static class BrowserDiscoveryUtils
{
public static List<BrowserComboItem> GetDefaultBrowserComboItems()
{
return new List<BrowserComboItem>
{
new BrowserComboItem("Chrome",ConfigHelper.ADB.Chrome_PackageName,ConfigHelper.ADB.Chrome_ForwardParameter_Remote,true,DiscoveryStateEnum.NotSearchedFor),
new BrowserComboItem("Opera",ConfigHelper.ADB.Opera_PackageName,ConfigHelper.ADB.Opera_ForwardParameter_Remote,true,DiscoveryStateEnum.NotSearchedFor),
new BrowserComboItem("SamsungInternet",ConfigHelper.ADB.SamsungInternet_PackageName,ConfigHelper.ADB.SamsungInternet_ForwardParameter_Remote,true,DiscoveryStateEnum.NotSearchedFor),
new BrowserComboItem("Edge",ConfigHelper.ADB.Edge_PackageName,ConfigHelper.ADB.EdgeAndBraveAndChrome_Base_ForwardParameterRemote__MissingPidAtEnd,false,DiscoveryStateEnum.NotSearchedFor),
new BrowserComboItem("Brave",ConfigHelper.ADB.Brave_PackageName,ConfigHelper.ADB.EdgeAndBraveAndChrome_Base_ForwardParameterRemote__MissingPidAtEnd,false,DiscoveryStateEnum.NotSearchedFor)
};
}

public static List<string> GetDevToolsSockets(string adbPath)
{
ClientAndDevice_Adb clientAndDevice_Adb = ImportUtils.ConnectAndGetAdbClientAndDevice(adbPath);
AdbClient client = clientAndDevice_Adb.client;
DeviceData device = clientAndDevice_Adb.device;

ConsoleOutputReceiver cOR = new();
client.ExecuteShellCommand(device, @"ss -a 2>/dev/null| grep devtools| cut -F 5", cOR);
string response = cOR.ToString();

return response.Split("\n").ToList();
}

public static void VerifyExistingSockets(List<BrowserComboItem> browserComboItemsToVerify, List<string> devToolsSocketsFound, string adbPath) //remember that C# passes objects and collections of objects as references.
{
for (int i = 0; i < browserComboItemsToVerify.Count; i++)
{
var browser = browserComboItemsToVerify[i];
var currBD = browser.BrowserDetails;

var socketFullName = browser.BrowserDetails.SocketNameFullOrPartial;
if ((!currBD.IsSocketNameFull && currBD.PackageName != null) || currBD.PackageName==ConfigHelper.ADB.Chrome_PackageName)//the || currBD.PackageName==ConfigHelper.ADB.Chrome_PackageName condition fixes the condition where one of the other chromium browsers somehow gets the name default devtools socket before chrome.
{

try
{
socketFullName = currBD.SocketNameFullOrPartial + "_" + ImportUtils.GetChromiumBrowserPid(adbPath, currBD.PackageName, false);
}
catch (PidNotParsedException pidEx)
{ //This mostly means the browser is not running, so we can safely mark the browser's sockets as not found.
browser.BrowserDetails = new BrowserDetailsStruct(currBD.BrowserName, currBD.PackageName, currBD.SocketNameFullOrPartial, currBD.IsSocketNameFull, DiscoveryStateEnum.NotFound);
continue;
}
}
if (devToolsSocketsFound.Any(s => (socketFullName == "localabstract:" + s.Replace("\r", "").Replace("\n", "").Replace("@", ""))))
{
DiscoveryStateEnum discoveryState = DiscoveryStateEnum.Verified;
if (currBD.PackageName == ConfigHelper.ADB.Chrome_PackageName && socketFullName != ConfigHelper.ADB.Chrome_ForwardParameter_Remote)
discoveryState = DiscoveryStateEnum.RediscoveredAndFixed;
else if (!currBD.IsSocketNameFull)
discoveryState = DiscoveryStateEnum.RediscoveredAndFilledRestOfTheSocket;
browser.BrowserDetails = new BrowserDetailsStruct(currBD.BrowserName, currBD.PackageName, socketFullName, true, discoveryState);
devToolsSocketsFound.RemoveAll(s => (socketFullName == "localabstract:" + s.Replace("\r", "").Replace("\n", "").Replace("@", "")));
}
else if(currBD.PackageName==ConfigHelper.ADB.Chrome_PackageName && devToolsSocketsFound.Any(s=>s==ConfigHelper.ADB.Chrome_ForwardParameter_Remote))
{
browser.BrowserDetails = new BrowserDetailsStruct(currBD.BrowserName, currBD.PackageName, ConfigHelper.ADB.Chrome_ForwardParameter_Remote, true, DiscoveryStateEnum.Verified);
//not removing this from devTools so that it can be verified later in the next stage.
}
else
{
browser.BrowserDetails = new BrowserDetailsStruct(currBD.BrowserName, currBD.PackageName, currBD.SocketNameFullOrPartial, currBD.IsSocketNameFull, DiscoveryStateEnum.NotFound);
}
}
}

public static void DiscoverNewSocketsAndOrFixKnownBrowsersWithUnexpectedSocketNames(List<BrowserComboItem> existingBrowserComboList, List<string> devToolsSocketsFound, string adbPath_ProvideIfNewBrowserDetailsNeeded=null) //remember that C# passes objects and collections of objects as references.
{
string adbPath = adbPath_ProvideIfNewBrowserDetailsNeeded;
HashSet<string> packageNameOfBrowsersThatCanBeFixed = existingBrowserComboList.Where(b => b.BrowserDetails.PackageName.Contains(ConfigHelper.ADB.EdgeAndBraveAndChrome_Base_ForwardParameterRemote__MissingPidAtEnd)).Select(b=>b.BrowserDetails.PackageName).ToHashSet();
foreach (string currConnSocket in devToolsSocketsFound)
{
if (currConnSocket == string.Empty)
continue;
string nSocket = currConnSocket.Replace("\r", "").Replace("\n", "").Replace("@", "");
string currSocketFull = "localabstract:" + nSocket;
string browserName = currSocketFull;
string packageName = null;
if(adbPath!=null) //this means that we are supposed to fill in the details for each newly discovered device.
{
var res = BrowserDiscoveryUtils.GetBrowserDetails(adbPath, currSocketFull);
browserName = res.OnlyNameBrowser;
packageName = res.AndroidPackageName;
int preexistingIndex = existingBrowserComboList.FindIndex(b => (b.BrowserDetails.PackageName == packageName));
if (-1!=preexistingIndex) //fixing old browsers.
{
var currBD = existingBrowserComboList[preexistingIndex].BrowserDetails;
DiscoveryStateEnum discoveryState = DiscoveryStateEnum.RediscoveredAndFilledRestOfTheSocket;

//if (packageNameOfBrowsersThatCanBeFixed.Contains(currBD.PackageName))
//{
if (currBD.PackageName == ConfigHelper.ADB.Chrome_PackageName)
{
//verifying chrome when it gets a different socket than Chrome default.
if (currSocketFull != ConfigHelper.ADB.Chrome_ForwardParameter_Remote)
discoveryState = DiscoveryStateEnum.RediscoveredAndFixed;
else if(currSocketFull == ConfigHelper.ADB.Chrome_ForwardParameter_Remote)
discoveryState = DiscoveryStateEnum.Verified;
}
else if (currBD.PackageName == ConfigHelper.ADB.Brave_PackageName || currBD.PackageName == ConfigHelper.ADB.Edge_PackageName) //verifying edge or brave when they get the default Chrome socket.
{
if (currSocketFull == ConfigHelper.ADB.Chrome_ForwardParameter_Remote)
discoveryState = DiscoveryStateEnum.RediscoveredAndFixed;
}
//}

existingBrowserComboList[preexistingIndex] = new BrowserComboItem(currBD.BrowserName, currBD.PackageName, currSocketFull, true, discoveryState);
return;
}
}
BrowserComboItem browser = new BrowserComboItem(browserName, packageName, currSocketFull, true, DiscoveryStateEnum.Discovered);
existingBrowserComboList.Add(browser);
}
}


public static BrowserJsonVersionDTO GetBrowserDetails(string adbPath,string forwardParameter_Remote)
{
IAdbConnector adbConnector = new DiscoveredSocketOnlyAdbConnector(adbPath, forwardParameter_Remote);
adbConnector.StartAdbJsonListServer();
HttpClient httpClient = new();
BrowserJsonVersionDTO response = httpClient.GetFromJsonAsync<BrowserJsonVersionDTO>(ConfigHelper.ADB.BrowserJsonVersionURL).Result;
return response;
}
}
}
86 changes: 86 additions & 0 deletions Auxiliary/ConfigHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace ChromeDroid_TabMan.Auxiliary
{
public static class ConfigHelper
{
public static bool SortGroupsInGroupedHtmlAndNetscapeBookmarksAlphabetically = true;
public static string UnidentifiedBaseUrlString => "**Unidentiffied BaseURLs**";
public static string ExportFilesPartialDefaultName { get { return "recoveredTabs(" + DateTime.Now.ToString("dd-mm-yy__HH-mm-ss")+")";} }
public static void InitializeConfig()
{
FileNamesAndPaths.InitializeConfig();
Logging.InitializeConfig();
Database.InitializeConfig();
}
public static class FileNamesAndPaths
{
public static string OutputPathDefaultExportDirectory => System.AppContext.BaseDirectory + @"Exports\";
public static string BookmarksGroupedDefaultFileName => "Bookmarks_Grouped - " + ExportFilesPartialDefaultName + ".html";
public static string BookmarksDefaultFileName => "Bookmarks - " + ExportFilesPartialDefaultName + ".html";
public static string ListDefaultFileName => "LIST - " + ExportFilesPartialDefaultName + ".html";
public static string CSVDefaultFileName => "CSV - " + ExportFilesPartialDefaultName + ".csv";
public static void InitializeConfig()
{
OutputJsonFileName = "_chromtabJSON.json";
PrevOutputJsonNewFileName = OutputJsonFileName + ".bak";
CurrentListOfURLsTxtFileName = "CurrentListOfURLs.txt";
CurrentListOfTitlesTxtFileName = "CurrentListOfTitles.txt";
}
public static string OutputJsonFileName { get; private set; }
public static string BackUpExtensionWithDot => ".bak";
public static string PrevOutputJsonNewFileName { get; private set; }
public static string CurrentListOfURLsTxtFileName { get; private set; }
public static string CurrentListOfTitlesTxtFileName { get; private set; }
}
public static class Logging
{
public static void InitializeConfig()
{
EnablePrintingInTabsListProcessingFromTxts = true;
}
public static bool EnablePrintingInTabsListProcessingFromTxts { get; private set; } //= false;
}
public static class Database
{
public static string DbFileExtensionWithDot => ".sqlite3.db";
public static string DbFileName => "SQLite DB - " + ExportFilesPartialDefaultName + DbFileExtensionWithDot;
public static void InitializeConfig()
{
//DbPath = "_LastTabs.db";
}
//public static string DbPath { get; private set; }
}
public static class ADB
{
public static string HostURL => "127.0.0.1:62001";
public static string Chrome_PackageName => "com.android.chrome";
public static string SamsungInternet_PackageName => "com.sec.android.app.sbrowser";
public static string Opera_PackageName => "com.opera.browser";
public static string Edge_PackageName => "com.microsoft.emmx";
public static string Brave_PackageName => "com.brave.browser";
public static string LocalHostString => "localhost";
public static string ForwardParameter_Local_PortOnly => "9223";
public static string ForwardParameter_Local => "tcp:"+ForwardParameter_Local_PortOnly;

public static string BaseLocalForwardedURL => "http://" + LocalHostString + ":" + ForwardParameter_Local_PortOnly;
public static string TabListEndpoint => "/json/list";
public static string TabsJsonListURL => BaseLocalForwardedURL + TabListEndpoint;
public static string BrowserVersionEndpoint => "/json/version";
public static string BrowserJsonVersionURL => BaseLocalForwardedURL + BrowserVersionEndpoint;
public static string Chrome_ForwardParameter_Remote => "localabstract:chrome_devtools_remote";
public static string SamsungInternet_ForwardParameter_Remote => "localabstract:Terrace_devtools_remote";
public static string Opera_ForwardParameter_Remote => "localabstract:com.opera.browser.devtools";
private static string EdgeAndBrave_ExceptionMessage_For_ForwardParameter_Remote => "For Edge and Brave, the ForwardParameter_Remote is the concatenation of \"localabstract:chrome_devtools_remote_\" with the process ID of the process of the browser instance at that time (can be found using pidof command).";
//public static string Edge_ForwardParameter_Remote => throw new Exception(EdgeAndBrave_ExceptionMessage_For_ForwardParameter_Remote);
//public static string Brave_ForwardParameter_Remote => throw new Exception(EdgeAndBrave_ExceptionMessage_For_ForwardParameter_Remote);
public static string EdgeAndBraveAndChrome_Base_ForwardParameterRemote__MissingPidAtEnd => Chrome_ForwardParameter_Remote;
}
}
}
Loading

0 comments on commit 0c5944e

Please sign in to comment.