From bba427d31081f99b7dc5e724308ca6151753ccd1 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Tue, 14 Jan 2025 00:13:38 -0800 Subject: initial commit --- server/Brokenithm-Evolved-iOS.sln | 25 ++ server/Brokenithm-Evolved-iOS/App.config | 6 + .../Brokenithm-Evolved-iOS.csproj | 108 +++++++ server/Brokenithm-Evolved-iOS/Brokenithm.ico | Bin 0 -> 12862 bytes server/Brokenithm-Evolved-iOS/Program.cs | 343 +++++++++++++++++++++ .../Properties/AssemblyInfo.cs | 36 +++ server/Brokenithm-Evolved-iOS/packages.config | 4 + 7 files changed, 522 insertions(+) create mode 100644 server/Brokenithm-Evolved-iOS.sln create mode 100644 server/Brokenithm-Evolved-iOS/App.config create mode 100644 server/Brokenithm-Evolved-iOS/Brokenithm-Evolved-iOS.csproj create mode 100644 server/Brokenithm-Evolved-iOS/Brokenithm.ico create mode 100644 server/Brokenithm-Evolved-iOS/Program.cs create mode 100644 server/Brokenithm-Evolved-iOS/Properties/AssemblyInfo.cs create mode 100644 server/Brokenithm-Evolved-iOS/packages.config (limited to 'server') diff --git a/server/Brokenithm-Evolved-iOS.sln b/server/Brokenithm-Evolved-iOS.sln new file mode 100644 index 0000000..4747dfe --- /dev/null +++ b/server/Brokenithm-Evolved-iOS.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29709.97 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Brokenithm-Evolved-iOS", "Brokenithm-Evolved-iOS\Brokenithm-Evolved-iOS.csproj", "{D127D9DF-1FB2-48F1-A8ED-F50D9852732A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D127D9DF-1FB2-48F1-A8ED-F50D9852732A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D127D9DF-1FB2-48F1-A8ED-F50D9852732A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D127D9DF-1FB2-48F1-A8ED-F50D9852732A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D127D9DF-1FB2-48F1-A8ED-F50D9852732A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C3AA92C4-7827-465C-9827-FB95FDFF4C29} + EndGlobalSection +EndGlobal diff --git a/server/Brokenithm-Evolved-iOS/App.config b/server/Brokenithm-Evolved-iOS/App.config new file mode 100644 index 0000000..ecdcf8a --- /dev/null +++ b/server/Brokenithm-Evolved-iOS/App.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/server/Brokenithm-Evolved-iOS/Brokenithm-Evolved-iOS.csproj b/server/Brokenithm-Evolved-iOS/Brokenithm-Evolved-iOS.csproj new file mode 100644 index 0000000..9f5fe70 --- /dev/null +++ b/server/Brokenithm-Evolved-iOS/Brokenithm-Evolved-iOS.csproj @@ -0,0 +1,108 @@ + + + + + Debug + AnyCPU + {D127D9DF-1FB2-48F1-A8ED-F50D9852732A} + Exe + Brokenithm_Evolved_iOS_Umi + Brokenithm-Evolved-iOS-Umi + v4.7.2 + 512 + true + true + + + + false + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + true + 1 + 1.0.0.%2a + false + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + true + + + true + + + Brokenithm.ico + + + + ..\packages\iMobileDevice-net.1.2.186\lib\net45\iMobileDevice-net.dll + + + + + + + + + + + + + + + + + + + + + False + Microsoft .NET Framework 4.7.2 %28x86 和 x64%29 + true + + + False + .NET Framework 3.5 SP1 + false + + + + + + + + + + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 + + + + \ No newline at end of file diff --git a/server/Brokenithm-Evolved-iOS/Brokenithm.ico b/server/Brokenithm-Evolved-iOS/Brokenithm.ico new file mode 100644 index 0000000..8033b43 Binary files /dev/null and b/server/Brokenithm-Evolved-iOS/Brokenithm.ico differ diff --git a/server/Brokenithm-Evolved-iOS/Program.cs b/server/Brokenithm-Evolved-iOS/Program.cs new file mode 100644 index 0000000..f6785cb --- /dev/null +++ b/server/Brokenithm-Evolved-iOS/Program.cs @@ -0,0 +1,343 @@ +using iMobileDevice; +using iMobileDevice.iDevice; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO.MemoryMappedFiles; +using System.Linq; +using System.Security.Principal; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace Brokenithm_Evolved_iOS +{ + class Program + { + public static Dictionary device_map = new Dictionary(); + public static Dictionary process_map = new Dictionary(); + public static Dictionary connection_map = new Dictionary(); + public static IiDeviceApi idevice; + public static MemoryMappedFile sharedBuffer; + public static MemoryMappedViewAccessor sharedBufferAccessor; + public static bool exiting = false; + public static iDeviceEventCallBack _eventCallback; + + static void Main(string[] args) + { + Console.Title = "Brokenithm-Evolved-iOS"; + Console.WriteLine("================================================="); + Console.WriteLine("= Brokenithm-Evolved-iOS (Umi) ="); + Console.WriteLine("= Brokenithm with full IO and USB connection ="); + Console.WriteLine("= v0.3 by esterTion ="); + Console.WriteLine("= Modified for Umiguri usage by Pinapelz ="); + Console.WriteLine("= Original: thebit.link ="); + Console.WriteLine("================================================="); + Console.WriteLine(""); + + NativeLibraries.Load(); + idevice = LibiMobileDevice.Instance.iDevice; + iDeviceError status; + _eventCallback = new iDeviceEventCallBack(eventCallback); + status = LibiMobileDevice.Instance.iDevice.idevice_event_subscribe(_eventCallback, IntPtr.Zero); + + MemoryMappedFileSecurity CustomSecurity = new MemoryMappedFileSecurity(); + SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null); + var acct = sid.Translate(typeof(NTAccount)) as NTAccount; + CustomSecurity.AddAccessRule(new System.Security.AccessControl.AccessRule(acct.ToString(), MemoryMappedFileRights.FullControl, System.Security.AccessControl.AccessControlType.Allow)); + sharedBuffer = MemoryMappedFile.CreateOrOpen("Local\\BROKENITHM_SHARED_BUFFER", 1024, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None, CustomSecurity, System.IO.HandleInheritability.Inheritable); + sharedBufferAccessor = sharedBuffer.CreateViewAccessor(); + + { + Thread ledThread = new Thread(new ThreadStart(outputLed)); + ledThread.Start(); + } + + Console.WriteLine("Waiting for device..."); + while (Console.ReadKey().Key != ConsoleKey.Q) { } + exiting = true; + } + + public static void eventCallback(ref iDeviceEvent e, IntPtr user_data) + { + iDeviceError status; + string udid = e.udidString; + switch (e.@event) + { + case iDeviceEventType.DeviceAdd: + { + Console.WriteLine(string.Format("device add\tudid: {0}", udid)); + if (device_map.ContainsKey(udid)) + { + return; + } + iDeviceHandle device; + status = idevice.idevice_new(out device, e.udidString); + if (status != iDeviceError.Success) + { + Console.WriteLine("Create device failed: {0}", status); + return; + } + device_map[udid] = device; + + Process process = new Process(); + process.StartInfo.FileName = @"umi-evolved\brokenithm-evolved-umi.exe"; + try + { + process.Start(); + + } + catch(Exception ex){ + Console.WriteLine("Unable to launch brokenithm-evolved-umi server. Is the .exe there?"); + Console.WriteLine("Current Directory: " + Environment.CurrentDirectory); + Console.WriteLine("Press Enter to continue..."); + Console.ReadLine(); + Environment.Exit(0); + } + process_map[udid] = process; + + Thread thread = new Thread(new ParameterizedThreadStart(connectDevice)); + thread.Start(udid); + break; + } + case iDeviceEventType.DevicePaired: + { + Console.WriteLine(string.Format("device paired\tudid: {0}", udid)); + break; + } + case iDeviceEventType.DeviceRemove: + { + Console.WriteLine(string.Format("device remove\tudid: {0}", udid)); + if (device_map.ContainsKey(udid)) + { + iDeviceHandle device = device_map[udid]; + device.Dispose(); + device_map.Remove(udid); + } + if (process_map.ContainsKey(udid)) + { + try + { + Process processToKill = process_map[udid]; + if (!processToKill.HasExited) + { + processToKill.Kill(); + } + processToKill.Dispose(); + process_map.Remove(udid); + } + catch (Exception ex) + { + Console.WriteLine($"Error killing process for device {udid}: {ex.Message}"); + } + } + break; + } + } + } + + public static void connectDevice(object arg) + { + string udid = (string)arg; + if (!device_map.ContainsKey(udid)) + { + return; + } + iDeviceHandle device = device_map[udid]; + iDeviceConnectionHandle conn; + iDeviceError status; + status = idevice.idevice_connect(device, 24864, out conn); + if (status != iDeviceError.Success) + { + //Console.WriteLine(string.Format("connect failed: {0}", status)); + Thread.Sleep(1000); + Thread thread = new Thread(new ParameterizedThreadStart(connectDevice)); + thread.Start(udid); + return; + } + + byte[] buf = new byte[256]; + uint read = 0; + status = idevice.idevice_connection_receive(conn, buf, 4, ref read); + if (status != iDeviceError.Success) + { + Console.WriteLine(string.Format("receive data failed: {0}", status)); + return; + } + if ( + buf[0] != 3 || + buf[1] != 'W' || + buf[2] != 'E' || + buf[3] != 'L' + ) + { + // welcome message + Console.WriteLine("received invalid data"); + conn.Dispose(); + return; + } + Console.WriteLine("connected to device"); + connection_map[udid] = conn; + { + Thread thread = new Thread(new ParameterizedThreadStart(readInputFromDevice)); + thread.Start(udid); + } + return; + } + public static void readInputFromDevice(object arg) + { + string udid = (string)arg; + if (!connection_map.ContainsKey(udid)) + { + return; + } + iDeviceConnectionHandle conn = connection_map[udid]; + iDeviceError status; + bool airEnabled = true; + + byte[] buf = new byte[256]; + uint read = 0; + while (true) + { + if (exiting) break; + status = idevice.idevice_connection_receive_timeout(conn, buf, 1, ref read, 5); + if (status != iDeviceError.Success) + { + if (status == iDeviceError.Timeout) + { + continue; + } + break; + } + byte len = buf[0]; + status = idevice.idevice_connection_receive_timeout(conn, buf, len, ref read, 5); + if (status != iDeviceError.Success) + { + break; + } + if (len >= 3+6+32 && + buf[0] == 'I' && + buf[1] == 'N' && + buf[2] == 'P') + { + // key input + if (airEnabled) + { + sharedBufferAccessor.WriteArray(0, buf, 3, 6 + 32); + } + else + { + sharedBufferAccessor.WriteArray(6, buf, 3 + 6, 32); + } + if (len > 3 + 6 + 32) + { + sharedBufferAccessor.WriteArray(6+32+96, buf, 3+6+32, len - (3 + 6 + 32)); + } + } + else if (len >= 4 && + buf[0] == 'A' && + buf[1] == 'I' && + buf[2] == 'R') + { + // air input control + airEnabled = buf[3] != 0; + Console.WriteLine(string.Format("Air input {0}", airEnabled ? "enabled" : "disabled")); + } + else if (len >= 4 && + buf[0] == 'F' && + buf[1] == 'N' && + buf[2] == 'C') + { + // function key + if (buf[3] == (byte)BNI_FUNCTION_KEYS.COIN) + { + sharedBufferAccessor.Write(6 + 32 + 96 + 2, 1); + } + else if (buf[3] == (byte)BNI_FUNCTION_KEYS.CARD) + { + sharedBufferAccessor.Write(6 + 32 + 96 + 3, 1); + } + } + else + { + if (len >= 3) + { + StringBuilder sb = new StringBuilder(); + sb.Append((char)buf[0]); + sb.Append((char)buf[1]); + sb.Append((char)buf[2]); + Console.WriteLine(string.Format("unknown packet: {0}", sb.ToString())); + } + else + { + Console.WriteLine("unknown packet"); + } + } + } + conn.Dispose(); + connection_map.Remove(udid); + Console.WriteLine("disconnected"); + if (exiting) return; + Thread.Sleep(1000); + { + Thread thread = new Thread(new ParameterizedThreadStart(connectDevice)); + thread.Start(udid); + } + } + + enum BNI_FUNCTION_KEYS { + COIN = 1, + CARD + }; + + private static byte[] prevLedRgb = new byte[32 * 3]; + private static int skipCount = 0; + public static void outputLed() + { + while (true) + { + if (exiting) break; + byte[] ledRgb = new byte[32 * 3]; + sharedBufferAccessor.ReadArray(6 + 32, ledRgb, 0, 32 * 3); + bool same = true; + for (int i = 0; i < 32 * 3; i++) + { + if (ledRgb[i] != prevLedRgb[i]) + { + same = false; + break; + } + } + if (!same) + { + BroadcastLEDStatus(ledRgb); + prevLedRgb = ledRgb; + skipCount = 0; + } + else + { + if (++skipCount > 50) + { + BroadcastLEDStatus(prevLedRgb); + skipCount = 0; + } + } + Thread.Sleep(10); + } + } + public static void BroadcastLEDStatus(byte[] led) + { + uint sent = 0; + byte[] head = { 99, (byte)'L', (byte)'E', (byte)'D' }; + foreach (var conn in connection_map) + { + try + { + idevice.idevice_connection_send(conn.Value, head, 4, ref sent); + idevice.idevice_connection_send(conn.Value, led, 96, ref sent); + } + catch (Exception e) { } + } + } + } +} diff --git a/server/Brokenithm-Evolved-iOS/Properties/AssemblyInfo.cs b/server/Brokenithm-Evolved-iOS/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..0e57bf6 --- /dev/null +++ b/server/Brokenithm-Evolved-iOS/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("Brokenithm-Evolved-iOS")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Brokenithm-Evolved-iOS")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("d127d9df-1fb2-48f1-a8ed-f50d9852732a")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/server/Brokenithm-Evolved-iOS/packages.config b/server/Brokenithm-Evolved-iOS/packages.config new file mode 100644 index 0000000..4266474 --- /dev/null +++ b/server/Brokenithm-Evolved-iOS/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file -- cgit v1.2.3