Form1.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. using RabbitMQ.Client;
  2. using System.Diagnostics;
  3. using System.Threading.Channels;
  4. using RabbitMQ.Client.Events;
  5. using Newtonsoft.Json;
  6. using System.Text;
  7. using System.Runtime.CompilerServices;
  8. using static System.Net.Mime.MediaTypeNames;
  9. using System.Security.Policy;
  10. using System.Xml.Linq;
  11. using smsdemo.HidSharp;
  12. namespace smsdemo
  13. {
  14. public partial class Form1 : Form
  15. {
  16. private int clickX = 991; // 点击的X坐标
  17. private int clickY = 1948; // 点击的Y坐标
  18. private int sendCount = 0; // 发送的短信数量
  19. private bool isServiceRun = false; // 是否服务化运行
  20. private bool isADBTap = false; // 是否adb点击
  21. private static IConnection connection;
  22. private static IChannel channel;
  23. private static AsyncEventingBasicConsumer consumer;
  24. private static string queueName = "queue_sms_local_send";
  25. private static string hostName = "100.64.0.25:5672";
  26. public Form1()
  27. {
  28. InitializeComponent();
  29. Connect();
  30. }
  31. #region mq
  32. private void Connect()
  33. {
  34. try
  35. {
  36. var factory = new ConnectionFactory { HostName = hostName, VirtualHost = "/", UserName = "admin", Password = "admin" };
  37. factory.Uri = new Uri("amqp://admin:admin@100.64.0.25:5672/");
  38. connection = factory.CreateConnectionAsync().Result;
  39. channel = connection.CreateChannelAsync().Result;
  40. //channel.QueueDeclareAsync(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
  41. consumer = new AsyncEventingBasicConsumer(channel);
  42. channel.QueueBindAsync(queue: "queue_sms_local_send", exchange: "exchange_sms_local_send", routingKey: "sms", arguments: null).Wait();
  43. consumer.ReceivedAsync += async (model, ea) =>
  44. {
  45. var body = ea.Body.ToArray();
  46. var message = Encoding.UTF8.GetString(body);
  47. try
  48. {
  49. dynamic json = JsonConvert.DeserializeObject(message)!;
  50. this.Invoke((MethodInvoker)delegate
  51. {
  52. textBox3.Text += $"Received JSON: {json}";
  53. });
  54. string? mobile = json.mobile;
  55. string? code = json.code;
  56. if (mobile != null && code != null)
  57. {
  58. this.Invoke((MethodInvoker)delegate
  59. {
  60. textBox1.Text = mobile.ToString();
  61. textBox2.Text = code.ToString();
  62. });
  63. var sendSmsResult = SendSMS(mobile, code);
  64. this.Invoke((MethodInvoker)delegate
  65. {
  66. textBox3.Text += ($"Received mobile: {mobile}, code: {code}");
  67. });
  68. }
  69. else
  70. {
  71. textBox3.Text += ("JSON does not contain 'mobile' or 'code' fields.");
  72. }
  73. }
  74. catch (JsonException ex)
  75. {
  76. this.Invoke((MethodInvoker)delegate
  77. {
  78. textBox3.Text+=($"Error deserializing JSON: {ex.Message}");
  79. });
  80. }
  81. await Task.CompletedTask; // Ensure all code paths return a Task
  82. };
  83. channel.BasicConsumeAsync(queue: queueName, autoAck: true, consumer: consumer);
  84. connection.ConnectionShutdownAsync += async (sender, args) =>
  85. {
  86. this.Invoke((MethodInvoker)delegate
  87. {
  88. textBox3.Text+=("Connection shut down. Reconnecting...");
  89. });
  90. await Task.Run(() => Reconnect());
  91. };
  92. }
  93. catch (Exception ex)
  94. {
  95. this.Invoke((MethodInvoker)delegate
  96. {
  97. textBox3.Text+=($"Connection error: {ex.Message}. Reconnecting in 5 seconds...");
  98. });
  99. System.Threading.Thread.Sleep(5000);
  100. Reconnect();
  101. }
  102. }
  103. private void Reconnect()
  104. {
  105. while (true)
  106. {
  107. try
  108. {
  109. Disconnect();
  110. Connect();
  111. textBox3.Text+=("Reconnected successfully.");
  112. break;
  113. }
  114. catch (Exception ex)
  115. {
  116. this.Invoke((MethodInvoker)delegate
  117. {
  118. textBox3.Text+=($"Reconnection failed: {ex.Message}. Retrying in 5 seconds...");
  119. });
  120. System.Threading.Thread.Sleep(5000);
  121. }
  122. }
  123. }
  124. private void Disconnect()
  125. {
  126. if (channel != null && channel.IsOpen)
  127. {
  128. channel.AbortAsync();
  129. }
  130. if (connection != null && connection.IsOpen)
  131. {
  132. connection.Dispose();
  133. }
  134. }
  135. #endregion
  136. #region send sms
  137. private string ExecuteAdbCommand(string arguments)
  138. {
  139. try
  140. {
  141. blink();
  142. ProcessStartInfo psi = new ProcessStartInfo
  143. {
  144. FileName = "adb.exe",
  145. Arguments = arguments,
  146. UseShellExecute = false,
  147. RedirectStandardOutput = true,
  148. CreateNoWindow = true
  149. };
  150. using (Process process = new Process { StartInfo = psi })
  151. {
  152. process.Start();
  153. string output = process.StandardOutput.ReadToEnd();
  154. process.WaitForExit();
  155. return output;
  156. }
  157. }
  158. catch (Exception ex)
  159. {
  160. return $"Error: {ex.Message}";
  161. }
  162. }
  163. private string SendSMS(string phone, string text)
  164. {
  165. //{"mobile":"13052379667","code":"123456\\ goods"}
  166. //string startCommand = $" start-server";
  167. //string startResult = ExecuteAdbCommand(startCommand);
  168. string deviceCommand = $" devices";
  169. string deviceResult = ExecuteAdbCommand(deviceCommand);
  170. //adb shell input text some % stext % swith % sspaces
  171. //adb shell input text $(echo "some text with spaces" | sed 's/ /\%s/g')
  172. // val message = text.replace("|", "\\|")
  173. //.replace("\"", "\\\"")
  174. //.replace("\'", "\\\'")
  175. //.replace("<", "\\<")
  176. //.replace(">", "\\>")
  177. //.replace(";", "\\;")
  178. //.replace("?", "\\?")
  179. //.replace("`", "\\`")
  180. //.replace("&", "\\&")
  181. //.replace("*", "\\*")
  182. //.replace("(", "\\(")
  183. //.replace(")", "\\)")
  184. //.replace("~", "\\~")
  185. //.replace(" ", "\\ ")
  186. string sendSmsCommand = $" shell am start -a android.intent.action.SENDTO -S -d sms:{phone} --es sms_body \" {text}\" --ez exit_on_sent true";
  187. this.Invoke((MethodInvoker)delegate
  188. {
  189. textBox3.Text += sendSmsCommand;
  190. });
  191. string sendSmsResult = ExecuteAdbCommand(sendSmsCommand);
  192. this.Invoke((MethodInvoker)delegate
  193. {
  194. textBox3.Text += sendSmsResult;
  195. });
  196. if (!sendSmsResult.StartsWith("Error"))
  197. {
  198. Thread.Sleep(1000);
  199. //string tapCommand1 = " input keyevent 22";
  200. //string tapResult1 = ExecuteAdbCommand(tapCommand1);
  201. //string tapCommand2 = " input keyevent 66";
  202. //string tapResult2 = ExecuteAdbCommand(tapCommand2);
  203. //oneplus5 991 1948
  204. //lg 1010 1414
  205. string tapCommand3 = $" shell input tap {clickX} {clickY}";
  206. this.Invoke((MethodInvoker)delegate
  207. {
  208. textBox3.Text += tapCommand3;
  209. });
  210. string tapResult3 = ExecuteAdbCommand(tapCommand3);
  211. //string stop = " kill-server";
  212. //string stopResult = ExecuteAdbCommand(stop);
  213. if (!tapResult3.StartsWith("Error"))
  214. {
  215. this.Invoke((MethodInvoker)delegate
  216. {
  217. textBox3.Text += tapResult3;
  218. });
  219. sendCount++;
  220. }
  221. }
  222. else
  223. {
  224. textBox3.Text+=($"执行命令时发生错误: {sendSmsResult}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  225. }
  226. return sendSmsResult;
  227. }
  228. #endregion
  229. private void button1_Click(object sender, EventArgs e)
  230. {
  231. TestBlinkViolet();
  232. textBox1.Text = "";
  233. textBox2.Text = "";
  234. }
  235. private void button2_Click(object sender, EventArgs e)
  236. {
  237. var phone = textBox1.Text;
  238. var text = textBox2.Text;
  239. if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(text))
  240. {
  241. textBox3.Text += ("Please enter a phone number and message text.");
  242. return;
  243. }
  244. TestBlinkViolet();
  245. var sendSmsResult = SendSMS(phone, text);
  246. }
  247. private void timer1_Tick(object sender, EventArgs e)
  248. {
  249. //检查adb连接
  250. TestBlinkGreen();
  251. //30s 检查一次
  252. ProcessStartInfo psi = new ProcessStartInfo
  253. {
  254. FileName = "adb.exe",
  255. Arguments = "devices",
  256. UseShellExecute = false,
  257. RedirectStandardOutput = true,
  258. CreateNoWindow = true
  259. };
  260. }
  261. private void blink()
  262. {
  263. try
  264. {
  265. LEDBlink led = new LEDBlink();
  266. led.Blink();
  267. }
  268. catch (Exception ex)
  269. {
  270. this.Invoke((MethodInvoker)delegate
  271. {
  272. textBox3.Text += ex.Message;
  273. });
  274. }
  275. }
  276. private void TestBlinkViolet()
  277. {
  278. try
  279. {
  280. LEDBlink led = new LEDBlink();
  281. led.TestBlinkViolet();
  282. }
  283. catch (Exception ex)
  284. {
  285. this.Invoke((MethodInvoker)delegate
  286. {
  287. textBox3.Text += ex.Message;
  288. });
  289. }
  290. }
  291. private void TestBlinkYellow()
  292. {
  293. try
  294. {
  295. LEDBlink led = new LEDBlink();
  296. led.TestBlinkYellow();
  297. }
  298. catch (Exception ex)
  299. {
  300. this.Invoke((MethodInvoker)delegate
  301. {
  302. textBox3.Text += ex.Message;
  303. });
  304. }
  305. }
  306. private void TestBlinkGreen()
  307. {
  308. try
  309. {
  310. LEDBlink led = new LEDBlink();
  311. led.TestBlinkGreen();
  312. }
  313. catch (Exception ex)
  314. {
  315. this.Invoke((MethodInvoker)delegate
  316. {
  317. textBox3.Text += ex.Message;
  318. });
  319. }
  320. }
  321. private void button3_Click(object sender, EventArgs e)
  322. {
  323. TestBlinkViolet();
  324. //初始化adb
  325. ProcessStartInfo psi = new ProcessStartInfo
  326. {
  327. FileName = "adb.exe",
  328. Arguments = "devices",
  329. UseShellExecute = false,
  330. RedirectStandardOutput = true,
  331. CreateNoWindow = true
  332. };
  333. //初始化mq
  334. Connect();
  335. }
  336. private void button4_Click(object sender, EventArgs e)
  337. {
  338. TestBlinkViolet();
  339. //using (MemoryStream ms = new MemoryStream(Properties.Resources.net_0))
  340. //{
  341. // Image image = Image.FromStream(ms);
  342. // // 假设 PictureBox 控件名为 pictureBox1
  343. // pictureBox1.Image = image;
  344. //}
  345. //using (MemoryStream ms = new MemoryStream(Properties.Resources.adb_0))
  346. //{
  347. // Image image = Image.FromStream(ms);
  348. // // 假设 PictureBox 控件名为 pictureBox1
  349. // pictureBox2.Image = image;
  350. //}
  351. //using (MemoryStream ms = new MemoryStream(Properties.Resources.error_1))
  352. //{
  353. // Image image = Image.FromStream(ms);
  354. // // 假设 PictureBox 控件名为 pictureBox1
  355. // pictureBox3.Image = image;
  356. //}
  357. //using (MemoryStream ms = new MemoryStream(Properties.Resources.send_1))
  358. //{
  359. // Image image = Image.FromStream(ms);
  360. // // 假设 PictureBox 控件名为 pictureBox1
  361. // pictureBox4.Image = image;
  362. //}
  363. ////自检
  364. //Reconnect();
  365. //Thread.Sleep(2000);
  366. ////网络
  367. //using (MemoryStream ms = new MemoryStream(Properties.Resources.net_1))
  368. //{
  369. // Image image = Image.FromStream(ms);
  370. // // 假设 PictureBox 控件名为 pictureBox1
  371. // pictureBox1.Image = image;
  372. //}
  373. //using (MemoryStream ms = new MemoryStream(Properties.Resources.adb_1))
  374. //{
  375. // Image image = Image.FromStream(ms);
  376. // // 假设 PictureBox 控件名为 pictureBox1
  377. // pictureBox2.Image = image;
  378. //}
  379. //using (MemoryStream ms = new MemoryStream(Properties.Resources.error_0))
  380. //{
  381. // Image image = Image.FromStream(ms);
  382. // // 假设 PictureBox 控件名为 pictureBox1
  383. // pictureBox3.Image = image;
  384. //}
  385. //using (MemoryStream ms = new MemoryStream(Properties.Resources.send_0))
  386. //{
  387. // Image image = Image.FromStream(ms);
  388. // // 假设 PictureBox 控件名为 pictureBox1
  389. // pictureBox4.Image = image;
  390. //}
  391. //adb
  392. }
  393. private void button5_Click(object sender, EventArgs e)
  394. {
  395. TestBlinkViolet();
  396. // 校准屏幕点击
  397. if (int.TryParse(this.textBox4.Text, out int parsedX) && int.TryParse(this.textBox5.Text, out int parsedY))
  398. {
  399. clickX = parsedX;
  400. clickY = parsedY;
  401. }
  402. else
  403. {
  404. textBox3.Text += ("请输入有效的整数值用于校准点击坐标。", "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  405. }
  406. }
  407. private void textBox4_TextChanged(object sender, EventArgs e)
  408. {
  409. }
  410. private void button6_Click(object sender, EventArgs e)
  411. {
  412. TestBlinkViolet();
  413. //统计
  414. textBox3.Text += ("共发送短信:" + sendCount);
  415. }
  416. private void textBox1_TextChanged(object sender, EventArgs e)
  417. {
  418. }
  419. }
  420. }