Skip to content

Commit 7bc2a87

Browse files
committed
Add hash to script tag
1 parent 837be0f commit 7bc2a87

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/main/java/com/lbry/globe/Main.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import io.netty.bootstrap.ServerBootstrap;
1212
import io.netty.channel.ChannelInitializer;
1313
import io.netty.channel.EventLoopGroup;
14-
import io.netty.channel.nio.NioEventLoopGroup;
14+
import io.netty.channel.MultiThreadIoEventLoopGroup;
15+
import io.netty.channel.nio.NioIoHandler;
1516
import io.netty.channel.socket.SocketChannel;
1617
import io.netty.channel.socket.nio.NioServerSocketChannel;
1718
import io.netty.handler.codec.http.HttpRequestDecoder;
@@ -34,7 +35,7 @@ public Main(String... args){
3435

3536
@Override
3637
public void run(){
37-
EventLoopGroup group = new NioEventLoopGroup();
38+
EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
3839
this.runTCPServerHTTP(group);
3940
}
4041

src/main/java/com/lbry/globe/handler/HTTPHandler.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.InputStream;
1515
import java.io.IOException;
1616
import java.net.URI;
17+
import java.security.MessageDigest;
1718
import java.util.*;
1819
import java.util.logging.Level;
1920
import java.util.logging.Logger;
@@ -27,6 +28,8 @@ public class HTTPHandler extends ChannelInboundHandlerAdapter{
2728
public static final AttributeKey<List<HttpContent>> ATTR_CONTENT = AttributeKey.newInstance("content");
2829
private static final Logger LOGGER = Logger.getLogger("Handler");
2930

31+
private static byte[] jsHash;
32+
3033
@Override
3134
public void channelRead(ChannelHandlerContext ctx,Object msg){
3235
if(msg instanceof HttpRequest){
@@ -62,7 +65,7 @@ private void handleResponse(ChannelHandlerContext ctx){
6265
status = 500;
6366
indexData = "Some error occured.".getBytes();
6467
}
65-
indexData = new String(indexData).replace("<div class=\"version\"></div>","<div class=\"version\">"+Main.class.getPackage().getImplementationVersion()+"</div>").getBytes();
68+
indexData = new String(indexData).replace("${GLOBE_JS_VERSION}",Base64.getEncoder().encodeToString(HTTPHandler.getJSHash()).replaceAll("=","")).replace("<div class=\"version\"></div>","<div class=\"version\">"+Main.class.getPackage().getImplementationVersion()+"</div>").getBytes();
6669
ByteBuf responseContent = Unpooled.copiedBuffer(indexData);
6770
FullHttpResponse response = new DefaultFullHttpResponse(request.protocolVersion(),HttpResponseStatus.valueOf(status),responseContent);
6871
response.headers().add("Content-Length",responseContent.capacity());
@@ -143,4 +146,14 @@ private static InputStream getResource(String name){
143146
return HTTPHandler.class.getClassLoader().getResourceAsStream(name);
144147
}
145148

149+
private static byte[] getJSHash(){
150+
if(HTTPHandler.jsHash==null){
151+
try{
152+
MessageDigest md = MessageDigest.getInstance("SHA-1");
153+
HTTPHandler.jsHash = md.digest(HTTPHandler.readResource(HTTPHandler.getResource("globe.js")));
154+
}catch(Exception ignored){}
155+
}
156+
return HTTPHandler.jsHash;
157+
}
158+
146159
}

src/main/resources/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<meta content="website" property="og:type" />
1111
<meta content="//globe.lbry.org" property="og:url" />
1212
<script src="//unpkg.com/globe.gl"></script>
13-
<script src="/globe.js"></script>
13+
<script src="/globe.js?v=${GLOBE_JS_VERSION}"></script>
1414
<title>LBRY Globe</title>
1515
</head>
1616
<body>

0 commit comments

Comments
 (0)