Пример #1
0
        protected static void makeGenericErrorReponse(Common.JsonValue resp, string what, int errorCode = -32001)
        {
            JsonValue error = new JsonValue(JsonValue.OBJECT);

            JsonValue code = new JsonValue();

            code = (long)errorCode;

            string msg;

            if (what != null)
            {
                msg = what;
            }
            else
            {
                msg = "Unknown application error";
            }

            JsonValue message = new JsonValue();

            message = msg;

            error.insert("code", code);
            error.insert("message", message);

            resp.insert.functorMethod("error", error);
        }
        public override bool beginArray(ulong size, Common.StringView name)
        {
            JsonValue val = new JsonValue(JsonValue.ARRAY);
            JsonValue res = chain[chain.Count - 1].insert.functorMethod((string)name, val);

            chain.Add(res);
            return(true);
        }
Пример #3
0
        private JsonValue getValue(Common.StringView name)
        {
            JsonValue val = chain[chain.Count - 1].functorMethod;

            if (val.isArray())
            {
                return(val[idxs[idxs.Count - 1]++]);
            }

            string strName = name;

            return(val.contains(strName) ? &val(strName) : null);
        }
        public override bool BeginObject(Common.StringView name)
        {
            JsonValue parent = chain[chain.Count - 1].functorMethod;
            JsonValue obj    = new JsonValue(JsonValue.OBJECT);

            if (parent.isObject())
            {
                chain.Add(parent.insert((string)name, obj));
            }
            else
            {
                chain.Add(parent.pushBack(obj));
            }

            return(true);
        }
Пример #5
0
        public override bool BeginArray(ref ulong size, Common.StringView name)
        {
            JsonValue parent  = chain[chain.Count - 1].functorMethod;
            string    strName = name;

            if (parent.contains(strName))
            {
                JsonValue arr = parent(strName);
                size = arr.size();
                chain.Add(arr);
                idxs.Add(0);
                return(true);
            }

            size = 0;
            return(false);
        }
Пример #6
0
        protected static void makeInvalidPasswordResponse(Common.JsonValue resp)
        {
            JsonValue error = new JsonValue(JsonValue.OBJECT);

            JsonValue code = new JsonValue();

            code = (long)CryptoNote.JsonRpc.errInvalidPassword;

            JsonValue message = new JsonValue();

            message = "Invalid or no rpc password";

            error.insert("code", code);
            error.insert("message", message);

            resp.insert.functorMethod("error", error);
        }
Пример #7
0
        protected static void makeMethodNotFoundResponse(Common.JsonValue resp)
        {
            JsonValue error = new JsonValue(JsonValue.OBJECT);

            JsonValue code = new JsonValue();

            code = (long)CryptoNote.JsonRpc.errMethodNotFound; //ambigous declaration of JsonValue::operator= (between int and JsonValue)

            JsonValue message = new JsonValue();

            message = "Method not found";

            error.insert("code", code);
            error.insert("message", message);

            resp.insert.functorMethod("error", error);
        }
Пример #8
0
        protected static void makeJsonParsingErrorResponse(ref Common.JsonValue resp)
        {
            resp = new JsonValue(JsonValue.OBJECT);
            resp.insert.functorMethod("jsonrpc", "2.0");
            resp.insert.functorMethod("id", null);

            JsonValue error = new JsonValue(JsonValue.OBJECT);
            JsonValue code  = new JsonValue();

            code = (long)CryptoNote.JsonRpc.errParseError; //ambigous declaration of JsonValue::operator= (between int and JsonValue)

            JsonValue message = "Parse error";

            error.insert("code", code);
            error.insert("message", message);

            resp.insert.functorMethod("error", error);
        }
Пример #9
0
        public override bool BeginObject(Common.StringView name)
        {
            JsonValue parent = chain[chain.Count - 1].functorMethod;

            if (parent.isArray())
            {
                JsonValue v = parent[idxs[idxs.Count - 1]++];
                chain.Add(v);
                return(true);
            }

            if (parent.contains((string)name))
            {
                JsonValue v = parent((string)name);
                chain.Add(v);
                return(true);
            }

            return(false);
        }
Пример #10
0
        protected static void makeErrorResponse(std::error_code ec, Common.JsonValue resp)
        {
            JsonValue error = new JsonValue(JsonValue.OBJECT);

            JsonValue code = new JsonValue();

            code = (long)CryptoNote.JsonRpc.errParseError; //Application specific error code

            JsonValue message = new JsonValue();

            message = ec.message();

            JsonValue data    = new JsonValue(JsonValue.OBJECT);
            JsonValue appCode = new JsonValue();

            appCode = (long)ec.value();
            data.insert("application_code", appCode);

            error.insert("code", code);
            error.insert("message", message);
            error.insert("data", data);

            resp.insert.functorMethod("error", error);
        }
//C++ TO C# CONVERTER TODO TASK: C# has no equivalent to ' = delete':
//  PaymentServiceJsonRpcServer(const PaymentServiceJsonRpcServer&) = delete;

        protected override void processJsonRpcRequest(Common.JsonValue req, Common.JsonValue resp)
        {
            try
            {
                prepareJsonResponse(req.functorMethod, resp.functorMethod);

                if (!config.serviceConfig.legacySecurity)
                {
                    string clientPassword;
                    if (!req.contains("password"))
                    {
                        makeInvalidPasswordResponse(resp.functorMethod);
                        return;
                    }
                    if (!req.functorMethod("password").isString())
                    {
                        makeInvalidPasswordResponse(resp.functorMethod);
                        return;
                    }
                    clientPassword = req.functorMethod("password").getString();

                    List <byte> rawData        = new List <byte>(clientPassword.GetEnumerator(), clientPassword.end());
                    Crypto.Hash hashedPassword = new Crypto.Hash();
                    Crypto.GlobalMembers.cn_slow_hash_v0(rawData.data(), rawData.Count, hashedPassword);
                    if (hashedPassword != config.rpcSecret)
                    {
                        makeInvalidPasswordResponse(resp.functorMethod);
                        return;
                    }
                }

                if (!req.contains("method"))
                {
                    logger.functorMethod(Logging.Level.WARNING) << "Field \"method\" is not found in json request: " << req.functorMethod;
                    makeGenericErrorReponse(resp.functorMethod, "Invalid Request", -3600);
                    return;
                }

                if (!req.functorMethod("method").isString())
                {
                    logger.functorMethod(Logging.Level.WARNING) << "Field \"method\" is not a string type: " << req.functorMethod;
                    makeGenericErrorReponse(resp.functorMethod, "Invalid Request", -3600);
                    return;
                }

                string method = req.functorMethod("method").getString();

                var it = handlers.find(method);
                if (it == handlers.end())
                {
                    logger.functorMethod(Logging.Level.WARNING) << "Requested method not found: " << method;
                    makeMethodNotFoundResponse(resp.functorMethod);
                    return;
                }

                logger.functorMethod(Logging.Level.DEBUGGING) << method << " request came";

                Common.JsonValue @params = new Common.JsonValue(Common.JsonValue.OBJECT);
                if (req.contains("params"))
                {
                    @params = req.functorMethod("params");
                }

//C++ TO C# CONVERTER TODO TASK: Iterators are only converted within the context of 'while' and 'for' loops:
                it.second(@params.functorMethod, resp.functorMethod);
            }
            catch (System.Exception e)
            {
                logger.functorMethod(Logging.Level.WARNING) << "Error occurred while processing JsonRpc request: " << e.Message;
                makeGenericErrorReponse(resp.functorMethod, e.Message);
            }
        }
Пример #12
0
        public void Configure(JsonValue val)
        {
            //TODO: Redo this

            //std::unique_lock<object> @lock = new std::unique_lock<object>(reconfigureLock);
            //loggers.Clear();
            //base.loggers.Clear();
            //Level globalLevel;
            //if (val.contains("globalLevel"))
            //{
            //    var levelVal = val("globalLevel");
            //    if (levelVal.isInteger())
            //    {
            //        globalLevel = (Level)levelVal.getInteger();
            //    }
            //    else
            //    {
            //        throw new System.Exception("parameter globalLevel has wrong type");
            //    }
            //}
            //else
            //{
            //    globalLevel = Level.TRACE;
            //}
            //List<string> globalDisabledCategories = new List<string>();

            //if (val.contains("globalDisabledCategories"))
            //{
            //    var globalDisabledCategoriesList = val("globalDisabledCategories");
            //    if (globalDisabledCategoriesList.isArray())
            //    {
            //        uint countOfCategories = globalDisabledCategoriesList.size();
            //        for (uint i = 0; i < countOfCategories; ++i)
            //        {
            //            var categoryVal = globalDisabledCategoriesList[i];
            //            if (categoryVal.isString())
            //            {
            //                globalDisabledCategories.Add(categoryVal.getString());
            //            }
            //        }
            //    }
            //    else
            //    {
            //        throw new System.Exception("parameter globalDisabledCategories has wrong type");
            //    }
            //}

            //if (val.contains("loggers"))
            //{
            //    var loggersList = val("loggers");
            //    if (loggersList.isArray())
            //    {
            //        uint countOfLoggers = loggersList.size();
            //        for (uint i = 0; i < countOfLoggers; ++i)
            //        {
            //            var loggerConfiguration = loggersList[i];
            //            if (!loggerConfiguration.isObject())
            //            {
            //                throw new System.Exception("loggers element must be objects");
            //            }

            //            Level level = Level.INFO;
            //            if (loggerConfiguration.contains("level"))
            //            {
            //                level = (Level)(loggerConfiguration("level").getInteger());
            //            }

            //            string type = loggerConfiguration("type").getString();
            //            std::unique_ptr<Logging.CommonLogger> logger = new std::unique_ptr<Logging.CommonLogger>();

            //            if (type == "console")
            //            {
            //                logger.reset(new ConsoleLogger(level));
            //            }
            //            else if (type == "file")
            //            {
            //                string filename = loggerConfiguration("filename").getString();
            //                var fileLogger = new FileLogger(level);
            //                fileLogger.init(filename);
            //                logger.reset(fileLogger);
            //            }
            //            else
            //            {
            //                throw new System.Exception("Unknown logger type: " + type);
            //            }

            //            if (loggerConfiguration.contains("pattern"))
            //            {
            //                logger.setPattern(loggerConfiguration("pattern").getString());
            //            }

            //            List<string> disabledCategories = new List<string>();
            //            if (loggerConfiguration.contains("disabledCategories"))
            //            {
            //                var disabledCategoriesVal = loggerConfiguration("disabledCategories");
            //                uint countOfCategories = disabledCategoriesVal.size();
            //                for (uint i = 0; i < countOfCategories; ++i)
            //                {
            //                    var categoryVal = disabledCategoriesVal[i];
            //                    if (categoryVal.isString())
            //                    {
            //                        logger.disableCategory(categoryVal.getString());
            //                    }
            //                }
            //            }

            //            loggers.emplace_back(std::move(logger));
            //            addLogger(*loggers[loggers.Count - 1]);
            //        }
            //    }
            //    else
            //    {
            //        throw new System.Exception("loggers parameter has wrong type");
            //    }
            //}
            //else
            //{
            //    throw new System.Exception("loggers parameter missing");
            //}
            //SetMaxLevel(globalLevel);
            //foreach (var category in globalDisabledCategories)
            //{
            //    DisableCategory(category);
            //}
        }