/// <inheritdoc />
        protected override ILayoutAlgorithm CreateConfiguredLayout(GraphControl graphControl)
        {
            var transformer = new GraphTransformer();

            transformer.Operation             = OperationItem;
            transformer.SubgraphLayoutEnabled = ActOnSelectionOnlyItem;
            transformer.RotationAngle         = RotationAngleItem;
            if (ApplyBestFitRotationItem && OperationItem == OperationType.Rotate)
            {
                var size = graphControl.InnerSize;
                ApplyBestFitRotationItem = true;

                var layoutGraph = new LayoutGraphAdapter(graphControl.Graph).CreateCopiedLayoutGraph();
                transformer.RotationAngle = GraphTransformer.FindBestFitRotationAngle(layoutGraph, size.Width, size.Height);
            }
            else
            {
                ApplyBestFitRotationItem = false;
            }

            transformer.ScaleFactor   = ScaleFactorItem;
            transformer.ScaleNodeSize = ScaleNodeSizeItem;
            transformer.TranslateX    = TranslateXItem;
            transformer.TranslateY    = TranslateYItem;

            return(transformer);
        }
Example #2
0
        public void Initialize()
        {
            if (isInitialized)
            {
                return;
            }

            var dbStops = DbManager.GetAllStops();

            _allStops   = dbStops.ToDictionary(s => s.DbId);
            _graphMap   = GraphTransformer.GetGraphMap();
            _stopGroups = new Dictionary <int, List <int> >();

            // StopGroup asszociatív lista inicializációja
            foreach (var stop in dbStops.Where(s => s.GroupId != null))
            {
                if (!_stopGroups.ContainsKey((int)stop.GroupId))
                {
                    _stopGroups.Add((int)stop.GroupId, new List <int>());
                }
                _stopGroups[(int)stop.GroupId].Add(stop.DbId);
            }

            isInitialized = true;
        }
Example #3
0
        ///<inheritdoc/>
        protected override void ConfigureLayout()
        {
            OptionGroup toplevelGroup   = (OptionGroup)Handler.GetGroupByName(TOP_LEVEL);
            OptionGroup generalGroup    = (OptionGroup)toplevelGroup.GetGroupByName(GENERAL);
            string      operationChoice = (string)generalGroup[OPERATION].Value;

            transformer.Operation             = operationEnum[operationChoice];
            transformer.SubgraphLayoutEnabled = (bool)generalGroup[ACT_ON_SELECTION_ONLY].Value;

            transformer.RotationAngle = (int)toplevelGroup.GetValue(ROTATE, ROTATION_ANGLE);
            if ((bool)toplevelGroup.GetValue(ROTATE, APPLY_BEST_FIT_ROTATION) &&
                ((string)toplevelGroup.GetValue(GENERAL, OPERATION)).Equals(ROTATE))
            {
                CanvasControl cv = Context.Lookup <CanvasControl>();
                if (cv != null)
                {
                    var size = cv.InnerSize;
                    applyBestFitRotation      = true;
                    transformer.RotationAngle =
                        GraphTransformer.FindBestFitRotationAngle(CurrentLayoutGraph, size.Width, size.Height);
                }
            }
            else
            {
                applyBestFitRotation = false;
            }

            transformer.ScaleFactor   = (double)toplevelGroup.GetValue(SCALE, SCALE_FACTOR);
            transformer.ScaleNodeSize = (bool)toplevelGroup.GetValue(SCALE, SCALE_NODE_SIZE);

            transformer.TranslateX = (double)toplevelGroup.GetValue(TRANSLATE, TRANSLATE_X);
            transformer.TranslateY = (double)toplevelGroup.GetValue(TRANSLATE, TRANSLATE_Y);
            LayoutAlgorithm        = transformer;
        }
Example #4
0
        public static IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "{clustername}/lineage/graph")] HttpRequest req,
            string clustername,
            ILogger log,
            ExecutionContext context)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            log.LogInformation("Executing lineage request");

            log.LogMetric(CLUSTERMETRIC, 1, new Dictionary <string, object>()
            {
                { "name", clustername },
            });

            var adxClient = ADXClient.Create(clustername, config, null);

            var scraper = new KustoScraper(adxClient);

            var lineage = scraper.Scrape(clustername);

            var result = GraphTransformer.Transfrom(lineage);

            return(new OkObjectResult(JsonConvert.SerializeObject(result)));
        }
Example #5
0
 public static void initDatabase(string BasePath, string appHomeDir = "")
 {
     if (GtfsDatabase.initDatabase(BasePath))
     {
         GraphTransformer.CreateStopGroups(200);
         GraphBuilder.BuildGraph();
         CostCalculator.CreateTimetableCache(appHomeDir);
     }
 }
Example #6
0
        static void GetGraphMap()
        {
            Console.WriteLine("Getting Graph Map...");
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            Console.WriteLine("Count of StopGroups  :\t" + DbDataManager.GetStopGroupsFromDb().Keys.Count);
            Console.WriteLine("CountOfStopsWithEdges:\t" + GraphTransformer.GetGraphMap().Keys.Count);
            stopwatch.Stop();

            Console.WriteLine("\t Idő: " + (stopwatch.ElapsedMilliseconds / 1000.0) + "s");
        }
Example #7
0
        /// <summary>
        /// Creates and returns the stopgroups
        /// </summary>
        /// <param name="maxDistance">add element if the maximum distance from the
        /// group elements is less or equal to this (metres)</param>
        /// <returns>stopgroups</returns>
        public static List <VMDL_StopGroup> GetStopGroups(double maxDistance)
        {
            var result = new List <VMDL_StopGroup>();

            GraphTransformer.GetStopGroups(maxDistance).ForEach(sg => {
                var stops = new List <VMDL_Stop>();
                sg.GetStops().ForEach(s => stops.Add(new VMDL_Stop(s.DbId, s.StopName, s.StopLatitude, s.StopLongitude, s.LocationType, s.ParentStation, s.WheelchairBoarding)));
                result.Add(new VMDL_StopGroup(stops, sg.HasDifferentNames, sg.avgLatitude, sg.avgLongitude, sg.name));
            });

            return(result);
        }
        /// <summary>
        /// Setup default values for various configuration parameters.
        /// </summary>
        public GraphTransformerConfig()
        {
            var transformer = new GraphTransformer();

            OperationItem            = OperationType.Scale;
            ActOnSelectionOnlyItem   = false;
            RotationAngleItem        = transformer.RotationAngle;
            ApplyBestFitRotationItem = false;
            ScaleFactorItem          = transformer.ScaleFactorX;
            ScaleNodeSizeItem        = transformer.ScaleNodeSize;
            TranslateXItem           = transformer.TranslateX;
            TranslateYItem           = transformer.TranslateY;
        }
Example #9
0
        static void CreateStopGroups()
        {
            Console.WriteLine("Creating Groups...");
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            Console.WriteLine("Before: " + DbDataManager.GetStopGroupsFromDb().Keys.Count);
            GraphTransformer.CreateStopGroups(100);
            Console.WriteLine("After: " + DbDataManager.GetStopGroupsFromDb().Keys.Count);
            stopwatch.Stop();

            Console.WriteLine("\t Idő: " + (stopwatch.ElapsedMilliseconds / 1000.0) + "s");
        }
Example #10
0
        /// <summary>
        /// Todo...
        /// </summary>
        public static void GetRoute()
        {
            var stopsTripsDict = new Dictionary <int, List <int> >();      // StopId -> List<-TripId->
            var tripsTimesDict = new Dictionary <int, List <StopTime> >(); // TripId -> List<StopTime>

            {
                var times = DbManager.GetAllStopTimesForDate(new DateTime(2013, 3, 1));
                times.ForEach(st =>
                {
                    // minden megállóhoz, hogy milyen útvonalakon (trip) szerepel
                    {
                        List <int> stopsTripsValue;
                        if (stopsTripsDict.TryGetValue(st.stopId, out stopsTripsValue))
                        {
                            stopsTripsValue.Add(st.tripId);
                        }
                        else
                        {
                            stopsTripsValue = new List <int>();
                            stopsTripsValue.Add(st.tripId);
                            stopsTripsDict.Add(st.stopId, stopsTripsValue);
                        }
                    }

                    // minden útvonalhoz (trip), hogy sorban milyen StopTime-ok szerepelnek benne
                    {
                        List <StopTime> tripsTimesValue;
                        if (tripsTimesDict.TryGetValue(st.tripId, out tripsTimesValue))
                        {
                            tripsTimesValue.Add(st);
                        }
                        else
                        {
                            tripsTimesValue = new List <StopTime>();
                            tripsTimesValue.Add(st);
                            tripsTimesDict.Add(st.tripId, tripsTimesValue);
                        }
                    }
                });
            }

            // setup stopgroups
            var stopGroups = GraphTransformer.GetStopGroups(100, stopsTripsDict, tripsTimesDict);
        }
Example #11
0
 /// <summary>
 /// Create a new instance.
 /// </summary>
 public GraphTransformerModule() : base(GRAPH_TRANSFORMER)
 {
     transformer = new GraphTransformer();
 }
Example #12
0
        static void Main(string[] args)
        {
            String inputFile;
            String targetFile;
            String targetNamespace;
            String targetClass;
            String targetMethod;
            int    depth;
            int    dimension;
            int    numberValidPaths;
            int    duplicateBasicBlockWeight;
            int    duplicateBasicBlockCorrectionValue;
            int    stateChangeWeight;
            int    stateChangeCorrectionValue;
            int    insertOpaquePredicateWeight;
            int    seed;

            // Add debugging code into the obfuscated method (dump obfuscation graphs and so on)
            bool graphTransformerDebug = false;

            // Should the obfuscated code contain information to trace the control flow?
            bool basicBlockTrace = false;

            // When debugging is active, should the whole obfuscation graph be dumped or only the vpaths in it?
            bool graphOnlyDumpVPaths = true;

            // The number of random interfaces that are added to the program
            int numberRandomInterfaces = 100;

            if (args.Length != 14)
            {
                System.Console.WriteLine("Needed parameters: <inputBinary> <outputBinary> <namespace> <class> <method> <depth> <dimension> <numberValidPaths> <duplicateBasicBlockWeight> <duplicateBasicBlockCorrectionValue> <stateChangeWeight> <stateChangeCorrectionValue> <insertOpaquePredicateWeight> <seed>");
                return;
            }
            else
            {
                inputFile                          = args[0];
                targetFile                         = args[1];
                targetNamespace                    = args[2];
                targetClass                        = args[3];
                targetMethod                       = args[4];
                depth                              = Convert.ToInt32(args[5]);
                dimension                          = Convert.ToInt32(args[6]);
                numberValidPaths                   = Convert.ToInt32(args[7]);
                duplicateBasicBlockWeight          = Convert.ToInt32(args[8]);
                duplicateBasicBlockCorrectionValue = Convert.ToInt32(args[9]);
                stateChangeWeight                  = Convert.ToInt32(args[10]);
                stateChangeCorrectionValue         = Convert.ToInt32(args[11]);
                insertOpaquePredicateWeight        = Convert.ToInt32(args[12]);
                seed = Convert.ToInt32(args[13]);
            }

            String logDir = Path.GetDirectoryName(targetFile);

            Log.Log logger = new Log.Log(logDir, "probfuscation_logfile.txt");

            System.Console.WriteLine("Obfuscating: " + inputFile);
            logger.writeLine("Obfuscating: " + inputFile);
            System.Console.WriteLine("Output file: " + targetFile);
            logger.writeLine("Output file: " + targetFile);
            System.Console.WriteLine("Target namespace: " + targetNamespace);
            logger.writeLine("Target namespace: " + targetNamespace);
            System.Console.WriteLine("Target class: " + targetClass);
            logger.writeLine("Target class: " + targetClass);
            System.Console.WriteLine("Target method: " + targetMethod);
            logger.writeLine("Target method: " + targetMethod);
            System.Console.WriteLine("Depth: " + depth);
            logger.writeLine("Depth: " + depth);
            System.Console.WriteLine("Dimension: " + dimension);
            logger.writeLine("Dimension: " + dimension);
            System.Console.WriteLine("Number of vpaths: " + numberValidPaths);
            logger.writeLine("Number of vpaths: " + numberValidPaths);
            System.Console.WriteLine("Basic Block duplication weight: " + duplicateBasicBlockWeight);
            logger.writeLine("Basic Block duplication weight: " + duplicateBasicBlockWeight);
            System.Console.WriteLine("Basic Block duplication correction value: " + duplicateBasicBlockCorrectionValue);
            logger.writeLine("Basic Block duplication correction value: " + duplicateBasicBlockCorrectionValue);
            System.Console.WriteLine("State change weight: " + stateChangeWeight);
            logger.writeLine("State change weight: " + stateChangeWeight);
            System.Console.WriteLine("State change correction value: " + stateChangeCorrectionValue);
            logger.writeLine("State change correction value: " + stateChangeCorrectionValue);
            System.Console.WriteLine("Opaque predicate weight: " + insertOpaquePredicateWeight);
            logger.writeLine("Opaque predicate weight: " + insertOpaquePredicateWeight);
            System.Console.WriteLine("Seed: " + seed);
            logger.writeLine("Seed: " + seed);

            // Seed PRNG for interfaces
            PRNGRandomInterfaces = new Random(seed);

            using (var host = new PeReader.DefaultHost()) {
                IModule /*?*/ module = host.LoadUnitFrom(inputFile) as IModule;

                if (module == null || module == Dummy.Module || module == Dummy.Assembly)
                {
                    Console.WriteLine(inputFile + " is not a PE file containing a CLR module or assembly.");
                    return;
                }

                module = new MetadataDeepCopier(host).Copy(module);

                if (module as Assembly == null)
                {
                    logger.writeLine("File does not have CIL assembly");
                    return;
                }

                // create analyzer object object
                CfgBuilder analyze = new Cfg.CfgBuilder(module, host, logger);

                PdbReader /*?*/ pdbReader = null;
                string          pdbFile   = Path.ChangeExtension(module.Location, "pdb");
                if (File.Exists(pdbFile))
                {
                    using (var pdbStream = File.OpenRead(pdbFile)) {
                        pdbReader = new PdbReader(pdbStream, host);
                    }
                }
                else
                {
                    logger.writeLine("Could not load the PDB file for '" + module.Name.Value + "' . Proceeding anyway.");
                }

                using (pdbReader) {
                    Microsoft.Cci.ILGenerator.LocalScopeProvider localScopeProvider = null;
                    if (pdbReader != null)
                    {
                        localScopeProvider = new ILGenerator.LocalScopeProvider(pdbReader);
                    }

                    // search the namespace the interface should be added to
                    IUnitNamespace foundNamespace = null;
                    foreach (var tempMember in module.UnitNamespaceRoot.Members)
                    {
                        if ((tempMember as IUnitNamespace) == null)
                        {
                            continue;
                        }

                        IUnitNamespace tempNamespace = (tempMember as IUnitNamespace);

                        if (tempNamespace.ToString() == targetNamespace)
                        {
                            foundNamespace = tempNamespace;
                            break;
                        }
                    }
                    if (foundNamespace == null)
                    {
                        throw new ArgumentException("Not able to find target namespace.");
                    }

                    // add created interface (and implemented methods) to all classes
                    bool classFound = false;
                    foreach (var tempClass in module.GetAllTypes())
                    {
                        if ((tempClass as NamespaceTypeDefinition) == null ||
                            tempClass.IsAbstract)
                        {
                            continue;
                        }

                        NamespaceTypeDefinition foundClass = (tempClass as NamespaceTypeDefinition);

                        if (foundClass.ContainingUnitNamespace.ToString() == "")
                        {
                            continue;
                        }
                        if (foundClass.ToString() != targetNamespace + "." + targetClass)
                        {
                            continue;
                        }
                        classFound = true;

                        Random           prng             = new Random();
                        GraphTransformer graphTransformer = new GraphTransformer(module, host, logger, prng, foundNamespace, foundClass, depth, dimension, graphTransformerDebug);

                        graphTransformer.duplicateBasicBlockWeight          = duplicateBasicBlockWeight;
                        graphTransformer.duplicateBasicBlockCorrectionValue = duplicateBasicBlockCorrectionValue;
                        graphTransformer.stateChangeWeight           = stateChangeWeight;
                        graphTransformer.stateChangeCorrectionValue  = stateChangeCorrectionValue;
                        graphTransformer.insertOpaquePredicateWeight = insertOpaquePredicateWeight;
                        graphTransformer.trace = basicBlockTrace;
                        graphTransformer.graphOnlyDumpVPaths   = graphOnlyDumpVPaths;
                        graphTransformer.debuggingDumpLocation = logDir;

                        // Add 100 random interfaces to the namespace
                        Helper testHelper = new Helper(module, host, logger);
                        List <NamespaceTypeDefinition> randomInterfaces = new List <NamespaceTypeDefinition>();
                        for (int i = 0; i < numberRandomInterfaces; i++)
                        {
                            String randName = randomString(20);
                            NamespaceTypeDefinition temp = testHelper.createNewInterface(randName, foundNamespace);
                            randomInterfaces.Add(temp);
                        }

                        InterfaceTransformer interfaceTransformer = new InterfaceTransformer(module, host, logger);
                        foreach (var classToAdd in module.GetAllTypes())
                        {
                            if ((classToAdd as NamespaceTypeDefinition) == null ||
                                classToAdd.IsAbstract ||
                                classToAdd.IsInterface ||
                                classToAdd.IsEnum ||
                                classToAdd.IsDelegate ||
                                classToAdd.IsGeneric ||
                                classToAdd.IsStruct)
                            {
                                continue;
                            }

                            if (((NamespaceTypeDefinition)classToAdd).ContainingUnitNamespace.ToString() == "")
                            {
                                continue;
                            }

                            /*
                             * // Use this code if you want to add standard interfaces to the target class
                             * interfaceTransformer.addStdInterfacesGivenByFile(@"e:\code\dotnet_standard_interfaces.txt");
                             *
                             * // add std interfaces to class
                             * if (foundClass != (classToAdd as NamespaceTypeDefinition)) {
                             *  foreach (ITypeDefinition temp in interfaceTransformer.getInterfacesList()) {
                             *      interfaceTransformer.addInterface((classToAdd as NamespaceTypeDefinition), temp);
                             *  }
                             * }
                             */

                            // Add random interfaces to the classes
                            List <NamespaceTypeDefinition> alreadyAdded = new List <NamespaceTypeDefinition>();
                            int max = PRNGRandomInterfaces.Next(numberRandomInterfaces);
                            NamespaceTypeDefinition interfaceClass = (classToAdd as NamespaceTypeDefinition);
                            logger.writeLine("Adding " + max + " random interfaces to class \"" + interfaceClass.ToString() + "\"");
                            for (int i = 0; i < max; i++)
                            {
                                NamespaceTypeDefinition randInterface = randomInterfaces.ElementAt(PRNGRandomInterfaces.Next(randomInterfaces.Count));
                                if (alreadyAdded.Contains(randInterface))
                                {
                                    continue;
                                }
                                alreadyAdded.Add(randInterface);
                                logger.writeLine("Adding interface: \"" + randInterface.ToString() + "\"");

                                // add nodes interface to class
                                if (interfaceClass.Interfaces != null)
                                {
                                    interfaceClass.Interfaces.Add(randInterface);
                                }
                                else
                                {
                                    interfaceClass.Interfaces = new List <ITypeReference>();
                                    interfaceClass.Interfaces.Add(randInterface);
                                }
                            }
                            logger.writeLine("");

                            // Add special interface for the obfuscation scheme to the class
                            // (makes sure that all needed attributes and methods are implemented)
                            graphTransformer.addNodeInterfaceToTargetClass((classToAdd as NamespaceTypeDefinition));
                        }

                        // Prepare obfuscation graph
                        graphTransformer.generateGraph(numberValidPaths);
                        graphTransformer.createGraphMethods();

                        // Search method to obfuscate
                        MethodDefinition methodToObfu = null;
                        foreach (MethodDefinition tempMethod in foundClass.Methods)
                        {
                            if (tempMethod.Name.ToString() == targetMethod)
                            {
                                methodToObfu = tempMethod;
                                break;
                            }
                        }
                        if (methodToObfu == null)
                        {
                            throw new ArgumentException("Not able to find target method.");
                        }

                        // Obfuscate target method
                        MethodCfg cfg = analyze.buildCfgForMethod(methodToObfu);
                        logger.dumpMethodCfg(cfg, "before");
                        graphTransformer.addObfuscationToMethod(cfg);
                        analyze.createMethodFromCfg(cfg);
                        logger.dumpMethodCfg(cfg, "after");

                        break;
                    }
                    if (!classFound)
                    {
                        throw new ArgumentException("Not able to find target class.");
                    }


                    /*
                     * This code can be used if not only one specific method should be obfuscated,
                     * but the whole class.
                     * List<ClassCfg> classCfgList = new List<ClassCfg>();
                     * foreach (var tempClass in module.GetAllTypes()) {
                     *  if ((tempClass as NamespaceTypeDefinition) == null
                     || tempClass.IsAbstract) {
                     ||     continue;
                     || }
                     ||
                     || // create basic blocks
                     || NamespaceTypeDefinition foundClass = (tempClass as NamespaceTypeDefinition);
                     ||
                     || logger.writeLine("Create CFG for class \"" + foundClass.Name.ToString() + "\"");
                     || ClassCfg temp = analyze.buildCfgForClass(foundClass);
                     || classCfgList.Add(temp);
                     || logger.writeLine("\n---------------------------------\n");
                     ||}
                     ||
                     ||// transform each function
                     ||NopTransformer transformator = new NopTransformer(module, host, logger);
                     ||foreach (ClassCfg tempClassCfg in classCfgList) {
                     || foreach (MethodCfg tempMethodCfg in tempClassCfg.methodCfgs) {
                     ||     logger.writeLine("Transform method CFG of \"" + tempMethodCfg.method.ToString() + "\"");
                     ||     transformator.addNopsToCfg(tempMethodCfg);
                     ||     logger.writeLine("\n---------------------------------\n");
                     || }
                     ||}
                     ||
                     ||foreach (ClassCfg tempClassCfg in classCfgList) {
                     || logger.writeLine("Create class from CFG for \"" + tempClassCfg.classObj.Name.ToString() + "\"");
                     || analyze.createClassFromCfg(tempClassCfg);
                     || logger.writeLine("\n---------------------------------\n");
                     ||}
                     */

                    using (var peStream = File.Create(targetFile)) {
                        using (var pdbWriter = new PdbWriter(Path.ChangeExtension(targetFile, ".pdb"), pdbReader)) {
                            PeWriter.WritePeToStream(module, host, peStream, pdbReader, localScopeProvider, pdbWriter);
                        }
                    }
                }
            }
        }