public static async Task<ToxResponse> sendRequest(Skynet host, ToxRequest req) { // if req is not send to local node if (host.tox.Id.ToString() != req.toToxId) { bool mResStatus = false; return await host.sendRequest(new ToxId(req.toToxId), req, out mResStatus); } string baseUrl = "http://localhost:" + host.httpPort + "/"; var request = (HttpWebRequest)WebRequest.Create(baseUrl + "api/" + req.url); request.Headers.Add("Uuid", req.uuid); request.Headers.Add("From-Node-Id", req.fromNodeId); request.Headers.Add("From-Tox-Id", req.fromToxId); request.Headers.Add("To-Node-Id", req.toNodeId); request.Headers.Add("To-Tox-Id", req.toToxId); request.Headers.Add("Skynet-Time", req.time + ""); request.Method = req.method.ToUpper(); request.ContentType = "application/json"; List<string> allowedMethods = new List<string> { "POST", "PUT", "PATCH" }; if (allowedMethods.Any(x => x == req.method.ToUpper())) { // only the above methods are allowed to add body data using (var streamWriter = new StreamWriter(request.GetRequestStream())) { streamWriter.Write(req.content); } } var response = await request.GetResponseAsync(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); return req.createResponse(responseString); }
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", } }; }
public NodeResponse Put(string nodeId, [FromBody] NodeId values) { IEnumerable <string> requestTime = new List <string>(); if (!Request.Headers.TryGetValues("Skynet-Time", out requestTime)) { return(new NodeResponse { statusCode = NodeResponseCode.InvalidRequest, description = "you need to add some http headers" }); } if (!Utils.Utils.isValidGuid(nodeId)) { return(new NodeResponse { statusCode = NodeResponseCode.InvalidRequest, description = "your node id is invalid", }); } Node targetNode = Node.AllLocalNodes.Where(x => x.selfNode.uuid == nodeId).DefaultIfEmpty(null).FirstOrDefault(); if (targetNode == null) { return(new NodeResponse { statusCode = NodeResponseCode.NotFound, description = "target node cannot be found on the client", }); } // check lock if (targetNode.nodeChangeLock.isLocked == true) { // target is locked, cannot be changed at this time return(new NodeResponse { statusCode = NodeResponseCode.TargetLocked, description = "target is locked", }); } else { long reqTime = long.Parse(requestTime.DefaultIfEmpty("0").FirstOrDefault()); if (reqTime < targetNode.brotherModifiedTime) { return(new NodeResponse { statusCode = NodeResponseCode.OutOfDate, description = "Your data is outofdate", }); } targetNode.parentModifiedTime = reqTime; targetNode.parent = values; Task.Run(async() => { // get parent node info, set grandparents Skynet host = Skynet.allInstance.Where(x => x.httpPort == Request.RequestUri.Port).FirstOrDefault(); bool status = false; ToxResponse getParentInfo = await host.sendRequest(new ToxId(values.toxid), new ToxRequest { url = "node/" + values.uuid, method = "get", content = null, fromNodeId = targetNode.selfNode.uuid, fromToxId = targetNode.selfNode.toxid, toNodeId = values.uuid, toToxId = values.toxid, time = Utils.Utils.UnixTimeNow(), uuid = Guid.NewGuid().ToString() }, out status); NodeResponse getParentInfoRes = JsonConvert.DeserializeObject <NodeResponse>(Encoding.UTF8.GetString(getParentInfo.content)); NodeInfo parentInfo = JsonConvert.DeserializeObject <NodeInfo>(getParentInfoRes.value); targetNode.grandParents = parentInfo.parent; targetNode.grandParentsModifiedTime = parentInfo.parentModifiedTime; await BoardCastChanges(targetNode); }); return(new NodeResponse { statusCode = NodeResponseCode.OK, description = "set parent success", value = JsonConvert.SerializeObject(values), time = reqTime, }); } }