Skip to content

Commit

Permalink
15 tui support (#16)
Browse files Browse the repository at this point in the history
* add jexer client

* add tui

* update check

* opt some logic

* update tui
  • Loading branch information
sunwu51 authored Apr 13, 2024
1 parent 71678e8 commit 616c1bd
Show file tree
Hide file tree
Showing 31 changed files with 1,506 additions and 76 deletions.
21 changes: 5 additions & 16 deletions README.md → README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@
# start
从github release下载`swapper-xx.jar`,运行指令,根据提示输入即可,如下:
```bash
# java >=9
$ java -jar swapper.jar

# java == 8 Linux/MacOs:
$ java -cp ${JAVA_HOME}/lib/tools.jar:swapper.jar w.Attach
# java == 8 Windows
$ java -cp "%JAVA_HOME%\lib\tools.jar";swapper.jar w.Attach


$ java -jar swapper-0.0.1-SNAPSHOT.jar
[0] 36200 swapper.jar
[1] 55908 com.example.springweb.SpringWebApplication
[2] Custom PID
Expand All @@ -35,14 +27,10 @@ $ java -cp "%JAVA_HOME%\lib\tools.jar";swapper.jar w.Attach
============The PID is 55908
============Attach finish
```
此时已经attach完成,到目标jvm的日志中可以看到如下log。

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

# usage
根据上面log中去请求页面`http://localhost:8000` 默认是8000端口,如果出现冲突会替换,以上面log中为准。
根据上面log中监听的http端口去请求页面`http://localhost:8000` 默认是8000端口,如果出现冲突会替换,以上面log中为准。

建议测试环境,jvm启动参数添加`-Xverify:none`否则部分信息不会打印
建议测试环境,jvm启动参数添加`-Xverify:none`否则部分异常信息日志不会打印

注意!! 工具中所有的类名,只能是类,不能是接口。
## 1 watch
Expand Down Expand Up @@ -182,7 +170,8 @@ public class TestService {
可以通过effected class按钮查看当前被修改的类,也可以指定uuid剔除某些transformer,或者reset删除全部。

![image](https://github.com/sunwu51/JVMByteSwapTool/assets/15844103/3144aab1-c6a6-4df2-9737-6f0e503b36a2)

# tui
目前引入了tui,方便一些服务器不能ip直接访问,而无法打开页面的场景,tui-client在jbs-client目录,用法与网页类似,通过`tab`切换选项,`enter`进行选中。



2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<artifactId>tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
<systemPath>${project.basedir}/lib/tools.jar</systemPath>
</dependency>
</dependencies>
<properties>
Expand Down
18 changes: 18 additions & 0 deletions jbs-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# A tui for JVMByteSwapperTool
Linux/MacOS supported

Use the source code
```bash
$ go mod tidy
$ go run . [options]
```
Or use the binary files in the release package (Only linux binary is provided, for other platforms, built by yourself)
```bash
$ ./jbs-client [options]
```
## options
```
--host localhost
--http_port 8000
--ws_port 18000
```
32 changes: 32 additions & 0 deletions jbs-client/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module github.com/sunwu51/jbs/client

go 1.18

require (
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.25.0
github.com/charmbracelet/lipgloss v0.9.1
github.com/gorilla/websocket v1.5.1
github.com/thoas/go-funk v0.9.3
)

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.6 // indirect
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
)
62 changes: 62 additions & 0 deletions jbs-client/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0=
github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw=
github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt5dywy4TcM=
github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg=
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.6 h1:Sovz9sDSwbOz9tgUy8JpT+KgCkPYJEN/oYzlJiYTNLg=
github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/thoas/go-funk v0.9.3 h1:7+nAEx3kn5ZJcnDm2Bh23N2yOtweO14bi//dvRtgLpw=
github.com/thoas/go-funk v0.9.3/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
76 changes: 76 additions & 0 deletions jbs-client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"flag"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/sunwu51/jbs/client/request"
"github.com/sunwu51/jbs/client/ui"
)

type Model struct {
state int
width int
height int
inputContainer ui.InputContainer
logContainer ui.LogContainer
}

func (m Model) Init() tea.Cmd {
return nil
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch x := msg.(type) {
case tea.KeyMsg:
if x.Type == tea.KeyCtrlC {
return m, tea.Quit
}
case tea.WindowSizeMsg:
m.width, m.height = x.Width, x.Height
ready := 1
m.state = ready
}
i, cmd1 := m.inputContainer.Update(msg)
l, cmd2 := m.logContainer.Update(msg)
m.inputContainer = i
m.logContainer = l
return m, tea.Batch(cmd1, cmd2)
}

func (m Model) View() string {
if m.width < 200 || m.height < 35 {
return "Window need to larger than 200x35"
}
initializing := 0
if m.state == initializing {
return "Initializing..."
}
iv := lipgloss.NewStyle().Width(m.width / 2).Render(
m.inputContainer.View())
lv := m.logContainer.View()
return lipgloss.JoinHorizontal(lipgloss.Top, iv, lv)
}

func main() {
h := flag.String("host", "localhost", "server host")
port1 := flag.Int("http_port", 8000, "http port")
port2 := flag.Int("ws_port", 18000, "ws port")
flag.Parse()
request.Host = *h
request.HttpPort = *port1
request.WsPort = *port2
p := tea.NewProgram(Model{
inputContainer: ui.NewInputContainer(),
logContainer: ui.NewLogContainer(),
})
updateMsgChan := make(chan request.AppendLogMsg)
go request.ConnectWebSocket(updateMsgChan)
go func() {
for msg := range updateMsgChan {
p.Send(msg)
}
}()
p.Run()
}
60 changes: 60 additions & 0 deletions jbs-client/request/ws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package request

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"

"github.com/gorilla/websocket"
)

type AppendLogMsg string

var ws *websocket.Conn
var Host string
var WsPort int
var HttpPort int

func ConnectWebSocket(updateMsgChan chan<- AppendLogMsg) {
dial := websocket.Dialer{}
c, _, err := dial.Dial(fmt.Sprintf("ws://%s:%d", Host, WsPort), nil)
ws = c
if err != nil {
log.Panic("dial:", err)
}
defer c.Close()

for {
_, message, err := c.ReadMessage()
if err != nil {
log.Fatal("read:", err)
break
}
j := make(map[string]string)
json.Unmarshal(message, &j)

updateMsgChan <- AppendLogMsg(time.Now().Format("[2006-01-02 15:04:05]") + " " + j["content"])
}
}

func SendMessage(msg string) error {
return ws.WriteMessage(websocket.TextMessage, []byte(msg))
}

func Reset() string {
url := fmt.Sprintf("http://%s:%d/reset", Host, HttpPort)
resp, err := http.Get(url)
if err != nil {
log.Panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Panic(err)
}

return string(body)
}
Loading

0 comments on commit 616c1bd

Please sign in to comment.