Esempio n. 1
0
        private void GetAccountLocationIfSpecified(AccountEntity pAccount, RESTRequestData pReq)
        {
            try
            {
                // The 'location' information is sent only when it changes
                JObject requestBodyJSON = pReq.RequestBodyJSON();
                if (requestBodyJSON.ContainsKey("location"))
                {
                    bodyUserHeartbeatLocation requestData = pReq.RequestBodyObject <bodyUserHeartbeatLocation>();

                    bodyHeartbeatLocationInfo loc = requestData.location;
                    pAccount.Location = new LocationInfo()
                    {
                        Connected      = loc.connected,
                        Path           = loc.path,
                        PlaceID        = loc.place_id,
                        DomainID       = loc.domain_id,
                        NetworkAddress = loc.network_address,
                        NetworkPort    = Int32.Parse(loc.network_port ?? "9400"),
                        NodeID         = loc.node_id,
                        Availability   = Enum.Parse <Discoverability>(loc.availability ?? "none", false)
                    };
                    pAccount.Updated();
                }
            }
            catch (Exception e)
            {
                Context.Log.Error("{0} GetAccountLocationIfSpecified: Exception parsing body of message. AccountID={1}. {2}",
                                  _logHeader, pAccount.AccountID, e);
                Context.Log.Error("{0} GetAccountLocationIfSpecified:     Body = {1}", _logHeader, pReq.RequestBody);
            }
        }
Esempio n. 2
0
 public RESTReplyData admin_post_accounts(RESTRequestData pReq, List <string> pArgs)
 {
     return(APITargetedAccountOperation(pReq, pArgs, (pRespBody, pSrcAcct, pTargetAcct) =>
     {
         try
         {
             JObject body = pReq.RequestBodyJSON();
             if (body.ContainsKey("accounts"))
             {
                 JObject acctInfo = (JObject)body["accounts"];
                 Tools.SetIfSpecified <string>(acctInfo, "username", ref pTargetAcct.Username);
                 Tools.SetIfSpecified <string>(acctInfo, "email", ref pTargetAcct.Email);
                 Tools.SetIfSpecified <string>(acctInfo, "public_key", ref pTargetAcct.Public_Key);
                 if (acctInfo.ContainsKey("images"))
                 {
                     JObject imageInfo = (JObject)acctInfo["images"];
                     if (pTargetAcct.Images == null)
                     {
                         pTargetAcct.Images = new UserImages();
                     }
                     Tools.SetIfSpecified <string>(imageInfo, "hero", ref pTargetAcct.Images.Hero);
                     Tools.SetIfSpecified <string>(imageInfo, "thumbnail", ref pTargetAcct.Images.Thumbnail);
                     Tools.SetIfSpecified <string>(imageInfo, "tiny", ref pTargetAcct.Images.Tiny);
                 }
             }
         }
         catch (Exception e)
         {
             Context.Log.Error("{0} POST /api/v1/account/%: exception parsing body '{1}' from {2} account {3}",
                               _logHeader, pReq.RequestBody, pReq.SenderKey, pSrcAcct.Username);
             pRespBody.RespondFailure("Exception parsing message body", e.ToString());
             pRespBody.ErrorData("error", "parse body failure");
         }
     }));
 }
Esempio n. 3
0
        public RESTReplyData domain_heartbeat(RESTRequestData pReq, List <string> pArgs)
        {
            RESTReplyData replyData = new RESTReplyData();  // The HTTP response info
            ResponseBody  respBody  = new ResponseBody();   // The request's "data" response info

            SessionEntity aSession = Sessions.Instance.UpdateSession(pReq.SenderKey);

            string domainID = pArgs.Count == 1 ? pArgs[0] : null;

            if (Domains.Instance.TryGetDomainWithID(domainID, out DomainEntity aDomain))
            {
                try
                {
                    // Since the body has a lot of optional fields, we need to pick
                    //    through what's sent so we only set what was sent.
                    JObject requestData = pReq.RequestBodyJSON();
                    JObject domainStuff = (JObject)requestData["domain"];
                    string  apiKey      = (string)domainStuff["api_key"];

                    // Context.Log.Debug("{0} domain_heartbeat. Received {1}", _logHeader, pReq.RequestBody);
                    if (VerifyDomainAccess(aDomain, pReq, apiKey, out string oFailureReason))
                    {
                        Tools.SetIfSpecified <string>(domainStuff, "version", ref aDomain.Version);
                        Tools.SetIfSpecified <string>(domainStuff, "protocol", ref aDomain.Protocol);
                        Tools.SetIfSpecified <string>(domainStuff, "network_address", ref aDomain.NetworkAddr);
                        Tools.SetIfSpecified <string>(domainStuff, "automatic_networking", ref aDomain.NetworkingMode);
                        Tools.SetIfSpecified <bool>(domainStuff, "restricted", ref aDomain.Restricted);

                        Tools.SetIfSpecified <int>(domainStuff, "capacity", ref aDomain.Capacity);
                        Tools.SetIfSpecified <string>(domainStuff, "description", ref aDomain.Description);
                        Tools.SetIfSpecified <string>(domainStuff, "maturity", ref aDomain.Maturity);
                        Tools.SetIfSpecified <string>(domainStuff, "restriction", ref aDomain.Restriction);
                        JArray hosts = (JArray)domainStuff["hosts"];
                        if (hosts != null)
                        {
                            aDomain.Hosts = domainStuff["hosts"].ToObject <string[]>();
                        }
                        JArray tags = (JArray)domainStuff["tags"];
                        if (tags != null)
                        {
                            aDomain.Tags = domainStuff["tags"].ToObject <string[]>();
                        }

                        JObject heartbeat = (JObject)domainStuff["heartbeat"];
                        if (heartbeat != null)
                        {
                            Tools.SetIfSpecified <int>(heartbeat, "num_users", ref aDomain.NumUsers);
                            Tools.SetIfSpecified <int>(heartbeat, "num_anon_users", ref aDomain.AnonUsers);
                            aDomain.TotalUsers = aDomain.NumUsers + aDomain.AnonUsers;
                            // TODO: what to do with user_hostnames
                        }

                        aDomain.TimeOfLastHeartbeat = DateTime.UtcNow;
                        aDomain.LastSenderKey       = pReq.SenderKey;

                        aDomain.Updated();
                    }
                    else
                    {
                        Context.Log.Error("{0} domain_heartbeat with bad authorization. domainID={1}",
                                          _logHeader, domainID);
                        respBody.RespondFailure(oFailureReason);
                        replyData.Status = (int)HttpStatusCode.Unauthorized;
                    }
                }
                catch (Exception e)
                {
                    Context.Log.Error("{0} domain_heartbeat: Exception processing body: {1}", _logHeader, e.ToString());
                    respBody.RespondFailure("exception processing body", e.ToString());
                    replyData.Status = (int)HttpStatusCode.BadRequest;
                }
            }
            else
            {
                Context.Log.Error("{0} domain_heartbeat: unknown domain. Returning 404", _logHeader);
                respBody.RespondFailure("no such domain");
                replyData.Status = (int)HttpStatusCode.NotFound; // this will trigger a new temporary domain name
            }

            replyData.SetBody(respBody, pReq);
            return(replyData);
        }