public void UpdateHistory(string statement) { try { if (!File.Exists(HistoryFilePath)) { File.Create(HistoryFilePath).Dispose(); } if (!SecuredCommands.Any(statement.Contains)) { List <string> history = ReadLine.GetHistory(); if (history.LastOrDefault() != statement) { ReadLine.AddHistory(statement); _historyCloned.Insert(0, statement); } } else { ReadLine.AddHistory(_removedString); _historyCloned.Insert(0, statement); } File.WriteAllLines(HistoryFilePath, _historyCloned.Distinct().Reverse().ToArray()); } catch (Exception e) { if (writeFailNotReported) { writeFailNotReported = false; _cliConsole.WriteErrorLine($"Could not write cmd history to {HistoryFilePath} {e.Message}"); } } }
public async Task <JsValue> PostJint(string method, params object[] parameters) { JsValue returnValue = JsValue.Null; try { if (_currentClient == null) { _cliConsole.WriteErrorLine("[INTERNAL ERROR] JSON RPC client not set."); } else { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); object?result = await _currentClient.Post <object>(method, parameters); stopwatch.Stop(); decimal totalMicroseconds = stopwatch.ElapsedTicks * (1_000_000m / Stopwatch.Frequency); Colorful.Console.WriteLine($"Request complete in {totalMicroseconds}μs"); string?resultString = result?.ToString(); if (resultString == "0x" || resultString == null) { returnValue = JsValue.Null; } else { returnValue = _jsonParser.Parse(resultString); } } } catch (HttpRequestException e) { _cliConsole.WriteErrorLine(" " + e.Message); _cliConsole.Write(" Use "); _cliConsole.WriteKeyword("node"); _cliConsole.WriteLine(".switch(\"ip:port\") to change the target machine"); _cliConsole.WriteLine(" Make sure that JSON RPC is enabled on the target machine (--JsonRpc.Enabled true)"); _cliConsole.WriteLine(" Make sure that firewall is open for the JSON RPC port on the target machine"); } catch (Exception e) { _cliConsole.WriteException(e); } return(returnValue); }
public void Error(string text, Exception ex = null) { _cliConsole.WriteErrorLine(text); if (ex != null) { _cliConsole.WriteException(ex); } }
public JsValue Execute(string statement) { try { Engine e = JintEngine.Execute(statement); return(e.GetCompletionValue()); } catch (ParserException e) { _cliConsole.WriteErrorLine(e.Message); } catch (CliArgumentParserException e) { _cliConsole.WriteErrorLine(e.Message); } catch (Exception e) { _cliConsole.WriteException(e); } return(JsValue.Null); }
public void Init() { try { _cliConsole.WriteInteresting($"Loading history file from {Path.Combine(AppDomain.CurrentDomain.BaseDirectory, HistoryFilePath)}" + Environment.NewLine); if (File.Exists(HistoryFilePath)) { foreach (string line in File.ReadLines(HistoryFilePath).Distinct().TakeLast(60)) { if (line != _removedString) { ReadLine.AddHistory(line); _historyCloned.Insert(0, line); } } } } catch (Exception e) { _cliConsole.WriteErrorLine($"Could not load cmd history from {HistoryFilePath} {e.Message}"); } }
private void LoadModule(CliModuleBase module) { CliModuleAttribute?cliModuleAttribute = module.GetType().GetCustomAttribute <CliModuleAttribute>(); if (cliModuleAttribute == null) { _cliConsole.WriteErrorLine( $"Could not load module {module.GetType().Name} bacause of a missing {nameof(CliModuleAttribute)}."); return; } _cliConsole.WriteLine($"module ({cliModuleAttribute.ModuleName})"); ModuleNames.Add(cliModuleAttribute.ModuleName); MethodsByModules[cliModuleAttribute.ModuleName] = new List <string>(); var methods = module.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (MethodInfo methodInfo in methods.OrderBy(m => m.Name)) { var cliProperty = methodInfo.GetCustomAttribute <CliPropertyAttribute>(); var cliFunction = methodInfo.GetCustomAttribute <CliFunctionAttribute>(); bool isProperty = cliProperty != null; string?objectName = cliProperty?.ObjectName ?? cliFunction?.ObjectName; string?itemName = cliProperty?.PropertyName ?? cliFunction?.FunctionName; if (objectName == null) { throw new InvalidDataException($"Method {methodInfo.Name} of {module.GetType().Name} should be decorated with one of {nameof(CliPropertyAttribute)} or {nameof(CliFunctionAttribute)}"); } ObjectInstance instance; if (!_objects.ContainsKey(objectName)) { instance = _engine.JintEngine.Object.Construct(Arguments.Empty); _engine.JintEngine.SetValue(objectName, instance); _objects[objectName] = instance; } instance = _objects[objectName]; var @delegate = CreateDelegate(methodInfo, module); DelegateWrapper nativeDelegate = new DelegateWrapper(_engine.JintEngine, @delegate); if (itemName != null) { if (isProperty) { _cliConsole.WriteKeyword($" {objectName}"); _cliConsole.WriteLine($".{itemName}"); MethodsByModules[objectName].Add(itemName); AddProperty(instance, itemName, nativeDelegate); } else { _cliConsole.WriteKeyword($" {objectName}"); _cliConsole.WriteLine($".{itemName}({string.Join(", ", methodInfo.GetParameters().Select(p => p.Name))})"); MethodsByModules[objectName].Add(itemName + "("); AddMethod(instance, itemName, nativeDelegate); } } } _cliConsole.WriteLine(); }