Esempio n. 1
0
        static Int32 Main(string[] args)
        {
            ConsoleClientOptions optionsParser = new ConsoleClientOptions();
            List <String>        nonOptionArgs = optionsParser.Parse(args);

            InternetHost host;

            if (nonOptionArgs.Count > 0)
            {
                if (nonOptionArgs.Count != 2)
                {
                    Console.WriteLine("Expected either 0 or 2 command line aruments but got {0}", nonOptionArgs.Count);
                    optionsParser.PrintUsage();
                    return(-1);
                }

                Proxy  proxy;
                String ipOrHost = Proxy.StripAndParseProxies(args[0], DnsPriority.IPv4ThenIPv6, out proxy);
                host = new InternetHost(ipOrHost, UInt16.Parse(args[1]), DnsPriority.IPv4ThenIPv6, proxy);
            }
            else
            {
                host = default(InternetHost);
            }

            ConsoleClient commandClient = new ConsoleClient(1024, 1024, host, new ConsoleMessageLogger("Connection"),
                                                            new ConnectionDataLoggerSingleLog(ConsoleDataLogger.Instance, "[SendData]", "[RecvData]"));

            commandClient.RunPrepare();
            commandClient.Run();

            return(0);
        }
Esempio n. 2
0
        public AccessorConnection(String connectorString)
        {
            Proxy  proxy;
            String ipOrHostOptionalPort = Proxy.StripAndParseProxies(connectorString, DnsPriority.IPv4ThenIPv6, out proxy);
            UInt16 port     = Tmp.DefaultPort;
            String ipOrHost = EndPoints.SplitIPOrHostAndOptionalPort(ipOrHostOptionalPort, ref port);

            this.accessorHost = new InternetHost(ipOrHost, port, DnsPriority.IPv4ThenIPv6, proxy);
        }
Esempio n. 3
0
        public static Socket NewSocketConnection(ref InternetHost host)
        {
            Socket    socket = new Socket(host.GetAddressFamilyForTcp(), SocketType.Stream, ProtocolType.Tcp);
            BufStruct dataLeftOverFromProxyConnect = default(BufStruct);

            host.Connect(socket, DnsPriority.IPv4ThenIPv6, ProxyConnectOptions.None, ref dataLeftOverFromProxyConnect);
            if (dataLeftOverFromProxyConnect.contentLength > 0)
            {
                throw new NotImplementedException();
            }
            return(socket);
        }
Esempio n. 4
0
        public static Proxy ParseProxy(String proxySpecifier, PriorityQuery <IPAddress> dnsPriorityQuery, Proxy proxyForProxy)
        {
            // format
            // http:<ip-or-host>:<port>
            // socks4:<ip-or-host>:<port>
            if (proxySpecifier == null || proxySpecifier.Length <= 0)
            {
                return(default(Proxy));
            }

            String[] splitStrings = proxySpecifier.Split(':');
            if (splitStrings.Length != 3)
            {
                throw new FormatException(String.Format("Invalid proxy '{0}', expected 'http:<host>:<port>', 'socks4:<host>:<port>' or 'socks5:<host>:<port>'", proxySpecifier));
            }

            String proxyTypeString = splitStrings[0];
            String ipOrHost        = splitStrings[1];
            String portString      = splitStrings[2];

            UInt16 port;

            if (!UInt16Parser.TryParse(portString, out port))
            {
                throw new FormatException(String.Format("Invalid port '{0}'", portString));
            }
            InternetHost proxyHost = new InternetHost(ipOrHost, port, dnsPriorityQuery, proxyForProxy);

            if (proxyTypeString.Equals("socks4", StringComparison.CurrentCultureIgnoreCase))
            {
                return(new Socks4Proxy(proxyHost, null));
            }
            else if (proxyTypeString.Equals("socks5", StringComparison.CurrentCultureIgnoreCase))
            {
                return(new Socks5NoAuthenticationConnectSocket(proxyHost));
            }
            else if (proxyTypeString.Equals("http", StringComparison.CurrentCultureIgnoreCase))
            {
                return(new HttpProxy(proxyHost));
            }
            else if (proxyTypeString.Equals("gateway", StringComparison.CurrentCultureIgnoreCase))
            {
                return(new GatewayProxy(proxyHost));
            }
            else if (proxyTypeString.Equals("httpconnect", StringComparison.CurrentCultureIgnoreCase))
            {
                return(new HttpConnectProxyProxy(proxyHost));
            }

            throw new FormatException(String.Format("Unexpected proxy type '{0}', expected 'gateway', 'http', 'httpconnect', 'socks4' or 'socks5'", proxyTypeString));
        }
Esempio n. 5
0
        static Int32 Main(string[] args)
        {
            TelnetOptions optionsParser = new TelnetOptions();
            List <String> nonOptionArgs = optionsParser.Parse(args);

            if (nonOptionArgs.Count != 1)
            {
                Console.WriteLine("Expected 1 argument but got {0}", nonOptionArgs.Count);
                optionsParser.PrintUsage();
                return(-1);
            }

            InternetHost?serverHost;

            if (nonOptionArgs.Count == 1)
            {
                String connectorString = nonOptionArgs[0];
                Proxy  proxy;
                String ipOrHostOptionalPort = Proxy.StripAndParseProxies(connectorString, DnsPriority.IPv4ThenIPv6, out proxy);
                UInt16 port     = DefaultPort;
                String ipOrHost = EndPoints.SplitIPOrHostAndOptionalPort(ipOrHostOptionalPort, ref port);
                serverHost = new InternetHost(ipOrHost, port, DnsPriority.IPv4ThenIPv6, proxy);
            }
            else
            {
                serverHost = null;
            }

            TelnetClient client = new TelnetClient(optionsParser.wantServerEcho.set, !optionsParser.disableColorDecoding.set);

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            if (serverHost != null)
            {
                BufStruct leftOver = default(BufStruct);
                socket.Connect(serverHost.Value, DnsPriority.IPv4ThenIPv6, ProxyConnectOptions.None, ref leftOver);
                if (leftOver.contentLength > 0)
                {
                    Console.WriteLine(Encoding.UTF8.GetString(leftOver.buf, 0, (int)leftOver.contentLength));
                }
            }

            client.Run(socket);

            return(0);
        }
Esempio n. 6
0
        public ConsoleClient(Int32 sendFileBufferSize, Int32 recvBufferSize, InternetHost serverHost,
                             MessageLogger messageLogger, IConnectionDataLogger connectionLogger)
        {
            this.sendFileBufferSize = sendFileBufferSize;
            this.recvBufferSize     = recvBufferSize;

            this.serverHost = serverHost;

            this.messageLogger    = messageLogger;
            this.connectionLogger = connectionLogger;

            commandDictionary = new Dictionary <String, CommandFunction>();
            commandDictionary.Add("open", OpenCommand);
            commandDictionary.Add("close", CloseCommand);
            commandDictionary.Add("send", SendCommand);
            commandDictionary.Add("sendfile", SendFileCommand);
            commandDictionary.Add("proxy", ProxyCommand);
            commandDictionary.Add("help", HelpCommand);
            commandDictionary.Add("exit", ExitCommand);
            commandDictionary.Add("echo", EchoCommand);
        }
Esempio n. 7
0
        public static Int32 Run(Options options, List <String> nonOptionArgs)
        {
            log = Console.Out;

            //
            // Options
            //
            if (nonOptionArgs.Count < 2)
            {
                return(options.ErrorAndUsage("Not enough arguments"));
            }

            String clientSideConnector = nonOptionArgs[0];
            String listenPortsString   = nonOptionArgs[1];

            {
                Proxy  proxy;
                String ipOrHostAndPort = Proxy.StripAndParseProxies(clientSideConnector, DnsPriority.IPv4ThenIPv6, out proxy);
                UInt16 port;
                String ipOrHost = EndPoints.SplitIPOrHostAndPort(ipOrHostAndPort, out port);
                server = new InternetHost(ipOrHost, port, DnsPriority.IPv4ThenIPv6, proxy);
            }
            SortedNumberSet listenPortSet = PortSet.ParsePortSet(listenPortsString);

            SelectServer selectServer = new SelectServer(false, new Buf(options.readBufferSize.ArgValue));
            IPAddress    listenIP     = IPAddress.Any;

            foreach (var port in listenPortSet)
            {
                IPEndPoint listenEndPoint = new IPEndPoint(listenIP, port);
                Socket     socket         = new Socket(listenEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                socket.Bind(listenEndPoint);
                socket.Listen(options.socketBackLog.ArgValue);
                selectServer.control.AddListenSocket(socket, AcceptCallback);
            }

            selectServer.Run();
            return(0);
        }
Esempio n. 8
0
        public void OpenCommand(String line)
        {
            String server = line.Peel(out line);

            if (String.IsNullOrEmpty(server))
            {
                if (String.IsNullOrEmpty(serverHost.targetEndPoint.ipOrHost))
                {
                    Console.WriteLine("Error: missing server and port");
                    return;
                }
            }
            else
            {
                line = (line == null) ? null : line.Trim();
                if (String.IsNullOrEmpty(line))
                {
                    Console.WriteLine("Error: missing port");
                    return;
                }
                UInt16 port;
                try
                {
                    port = UInt16.Parse(line);
                }
                catch (FormatException)
                {
                    Console.WriteLine("Error: invalid port '{0}'", line);
                    return;
                }

                Proxy  proxy;
                String ipOrHost = Proxy.StripAndParseProxies(server, DnsPriority.IPv4ThenIPv6, out proxy);
                serverHost = new InternetHost(ipOrHost, port, DnsPriority.IPv4ThenIPv6, proxy);
            }
            Open();
        }
Esempio n. 9
0
 public GatewayProxy(InternetHost host)
     : base(host)
 {
 }
Esempio n. 10
0
        public Socket MakeNewSocketAndConnectOnPort(UInt16 port)
        {
            InternetHost tempHost = new InternetHost(accessorHost, port);

            return(NewSocketConnection(ref tempHost));
        }
Esempio n. 11
0
 public void ProxyCommand(String line)
 {
     serverHost = new InternetHost(serverHost, Proxy.ParseProxy(line, DnsPriority.IPv4ThenIPv6, null));
 }
Esempio n. 12
0
 public Socks4Proxy(InternetHost host, byte[] userID)
     : base(host)
 {
     this.userID = userID;
 }
Esempio n. 13
0
        static Int32 Main(string[] args)
        {
            DnsClientOptions optionsParser = new DnsClientOptions();
            List <String>    nonOptionArgs = optionsParser.Parse(args);

            if (nonOptionArgs.Count > 1)
            {
                Console.WriteLine("Expected up to 1 non-option argument, you gave {0}", nonOptionArgs.Count);
                optionsParser.PrintUsage();
                return(-1);
            }

            InternetHost host;

            if (nonOptionArgs.Count == 1)
            {
                Proxy  proxy;
                String ipOrHostAndPort = Proxy.StripAndParseProxies(nonOptionArgs[0], DnsPriority.IPv4ThenIPv6, out proxy);
                UInt16 port;
                String ipOrHost = EndPoints.SplitIPOrHostAndPort(ipOrHostAndPort, out port);
                host = new InternetHost(ipOrHost, port, DnsPriority.IPv4ThenIPv6, proxy);
            }

            Byte[] packet = new Byte[1024];
            UInt32 offset = 0;

            DnsQueryHeader query = new DnsQueryHeader();

            query.id               = 0x1234;
            query.opCode           = DnsOpCode.Query;
            query.recursionDesired = false;
            query.qdCount          = 0;
            query.anCount          = 0;
            query.nsCount          = 0;
            query.arCount          = 0;


            offset = query.InsertDnsHeader(packet, offset);


            for (int i = 0; i < offset; i++)
            {
                Console.WriteLine("[{0}] {1:X}", i, packet[i]);
            }

            Socket   sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            EndPoint ep   = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 53);

            sock.SendTo(packet, (int)offset, SocketFlags.None, ep);

            sock.ReceiveTimeout = 500;
            int received = sock.ReceiveFrom(packet, ref ep);

            if (received > 0)
            {
                Console.WriteLine("Received {0} Bytes: {1}", received, BitConverter.ToString(packet, received));
            }
            else
            {
                Console.WriteLine("Received = {0}", received);
            }



            return(0);
        }
Esempio n. 14
0
 public InternetHost(InternetHost other, Proxy proxy)
 {
     this.targetEndPoint   = other.targetEndPoint;
     this.dnsPriorityQuery = other.dnsPriorityQuery;
     this.proxy            = proxy;
 }
Esempio n. 15
0
 public InternetHost(InternetHost other, UInt16 port)
 {
     this.targetEndPoint   = new StringEndPoint(other.targetEndPoint, port);
     this.dnsPriorityQuery = other.dnsPriorityQuery;
     this.proxy            = other.proxy;
 }
Esempio n. 16
0
 public HttpProxy(InternetHost host)
     : base(host)
 {
 }
Esempio n. 17
0
 public HttpConnectProxyProxy(InternetHost host)
     : base(host)
 {
 }
Esempio n. 18
0
        public InternetHost host; // Cannot be readonly because it is a struct with non-readonly fields

        /*
         * /// <summary>
         * /// endPoint cannot be null
         * /// </summary>
         * public Proxy(String ipOrHost, IPEndPoint endPoint)
         * {
         *  this.ipOrHost = ipOrHost;
         *  this.endPoint = endPoint;
         * }
         */
        protected Proxy(InternetHost host)
        {
            this.host = host;
        }
Esempio n. 19
0
 public Socks5NoAuthenticationConnectSocket(InternetHost host)
     : base(host)
 {
     //int maxAuthenticationBuffer = 3 + usernameBytes.Length + passwordBytes.Length;
     this.buffer = new Byte[21];
 }
Esempio n. 20
0
 public TcpCallback(InternetHost serverHost)
 {
     this.serverHost = serverHost;
 }