2

我有一个 IP 绑定到 Socks5 的问题。我有几个 IP 地址。

所有传入连接都绑定到默认 IP。没有默认IP如何绑定任何IP?

任何身体帮助我?谢谢

这是我的代码的一部分。

        Console.WriteLine("new client connected: " + socksClient.Client.RemoteEndPoint.ToString());
        NetworkStream socksClientStream = socksClient.GetStream();
        /* *****************************************************
         * supported methods part
         * ***************************************************** */
        byte[] authFields = new byte[2];
        socksClientStream.Read(authFields, 0, 2);
        Console.WriteLine(string.Format("authFields: version {0} methods {1}", authFields[0], authFields[1]));
        // read n supported methods 
        byte[] methods = new byte[authFields[1]];
        socksClientStream.Read(methods, 0, methods.Length);
        Console.WriteLine("socks client supports " + methods.Length.ToString() + " methods");
        /* *****************************************************
         * method selection part
         * ***************************************************** */
        byte[] selectedAuthMethod = { SOCKS_VERSION, SOCKS_NOAUTH };
        socksClientStream.Write(selectedAuthMethod, 0, 2);

        // return version and auth method "no auth"

        byte[] requestFields = new byte[4];
        socksClientStream.Read(requestFields, 0, 4);
        Console.WriteLine("socksclient requests address type " + requestFields[3].ToString());

        // adresstypes ipv4 and hostname are supported
        string connection_target = "";
        int target_port;
        if (requestFields[3] == SOCKS_IPV4ADDR )
        {
            //ip4v adress is requested
            byte[] target_data = new byte[4];
            socksClientStream.Read(target_data, 0, 4);
            IPAddress ip = new IPAddress(target_data);
            connection_target = ip.ToString();
        }
        else if (requestFields[3] == SOCKS_DNSNAME )
        {
            //domainname requested
            //doc: byteorder
            byte[] domainname_length = new byte[1];
            // read the following domain name length
            socksClientStream.Read(domainname_length, 0, 1);
            byte[] target_data = new byte[domainname_length[0]];
            // read a domain name of requseted length
            socksClientStream.Read(target_data, 0, domainname_length[0]);
            //interpret the domainname in default encoding
            connection_target = Encoding.Default.GetString(target_data);
        }
        else
        {
            //unsupported addresstype
            Console.WriteLine("unsupported addresstype " + requestFields[3].ToString());

        }

        //if requested address is supported
        if (connection_target != "")
        {

           /* *****************************************************
           * server client connection part
           * ***************************************************** */


            //doc: byteorder
            byte[] bintargetport = new byte[2];
            socksClientStream.Read(bintargetport, 0, 2);
            byte[] tmp_byteorder = new byte[2];
            tmp_byteorder[0] = bintargetport[1];
            tmp_byteorder[1] = bintargetport[0];
            target_port = (int)BitConverter.ToUInt16(tmp_byteorder, 0);
            Console.WriteLine("requesting " + connection_target + ":" + target_port.ToString());

            serverClient = new TcpClient(connection_target, target_port);


            /* *****************************************************
            * reply part
            * ***************************************************** */

            if (serverClient.Connected)
            {
                // reply successful audience
                byte[] reply = new byte[10];
                //version
                reply[0] = SOCKS_VERSION ;
                // replycode
                reply[1] = SOCKS_REPLYSUCCESS ;
                //reserved and 0
                reply[2] = 0;
                // addresstype
                reply[3] = 1;
                string ip = serverClient.Client.LocalEndPoint.ToString().Split(':')[0];
                IPAddress ipaddr = IPAddress.Parse(ip);
                reply[4] = ipaddr.GetAddressBytes()[0];
                reply[5] = ipaddr.GetAddressBytes()[1];
                reply[6] = ipaddr.GetAddressBytes()[2];
                reply[7] = ipaddr.GetAddressBytes()[3];
                int port = int.Parse(serverClient.Client.LocalEndPoint.ToString().Split(':')[1]);
                // read unsigned integer in networkoctet order
                reply[8] = BitConverter.GetBytes((UInt16)port)[0];
                reply[9] = BitConverter.GetBytes((UInt16)port)[1];
                socksClientStream.Write(reply, 0, 10);
                Console.WriteLine("writing reply");

               /* *****************************************************
                * tcp redirection
                * ***************************************************** */


                NetworkStream serverClientStream = serverClient.GetStream();

                bool ioError = false;

                // forward tcp data till one of the connected endpoints is disconnected
                while (serverClient.Connected && socksClient.Connected && !ioError )
                {
                     ....
                }
4

0 回答 0