Exemplo n.º 1
0
 private static void Main()
 {
     Log.AllowLogging();
     try
     {
         var javaPath = string.Empty;
         var exePath = PathEx.Combine("%CurDir%\\JDownloader_v2.0\\JDownloader2.exe");
         var envVar = EnvironmentEx.GetVariableValue("CurDir");
         if (!File.Exists(exePath))
         {
             var drive = new DriveInfo(envVar).RootDirectory.Root.Name;
             var JavaDir = drive;
             foreach (var dirName in envVar.Split('\\'))
                 try
                 {
                     if (drive.Contains(dirName))
                         continue;
                     JavaDir = Path.Combine(JavaDir, dirName);
                     string tmpDir;
                     if (Environment.Is64BitOperatingSystem)
                     {
                         tmpDir = Path.Combine(JavaDir, "CommonFiles\\Java64");
                         if (Directory.Exists(tmpDir))
                             foreach (var file in Directory.GetFiles(tmpDir, "javaw.exe", SearchOption.AllDirectories))
                             {
                                 JavaDir = tmpDir;
                                 javaPath = file;
                                 break;
                             }
                     }
                     tmpDir = Path.Combine(JavaDir, "CommonFiles\\Java64");
                     if (!File.Exists(tmpDir))
                         continue;
                     if (!Directory.Exists(tmpDir))
                         continue;
                     foreach (var file in Directory.GetFiles(tmpDir, "javaw.exe", SearchOption.AllDirectories))
                     {
                         JavaDir = tmpDir;
                         javaPath = file;
                         break;
                     }
                 }
                 catch (Exception ex)
                 {
                     Log.Write(ex);
                 }
             if (!File.Exists(javaPath))
             {
                 if (File.Exists($"{exePath}.disabled"))
                     File.Move($"{exePath}.disabled", exePath);
                 var updExePath = PathEx.Combine("%CurDir%\\JDownloader_v2.0\\JDownloader2Update.exe");
                 if (File.Exists($"{updExePath}.disabled"))
                     File.Move($"{updExePath}.disabled", updExePath);
             }
         }
         if (!File.Exists(javaPath) && !File.Exists(exePath))
         {
             MessageBox.Show(@"Java Portable not found!", @"JDownloader 2 Portable", MessageBoxButtons.OK, MessageBoxIcon.Error);
             return;
         }
         var jDownloader = Path.Combine("%CurDir%\\JDownloader_v2.0\\JDownloader.jar");
         if (!File.Exists(jDownloader))
         {
             MessageBox.Show(@"JDownloader 2 not found!", @"JDownloader 2 Portable", MessageBoxButtons.OK, MessageBoxIcon.Error);
             return;
         }
         bool newInstance;
         using (new Mutex(true, Process.GetCurrentProcess().ProcessName, out newInstance))
         {
             var jdDir = Path.GetDirectoryName(jDownloader);
             if (newInstance)
             {
                 var jreUsageDir = PathEx.Combine("%UserProfile%\\.oracle_jre_usage");
                 if (!Directory.Exists(jreUsageDir))
                     Directory.CreateDirectory(jreUsageDir);
                 Data.SetAttributes(jreUsageDir, FileAttributes.Hidden);
                 if (File.Exists(exePath))
                 {
                     using (var p = ProcessEx.Start(exePath, EnvironmentEx.CommandLine(false), false, false))
                         if (!p?.HasExited == true)
                             p?.WaitForExit();
                 }
                 else
                 {
                     using (var p = ProcessEx.Start(javaPath, jdDir, $"-jar \"{jDownloader}\" {EnvironmentEx.CommandLine(false)}".Trim(), false, false))
                         if (!p?.HasExited == true)
                             p?.WaitForExit();
                 }
                 if (Directory.Exists(jreUsageDir))
                     Directory.Delete(jreUsageDir, true);
             }
             else
             {
                 if (File.Exists(exePath))
                     ProcessEx.Start(exePath, EnvironmentEx.CommandLine(false));
                 else
                     ProcessEx.Start(javaPath, jdDir, $"-jar \"{jDownloader}\" {EnvironmentEx.CommandLine(false)}".Trim());
             }
         }
     }
     catch (Exception ex)
     {
         Log.Write(ex);
     }
 }
Exemplo n.º 2
0
        private static void Main()
        {
            Log.AllowLogging();
            Ini.File("%CurDir%\\JavaPortable.ini");
            Java.Add("exe", "bin\\javaw.exe");
            if (!JavaExists())
                try
                {
                    var drive = new DriveInfo(EnvironmentEx.GetVariableValue("CurDir")).RootDirectory.Root.Name;
                    var javaDir = drive;
                    string javaPath = null;
                    var envValue = EnvironmentEx.GetVariableValue("CurDir");
                    foreach (var dirName in envValue.Split('\\'))
                        try
                        {
                            if (drive.Contains(dirName))
                                continue;
                            javaDir = Path.Combine(javaDir, dirName);
                            if (File.Exists(Path.Combine(javaDir, $"CommonFiles\\{JavaVar}\\{Java["exe"]}")))
                            {
                                javaDir = Path.Combine(javaDir, $"CommonFiles\\{JavaVar}");
                                javaPath = Path.Combine(javaDir, Java["exe"]);
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Write(ex);
                        }

                    if (File.Exists(javaPath))
                    {
                        Ini.Write("Location", JavaVar, javaDir.Replace(envValue, "%CurDir%"));
                        throw new OperationCanceledException($"Java found in '{javaDir}'.");
                    }

                    MessageBox.Show($@"Please select the root folder of Java RE ({(Environment.Is64BitProcess ? "x64" : "x86")}) Portable.", @"Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    while (true)
                    {
                        using (var dialog = new FolderBrowserDialog())
                        {
                            dialog.Description = $@"Java Runtime Environments ({(Environment.Is64BitProcess ? "x64" : "x86")}):";
                            dialog.ShowDialog(new Form { ShowIcon = false, TopMost = true });
                            var tmp = Path.Combine(dialog.SelectedPath, Java["exe"]);
                            if (File.Exists(tmp) && PathEx.FileIs64Bit(tmp))
                                javaDir = dialog.SelectedPath.Replace(envValue, "%CurDir%");
                            var tmpPath = Path.Combine(javaDir, Java["exe"]);
                            if (string.IsNullOrWhiteSpace(javaDir) || !File.Exists(tmpPath))
                                if (MessageBox.Show(@"Java Portable not found!", $@"Java RE ({(Environment.Is64BitProcess ? "x64" : "x86")}) NOT Found", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                                    continue;
                        }
                        break;
                    }
                    if (!string.IsNullOrWhiteSpace(javaDir))
                        Ini.Write("Location", JavaVar, javaDir);
                }
                catch (Exception ex)
                {
                    Log.Write(ex);
                }
            if (!JavaExists())
            {
                MessageBox.Show(@"Java Portable not found!", $@"Java Portable{(Environment.Is64BitProcess ? " (x64)" : string.Empty)}", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Environment.Exit(-1);
            }
            if (File.Exists(Ini.File()))
            {
                string appPath = null;
                if (EnvironmentEx.CommandLineArgs(false).Count < 1)
                {
                    using (var dialog = new OpenFileDialog())
                    {
                        dialog.Filter = @"Java Files (*.exe, *.jar)|*.exe;*.jar|All Files (*.*)|*.*";
                        dialog.Multiselect = false;
                        dialog.Title = @"Select a Java Application";
                        dialog.ShowDialog(new Form { ShowIcon = false, TopMost = true });
                        appPath = dialog.FileName;
                    }
                }
                else
                {
                    if (File.Exists(EnvironmentEx.CommandLineArgs(false)[0]))
                        appPath = EnvironmentEx.CommandLineArgs(false)[0];
                }
                try
                {
                    if (!string.IsNullOrWhiteSpace(appPath))
                    {
                        var appPathMd5 = appPath.EncryptToMd5();
                        if (string.IsNullOrWhiteSpace(Ini.Read("Shortcuts", appPathMd5)))
                        {
                            if (MessageBox.Show(@"Java Portable not found!", @"Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                try
                                {
                                    var name = Path.GetFileNameWithoutExtension(appPath);
                                    if (!Data.CreateShortcut(Application.ExecutablePath, $"%DesktopDir%\\{name}.lnk", EnvironmentEx.CommandLineArgs(false).Count > 0 ? EnvironmentEx.CommandLine(false) : $"\"{appPath}\""))
                                        throw new Exception();
                                    MessageBox.Show($@"Desktop shortcut for {name} created.", @"Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                                catch
                                {
                                    MessageBox.Show(@"Java Portable not found!", @"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                }
                            if (MessageBox.Show(@"Java Portable not found!", @"Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                                Ini.Write("Shortcuts", appPathMd5, "True");
                        }
                        var jreUsageDir = PathEx.Combine("%UserProfile%\\.oracle_jre_usage");
                        if (!Directory.Exists(jreUsageDir))
                            Directory.CreateDirectory(jreUsageDir);
                        Data.SetAttributes(jreUsageDir, FileAttributes.Hidden);
                        var filePath = Path.Combine(PathEx.Combine(Ini.Read("Location", JavaVar)), Java["exe"]);
                        var workDir = Path.Combine(PathEx.Combine(Ini.Read("Location", JavaVar)), Java["exe"]);
                        var args = $"-jar {(EnvironmentEx.CommandLineArgs(false).Count > 0 ? EnvironmentEx.CommandLine(false) : $"\"{appPath}\"")}";
                        using (var p = ProcessEx.Start(filePath, workDir, args, false, false))
                        {
                            if (p.Id <= 0)
                                throw new NotSupportedException();
                            if (!p.HasExited)
                                p.WaitForExit();
                        }
                    }
                }
                catch
                {
                    MessageBox.Show(@"Java Portable not found!", @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
Exemplo n.º 3
0
        private static void Main()
        {
            Log.AllowLogging();
            try
            {
                bool newInstance;
                using (new Mutex(true, Process.GetCurrentProcess().ProcessName, out newInstance))
                {
                    if (!newInstance)
                        return;
                    var appPath = PathEx.Combine("%CurDir%\\App\\ZedAttackProxy\\zap-2.5.0.jar");
                    if (!File.Exists(appPath) || Process.GetProcessesByName("zap").Length > 0)
                        return;

                    var appDrive = new DriveInfo(EnvironmentEx.GetVariableValue("CurDir")).RootDirectory.Root.Name;
                    var javaPath = appDrive;
                    foreach (var dirName in EnvironmentEx.GetVariableValue("CurDir").Split('\\'))
                    {
                        if (appDrive.Contains(dirName))
                            continue;
                        javaPath = Path.Combine(javaPath, dirName);
                        if (Environment.Is64BitOperatingSystem)
                        {
                            if (File.Exists(Path.Combine(javaPath, "Java64Portable\\CommonFiles\\Java64\\bin\\javaw.exe")))
                            {
                                javaPath = Path.Combine(javaPath, "Java64Portable\\CommonFiles\\Java64\\bin\\javaw.exe");
                                break;
                            }
                            if (File.Exists(Path.Combine(javaPath, "CommonFiles\\Java64\\bin\\javaw.exe")))
                            {
                                javaPath = Path.Combine(javaPath, "CommonFiles\\Java64\\bin\\javaw.exe");
                                break;
                            }
                        }
                        if (File.Exists(Path.Combine(javaPath, "Java64Portable\\CommonFiles\\Java\\bin\\javaw.exe")))
                        {
                            javaPath = Path.Combine(javaPath, "Java64Portable\\CommonFiles\\Java\\bin\\javaw.exe");
                            break;
                        }
                        if (File.Exists(Path.Combine(javaPath, "JavaPortable\\CommonFiles\\Java\\bin\\javaw.exe")))
                        {
                            javaPath = Path.Combine(javaPath, "JavaPortable\\CommonFiles\\Java\\bin\\javaw.exe");
                            break;
                        }
                        if (File.Exists(Path.Combine(javaPath, "CommonFiles\\Java\\bin\\javaw.exe")))
                        {
                            javaPath = Path.Combine(javaPath, "CommonFiles\\Java\\bin\\javaw.exe");
                            break;
                        }
                    }
                    if (!File.Exists(javaPath))
                    {
                        MessageBox.Show(@"Java Portable not found!", @"Zed Attack Proxy Portable", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    var portableProfile = PathEx.Combine("%CurDir%\\Data");
                    if (!Directory.Exists(portableProfile))
                        Directory.CreateDirectory(portableProfile);
                    var userProfile = PathEx.Combine("%UserProfile%\\OWASP ZAP");
                    Data.DirLink(userProfile, portableProfile, true);

                    using (var p = ProcessEx.Start(javaPath, Path.GetDirectoryName(appPath), $"-Xmx512m -XX:PermSize=256M -jar \"{appPath}\" {EnvironmentEx.CommandLine(false)}", false, false))
                        if (!p?.HasExited == true)
                            p?.WaitForExit();

                    Data.DirUnLink(userProfile, true);
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }
        }