|
| 1 | +import javafx.application.Application; |
| 2 | +import javafx.event.ActionEvent; |
| 3 | +import javafx.event.EventHandler; |
| 4 | +import javafx.geometry.Insets; |
| 5 | +import javafx.scene.Scene; |
| 6 | +import javafx.scene.control.Alert; |
| 7 | +import javafx.scene.control.Button; |
| 8 | +import javafx.scene.control.TextArea; |
| 9 | +import javafx.scene.control.TextField; |
| 10 | +import javafx.scene.layout.AnchorPane; |
| 11 | +import javafx.scene.layout.HBox; |
| 12 | +import javafx.scene.layout.VBox; |
| 13 | +import javafx.stage.FileChooser; |
| 14 | +import javafx.stage.Stage; |
| 15 | +import jodd.http.HttpRequest; |
| 16 | +import jodd.http.HttpResponse; |
| 17 | + |
| 18 | + |
| 19 | +import java.io.*; |
| 20 | +import java.util.regex.Matcher; |
| 21 | +import java.util.regex.Pattern; |
| 22 | + |
| 23 | +public class Druid_Sessions extends Application { |
| 24 | + public static void main(String[] args) { |
| 25 | + launch(args); |
| 26 | + } |
| 27 | + |
| 28 | + public static TextArea result = new TextArea(); |
| 29 | + |
| 30 | + public void start(Stage primaryStage) { |
| 31 | + AnchorPane ap = new AnchorPane(); |
| 32 | + HBox hbox = new HBox(8); |
| 33 | + hbox.setPadding(new Insets(10)); |
| 34 | + |
| 35 | + final TextField input = new TextField(); |
| 36 | + input.setPrefWidth(330); |
| 37 | + input.setPromptText("请输入URL"); |
| 38 | + input.setFocusTraversable(false); |
| 39 | + |
| 40 | + Button url = new Button("远程获取"); |
| 41 | + Button file = new Button("本地获取"); |
| 42 | + |
| 43 | + hbox.getChildren().addAll(input,url,file); |
| 44 | + |
| 45 | + HBox hbox2 = new HBox(); |
| 46 | + hbox2.setPadding(new Insets(10)); |
| 47 | + |
| 48 | + result.setPrefWidth(475); |
| 49 | + result.setPrefHeight(500); |
| 50 | + hbox2.getChildren().add(result); |
| 51 | + |
| 52 | + url.setOnAction(new EventHandler<ActionEvent>() { |
| 53 | + @Override |
| 54 | + public void handle(ActionEvent event) { |
| 55 | + String url = input.getText(); |
| 56 | + boolean b = url.contains(".json") && url.contains("http://") | url.contains("https://"); |
| 57 | + if (b){ |
| 58 | + getUrl(url); |
| 59 | + } else { |
| 60 | + Alert alert = new Alert(Alert.AlertType.ERROR); |
| 61 | + alert.setHeaderText("URL 输入错误"); |
| 62 | + alert.setContentText("请查看是否带有 http:// 或 https://, 是否访问的是 json 文件:\n" + |
| 63 | + "http://127.0.0.1/druid/websession.json\n" + |
| 64 | + "http://127.0.0.1/system/druid/websession.json\n" + |
| 65 | + "http://127.0.0.1/webpage/system/druid/websession.json"); |
| 66 | + alert.show(); |
| 67 | + } |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + file.setOnAction(new EventHandler<ActionEvent>() { |
| 72 | + @Override |
| 73 | + public void handle(ActionEvent event) { |
| 74 | + Stage stage = new Stage(); |
| 75 | + FileChooser fc = new FileChooser(); |
| 76 | + File filePath = fc.showOpenDialog(stage); |
| 77 | + getFile(filePath.getAbsolutePath()); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + VBox vBox = new VBox(); |
| 82 | + vBox.getChildren().addAll(hbox,hbox2); |
| 83 | + ap.getChildren().addAll(vBox); |
| 84 | + |
| 85 | + Scene scene = new Scene(ap); |
| 86 | + primaryStage.setScene(scene); |
| 87 | + primaryStage.setTitle("Get Druid Sessions By: yuyan-sec"); |
| 88 | + primaryStage.setWidth(510); |
| 89 | + primaryStage.setHeight(600); |
| 90 | + primaryStage.setResizable(false); |
| 91 | + primaryStage.show(); |
| 92 | + } |
| 93 | + |
| 94 | + private static void getUrl(String url){ |
| 95 | + HttpResponse response = HttpRequest.get(url).send(); |
| 96 | + parse(response.bodyText()); |
| 97 | + } |
| 98 | + |
| 99 | + private static void getFile(String file){ |
| 100 | + File fileName = new File(file); |
| 101 | + Long fileLength = fileName.length(); |
| 102 | + byte[] fileContent = new byte[fileLength.intValue()]; |
| 103 | + try { |
| 104 | + FileInputStream in = new FileInputStream(fileName); |
| 105 | + in.read(fileContent); |
| 106 | + in.close(); |
| 107 | + } catch (IOException e) { |
| 108 | + e.printStackTrace(); |
| 109 | + } |
| 110 | + parse(new String(fileContent)); |
| 111 | + } |
| 112 | + |
| 113 | + private static void parse(String line){ |
| 114 | + Pattern p = Pattern.compile("[A-Za-z0-9]{32}"); |
| 115 | + Matcher m = p.matcher(line); |
| 116 | + String session = ""; |
| 117 | + while(m.find()){ |
| 118 | + session += m.group() + "\n"; |
| 119 | + } |
| 120 | + result.setText(session); |
| 121 | + } |
| 122 | +} |
0 commit comments