Skip to content

Commit

Permalink
[feat] config the web port by params
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwu51 committed Sep 7, 2024
1 parent dd7f4d9 commit e55d7d3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ $ java -jar swapper.jar

Visit this url `http://localhost:8000` then you will get the following Web UI.

If you want to change the http server port or web socket port:
```bash
$ java -jar -Dw_http_port=9999 -Dw_ws_port_19999 swapper.jar
```

![image](https://i.imgur.com/WSKkrxX.png)

Now you can enjoy the functionalities of swapper tool.
Expand Down
38 changes: 15 additions & 23 deletions src/main/java/w/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public static void agentmain(String arg, Instrumentation instrumentation) throws
SpringUtils.initFromLoadedClasses();

// 2 start http and websocket server
startHttpd(DEFAULT_HTTP_PORT);
startWebsocketd(DEFAULT_WEBSOCKET_PORT);
startHttpd();
startWebsocketd();

// 3 init execInstance
initExecInstance();
Expand All @@ -50,31 +50,23 @@ public static void agentmain(String arg, Instrumentation instrumentation) throws
schedule();
}

private static void startHttpd(int port) throws IOException {
if (port > 8100) {
System.err.println("Httpd start failed " + port);
throw new IOException("Httpd start failed");
}
try {
new Httpd(port).start(5000, false);
System.out.println("Http server start at port "+ port);
} catch (IOException e) {
startHttpd(port + 1);
private static void startHttpd() throws IOException {
int port = DEFAULT_HTTP_PORT;
if (System.getProperty("http_port") != null) {
port = Integer.parseInt(System.getProperty("http_port"));
}
new Httpd(port).start(5000, false);
System.out.println("Http server start at port "+ port);
}

private static void startWebsocketd(int port) throws IOException {
if (port > 18100) {
System.err.println("Websocketd start failed");
throw new IOException("Websocketd start failed");
}
try {
new Websocketd(port).start(24 * 60 * 60000, false);
System.out.println("Websocket server start at port " + port);
Global.wsPort = port;
} catch (IOException e) {
startWebsocketd(port + 1);
private static void startWebsocketd() throws IOException {
int port = DEFAULT_WEBSOCKET_PORT;
if (System.getProperty("ws_port") != null) {
port = Integer.parseInt(System.getProperty("ws_port"));
}
new Websocketd(port).start(24 * 60 * 60000, false);
System.out.println("Websocket server start at port " + port);
Global.wsPort = port;
}

private static void initExecInstance() throws CannotCompileException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, NotFoundException, IOException {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/w/Attach.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ public static void main(String[] args) throws Exception {
throw e;
}
}
String port = System.getProperty("w_http_port");
if (port == null) {
port = "8000";
}
System.out.println("============Attach finish");
System.out.println("============Web server started at http://localhost:" + port);
}

private static URL toolsJarUrl() throws Exception {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/w/web/Httpd.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public Response serve(String uri, Method method,
return serveFile(uri);
}
if (method == Method.POST) {
// deprecated, use the ui config port
switch (uri) {
case "/wsPort":
return newFixedLengthResponse(Global.wsPort + "");
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/nanohttpd/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/png" href="https://raw.githubusercontent.com/sunwu51/JVMByteSwapTool/master/sw-ico.png" />
<link rel="stylesheet" crossorigin="" href="https://cdn.jsdelivr.net/gh/sunwu51/SwapperUI@03f7f9f73dcbe649e1506835963406587d473b1c/assets/index.css">
<script type="module" crossorigin src="https://cdn.jsdelivr.net/gh/sunwu51/SwapperUI@03f7f9f73dcbe649e1506835963406587d473b1c/assets/index.js"></script>
<link rel="stylesheet" crossorigin="" href="https://cdn.jsdelivr.net/gh/sunwu51/SwapperUI@2c72a1230d56a24b498dab09de8a4cafb06bb356/assets/index.css">
<script type="module" crossorigin src="https://cdn.jsdelivr.net/gh/sunwu51/SwapperUI@2c72a1230d56a24b498dab09de8a4cafb06bb356/assets/index.js"></script>
</head>
<body>
<div id="root"></div>
Expand Down

0 comments on commit e55d7d3

Please sign in to comment.