Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
refactor 2
Browse files Browse the repository at this point in the history
  • Loading branch information
AtlasTheProto committed Sep 29, 2021
1 parent a3ffedd commit df74040
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
1 change: 1 addition & 0 deletions ADBForwarder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="DotNetZip" Version="1.15.0" />
<PackageReference Include="SharpAdbClient" Version="2.3.23" />
<PackageReference Include="Usb.Events" Version="10.0.0" />
</ItemGroup>

</Project>
69 changes: 43 additions & 26 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,83 @@
using SharpAdbClient;
using System.Net;
using System.IO;
using System.Collections.Generic;
using Ionic.Zip;
using Usb.Events;

namespace ADBForwarder
{
class Program
{
//const string sidequest = "\\Programs\\SideQuest\\resources\\app.asar.unpacked\\build\\platform-tools\\adb.exe";
//static string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
static string adbPath = Path.GetTempPath() + "\\adb\\platform-tools\\adb.exe";
static AdbClient client = new AdbClient();
static AdbServer server = new AdbServer();
static Uri uri = new Uri("https://dl.google.com/android/repository/platform-tools-latest-windows.zip");

static IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, AdbClient.AdbServerPort);
List<DeviceData> devices = new List<DeviceData>();


static void Main(string[] args)
{
var endPoint = new IPEndPoint(IPAddress.Loopback, AdbClient.AdbServerPort);

AdbServer server = new AdbServer();

if(!File.Exists(adbPath))
{
Console.WriteLine("ADB not found, downloading in the background...");
DownloadADB();
Console.WriteLine($"Download Successful!\nLocated at: {adbPath}\nStarting ADB Daemon...");
var result = server.StartServer(adbPath, restartServerIfNewer: false);

}
else
{
var result = server.StartServer(adbPath, restartServerIfNewer: false);
Console.WriteLine("Starting ADB Daemon...");
}

var client = new AdbClient();
client.Connect(endPoint);

DeviceMonitor monitor = new DeviceMonitor(new AdbSocket(endPoint));
monitor.DeviceConnected += Monitor_DeviceConnected;
monitor.DeviceDisconnected += Monitor_DeviceDisconnected;

monitor.Start();

var devices = client.GetDevices();

List<DeviceData> devices = client.GetDevices();

while (true)
{
System.Threading.Thread.Sleep(1000);
foreach (var device in devices)
// Main thread needs to stay alive, 100ms is acceptable idle time
System.Threading.Thread.Sleep(100);
}
}

private static void Monitor_DeviceDisconnected(object sender, DeviceDataEventArgs e)
{
Console.WriteLine($"Event: Disconnection\n Device: {e.Device.Serial}");
}

private static void Monitor_DeviceConnected(object sender, DeviceDataEventArgs e)
{

Console.WriteLine($"Event: Connection\n Device: {e.Device.Serial}");
System.Threading.Thread.Sleep(1000); // dont do shit immediately
Forward();
}

static void Forward()
{
List<DeviceData> devices = client.GetDevices();

foreach (var device in devices)
{
if(device.Name == "hollywood")
{
if (device.Name == "hollywood")
{
try
{
client.CreateForward(device, 9943, 9943);
client.CreateForward(device, 9944, 9944);
}
catch
{
//do nothing
//TODO: make this better
}
}
client.CreateForward(device, 9943, 9943);
client.CreateForward(device, 9944, 9944);
Console.WriteLine("Forwarded!");

}
}
}

static void DownloadADB()
{
using (var client = new WebClient())
Expand Down

0 comments on commit df74040

Please sign in to comment.