using RabbitMQ.Client; using System.Diagnostics; using System.Threading.Channels; using RabbitMQ.Client.Events; using Newtonsoft.Json; using System.Text; using System.Runtime.CompilerServices; using static System.Net.Mime.MediaTypeNames; using System.Security.Policy; using System.Xml.Linq; using smsdemo.HidSharp; namespace smsdemo { public partial class Form1 : Form { private int clickX = 991; // 点击的X坐标 private int clickY = 1948; // 点击的Y坐标 private int sendCount = 0; // 发送的短信数量 private bool isServiceRun = false; // 是否服务化运行 private bool isADBTap = false; // 是否adb点击 private static IConnection connection; private static IChannel channel; private static AsyncEventingBasicConsumer consumer; private static string queueName = "queue_sms_local_send"; private static string hostName = "100.64.0.25:5672"; public Form1() { InitializeComponent(); Connect(); } #region mq private void Connect() { try { var factory = new ConnectionFactory { HostName = hostName, VirtualHost = "/", UserName = "admin", Password = "admin" }; factory.Uri = new Uri("amqp://admin:admin@100.64.0.25:5672/"); connection = factory.CreateConnectionAsync().Result; channel = connection.CreateChannelAsync().Result; //channel.QueueDeclareAsync(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null); consumer = new AsyncEventingBasicConsumer(channel); channel.QueueBindAsync(queue: "queue_sms_local_send", exchange: "exchange_sms_local_send", routingKey: "sms", arguments: null).Wait(); consumer.ReceivedAsync += async (model, ea) => { var body = ea.Body.ToArray(); var message = Encoding.UTF8.GetString(body); try { dynamic json = JsonConvert.DeserializeObject(message)!; this.Invoke((MethodInvoker)delegate { textBox3.Text += $"Received JSON: {json}"; }); string? mobile = json.mobile; string? code = json.code; if (mobile != null && code != null) { this.Invoke((MethodInvoker)delegate { textBox1.Text = mobile.ToString(); textBox2.Text = code.ToString(); }); var sendSmsResult = SendSMS(mobile, code); this.Invoke((MethodInvoker)delegate { textBox3.Text += ($"Received mobile: {mobile}, code: {code}"); }); } else { textBox3.Text += ("JSON does not contain 'mobile' or 'code' fields."); } } catch (JsonException ex) { this.Invoke((MethodInvoker)delegate { textBox3.Text+=($"Error deserializing JSON: {ex.Message}"); }); } await Task.CompletedTask; // Ensure all code paths return a Task }; channel.BasicConsumeAsync(queue: queueName, autoAck: true, consumer: consumer); connection.ConnectionShutdownAsync += async (sender, args) => { this.Invoke((MethodInvoker)delegate { textBox3.Text+=("Connection shut down. Reconnecting..."); }); await Task.Run(() => Reconnect()); }; } catch (Exception ex) { this.Invoke((MethodInvoker)delegate { textBox3.Text+=($"Connection error: {ex.Message}. Reconnecting in 5 seconds..."); }); System.Threading.Thread.Sleep(5000); Reconnect(); } } private void Reconnect() { while (true) { try { Disconnect(); Connect(); textBox3.Text+=("Reconnected successfully."); break; } catch (Exception ex) { this.Invoke((MethodInvoker)delegate { textBox3.Text+=($"Reconnection failed: {ex.Message}. Retrying in 5 seconds..."); }); System.Threading.Thread.Sleep(5000); } } } private void Disconnect() { if (channel != null && channel.IsOpen) { channel.AbortAsync(); } if (connection != null && connection.IsOpen) { connection.Dispose(); } } #endregion #region send sms private string ExecuteAdbCommand(string arguments) { try { blink(); ProcessStartInfo psi = new ProcessStartInfo { FileName = "adb.exe", Arguments = arguments, UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true }; using (Process process = new Process { StartInfo = psi }) { process.Start(); string output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); return output; } } catch (Exception ex) { return $"Error: {ex.Message}"; } } private string SendSMS(string phone, string text) { //{"mobile":"13052379667","code":"123456\\ goods"} //string startCommand = $" start-server"; //string startResult = ExecuteAdbCommand(startCommand); string deviceCommand = $" devices"; string deviceResult = ExecuteAdbCommand(deviceCommand); //adb shell input text some % stext % swith % sspaces //adb shell input text $(echo "some text with spaces" | sed 's/ /\%s/g') // val message = text.replace("|", "\\|") //.replace("\"", "\\\"") //.replace("\'", "\\\'") //.replace("<", "\\<") //.replace(">", "\\>") //.replace(";", "\\;") //.replace("?", "\\?") //.replace("`", "\\`") //.replace("&", "\\&") //.replace("*", "\\*") //.replace("(", "\\(") //.replace(")", "\\)") //.replace("~", "\\~") //.replace(" ", "\\ ") string sendSmsCommand = $" shell am start -a android.intent.action.SENDTO -S -d sms:{phone} --es sms_body \" {text}\" --ez exit_on_sent true"; this.Invoke((MethodInvoker)delegate { textBox3.Text += sendSmsCommand; }); string sendSmsResult = ExecuteAdbCommand(sendSmsCommand); this.Invoke((MethodInvoker)delegate { textBox3.Text += sendSmsResult; }); if (!sendSmsResult.StartsWith("Error")) { Thread.Sleep(1000); //string tapCommand1 = " input keyevent 22"; //string tapResult1 = ExecuteAdbCommand(tapCommand1); //string tapCommand2 = " input keyevent 66"; //string tapResult2 = ExecuteAdbCommand(tapCommand2); //oneplus5 991 1948 //lg 1010 1414 string tapCommand3 = $" shell input tap {clickX} {clickY}"; this.Invoke((MethodInvoker)delegate { textBox3.Text += tapCommand3; }); string tapResult3 = ExecuteAdbCommand(tapCommand3); //string stop = " kill-server"; //string stopResult = ExecuteAdbCommand(stop); if (!tapResult3.StartsWith("Error")) { this.Invoke((MethodInvoker)delegate { textBox3.Text += tapResult3; }); sendCount++; } } else { textBox3.Text+=($"执行命令时发生错误: {sendSmsResult}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } return sendSmsResult; } #endregion private void button1_Click(object sender, EventArgs e) { TestBlinkViolet(); textBox1.Text = ""; textBox2.Text = ""; } private void button2_Click(object sender, EventArgs e) { var phone = textBox1.Text; var text = textBox2.Text; if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(text)) { textBox3.Text += ("Please enter a phone number and message text."); return; } TestBlinkViolet(); var sendSmsResult = SendSMS(phone, text); } private void timer1_Tick(object sender, EventArgs e) { //检查adb连接 TestBlinkGreen(); //30s 检查一次 ProcessStartInfo psi = new ProcessStartInfo { FileName = "adb.exe", Arguments = "devices", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true }; } private void blink() { try { LEDBlink led = new LEDBlink(); led.Blink(); } catch (Exception ex) { this.Invoke((MethodInvoker)delegate { textBox3.Text += ex.Message; }); } } private void TestBlinkViolet() { try { LEDBlink led = new LEDBlink(); led.TestBlinkViolet(); } catch (Exception ex) { this.Invoke((MethodInvoker)delegate { textBox3.Text += ex.Message; }); } } private void TestBlinkYellow() { try { LEDBlink led = new LEDBlink(); led.TestBlinkYellow(); } catch (Exception ex) { this.Invoke((MethodInvoker)delegate { textBox3.Text += ex.Message; }); } } private void TestBlinkGreen() { try { LEDBlink led = new LEDBlink(); led.TestBlinkGreen(); } catch (Exception ex) { this.Invoke((MethodInvoker)delegate { textBox3.Text += ex.Message; }); } } private void button3_Click(object sender, EventArgs e) { TestBlinkViolet(); //初始化adb ProcessStartInfo psi = new ProcessStartInfo { FileName = "adb.exe", Arguments = "devices", UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true }; //初始化mq Connect(); } private void button4_Click(object sender, EventArgs e) { TestBlinkViolet(); //using (MemoryStream ms = new MemoryStream(Properties.Resources.net_0)) //{ // Image image = Image.FromStream(ms); // // 假设 PictureBox 控件名为 pictureBox1 // pictureBox1.Image = image; //} //using (MemoryStream ms = new MemoryStream(Properties.Resources.adb_0)) //{ // Image image = Image.FromStream(ms); // // 假设 PictureBox 控件名为 pictureBox1 // pictureBox2.Image = image; //} //using (MemoryStream ms = new MemoryStream(Properties.Resources.error_1)) //{ // Image image = Image.FromStream(ms); // // 假设 PictureBox 控件名为 pictureBox1 // pictureBox3.Image = image; //} //using (MemoryStream ms = new MemoryStream(Properties.Resources.send_1)) //{ // Image image = Image.FromStream(ms); // // 假设 PictureBox 控件名为 pictureBox1 // pictureBox4.Image = image; //} ////自检 //Reconnect(); //Thread.Sleep(2000); ////网络 //using (MemoryStream ms = new MemoryStream(Properties.Resources.net_1)) //{ // Image image = Image.FromStream(ms); // // 假设 PictureBox 控件名为 pictureBox1 // pictureBox1.Image = image; //} //using (MemoryStream ms = new MemoryStream(Properties.Resources.adb_1)) //{ // Image image = Image.FromStream(ms); // // 假设 PictureBox 控件名为 pictureBox1 // pictureBox2.Image = image; //} //using (MemoryStream ms = new MemoryStream(Properties.Resources.error_0)) //{ // Image image = Image.FromStream(ms); // // 假设 PictureBox 控件名为 pictureBox1 // pictureBox3.Image = image; //} //using (MemoryStream ms = new MemoryStream(Properties.Resources.send_0)) //{ // Image image = Image.FromStream(ms); // // 假设 PictureBox 控件名为 pictureBox1 // pictureBox4.Image = image; //} //adb } private void button5_Click(object sender, EventArgs e) { TestBlinkViolet(); // 校准屏幕点击 if (int.TryParse(this.textBox4.Text, out int parsedX) && int.TryParse(this.textBox5.Text, out int parsedY)) { clickX = parsedX; clickY = parsedY; } else { textBox3.Text += ("请输入有效的整数值用于校准点击坐标。", "输入错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void textBox4_TextChanged(object sender, EventArgs e) { } private void button6_Click(object sender, EventArgs e) { TestBlinkViolet(); //统计 textBox3.Text += ("共发送短信:" + sendCount); } private void textBox1_TextChanged(object sender, EventArgs e) { } } }