Пример #1
0
        public SoftEtherParameterCollection CallMethod(string functionName,
                                                       SoftEtherParameterCollection payload = null)
        {
            if (payload == null)
            {
                payload = new SoftEtherParameterCollection();
            }

            payload.RemoveNullParameters();
            payload.Add("function_name", functionName);

            var serializedPayload = SoftEtherProtocol.Serialize(payload);
            var serializedLength  = SoftEtherProtocol.SerializeInt(serializedPayload.Length);

            _socket.Write(serializedLength);
            _socket.Write(serializedPayload);

            _socket.Flush();

            var dataLength = new byte[4];
            var bytesRead  = _socket.Read(dataLength, 0, 4);

            if (bytesRead != 4)
            {
                throw new Exception("Failed to read dataLength");
            }

            var dataLengthAsInt = SoftEtherProtocol.DeserializeInt(dataLength);
            var responseBuffer  = new byte[dataLengthAsInt];

            bytesRead = 0;
            for (var i = 0; i < 10; i++) //retrie 10 times to read all data
            {
                bytesRead += _socket.Read(responseBuffer, bytesRead, dataLengthAsInt - bytesRead);
                if (bytesRead == dataLengthAsInt)
                {
                    break;
                }
                Thread.Sleep(50);
            }

            if (bytesRead != dataLengthAsInt)
            {
                throw new Exception("read less than dataLength");
            }

            var response = SoftEtherProtocol.Deserialize(responseBuffer);

            return(response);
        }
Пример #2
0
        public AuthResult Authenticate(byte[] passwordHash, string hubName = null)
        {
            if (RandomFromServer == null)
            {
                return new AuthResult {
                           Error = SoftEtherError.ConnectFailed
                }
            }
            ;

            var authPayload = new SoftEtherParameterCollection
            {
                { "method", "admin" },
                { "client_str", "SoftEtherNet" },
                { "client_ver", 1 },
                { "client_build", 0 }
            };

            if (!string.IsNullOrWhiteSpace(hubName))
            {
                authPayload.Add("hubname", hubName);
            }

            var securePassword = CreateSaltedHash(passwordHash, RandomFromServer);

            authPayload.Add("secure_password", securePassword);

            var serializedAuthPayload = SoftEtherProtocol.Serialize(authPayload);

            _socket.SendHttpRequest("POST", "/vpnsvc/vpn.cgi", serializedAuthPayload,
                                    SoftEtherNetwork.GetDefaultHeaders());

            var authResponse = _socket.GetHttpResponse();

            if (authResponse.code != 200)
            {
                return new AuthResult {
                           Error = SoftEtherError.AuthFailed
                }
            }
            ;

            var authDict = SoftEtherProtocol.Deserialize(authResponse.body);

            return(AuthResult.Deserialize(authDict));
        }