aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/tweaker/DownloadSourceChecker.java
blob: 7947b6ebc9e132e94b5d37358bc765a5c940b544 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package at.hannibal2.skyhanni.tweaker;

import at.hannibal2.skyhanni.utils.OSUtils;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.net.URI;
import java.net.URL;
import java.util.concurrent.atomic.AtomicBoolean;

public class DownloadSourceChecker {

    private static final String MOD_VERSION = "@MOD_VERSION@";
    private static final String GITHUB_REPO = "511310721";
    private static final String GITHUB_REPO_TEXT = "repo_id=" + GITHUB_REPO;
    private static final String MODRINTH_URL = "/data/byNkmv5G/";

    private static final String[] SECURITY_POPUP = {
        "The file you are trying to run is hosted on a non-trusted domain.",
        "",
        "Host: %s",
        "",
        "Please download the file from a trusted source.",
        "",
        "IF YOU DO NOT KNOW WHAT YOU ARE DOING, CLOSE THIS WINDOW!",
        "",
        "And download from the official link below."
    };

    public static void init() {
        if (!OSUtils.INSTANCE.isWindows()) return;
        URI host = getDangerousHost();
        if (host != null) {
            openMenu(host);
        }
    }

    private static void openMenu(URI host) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        JFrame frame = new JFrame();
        frame.setUndecorated(true);
        frame.setAlwaysOnTop(true);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        AtomicBoolean close = new AtomicBoolean(true);

        JPanel links = new JPanel();

        links.add(TweakerUtils.createButton(
            "Discord",
            () -> TweakerUtils.openUrl("https://discord.com/invite/skyhanni-997079228510117908")
        ));

        links.add(TweakerUtils.createButton(
            "Official Download",
            () -> TweakerUtils.openUrl("https://github.com/hannibal002/SkyHanni/releases")
        ));

        JPanel buttons = new JPanel();

        buttons.add(TweakerUtils.createButton(
            "Close",
            () -> {
                close.set(true);
                frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
            }
        ));

        JOptionPane.showOptionDialog(
            frame,
            String.format(String.join("\n", SECURITY_POPUP), uriToSimpleString(host)),
            "SkyHanni " + MOD_VERSION + " Security Error",
            JOptionPane.DEFAULT_OPTION,
            JOptionPane.ERROR_MESSAGE,
            null,
            new JPanel[] { links, buttons },
            links
        );

        if (!close.get()) return;
        TweakerUtils.exit();
    }

    private static String uriToSimpleString(URI uri) {
        return uri.getScheme() + "://" + uri.getHost() + uri.getPath();
    }

    private static URI getDangerousHost() {
        try {
            URL url = DownloadSourceChecker.class.getProtectionDomain().getCodeSource().getLocation();
            File file = new File(url.getFile());
            if (!file.isFile()) return null;
            URI host = getHost(file);
            if (host == null) return null;

            if (host.getHost().equals("objects.githubusercontent.com")) {
                if (host.getQuery().contains(GITHUB_REPO_TEXT)) {
                    return null;
                } else if (host.getPath().contains("/" + GITHUB_REPO + "/")) {
                    return null;
                }
            } else if (host.getHost().equals("cdn.modrinth.com") && host.getPath().startsWith(MODRINTH_URL)) {
                return null;
            }
            return host;
        } catch (Exception ignored) {
            return null;
        }
    }

    private static URI getHost(File file) throws Exception {
        final File adsFile = new File(file.getAbsolutePath() + ":Zone.Identifier:$DATA");
        String host = null;
        try (BufferedReader reader = new BufferedReader(new FileReader(adsFile))) {
            String line = reader.readLine();
            while (line != null) {
                if (line.startsWith("HostUrl=")) {
                    host = line.substring(8);
                    break;
                }
                line = reader.readLine();
            }
        }
        return host != null ? new URI(host) : null;
    }
}