Add hash to script tag

This commit is contained in:
Ben van Hartingsveldt 2025-07-11 19:20:12 +02:00
parent 837be0fd4e
commit 7bc2a8746c
No known key found for this signature in database
GPG key ID: 261AA214130CE7AB
3 changed files with 18 additions and 4 deletions

View file

@ -11,7 +11,8 @@ import com.lbry.globe.util.GeoIP;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.MultiThreadIoEventLoopGroup;
import io.netty.channel.nio.NioIoHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpRequestDecoder;
@ -34,7 +35,7 @@ public class Main implements Runnable{
@Override
public void run(){
EventLoopGroup group = new NioEventLoopGroup();
EventLoopGroup group = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
this.runTCPServerHTTP(group);
}

View file

@ -14,6 +14,7 @@ import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.URI;
import java.security.MessageDigest;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -27,6 +28,8 @@ public class HTTPHandler extends ChannelInboundHandlerAdapter{
public static final AttributeKey<List<HttpContent>> ATTR_CONTENT = AttributeKey.newInstance("content");
private static final Logger LOGGER = Logger.getLogger("Handler");
private static byte[] jsHash;
@Override
public void channelRead(ChannelHandlerContext ctx,Object msg){
if(msg instanceof HttpRequest){
@ -62,7 +65,7 @@ public class HTTPHandler extends ChannelInboundHandlerAdapter{
status = 500;
indexData = "Some error occured.".getBytes();
}
indexData = new String(indexData).replace("<div class=\"version\"></div>","<div class=\"version\">"+Main.class.getPackage().getImplementationVersion()+"</div>").getBytes();
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();
ByteBuf responseContent = Unpooled.copiedBuffer(indexData);
FullHttpResponse response = new DefaultFullHttpResponse(request.protocolVersion(),HttpResponseStatus.valueOf(status),responseContent);
response.headers().add("Content-Length",responseContent.capacity());
@ -143,4 +146,14 @@ public class HTTPHandler extends ChannelInboundHandlerAdapter{
return HTTPHandler.class.getClassLoader().getResourceAsStream(name);
}
private static byte[] getJSHash(){
if(HTTPHandler.jsHash==null){
try{
MessageDigest md = MessageDigest.getInstance("SHA-1");
HTTPHandler.jsHash = md.digest(HTTPHandler.readResource(HTTPHandler.getResource("globe.js")));
}catch(Exception ignored){}
}
return HTTPHandler.jsHash;
}
}

View file

@ -10,7 +10,7 @@
<meta content="website" property="og:type" />
<meta content="//globe.lbry.org" property="og:url" />
<script src="//unpkg.com/globe.gl"></script>
<script src="/globe.js"></script>
<script src="/globe.js?v=${GLOBE_JS_VERSION}"></script>
<title>LBRY Globe</title>
</head>
<body>