背景介绍:最近在弄毕设,需要画一些图。因为是使用git管理代码的,所以在里面直接放二进制的图片文件不方便,想着放格式化后的svg。可是用processOn画图每次都得导出手动格式化,太麻烦,就弄了个processOn的离线桌面版,编辑图形之后自动保存且导出svg到指定目录。
代码就不放了,直接说原理,自己弄一个很快的
其实很简单,就是缓存所有相关的http请求,然后重放即可
细节:
这里吹一下java的netty,內建支持http的解析,写起来很快。
核心代码也就几行,知道意思就行,getdef需要将本地文件替换响应体
@Override
public void beforeRequest(Channel clientChannel, HttpRequest httpRequest, HttpProxyInterceptPipeline pipeline) throws Exception {
re=httpRequest;
if(responseMap.containsKey(re.uri())){
log.info("usage cache for {}",re.uri());
//直接返回
clientChannel.writeAndFlush(responseMap.get(re.uri()).copy());
return ;
}else{
if(re.uri().startsWith("/getdef")){
if(responseMap.containsKey("getdef")){
//
clientChannel.writeAndFlush(getDefine());
return ;
}
}
}
//获取原始数据
handleProxyData(clientChannel, httpRequest,true);
}
@Override
public void afterResponse(Channel clientChannel, Channel proxyChannel, HttpResponse httpResponse,
HttpProxyInterceptPipeline pipeline)throws Exception {
if(httpResponseinstanceof FullHttpResponse){
//协作请求
if(!re.uri().startsWith("/diagraming/poll")){
//复制响应体
FullHttpResponse duplicate = ((FullHttpResponse) httpResponse).copy();
//cache it
if(!responseMap.containsKey(re.uri())){
log.info("caching : {}",re.uri());
if(re.uri().startsWith("/getdef")){
responseMap.put("getdef",duplicate);
}else{
responseMap.put(re.uri(),duplicate);
synchronized (responseMap){
ObjectOutputStream objectOutputStream =new ObjectOutputStream(new FileOutputStream("./data.map"));
objectOutputStream.writeObject(responseMap);
objectOutputStream.flush();
objectOutputStream.close();
}
}
}else {
log.error("the url:{} is cached,but get the response",re.uri());
}
}
}
clientChannel.writeAndFlush(httpResponse);
然后有个需要注意的点,就是processon启用了https,所以在代理服务器其实是看不到明文的.这样是为了安全,防止第三方(代理服务器)窃取机密,可是现在我们既是接收者也是第三方, 所以再严密的防盗门也防不住我们直接拿钥匙开门。这里再夸一下netty,带ssl支持,只需要自己弄个证书,再在浏览器中设置信任就可以,具体方法搜索就有。
接下来就是客户端了,也很简单,弄个web控件,代理配置到自己搭的伪代理。我使用了javafx的webView,其实其他的也可以,主要是它的javascript和java交互比较方便。然后hook几个javascript事件即可
注意的两个点:
1.导出svg的话,有个函数exportSVG调用即可:)
2.保存的话,processon在离线状态下会把数据写到Local Storage,所以:)
缺点:
上传图片什么的还得再看看,准备用spring搭个伪服务器模仿一下即可
协作什么的就不弄了,离线版哪来的协作hhh