org.aoju.bus.forest.Launcher Maven / Gradle / Ivy
/*
* The MIT License
*
* Copyright (c) 2017 aoju.org All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.aoju.bus.forest;
import org.aoju.bus.forest.algorithm.Key;
import org.aoju.bus.forest.provider.DecryptorProvider;
import org.aoju.bus.forest.provider.EncryptorProvider;
import org.aoju.bus.forest.provider.JdkDecryptorProvider;
import org.aoju.bus.forest.provider.JdkEncryptorProvider;
import java.io.*;
import java.net.URI;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.Arrays;
import java.util.Properties;
import java.util.Scanner;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
/**
* Spring-Boot 启动器
*
* @author Kimi Liu
* @version 3.6.8
* @since JDK 1.8+
*/
public class Launcher {
public final String[] args;
public final DecryptorProvider decryptorProvider;
public final EncryptorProvider encryptorProvider;
public final Key key;
public Launcher(String... args) throws Exception {
this.args = args;
String algorithm = Consts.DEFAULT_ALGORITHM;
int keysize = Consts.DEFAULT_KEYSIZE;
int ivsize = Consts.DEFAULT_IVSIZE;
String password = null;
String keypath = null;
for (String arg : args) {
if (arg.toLowerCase().startsWith(Consts.XJAR_ALGORITHM)) {
algorithm = arg.substring(Consts.XJAR_ALGORITHM.length());
}
if (arg.toLowerCase().startsWith(Consts.XJAR_KEYSIZE)) {
keysize = Integer.valueOf(arg.substring(Consts.XJAR_KEYSIZE.length()));
}
if (arg.toLowerCase().startsWith(Consts.XJAR_IVSIZE)) {
ivsize = Integer.valueOf(arg.substring(Consts.XJAR_IVSIZE.length()));
}
if (arg.toLowerCase().startsWith(Consts.XJAR_PASSWORD)) {
password = arg.substring(Consts.XJAR_PASSWORD.length());
}
if (arg.toLowerCase().startsWith(Consts.XJAR_KEYFILE)) {
keypath = arg.substring(Consts.XJAR_KEYFILE.length());
}
}
ProtectionDomain domain = this.getClass().getProtectionDomain();
CodeSource source = domain.getCodeSource();
URI location = (source == null ? null : source.getLocation().toURI());
String filepath = (location == null ? null : location.getSchemeSpecificPart());
if (filepath != null) {
File file = new File(filepath);
JarFile jar = new JarFile(file, false);
Manifest manifest = jar.getManifest();
Attributes attributes = manifest.getMainAttributes();
if (attributes.getValue(Consts.XJAR_ALGORITHM_KEY) != null) {
algorithm = attributes.getValue(Consts.XJAR_ALGORITHM_KEY);
}
if (attributes.getValue(Consts.XJAR_KEYSIZE_KEY) != null) {
keysize = Integer.valueOf(attributes.getValue(Consts.XJAR_KEYSIZE_KEY));
}
if (attributes.getValue(Consts.XJAR_IVSIZE_KEY) != null) {
ivsize = Integer.valueOf(attributes.getValue(Consts.XJAR_IVSIZE_KEY));
}
if (attributes.getValue(Consts.XJAR_PASSWORD_KEY) != null) {
password = attributes.getValue(Consts.XJAR_PASSWORD_KEY);
}
}
Properties key = null;
File keyfile = null;
if (keypath != null) {
String path = Builder.absolutize(keypath);
File file = new File(path);
if (file.exists() && file.isFile()) {
keyfile = file;
try (InputStream in = new FileInputStream(file)) {
key = new Properties();
key.load(in);
}
} else {
throw new FileNotFoundException("could not find key file at path: " + file.getCanonicalPath());
}
} else {
String path = Builder.absolutize("xjar.key");
File file = new File(path);
if (file.exists() && file.isFile()) {
keyfile = file;
try (InputStream in = new FileInputStream(file)) {
key = new Properties();
key.load(in);
}
}
}
String hold = null;
if (key != null) {
Set names = key.stringPropertyNames();
for (String name : names) {
switch (name.toLowerCase()) {
case Consts.XJAR_KEY_ALGORITHM:
algorithm = key.getProperty(name);
break;
case Consts.XJAR_KEY_KEYSIZE:
keysize = Integer.valueOf(key.getProperty(name));
break;
case Consts.XJAR_KEY_IVSIZE:
ivsize = Integer.valueOf(key.getProperty(name));
break;
case Consts.XJAR_KEY_PASSWORD:
password = key.getProperty(name);
break;
case Consts.XJAR_KEY_HOLD:
hold = key.getProperty(name);
default:
break;
}
}
}
if (hold == null || !Arrays.asList("true", "1", "yes", "y").contains(hold.trim().toLowerCase())) {
if (keyfile != null && keyfile.exists() && !keyfile.delete() && keyfile.exists()) {
throw new IOException("could not delete key file: " + keyfile.getCanonicalPath());
}
}
if (password == null && System.console() != null) {
Console console = System.console();
char[] chars = console.readPassword("password:");
password = new String(chars);
}
if (password == null) {
System.out.print("password:");
Scanner scanner = new Scanner(System.in);
password = scanner.nextLine();
}
this.decryptorProvider = new JdkDecryptorProvider(algorithm);
this.encryptorProvider = new JdkEncryptorProvider(algorithm);
this.key = Builder.key(algorithm, keysize, ivsize, password);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy