Exemple #1
0
        Session SessionFromArgs(string[] args)
        {
            if (args.Length == 0)
            {
                return(null);
            }
            try
            {
                ConnectionSettings cs       = null;
                string             listener = null;
                bool udp = false;
                for (int i = 0; i < args.Length; i++)
                {
                    var arg = args[i];
                    if (arg.Equals("-l", StringComparison.OrdinalIgnoreCase) && args.Length > i + 1)
                    {
                        arg = args[++i];
                        if (int.TryParse(arg, out var port))
                        {
                            listener = "0.0.0.0:" + port;
                        }
                        else
                        {
                            listener = arg;
                        }
                        continue;
                    }
                    if (arg.Equals("-u", StringComparison.OrdinalIgnoreCase))
                    {
                        udp = true;
                        continue;
                    }

                    if (args[i].StartsWith("-"))
                    {
                        throw new InvalidOperationException("Invalid switch: " + args[i]);
                    }

                    cs = ConnectionSettings.TryCreateFromString(args[i]);
                }

                udp |= cs?.UDPChecked ?? false; // enable UDP when target was url like udp://1.2.3.4:56

                if (Net.TryParseUri(listener, out var listenerUri) && cs == null)
                {
                    cs = ConnectionSettings.New();
                }

                if (cs != null)
                {
                    cs.TCPChecked = !udp;
                    cs.UDPChecked = udp;
                }
                if (listenerUri != null)
                {
                    cs.ServerChecked            = true;
                    cs.ServerSettings.IPAddress = listenerUri.GetTrimmedHost(); // expected IP address
                    cs.ServerSettings.Port      = listenerUri.Port;
                }

                return(cs?.CreateSession());
            }
            catch (Exception ex)
            {
                string msg = "";
                if (ex is ValueException)
                {
                    msg = ex.Message + "\n\n";
                }
                MessageBox.Show(msg + "Usage: swiddler.exe [remote_ip:port] [-l [listener_ip:]port] [-u]", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                Environment.Exit(1);
            }

            return(null);
        }