Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #52 from jianjianai/dev
Browse files Browse the repository at this point in the history
修复java服务端验证码无法验证的bug
  • Loading branch information
jianjianai authored May 22, 2023
2 parents 14d297b + b8d89e4 commit 28aa138
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 8 deletions.
43 changes: 43 additions & 0 deletions ServerConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"h1": "NewBingGoGo",
"h2": "简单开始和NewBing聊天",
"p":"",
"firstMessages":[
"好的,我已清理好板子,可以重新开始了。我可以帮助你探索什么?",
"明白了,我已经抹去了过去,专注于现在。我们现在应该探索什么?",
"重新开始总是很棒。问我任何问题!",
"好了,我已经为新的对话重置了我的大脑。你现在想聊些什么?",
"很好,让我们来更改主题。你在想什么?",
"谢谢你帮我理清头绪! 我现在能帮你做什么?",
"没问题,很高兴你喜欢上一次对话。让我们转到一个新主题。你想要了解有关哪些内容的详细信息?",
"谢谢你! 知道你什么时候准备好继续前进总是很有帮助的。我现在能为你回答什么问题?",
"当然,我已准备好进行新的挑战。我现在可以为你做什么?"
],
"firstProposes":[
"教我一个新单词",
"我需要有关家庭作业的帮助",
"我想学习一项新技能",
"最深的海洋是哪个?",
"一年有多少小时?",
"宇宙是如何开始的?",
"寻找非虚构作品",
"火烈鸟为何为粉色?",
"有什么新闻?",
"让我大笑",
"给我看鼓舞人心的名言",
"世界上最小的哺乳动物是什么?",
"向我显示食谱",
"最深的海洋是哪个?",
"为什么人类需要睡眠?",
"教我有关登月的信息",
"我想学习一项新技能",
"如何创建预算?",
"给我说个笑话",
"全息影像的工作原理是什么?",
"如何设定可实现的目标?",
"金字塔是如何建成的?",
"激励我!",
"宇宙是如何开始的?",
"如何制作蛋糕?"
]
}
16 changes: 15 additions & 1 deletion src/main/java/cn/jja8/newbinggogo/NewBingGoGoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.BiConsumer;

public class NewBingGoGoServer extends NanoWSD {
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
Expand All @@ -28,7 +29,7 @@ public static void main(String[] args) {
.load(new File("Cookies.yml"))
.as(Config.class)
.save(new File("Cookies.yml"));

WebWork.init();
//启动
try{
int porint = Integer.parseInt(args[0]);
Expand Down Expand Up @@ -255,6 +256,8 @@ public static Response goUrl(IHTTPSession session, String stringUrl, Map<String,
return getReturnError("此魔法链接服务器请求被bing拒绝!请稍后再试。错误代码:"+code,null,false);
}

Map<String, java.util.List<String>> responseHeader = urlConnection.getHeaderFields();

//将数据全部读取然后关闭流和链接
int len = urlConnection.getContentLength();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(Math.max(len, 0));
Expand All @@ -280,6 +283,17 @@ public static Response goUrl(IHTTPSession session, String stringUrl, Map<String,
byteArrayInputStream,
len
);
//保留返回头
responseHeader.forEach((s, strings) -> {
if(s!=null
&& !s.equals("Content-Length")
&& !s.equals("Content-Type")
){
for (String string : strings) {
response.addHeader(s, string);
}
}
});
response.addHeader("cookieID", String.valueOf(cookieID));
return response;
}
Expand Down
55 changes: 48 additions & 7 deletions src/main/java/cn/jja8/newbinggogo/WebWork.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,38 @@

import fi.iki.elonen.NanoHTTPD;

import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URL;

public class WebWork {
static File serverConfig;
static void init(){
File serverConfig = new File("ServerConfig.json");
try {
URL url = WebWork.class.getClassLoader().getResource("web/resource/config.json");
if(url!=null && serverConfig.createNewFile()){
try (InputStream in = url.openStream()){
try(OutputStream out = new FileOutputStream(serverConfig)){
byte[] buff = new byte[1024];
int len;
while ((len = in.read(buff))>=0){
out.write(buff,0,len);
}
}
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
if (serverConfig.isFile()&&serverConfig.canRead()) {
WebWork.serverConfig = serverConfig;
}
}
public static NanoHTTPD.Response getFile(String path) {
if(path.startsWith("/")){
path = path.substring(1);
}
URL url = WebWork.class.getClassLoader().getResource(path);
if(url==null){
return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND,"text/html; charset=utf-8","页面不存在!");
}

//文件属性
String mimeType = "text/html; charset=utf-8";
if(path.endsWith(".html")){
Expand All @@ -28,8 +47,30 @@ public static NanoHTTPD.Response getFile(String path) {
} else if (path.endsWith(".ico")) {
mimeType = "image/png";
}


InputStream inputStream;
if(serverConfig!=null && path.equals("web/resource/config.json")){
try {
NanoHTTPD.Response response = NanoHTTPD.newFixedLengthResponse(
NanoHTTPD.Response.Status.OK,
mimeType,
new FileInputStream(serverConfig),
-1
);
response.addHeader("cache-control","max-age=14400");
return response;
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}

URL url = WebWork.class.getClassLoader().getResource(path);
if(url==null){
return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND,"text/html; charset=utf-8","页面不存在!");
}
try{
InputStream inputStream = url.openStream();
inputStream = url.openStream();
NanoHTTPD.Response response = NanoHTTPD.newFixedLengthResponse(
NanoHTTPD.Response.Status.OK,
mimeType,
Expand Down

0 comments on commit 28aa138

Please sign in to comment.