Beispiel #1
0
        private static async Task parseCSharp(IOwinContext context, FormData formdata)
        {
            try
            {
                var file = formdata.mapFiles["file"];
                var code = System.Text.Encoding.UTF8.GetString(file);

                //编译
                List <string> codes = new List <string>();
                codes.Add(code);
                CompilerResults r = null;

                try
                {
                    r = gencode.GenCode(codes, true);
                    if (r.Errors.Count > 0)
                    {
                        MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                        json["tag"] = new MyJson.JsonNode_ValueNumber(-3);
                        json["msg"] = new MyJson.JsonNode_ValueString("compile fail.");
                        MyJson.JsonNode_Array errs = new MyJson.JsonNode_Array();
                        json["errors"] = errs;
                        for (var i = 0; i < r.Errors.Count; i++)
                        {
                            MyJson.JsonNode_Object errtag = new MyJson.JsonNode_Object();
                            errs.Add(errtag);
                            errtag.SetDictValue("msg", r.Errors[i].ErrorText);
                            errtag.SetDictValue("line", r.Errors[i].Line);
                            errtag.SetDictValue("col", r.Errors[i].Column);
                            errtag.SetDictValue("tag", r.Errors[i].IsWarning ? "警告" : "错误");
                        }
                        await context.Response.WriteAsync(json.ToString());

                        return;
                    }
                }
                catch (Exception err)
                {
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-2);
                    json["msg"] = new MyJson.JsonNode_ValueString("unknown fail on comp.");
                    json["err"] = new MyJson.JsonNode_ValueString(err.ToString());
                    await context.Response.WriteAsync(json.ToString());

                    return;
                }
                //conv
                try
                {
                    var st = System.IO.File.OpenRead(r.PathToAssembly);
                    using (st)
                    {
                        var logjson = new Log2Json();

                        var module = new ILModule();
                        module.LoadModule(st, null);

                        var conv  = new Neo.Compiler.MSIL.ModuleConverter(logjson);
                        var neomd = conv.Convert(module);
                        //var mm = neomd.mapMethods[neomd.mainMethod];


                        var bs = neomd.Build();
                        if (bs != null)
                        {
                            {
                                MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                                json["tag"] = new MyJson.JsonNode_ValueNumber(0);
                                StringBuilder sb     = new StringBuilder();
                                StringBuilder sb2    = new StringBuilder();
                                var           hash   = System.Security.Cryptography.SHA256.Create();
                                var           hashbs = hash.ComputeHash(bs);
                                foreach (var b in bs)
                                {
                                    sb.Append(b.ToString("X02"));
                                }
                                foreach (var b in hashbs)
                                {
                                    sb2.Append(b.ToString("X02"));
                                }
                                json["hex"]  = new MyJson.JsonNode_ValueString(sb.ToString());
                                json["hash"] = new MyJson.JsonNode_ValueString(sb2.ToString());

                                var funcsigns = new MyJson.JsonNode_Object();
                                json["funcsigns"] = funcsigns;
                                foreach (var function in neomd.mapMethods)
                                {
                                    var mm       = function.Value;
                                    var ps       = mm.name.Split(new char[] { ' ', '(' }, StringSplitOptions.RemoveEmptyEntries);
                                    var funcsign = new MyJson.JsonNode_Object();
                                    funcsigns[ps[1]] = funcsign;
                                    funcsign.SetDictValue("name", ps[1]);
                                    var rtype = ConvCSharpType(mm.returntype);
                                    funcsign.SetDictValue("returntype", rtype);
                                    MyJson.JsonNode_Array funcparams = new MyJson.JsonNode_Array();
                                    funcsign["params"] = funcparams;
                                    if (mm.paramtypes != null)
                                    {
                                        foreach (var v in mm.paramtypes)
                                        {
                                            var ptype = ConvCSharpType(v.type);
                                            var item  = new MyJson.JsonNode_Object();
                                            funcparams.Add(item);
                                            item.SetDictValue("name", v.name);
                                            item.SetDictValue("type", ptype);
                                        }
                                    }
                                }
                                await context.Response.WriteAsync(json.ToString());

                                return;
                            }
                        }
                        else
                        {
                            {
                                MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                                json["tag"]  = new MyJson.JsonNode_ValueNumber(-4);
                                json["msg"]  = new MyJson.JsonNode_ValueString("compile fail.");
                                json["info"] = logjson.array;
                                await context.Response.WriteAsync(json.ToString());

                                return;
                            }
                        }
                    }
                }
                catch (Exception err)
                {
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-2);
                    json["msg"] = new MyJson.JsonNode_ValueString("unknown fail on conv.");
                    json["err"] = new MyJson.JsonNode_ValueString(err.ToString());
                    await context.Response.WriteAsync(json.ToString());

                    return;
                }
            }
            catch
            {
                {
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-2);
                    json["msg"] = new MyJson.JsonNode_ValueString("parse fail.");
                    await context.Response.WriteAsync(json.ToString());

                    return;
                }
            }
        }
Beispiel #2
0
        private static async Task parseJAVA(IOwinContext context, FormData formdata)
        {
            try
            {
                string dictname  = null;
                string classname = null;
                var    file      = formdata.mapFiles["file"];
                var    code      = System.Text.Encoding.UTF8.GetString(file);

                //准备临时目录
                {
                    Random i   = new Random();
                    var    num = i.Next();

                    while (System.IO.Directory.Exists("tmp\\tmp_" + num.ToString("X08")))
                    {
                        num++;
                    }
                    dictname = "tmp\\tmp_" + num.ToString("X08");

                    var fc     = code.IndexOf("class");
                    int ibegin = -1;

                    for (int ib = fc + 6; ib < fc + 100; ib++)
                    {
                        if (ibegin < 0)
                        {
                            if (code[ib] == ' ')
                            {
                                continue;
                            }
                            else
                            {
                                ibegin = ib;
                            }
                        }
                        else
                        {
                            if (code[ib] == ' ' || code[ib] == '}')
                            {
                                classname = code.Substring(ibegin, ib - ibegin);
                                break;
                            }
                        }
                    }
                }
                System.IO.Directory.CreateDirectory(dictname);
                string filename = System.IO.Path.Combine(dictname, classname + ".java");
                System.IO.File.WriteAllText(filename, code);
                string jarfile = "AntShares.SmartContract.Framework.jar";
                System.IO.File.Copy(jarfile, System.IO.Path.Combine(dictname, jarfile));

                //编译
                try
                {
                    System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
                    info.FileName               = "cmd.exe";
                    info.UseShellExecute        = false;
                    info.RedirectStandardOutput = true;
                    info.RedirectStandardInput  = true;
                    info.RedirectStandardError  = true;
                    info.WorkingDirectory       = dictname;
                    var proc = System.Diagnostics.Process.Start(info);
                    proc.StandardInput.WriteLine("javac -cp " + jarfile + " " + classname + ".java");
                    proc.StandardInput.WriteLine("exit");


                    string        back      = proc.StandardError.ReadToEnd();
                    string        inerror   = "";
                    int           line      = -1;
                    string        tag       = "";
                    List <string> outline   = new List <string>();
                    List <int>    errorline = new List <int>();
                    List <string> errorTag  = new List <string>();
                    string[]      lines     = back.Split(new string[] { "\r\n" }, StringSplitOptions.None);
                    for (var i = 0; i < lines.Length; i++)
                    {
                        if (inerror == "")
                        {
                            var mm = lines[i].Split(':');
                            if (mm.Length > 3)
                            {
                                line     = int.Parse(mm[1]);
                                inerror += mm[3];
                                tag      = mm[2];
                            }
                        }
                        else
                        {
                            if (lines[i].IndexOf("^") >= 0)
                            {
                                outline.Add(inerror);
                                errorline.Add(line);
                                errorTag.Add(tag);
                                inerror = "";
                            }
                            else
                            {
                                inerror += "\n" + lines[i];
                            }
                        }
                    }

                    if (outline.Count == 0)
                    {
                        //succ
                    }
                    else
                    {
                        MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                        json["tag"] = new MyJson.JsonNode_ValueNumber(-3);
                        json["msg"] = new MyJson.JsonNode_ValueString("compile fail.");

                        MyJson.JsonNode_Array errs = new MyJson.JsonNode_Array();
                        json["errors"] = errs;
                        for (var i = 0; i < outline.Count; i++)
                        {
                            MyJson.JsonNode_Object errtag = new MyJson.JsonNode_Object();
                            errs.Add(errtag);
                            errtag.SetDictValue("msg", outline[i]);
                            errtag.SetDictValue("line", errorline[i]);
                            errtag.SetDictValue("tag", errorTag[i]);
                            //errtag.SetDictValue("col", r.Errors[i].Column);
                        }
                        await context.Response.WriteAsync(json.ToString());

                        return;
                    }
                }
                catch (Exception err)
                {
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-2);
                    json["msg"] = new MyJson.JsonNode_ValueString("unknown fail on comp.");
                    json["err"] = new MyJson.JsonNode_ValueString(err.ToString());
                    await context.Response.WriteAsync(json.ToString());

                    return;
                }

                //conv
                try
                {
                    JavaModule module = new JavaModule();
                    module.LoadJar(jarfile);
                    module.LoadClass(System.IO.Path.Combine(dictname, classname + ".class"));
                    var logjson = new Log2Json();
                    var conv    = new Neo.Compiler.JVM.ModuleConverter(logjson);
                    var neomd   = conv.Convert(module);
                    var mm      = neomd.mapMethods[neomd.mainMethod];

                    var bs = neomd.Build();
                    if (bs != null)
                    {
                        MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                        json["tag"] = new MyJson.JsonNode_ValueNumber(0);
                        StringBuilder sb     = new StringBuilder();
                        StringBuilder sb2    = new StringBuilder();
                        var           hash   = System.Security.Cryptography.SHA256.Create();
                        var           hashbs = hash.ComputeHash(bs);
                        foreach (var b in bs)
                        {
                            sb.Append(b.ToString("X02"));
                        }
                        foreach (var b in hashbs)
                        {
                            sb2.Append(b.ToString("X02"));
                        }
                        json["hex"]  = new MyJson.JsonNode_ValueString(sb.ToString());
                        json["hash"] = new MyJson.JsonNode_ValueString(sb2.ToString());

                        json["returntype"] = new MyJson.JsonNode_ValueString(mm.returntype);
                        MyJson.JsonNode_Array funcparams = new MyJson.JsonNode_Array();
                        json["params"] = funcparams;
                        if (mm.paramtypes != null)
                        {
                            foreach (var v in mm.paramtypes)
                            {
                                funcparams.Add(new MyJson.JsonNode_ValueString(v.type));
                            }
                        }

                        await context.Response.WriteAsync(json.ToString());

                        return;
                    }


                    else
                    {
                        {
                            MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                            json["tag"]  = new MyJson.JsonNode_ValueNumber(-4);
                            json["msg"]  = new MyJson.JsonNode_ValueString("compile fail.");
                            json["info"] = logjson.array;
                            await context.Response.WriteAsync(json.ToString());

                            return;
                        }
                    }
                }
                catch (Exception err)
                {
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-2);
                    json["msg"] = new MyJson.JsonNode_ValueString("unknown fail on conv.");
                    json["err"] = new MyJson.JsonNode_ValueString(err.ToString());
                    await context.Response.WriteAsync(json.ToString());

                    return;
                }
            }
            catch
            {
                {
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-2);
                    json["msg"] = new MyJson.JsonNode_ValueString("parse fail.");
                    await context.Response.WriteAsync(json.ToString());

                    return;
                }
            }
        }
Beispiel #3
0
        public void HandleRequest(IOwinContext context, string rootpath, string relativePath)
        {
            var api      = relativePath.ToLower();
            var formdata = FormData.FromRequest(context.Request);

            if (formdata == null)
            {
                context.Response.StatusCode  = 500;
                context.Response.ContentType = "text/plain";
                MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                json["msg"] = new MyJson.JsonNode_ValueString("formdata format error.");
                json["tag"] = new MyJson.JsonNode_ValueNumber(-1);
                context.Response.Write(json.ToString());
                return;
            }
            if (linking && relativePath != "comfirmpassword")
            {
                MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                json["tag"] = new MyJson.JsonNode_ValueNumber(-1);
                json["msg"] = new MyJson.JsonNode_ValueString("server is busy");
                context.Response.Write(json.ToString());
                return;
            }
            if (relativePath == "ver")
            {
                ver(context, formdata);
                return;
            }
            else if (relativePath == "state")
            {//查询状态
                var signer = NeoDun.Signer.Ins;
                MyJson.JsonNode_Object jsonr = new MyJson.JsonNode_Object();
                jsonr["tag"] = new MyJson.JsonNode_ValueNumber(0);
                string dc = signer.CheckDevice();
                jsonr["count"] = new MyJson.JsonNode_ValueString(dc);

                if (!string.IsNullOrEmpty(dc))
                {
                    jsonr["type"] = new MyJson.JsonNode_ValueString(signer.CheckDeviceTag());
                }
                else
                {
                    jsonr["type"] = new MyJson.JsonNode_ValueString("_null_");
                }
                context.Response.Write(jsonr.ToString());
            }
            else if (relativePath == "listaddress")
            {
                var signer = NeoDun.Signer.Ins;
                MyJson.JsonNode_Object jsonr = new MyJson.JsonNode_Object();
                jsonr["tag"] = new MyJson.JsonNode_ValueNumber(0);
                MyJson.JsonNode_Array adds = new MyJson.JsonNode_Array();
                jsonr["addresses"] = adds;
                string dc = signer.CheckDevice();
                if (!string.IsNullOrEmpty(dc))
                {
                    foreach (var add in signer.addressPool.addresses)
                    {
                        MyJson.JsonNode_Object item = new MyJson.JsonNode_Object();
                        adds.Add(item);
                        item["type"]    = new MyJson.JsonNode_ValueString(add.type.ToString());
                        item["address"] = new MyJson.JsonNode_ValueString(add.AddressText);
                    }
                }
                context.Response.Write(jsonr.ToString());
            }
            else if (relativePath == "sign")
            {
                linking = true;
                sign(context, formdata);
                return;
            }
            else if (relativePath == "addaddress")
            {
                linking = true;
                addAddress(context, formdata);
            }
            else if (relativePath == "deladdress")
            {
                linking = true;
                delAddress(context, formdata);
                return;
            }
            else
            {
                MyJson.JsonNode_Object jsonr = new MyJson.JsonNode_Object();
                jsonr["tag"] = new MyJson.JsonNode_ValueNumber(-1000);
                jsonr["msg"] = new MyJson.JsonNode_ValueString("unknown cmd.");
            }

            return;
        }
Beispiel #4
0
        public async Task HandleRequest(IOwinContext context, string rootpath, string relativePath)
        {
            var api      = relativePath.ToLower();
            var formdata = await FormData.FromRequest(context.Request);

            if (formdata == null)
            {
                context.Response.StatusCode  = 500;
                context.Response.ContentType = "text/plain";
                MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                json["msg"] = new MyJson.JsonNode_ValueString("formdata format error.");
                json["tag"] = new MyJson.JsonNode_ValueNumber(-1);
                await context.Response.WriteAsync(json.ToString());

                return;
            }

            if (relativePath == "ver")
            {
                await ver(context, formdata);

                return;
            }
            else if (relativePath == "filemap_list")
            {
                await filemap_list(context, formdata);

                return;
            }
            else if (relativePath == "filemap_add")
            {
                await filemap_add(context, formdata);

                return;
            }
            else if (relativePath == "filemap_remove")
            {
                await filemap_remove(context, formdata);

                return;
            }
            else if (relativePath == "file_list")
            {
                await file_list(context, formdata);

                return;
            }
            else if (relativePath == "file_save")
            {
                await file_save(context, formdata);

                return;
            }
            else if (relativePath == "file_remove")
            {
                await file_remove(context, formdata);

                return;
            }
            else if (relativePath == "file_cmd")
            {
                await file_cmd(context, formdata);

                return;
            }
            else
            {
                MyJson.JsonNode_Object jsonr = new MyJson.JsonNode_Object();
                jsonr["tag"] = new MyJson.JsonNode_ValueNumber(-1000);
                jsonr["msg"] = new MyJson.JsonNode_ValueString("unknown cmd.");
                await context.Response.WriteAsync(jsonr.ToString());
            }
            return;
        }
Beispiel #5
0
        public static async Task <FormData> FromRequest(IOwinRequest request)
        {
            try

            {
                FormData data = new FormData();
                foreach (var kv in request.Query)
                {
                    data.mapParams[kv.Key] = kv.Value[0];
                }
                if (request.Method.ToUpper() == "POST")
                {
                    if (request.ContentType == null)
                    {
                        return(data);
                    }
                    else if (request.ContentType == "application/x-www-form-urlencoded")
                    {
                        byte[] allfile = null;
                        int    seek    = 0;
                        var    clen    = request.Headers["Content-Length"];
                        if (clen != null)
                        {
                            int leng = int.Parse(clen);
                            allfile = new byte[leng];

                            while (request.Body.CanRead)
                            {
                                int read = await request.Body.ReadAsync(allfile, seek, leng - seek);

                                seek += read;
                                if (read == 0)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            allfile = new byte[4 * 1024 * 1024];

                            while (request.Body.CanRead)
                            {
                                int read = await request.Body.ReadAsync(allfile, seek, 1024);

                                seek += read;
                                if (read == 0)
                                {
                                    break;
                                }
                            }
                        }


                        string text  = System.Text.Encoding.UTF8.GetString(allfile, 0, seek);
                        var    infos = text.Split(new char[] { '=', '&' });
                        for (var i = 0; i < infos.Length / 2; i++)
                        {
                            data.mapParams[infos[i * 2]] = Uri.UnescapeDataString(infos[i * 2 + 1]);
                        }
                    }
                    else if (request.ContentType.IndexOf("multipart/form-data;") == 0)
                    {
                        byte[] allfile = null;
                        int    seek    = 0;
                        var    clen    = request.Headers["Content-Length"];
                        if (clen != null)
                        {
                            int leng = int.Parse(clen);
                            allfile = new byte[leng];

                            while (request.Body.CanRead)
                            {
                                int read = await request.Body.ReadAsync(allfile, seek, leng - seek);

                                seek += read;
                                if (read == 0)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            allfile = new byte[4 * 1024 * 1024];

                            while (request.Body.CanRead)
                            {
                                int read = await request.Body.ReadAsync(allfile, seek, 1024);

                                seek += read;
                                if (read == 0)
                                {
                                    break;
                                }
                            }
                        }


                        var iSplitTag = request.ContentType.IndexOf("=") + 1;
                        var sSplitTag = "--" + request.ContentType.Substring(iSplitTag);
                        var bSplitTag = System.Text.Encoding.ASCII.GetBytes(sSplitTag);

                        int iTag = ByteIndexOf(allfile, seek, bSplitTag, 0);
                        if (iTag < 0)
                        {
                            string s = System.Text.Encoding.ASCII.GetString(allfile, 0, seek);
                        }
                        else
                        {
                            while (iTag >= 0)
                            {
                                int iTagNext = ByteIndexOf(allfile, seek, bSplitTag, iTag + 1);
                                if (iTagNext < 0)
                                {
                                    break;
                                }
                                var           bs         = System.Text.Encoding.ASCII.GetBytes("\r\n\r\n");
                                int           iStart     = iTag + bSplitTag.Length + 2;
                                int           iDataStart = ByteIndexOf(allfile, seek, bs, iStart) + 4;
                                string        s          = System.Text.Encoding.ASCII.GetString(allfile, iStart, iDataStart - iStart);
                                List <string> infos      = new List <string>(s.Split(new string[] { "; ", ": ", "\r\n", "=" }, StringSplitOptions.None));
                                var           i          = infos.IndexOf("name");
                                var           name       = infos[i + 1].Substring(1);
                                name = name.Substring(0, name.Length - 1);

                                byte[] ddata = new byte[iTagNext - iDataStart - 2];
                                Array.Copy(allfile, iDataStart, ddata, 0, ddata.Length);
                                if (infos.Contains("application/octet-stream"))
                                {
                                    data.mapFiles[name] = ddata;
                                }
                                else
                                {
                                    string txtData = System.Text.Encoding.UTF8.GetString(ddata);

                                    data.mapParams[name] = Uri.UnescapeDataString(txtData);
                                }
                                iTag = iTagNext;
                            }
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
                return(data);
            }
            catch
            {
                return(null);
            }
        }
Beispiel #6
0
        public async void HandleRequest(IOwinContext context, string rootpath, string relativePath)
        {
            try
            {
                var api      = relativePath.ToLower();
                var formdata = FormData.FromRequest(context.Request);
                if (formdata == null)
                {
                    context.Response.StatusCode  = 500;
                    context.Response.ContentType = "text/plain";
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["msg"] = new MyJson.JsonNode_ValueString("formdata format error.");
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-1);
                    context.Response.Write(json.ToString());
                    return;
                }
                else if (relativePath == "state")
                {//查询状态
                    var signer = NeoDun.Signer.Ins;
                    MyJson.JsonNode_Object jsonr = new MyJson.JsonNode_Object();
                    jsonr["tag"] = new MyJson.JsonNode_ValueNumber(0);
                    string dc = signer.CheckDevice();
                    jsonr["count"] = new MyJson.JsonNode_ValueString(dc);

                    if (!string.IsNullOrEmpty(dc))
                    {
                        jsonr["type"] = new MyJson.JsonNode_ValueString(signer.CheckDeviceTag());
                    }
                    else
                    {
                        jsonr["type"] = new MyJson.JsonNode_ValueString("_null_");
                    }
                    context.Response.Write(jsonr.ToString());
                }
                else if (relativePath == "listaddress")
                {
                    var signer = NeoDun.Signer.Ins;
                    MyJson.JsonNode_Object jsonr = new MyJson.JsonNode_Object();
                    jsonr["tag"] = new MyJson.JsonNode_ValueNumber(0);
                    MyJson.JsonNode_Array adds = new MyJson.JsonNode_Array();
                    jsonr["addresses"] = adds;
                    string dc = signer.CheckDevice();
                    if (!string.IsNullOrEmpty(dc))
                    {
                        foreach (var add in signer.addressPool.addresses)
                        {
                            MyJson.JsonNode_Object item = new MyJson.JsonNode_Object();
                            adds.Add(item);
                            item["type"]    = new MyJson.JsonNode_ValueString(add.type.ToString());
                            item["address"] = new MyJson.JsonNode_ValueString(add.AddressText);
                            item["name"]    = new MyJson.JsonNode_ValueString(add.name);
                        }
                    }
                    context.Response.Write(jsonr.ToString());
                }
                else if (relativePath == "sign")
                {
                    if (doing)
                    {
                        return;
                    }
                    if (formdata.mapParams.ContainsKey("address") == false || formdata.mapParams.ContainsKey("data") == false)
                    {
                        context.Response.Write("need param,address & data");
                        return;
                    }
                    var address = formdata.mapParams["address"];
                    var data    = formdata.mapParams["data"];

                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();

                    doing = true;

                    sign(json, address, data);

                    while (true)
                    {
                        if (json.Keys.Count > 0)
                        {
                            doing = false;
                            //读出来,拼为http响应,发回去
                            context.Response.Write(json.ToString());
                            break;
                        }
                    }
                }
                else
                {
                    MyJson.JsonNode_Object jsonr = new MyJson.JsonNode_Object();
                    jsonr["tag"] = new MyJson.JsonNode_ValueNumber(-1000);
                    jsonr["msg"] = new MyJson.JsonNode_ValueString("unknown cmd.");
                }
                return;
            }
            catch (Exception e)
            {
                context.Response.Write(e.ToString());
            }

            async void sign(MyJson.JsonNode_Object json, string address, string data)
            {
                driver_win.helper.Result _result = await ManagerControl.Ins.ToDo(driver_win.helper.EnumControl.SignData, data, address);

                var result    = (MyJson.JsonNode_Object)_result.data;
                var enumError = _result.errorCode;

                json["msg"] = new MyJson.JsonNode_ValueString(((UInt16)enumError).ToString("x4"));
                if (result != null)
                {
                    json["signdata"] = result.ContainsKey("signdata") ? result["signdata"] : new MyJson.JsonNode_ValueString("");
                    json["pubkey"]   = result.ContainsKey("pubkey") ? result["pubkey"] : new MyJson.JsonNode_ValueString("");
                }
            }
        }
Beispiel #7
0
        private static async Task parseCSharp(IOwinContext context, FormData formdata)
        {
            try
            {
                var file = formdata.mapFiles["file"];
                var code = System.Text.Encoding.UTF8.GetString(file);

                //编译
                List <string> codes = new List <string>();
                codes.Add(code);
                CompilerResults r = null;

                try
                {
                    r = gencode.GenCode(codes, true);
                    if (r.Errors.Count > 0)
                    {
                        MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                        json["tag"] = new MyJson.JsonNode_ValueNumber(-3);
                        json["msg"] = new MyJson.JsonNode_ValueString("compile fail.");
                        MyJson.JsonNode_Array errs = new MyJson.JsonNode_Array();
                        json["errors"] = errs;
                        for (var i = 0; i < r.Errors.Count; i++)
                        {
                            MyJson.JsonNode_Object errtag = new MyJson.JsonNode_Object();
                            errs.Add(errtag);
                            errtag.SetDictValue("msg", r.Errors[i].ErrorText);
                            errtag.SetDictValue("line", r.Errors[i].Line);
                            errtag.SetDictValue("col", r.Errors[i].Column);
                            errtag.SetDictValue("tag", r.Errors[i].IsWarning ? "警告" : "错误");
                        }
                        await context.Response.WriteAsync(json.ToString());

                        return;
                    }
                }
                catch (Exception err)
                {
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-2);
                    json["msg"] = new MyJson.JsonNode_ValueString("unknown fail on comp.");
                    json["err"] = new MyJson.JsonNode_ValueString(err.ToString());
                    await context.Response.WriteAsync(json.ToString());

                    return;
                }
                //conv
                try
                {
                    var st = System.IO.File.OpenRead(r.PathToAssembly);
                    using (st)
                    {
                        var logjson = new Log2Json();
                        var bs      = AntShares.Compiler.MSIL.Converter.Convert(st, logjson);
                        if (bs != null)
                        {
                            {
                                MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                                json["tag"] = new MyJson.JsonNode_ValueNumber(0);
                                StringBuilder sb     = new StringBuilder();
                                StringBuilder sb2    = new StringBuilder();
                                var           hash   = System.Security.Cryptography.SHA256.Create();
                                var           hashbs = hash.ComputeHash(bs);
                                foreach (var b in bs)
                                {
                                    sb.Append(b.ToString("X02"));
                                }
                                foreach (var b in hashbs)
                                {
                                    sb2.Append(b.ToString("X02"));
                                }
                                json["hex"]  = new MyJson.JsonNode_ValueString(sb.ToString());
                                json["hash"] = new MyJson.JsonNode_ValueString(sb2.ToString());
                                await context.Response.WriteAsync(json.ToString());

                                return;
                            }
                        }
                        else
                        {
                            {
                                MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                                json["tag"]  = new MyJson.JsonNode_ValueNumber(-4);
                                json["msg"]  = new MyJson.JsonNode_ValueString("compile fail.");
                                json["info"] = logjson.array;
                                await context.Response.WriteAsync(json.ToString());

                                return;
                            }
                        }
                    }
                }
                catch (Exception err)
                {
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-2);
                    json["msg"] = new MyJson.JsonNode_ValueString("unknown fail on conv.");
                    json["err"] = new MyJson.JsonNode_ValueString(err.ToString());
                    await context.Response.WriteAsync(json.ToString());

                    return;
                }
            }
            catch
            {
                {
                    MyJson.JsonNode_Object json = new MyJson.JsonNode_Object();
                    json["tag"] = new MyJson.JsonNode_ValueNumber(-2);
                    json["msg"] = new MyJson.JsonNode_ValueString("parse fail.");
                    await context.Response.WriteAsync(json.ToString());

                    return;
                }
            }
        }