Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using System.Net;
- using System.Net.Sockets;
- using System.ComponentModel;
- using System.Diagnostics;
- namespace ViewMyS
- {
- /// <summary>
- /// Interaktionslogik für MainWindow.xaml
- /// </summary>
- ///
- public partial class MainWindow : Window
- {
- //###############
- string INC = "";
- bool logme = false;
- string opip; //OPPONENT IP
- string result; //RÜCKGABEWERT
- string sendresult; //AUSGABE RÜCKGABEWERT
- bool running = false;
- bool sendonvoid = false;
- //###############
- public MainWindow()
- {
- InitializeComponent();
- }
- //################
- bool ende = false;
- string comstring;
- //#################
- BackgroundWorker bw = new BackgroundWorker();
- //#################
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- if (running == true)
- {
- MessageBox.Show("The System is listening, you cannot start another instance of the Listener-Process!");
- }
- WorkStart();
- running = true;
- }
- private void WorkStart()
- {
- try
- {
- bw.DoWork += new DoWorkEventHandler(bw_DoWork);
- bw.RunWorkerAsync();
- }
- catch (Exception)
- {
- return; //Damit bei mehrfachem klicken keine Fehlermeldung auftritt.
- }
- }
- private void bw_DoWork(object sender, DoWorkEventArgs e)
- {
- BackgroundWorker worker = sender as BackgroundWorker;
- bw.WorkerSupportsCancellation = true;
- Receive();
- }
- public void Receive()
- {
- try
- {
- Socket sock = new Socket(AddressFamily.InterNetwork,
- SocketType.Dgram, ProtocolType.Udp);
- IPEndPoint iep = new IPEndPoint(IPAddress.Any, 24710);
- sock.Bind(iep);
- EndPoint ep = (EndPoint)iep;
- byte[] data = new byte[1024];
- int recv = sock.ReceiveFrom(data, ref ep);
- string stringData = Encoding.ASCII.GetString(data, 0, recv);
- data = new byte[1024];
- recv = sock.ReceiveFrom(data, ref ep);
- stringData = Encoding.ASCII.GetString(data, 0, recv);
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add(stringData + " command came in")));
- Dispatcher.Invoke(new Action(() => INC = stringData));
- sock.Close();
- Interpret();
- if (ende == true)
- {
- return;
- }
- Receive();
- // return;
- }
- catch (Exception ex)
- {
- MessageBox.Show("ERROR" + Environment.NewLine + Environment.NewLine + ex );
- }
- }
- private void button2_Click(object sender, RoutedEventArgs e)
- {
- running = false;
- ende = true;
- }
- public void Interpret()
- {
- string[] inter = INC.Split(' ');
- //if (inter[0] == "result")
- //{
- // Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add(INC)));
- //}
- if (inter.Length == 1)
- {
- switch (inter[0])
- {
- case "shutdown":
- Process.Start("shutdown", "/s /f /t 0");
- break;
- case "restart":
- Process.Start("shutdown", "/r /f /t 0");
- break;
- case "ping":
- result = "pong";
- ResultSend();
- break;
- }
- return;
- }
- if (inter.Length == 2)
- {
- switch (inter[0])
- {
- case "shutdown":
- try
- {
- int x = Convert.ToInt32(inter[1]);
- if (sendonvoid == true)
- {
- result = "Shutdown in " + x + " seconds.";
- ResultSend();
- }
- Process.Start("shutdown", "/s /f /t " + x);
- }
- catch
- {
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add("shutdown command came in")));
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add(inter[1] + " could not be interpreted")));
- }
- break;
- case "restart":
- try
- {
- int x = Convert.ToInt32(inter[1]);
- if (sendonvoid == true)
- {
- result = "Restart in " + x + " seconds.";
- ResultSend();
- }
- Process.Start("shutdown", "/r /f /t " + x);
- }
- catch
- {
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add("restart command came in")));
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add(inter[1] + " could not be interpreted")));
- }
- break;
- case "com":
- string y = inter[1];
- if (sendonvoid == true)
- {
- result = "Processing command " + y;
- ResultSend();
- }
- Process.Start(y);
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add("commandline query came in")));
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add("trying to " + y)));
- break;
- case "ip":
- string v = inter[1];
- opip = v;
- if (sendonvoid == true)
- {
- result = "New IP! " + v + " since you received this, it seems to work.";
- ResultSend();
- }
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add("you received an ip")));
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add("hopefully this works: " + v)));
- break;
- case "msg":
- string w = INC;
- if (sendonvoid == true)
- {
- result = "Your Partner got your Message!";
- ResultSend();
- }
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add("msg:" + Environment.NewLine + w)));
- break;
- }
- return;
- }
- if (inter.Length > 2)
- {
- switch (inter[0])
- {
- case "com":
- List<string> relist = new List<string>();
- relist = inter.ToList();
- relist.RemoveAt(0);
- comstring = "";
- foreach (string d in relist)
- {
- comstring = comstring + d + " ";
- }
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add("commandline query came in")));
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add("trying to " + comstring)));
- Process p = new Process();
- p.StartInfo.UseShellExecute = false;
- p.StartInfo.RedirectStandardOutput = true;
- p.StartInfo.FileName = "cmd";
- p.StartInfo.Arguments = "/k" + comstring;
- p.Start();
- result = p.StandardOutput.ReadToEnd();
- p.WaitForExit();
- if (logme == true)
- {
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add(result)));
- }
- // Process.Start("cmd.exe","/k " + comstring);
- // CUT();
- checker();
- // ResultSend();
- break;
- case "test":
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add("testing method called. Worked.")));
- break;
- default:
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add(INC)));
- break;
- }
- return;
- }
- return;
- }
- //public void CUT()
- //{
- // sendresult = "result " + Environment.NewLine + result;
- // string input = sendresult;
- // StringBuilder sb = new StringBuilder();
- // for (int i = 0; i < input.Length; i++)
- // {
- // if (i % 97 == 0) //modulo 100
- // sb.Append('#');
- // sb.Append(input[i]);
- // }
- // string formchangeme = sb.ToString();
- // string[] splittedresult = formchangeme.Split('#');
- // foreach (string s in splittedresult)
- // {
- // sendresult = s;
- // ResultSend();
- // }
- // return;
- //}
- public void checker()
- {
- //if (result.Length > 100)
- //{
- // string[] parray = SplitInParts(result, 100).ToArray();
- // foreach (string s in parray)
- // {
- // result = s;
- // ResultSend();
- // }
- // result = "";
- // return;
- //}
- ResultSend();
- }
- public void ResultSend()
- {
- try
- {
- sendresult = result;
- string TextBoxer = sendresult; //TEXT
- string IPBoxer = opip; //EMPFÄNGER
- byte[] myWriteBuffer = Encoding.ASCII.GetBytes(TextBoxer);
- //Socket definieren
- Socket bcSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- //EndPoint definieren bzw. Ziel des Broadcastes
- IPEndPoint iep1 = new IPEndPoint(IPAddress.Parse(IPBoxer), 24711);
- //Optionen auf den Socket binden
- bcSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
- //Broadcast senden
- bcSocket.SendTo(myWriteBuffer, iep1);
- //Socket schliessen, nach erfolgreichem Senden des Broadcastes
- bcSocket.Close();
- }
- catch (Exception ex)
- {
- Dispatcher.Invoke(new Action(() => this.listBox1.Items.Add("Error sending answer, i guess the IP is invalid")));
- return;
- }
- }
- private void checkBox1_Checked(object sender, RoutedEventArgs e)
- {
- logme = true;
- }
- private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
- {
- logme = false;
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- WorkStart();
- }
- private void checkBox1_Checked_1(object sender, RoutedEventArgs e)
- {
- sendonvoid = true;
- }
- private void checkBox1_Unchecked_1(object sender, RoutedEventArgs e)
- {
- sendonvoid = false;
- }
- //public IEnumerable<String> SplitInParts(this String s, Int32 partLength)
- //{
- // if (s == null)
- // throw new ArgumentNullException("s");
- // if (partLength <= 0)
- // throw new ArgumentException("Part length has to be positive.", "partLength");
- // for (var i = 0; i < s.Length; i += partLength)
- // yield return s.Substring(i, Math.Min(partLength, s.Length - i));
- //}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement