Beispiel #1
0
        public void setParentNode()
        {
            Task.Run(() => {
                Skynet.Base.Skynet sender = new Skynet.Base.Skynet();
                Node mNode = new Node(new List <NodeId>(), mSkynet);
                Task.Run(async() => {
                    // create a node

                    Node parentNode = new Node(new List <NodeId>(), sender);

                    long timeStamp  = Skynet.Utils.Utils.UnixTimeNow();
                    ToxResponse res = await RequestProxy.sendRequest(mSkynet, new ToxRequest
                    {
                        uuid       = Guid.NewGuid().ToString(),
                        url        = "node/" + mNode.selfNode.uuid + "/parent",
                        method     = "put",
                        content    = JsonConvert.SerializeObject(parentNode.selfNode),
                        fromNodeId = parentNode.selfNode.uuid,
                        fromToxId  = sender.tox.Id.ToString(),
                        toNodeId   = mNode.selfNode.uuid,
                        toToxId    = mSkynet.tox.Id.ToString(),
                        time       = timeStamp,
                    });
                    NodeResponse mRes = JsonConvert.DeserializeObject <NodeResponse>(res.content);
                    Console.WriteLine("value: " + mRes.value);
                    Assert.AreEqual(mRes.statusCode, NodeResponseCode.OK);
                    Assert.AreEqual(timeStamp, mRes.time);
                    Assert.AreEqual(timeStamp, mNode.parentModifiedTime);
                }).GetAwaiter().GetResult();
            });
        }
Beispiel #2
0
        public void PutTest()
        {
            Node node1 = new Node(mSkynet);
            Node node2 = new Node(mSkynet);
            Node node3 = new Node(mSkynet);

            node2.parent             = node1.selfNode;
            node2.parentModifiedTime = Skynet.Utils.Utils.UnixTimeNow();
            Task.Run(async() => {
                ToxResponse response = await RequestProxy.sendRequest(mSkynet, new ToxRequest {
                    uuid       = Guid.NewGuid().ToString(),
                    url        = "node/" + node3.selfNode.uuid + "/grandParents",
                    content    = JsonConvert.SerializeObject(node1.selfNode),
                    method     = "put",
                    fromNodeId = node2.selfNode.uuid,
                    fromToxId  = node2.selfNode.toxid,
                    toNodeId   = node3.selfNode.uuid,
                    toToxId    = node3.selfNode.toxid,
                    time       = node2.parentModifiedTime
                });

                NodeResponse res = JsonConvert.DeserializeObject <NodeResponse>(response.content);
                Assert.AreEqual(node1.selfNode.uuid, node3.grandParents.uuid);
                Assert.AreEqual(node2.parentModifiedTime, node3.grandParentsModifiedTime);
            }).GetAwaiter().GetResult();
        }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="DevAchievements.Infrastructure.AchievementProviders.GitHub.GitHubAchievementProvider"/> class.
 /// </summary>
 public GitHubAchievementProvider()
     : base(new AchievementIssuer("GitHub")
 {
     LogoUrl = "https://github.global.ssl.fastly.net/images/modules/logos_page/GitHub-Logo.png"
 })
 {
     m_requestProxy = new RequestProxy(new NullLogger(), new AnonymousAuthenticationProvider());
 }
Beispiel #4
0
        protected override void ConfigureInMemoryReceiveEndpoint(IInMemoryReceiveEndpointConfigurator configurator)
        {
            var testActivity   = GetActivityContext <TestActivity>();
            var secondActivity = GetActivityContext <SecondTestActivity>();

            _requestProxy  = new RequestProxy(testActivity, secondActivity);
            _responseProxy = new ResponseProxy();

            configurator.Instance(_requestProxy);
            configurator.Instance(_responseProxy);

            _requestAddress = configurator.InputAddress;
        }
Beispiel #5
0
        public void exception_should_be_thrown_when_specifying_empty_url()
        {
            Scenario.StartNew(this, scenario =>
            {
                scenario.Given("an empty url is specified");

                scenario.When("trying to create a RequestProxy");

                scenario.Then("an exception should be thrown", () =>
                {
                    IRequestProxy requestProxy = new RequestProxy("");
                });
            });
        }
Beispiel #6
0
        public void should_return_null_when_server_is_unavailable()
        {
            IRequestProxy requestProxy = new RequestProxy("http://localhost:9999");
            Scenario.StartNew(this, scenario =>
            {
                scenario.Given("Hudson server is down");

                scenario.When("trying to fetch root");

                scenario.Then("an empty root object will be returned", () =>
                {
                    Root root = requestProxy.Execute<Root>("api/json");
                    root.ShouldBeNull();
                });
            });
        }
Beispiel #7
0
        private string ProxyLog(RequestProxy requestProxy)
        {
            string proxyDetails = Constants.PROXY_SETTINGS + Constants.PROXY_HOST + requestProxy.Host + ", " + Constants.PROXY_PORT + requestProxy.Port.ToString();

            if (requestProxy.User != null)
            {
                proxyDetails += ", " + Constants.PROXY_USER + requestProxy.User;
            }

            if (requestProxy.UserDomain != null)
            {
                proxyDetails += ", " + Constants.PROXY_DOMAIN + requestProxy.UserDomain;
            }

            return(proxyDetails);
        }
Beispiel #8
0
        public void relatedNodesStatusChanged(NodeId targetNode)
        {
            if (targetNode == nodeChangeLock.from)
            {
                nodeChangeLock.isLocked = false;
            }
            // child nodes offline
            NodeId childNodeToRemove = childNodes.Where(x => x.uuid == targetNode.uuid).DefaultIfEmpty(null).FirstOrDefault();

            if (childNodeToRemove != null)
            {
                childNodes.Remove(targetNode);
                Task.Run(async() =>
                {
                    ToxResponse res = await RequestProxy.sendRequest(mSkynet, new ToxRequest
                    {
                        uuid       = Guid.NewGuid().ToString(),
                        url        = "node/" + selfNode.uuid + "/childNodes",
                        method     = "put",
                        content    = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(childNodes)),
                        fromNodeId = selfNode.uuid,
                        fromToxId  = selfNode.toxid,
                        toNodeId   = selfNode.uuid,
                        toToxId    = selfNode.toxid,
                        time       = childNodesModifiedTime,
                    });
                });
                return;
            }
            // parent node offline
            if (targetNode.uuid == parent.uuid)
            {
                Task.Run(async() =>
                {
                    if (grandParents == null)
                    {
                        return;
                    }
                    bool isConnected = await joinNetByTargetParents(new List <NodeId> {
                        grandParents
                    });
                    // rejoin net might be failed, grandparents may also offline
                });
            }

            // grand parents and brothers will not be processed, just wait for request from parents
        }
Beispiel #9
0
        public async Task <NodeResponse> Get(string id)
        {
            Skynet curHost = Skynet.allInstance.Where(x => x.httpPort == Request.RequestUri.Port).FirstOrDefault();

            if (!ToxId.IsValid(id))
            {
                return(new NodeResponse
                {
                    statusCode = NodeResponseCode.InvalidRequest,
                    description = "your tox id is invalid",
                });
            }

            // check if target tox client is local client
            if (curHost.tox.Id.ToString() == id)
            {
                // list all nodes on target tox client
                return(new NodeResponse
                {
                    statusCode = NodeResponseCode.OK,
                    description = "success",
                    value = JsonConvert.SerializeObject(new ToxClient
                    {
                        Id = id,
                        nodes = Node.AllLocalNodes.Select(x => x.getInfo()).ToList()
                    })
                });
            }
            // if not, send tox req to target tox client
            bool        reqStatus    = false;
            ToxResponse nodeResponse = await curHost.sendRequest(new ToxId(id), RequestProxy.toNodeRequest(Request), out reqStatus);

            if (nodeResponse != null)
            {
                return(JsonConvert.DeserializeObject <NodeResponse>(Encoding.UTF8.GetString(nodeResponse.content)));
            }
            else
            {
                return new NodeResponse
                       {
                           statusCode  = NodeResponseCode.NotFound,
                           description = "target does not exist or target is current offline",
                       }
            };
        }
Beispiel #10
0
        private string ProxyLog(RequestProxy requestProxy)
        {
            StringBuilder proxyStringBuilder = new StringBuilder();

            proxyStringBuilder.Append(Constants.PROXY_SETTINGS);

            proxyStringBuilder.Append(Constants.PROXY_HOST).Append(requestProxy.Host).Append(", ");

            proxyStringBuilder.Append(Constants.PROXY_PORT).Append(requestProxy.Port.ToString());

            if (requestProxy.User != null)
            {
                proxyStringBuilder.Append(", ").Append(Constants.PROXY_USER).Append(requestProxy.User);
            }

            if (requestProxy.UserDomain != null)
            {
                proxyStringBuilder.Append(", ").Append(Constants.PROXY_DOMAIN).Append(requestProxy.UserDomain);
            }

            return(proxyStringBuilder.ToString());
        }
Beispiel #11
0
        public async Task <NodeResponse> sendRequest(NodeId target, string content, string method,
                                                     string url, long time = 0)
        {
            ToxResponse response = await RequestProxy.sendRequest(mSkynet, new ToxRequest {
                uuid       = Guid.NewGuid().ToString(),
                url        = url,
                method     = method,
                content    = Encoding.UTF8.GetBytes(content),
                fromNodeId = selfNode.uuid,
                fromToxId  = selfNode.toxid,
                toNodeId   = target.uuid,
                toToxId    = target.toxid,
                time       = time
            });

            if (response == null)
            {
                return(null); // request send failed
            }
            NodeResponse res = JsonConvert.DeserializeObject <NodeResponse>(Encoding.UTF8.GetString(response.content));

            return(res);
        }
Beispiel #12
0
        /// <summary>
        /// method to join skynet by set target parents
        /// </summary>
        /// <returns>
        /// the target distributed
        /// </returns>
        public async Task <bool> joinNetByTargetParents(List <NodeId> parentsList)
        {
            List <NodeId> targetNodeList   = parentsList;
            List <NodeId> checkedNodesList = new List <NodeId>();
            NodeId        target           = null;

            while (targetNodeList.Count > 0 && target == null)
            {
                NodeId     parentNode   = targetNodeList[0];
                ToxRequest addParentReq = new ToxRequest
                {
                    url        = "node/" + parentNode.uuid + "/childNodes",
                    method     = "post",
                    uuid       = Guid.NewGuid().ToString(),
                    content    = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(selfNode)),
                    fromNodeId = selfNode.uuid,
                    fromToxId  = selfNode.toxid,
                    toNodeId   = parentNode.uuid,
                    toToxId    = parentNode.toxid,
                    time       = Utils.Utils.UnixTimeNow(),
                };
                ToxResponse mRes = await RequestProxy.sendRequest(mSkynet, addParentReq);

                // send req failed or target is currently locked, ie target is not avaliable right now. remove target node from nodelist
                NodeResponse addParentResponse = JsonConvert.DeserializeObject <NodeResponse>(Encoding.UTF8.GetString(mRes.content));

                if (addParentResponse.statusCode == NodeResponseCode.TargetLocked ||
                    addParentResponse.statusCode == NodeResponseCode.TargetIsFull)
                {
                    targetNodeList.Remove(parentNode);
                    if (!checkedNodesList.Contains <NodeId>(parentNode))
                    {
                        checkedNodesList.Add(parentNode);
                    }
                    // new nodes, not checked yet
                    List <NodeId> newTargetsList = JsonConvert.DeserializeObject <List <NodeId> >(addParentResponse.value).Where((mnode) =>
                    {
                        return(!checkedNodesList.Contains <NodeId>(mnode) && !targetNodeList.Contains <NodeId>(mnode));
                    }).ToList();
                    targetNodeList = targetNodeList.Concat(newTargetsList).ToList();
                    continue;
                }
                else if (addParentResponse.statusCode == NodeResponseCode.OK)
                {
                    // set parent and connect status
                    target = new NodeId
                    {
                        toxid = mRes.fromToxId,
                        uuid  = mRes.fromNodeId
                    };
                    isConnected = true;
                    break;
                }
                else
                {
                    // try to connect next target
                    targetNodeList.Remove(parentNode);
                    if (!checkedNodesList.Contains <NodeId>(parentNode))
                    {
                        checkedNodesList.Add(parentNode);
                    }
                }
            }

            if (target != null)
            {
                isConnected = true;
                // set parents, will boardcast grandparents change to child nodes, and set target grandparents
                ToxResponse setParentResponse = await RequestProxy.sendRequest(mSkynet, new ToxRequest
                {
                    url        = "node/" + selfNode.uuid + "/parent",
                    method     = "put",
                    content    = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(target)),
                    fromNodeId = selfNode.uuid,
                    fromToxId  = selfNode.toxid,
                    toNodeId   = selfNode.uuid,
                    toToxId    = selfNode.uuid,
                    uuid       = Guid.NewGuid().ToString(),
                    time       = Utils.Utils.UnixTimeNow(),
                });
            }
            else
            {
                parent      = null;
                isConnected = false;
            }
            return(isConnected);
        }
Beispiel #13
0
        /// <summary>
        /// parse request from ruiji expression
        /// </summary>
        /// <param name="expression">ruiji expression</param>
        /// <returns>parse result with crawl request</returns>
        public ParseResult <Request> ParseRequest(string expression)
        {
            var result = new ParseResult <Request>(expression);

            var request = new Request();

            var jObj = JObject.FromObject(request);

            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(expression)))
                using (var reader = new StreamReader(stream))
                {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();

                        if (line.StartsWith("[") && line.EndsWith("]"))
                        {
                            var name = line.TrimStart('[').TrimEnd(']');
                            if (name == "address")
                            {
                                name = "uri";
                            }

                            var property = jObj.Property(name);
                            if (property != null)
                            {
                                line = reader.ReadLine();
                                if (string.IsNullOrEmpty(line))
                                {
                                    continue;
                                }

                                switch (name)
                                {
                                case "uri":
                                {
                                    if (!Uri.IsWellFormedUriString(line, UriKind.Absolute))
                                    {
                                        result.Messages.Add("address is not wellformated");
                                        continue;
                                    }
                                    property.Value = line;
                                    break;
                                }

                                case "proxy":
                                {
                                    var sp = line.Split(' ');
                                    if (sp.Length < 2)
                                    {
                                        result.Messages.Add("proxy must set with ip and port");
                                        continue;
                                    }

                                    var proxy = new RequestProxy(sp[0], Convert.ToInt32(sp[1]));
                                    if (sp.Length > 2)
                                    {
                                        proxy.Scheme = sp[2];
                                    }
                                    if (sp.Length > 3)
                                    {
                                        proxy.Username = sp[3];
                                    }
                                    if (sp.Length > 4)
                                    {
                                        proxy.Password = sp[4];
                                    }

                                    property.Value = JToken.FromObject(proxy);
                                    break;
                                }

                                case "headers":
                                {
                                    var headers = new List <WebHeader>();
                                    while (!string.IsNullOrEmpty(line) && !reader.EndOfStream)
                                    {
                                        var sp = line.Split(':');
                                        if (sp.Length < 2)
                                        {
                                            result.Messages.Add("header is not expected");
                                            continue;
                                        }

                                        headers.Add(new WebHeader(line.Substring(0, line.IndexOf(':')), line.Substring(line.IndexOf(':') + 1)));

                                        line = reader.ReadLine();
                                    }

                                    if (headers.Count > 0)
                                    {
                                        property.Value = JToken.FromObject(headers);
                                    }

                                    break;
                                }

                                case "data":
                                {
                                    var data = "";
                                    while (!string.IsNullOrEmpty(line) && !reader.EndOfStream)
                                    {
                                        data += "\n" + line;

                                        line = reader.ReadLine();
                                    }

                                    data = data.Trim();
                                    if (data.StartsWith("{") && data.EndsWith("}"))
                                    {
                                        property.Value = JToken.FromObject(JsonConvert.DeserializeObject <object>(data));
                                    }
                                    else
                                    {
                                        property.Value = data;
                                    }


                                    break;
                                }

                                default:
                                {
                                    property.Value = line;
                                    break;
                                }
                                }
                            }
                        }
                    }

                    result.Result = jObj.ToObject <Request>();
                }

            return(result);
        }
Beispiel #14
0
        /// <summary>
        /// This method makes a Zoho CRM Rest API request.
        /// </summary>
        /// <param name="converterInstance">A Converter class instance to call appendToRequest method.</param>
        /// <returns>HttpWebResponse class instance or null</returns>
        public HttpWebResponse FireRequest(Converter converterInstance)
        {
            SetQueryParams();

            HttpWebRequest requestObj = (HttpWebRequest)WebRequest.Create(url);

            RequestProxy requestProxy = Initializer.GetInitializer().RequestProxy;

            if (requestProxy != null)
            {
                //Validate proxy address
                var proxyURI = new Uri(string.Format("{0}:{1}", requestProxy.Host, requestProxy.Port));

                ICredentials credentials = null;

                if (requestProxy.User != null)
                {
                    //Set credentials
                    credentials = new NetworkCredential(requestProxy.User, requestProxy.Password, requestProxy.UserDomain);
                }

                //Set proxy
                requestObj.Proxy = new WebProxy(proxyURI, true, null, credentials);

                SDKLogger.LogInfo(this.ProxyLog(requestProxy));
            }

            requestObj.Timeout = Initializer.GetInitializer().SDKConfig.Timeout;

            SetRequestMethod(requestObj);

            if (contentType != null)
            {
                this.SetContentTypeHeader(ref requestObj);
            }

            SetQueryHeaders(ref requestObj);

            if (requestBody != null)
            {
                converterInstance.AppendToRequest(requestObj, requestBody);
            }

            SDKLogger.LogInfo(ToString());

            HttpWebResponse response;

            try
            {
                response = (HttpWebResponse)requestObj.GetResponse();
            }
            catch (WebException e)
            {
                if (e.Response == null)
                {
                    throw;
                }

                response = (HttpWebResponse)e.Response;
            }

            return(response);
        }
Beispiel #15
0
        private void Send(string data, int redirections)
        {
            _data = data;
            Encoding encode = Encoding.GetEncoding(_charset);

            _headers.Remove("Content-Length");
            if (!string.IsNullOrEmpty(data) && string.Compare(_method, "post", true) == 0)
            {
                _headers["Content-Length"] = string.Concat(Encoding.GetEncoding(_charset).GetBytes(data).Length);
                if (string.IsNullOrEmpty(_headers["Content-Type"]))
                {
                    _headers["Content-Type"] = "application/x-www-form-urlencoded; charset=" + _charset;
                }
                else if (_headers["Content-Type"].IndexOf("multipart/form-data") == -1)
                {
                    if (_headers["Content-Type"].IndexOf("application/x-www-form-urlencoded") == -1)
                    {
                        _headers["Content-Type"] += "; application/x-www-form-urlencoded";
                    }
                    if (_headers["Content-Type"].IndexOf("charset=") == -1)
                    {
                        _headers["Content-Type"] += "; charset=" + _charset;
                    }
                }
                data += "\r\n\r\n";
            }
            Uri uri = new Uri(_action);

            if (_cookieContainer != null)
            {
                CookieContainer cc = new CookieContainer();
                if (_headers["Cookie"] != null)
                {
                    cc.SetCookies(uri, _headers["Cookie"]);
                }
                Uri uri2 = new Uri(uri.AbsoluteUri.Insert(uri.Scheme.Length + 3, "httprequest."));
                CookieCollection cookies = _cookieContainer.GetCookies(uri);
                foreach (Cookie cookie in cookies)
                {
                    cc.SetCookies(uri, string.Concat(cookie));
                }
                cookies = _cookieContainer.GetCookies(uri2);
                foreach (Cookie cookie in cookies)
                {
                    cc.SetCookies(uri, string.Concat(cookie));
                }
                _headers["Cookie"] = cc.GetCookieHeader(uri);
                if (string.IsNullOrEmpty(_headers["Cookie"]))
                {
                    _headers.Remove("Cookie");
                }
            }
            _headers["Host"] = uri.Authority;
            string http = _method + " " + uri.PathAndQuery + " HTTP/1.1\r\n";

            foreach (string head in _headers)
            {
                http += head + ": " + _headers[head] + "\r\n";
            }
            http += "\r\n" + data;
            _head = http;
            if (_proxy != null)
            {
                RequestProxy pr = new RequestProxy();
                pr.Method     = _method;
                pr.Action     = _action;
                pr.Charset    = _charset;
                pr.Head       = _head;
                pr.Data       = data;
                pr.Connection = _proxyConnection;
                pr.Timeout    = _timeout;
                pr.MaximumAutomaticRedirections = _maximumAutomaticRedirections;
                ResponseProxy response = _proxy.Send(pr);
                Action    = response.RequestAction;
                _method   = response.RequestMethod;
                _headers  = Utils.ParseHttpRequestHeader(response.RequestHead);
                _response = new HttpResponse(this, response.ResponseHead);
                _response.SetStream(response.Response);
            }
            else
            {
                byte[] request = encode.GetBytes(http);
                if (_client == null || _remote == null || string.Compare(_remote.Authority, uri.Authority, true) != 0)
                {
                    _remote = uri;
                    this.Close();
                    _client = new TcpClient(uri.Host, uri.Port);
                }
                try
                {
                    _stream = getStream(uri);
                    _stream.Write(request, 0, request.Length);
                }
                catch
                {
                    this.Close();
                    _client = new TcpClient(uri.Host, uri.Port);
                    _stream = getStream(uri);
                    _stream.Write(request, 0, request.Length);
                }
                receive(_stream, redirections, uri, encode);
            }
        }