Spaces:
jiome
/
Sleeping

jiome commited on
Commit
21a2221
·
verified ·
1 Parent(s): 147aee6

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +14 -11
index.js CHANGED
@@ -38,8 +38,7 @@ const isipok = async (ip) => {
38
  return { ip, status: true, region: rg };
39
  }
40
 
41
- const testAll = async (startIP, endIP) => {
42
- const results = [];
43
  const [startI, startI0, startI1, startI2] = startIP.split('.').map(Number);
44
  const [endI, endI0, endI1, endI2] = endIP.split('.').map(Number);
45
 
@@ -66,7 +65,7 @@ const testAll = async (startIP, endIP) => {
66
  try {
67
  const result = await isipok(XForwardedForIP);
68
  if (result.status) {
69
- results.push([result.ip, result.region]);
70
  }
71
  } catch (error) {
72
  console.error(error);
@@ -91,13 +90,13 @@ const testAll = async (startIP, endIP) => {
91
  break;
92
  }
93
  }
94
- return results;
95
  }
96
 
97
  app.post("/test", async (req, res) => {
98
  const { startIP, endIP } = req.body;
99
- const results = await testAll(startIP, endIP);
100
- res.json(results);
101
  });
102
 
103
  app.get("/", (req, res) => {
@@ -123,7 +122,6 @@ app.get("/", (req, res) => {
123
  <textarea id="results" rows="10" cols="50"></textarea>
124
  <br>
125
  <button id="copyButton">复制</button>
126
-
127
  <script>
128
  document.getElementById('ipForm').addEventListener('submit', async (event) => {
129
  event.preventDefault();
@@ -136,10 +134,16 @@ app.get("/", (req, res) => {
136
  },
137
  body: JSON.stringify({ startIP, endIP })
138
  });
139
- const results = await response.json();
140
- document.getElementById('results').value = JSON.stringify(results, null, 0);
 
 
 
 
 
 
 
141
  });
142
-
143
  document.getElementById('copyButton').addEventListener('click', () => {
144
  const results = document.getElementById('results');
145
  results.select();
@@ -153,4 +157,3 @@ app.get("/", (req, res) => {
153
 
154
  app.listen(port, () => {
155
  console.log(`Server is running at http://localhost:${port}`);
156
- });
 
38
  return { ip, status: true, region: rg };
39
  }
40
 
41
+ const testAll = async (startIP, endIP, res) => {
 
42
  const [startI, startI0, startI1, startI2] = startIP.split('.').map(Number);
43
  const [endI, endI0, endI1, endI2] = endIP.split('.').map(Number);
44
 
 
65
  try {
66
  const result = await isipok(XForwardedForIP);
67
  if (result.status) {
68
+ res.write(JSON.stringify([result.ip, result.region]) + "\n");
69
  }
70
  } catch (error) {
71
  console.error(error);
 
90
  break;
91
  }
92
  }
93
+ res.end();
94
  }
95
 
96
  app.post("/test", async (req, res) => {
97
  const { startIP, endIP } = req.body;
98
+ res.setHeader('Content-Type', 'text/plain');
99
+ await testAll(startIP, endIP, res);
100
  });
101
 
102
  app.get("/", (req, res) => {
 
122
  <textarea id="results" rows="10" cols="50"></textarea>
123
  <br>
124
  <button id="copyButton">复制</button>
 
125
  <script>
126
  document.getElementById('ipForm').addEventListener('submit', async (event) => {
127
  event.preventDefault();
 
134
  },
135
  body: JSON.stringify({ startIP, endIP })
136
  });
137
+ const reader = response.body.getReader();
138
+ const decoder = new TextDecoder();
139
+ let results = '';
140
+ while (true) {
141
+ const { done, value } = await reader.read();
142
+ if (done) break;
143
+ results += decoder.decode(value);
144
+ document.getElementById('results').value = results;
145
+ }
146
  });
 
147
  document.getElementById('copyButton').addEventListener('click', () => {
148
  const results = document.getElementById('results');
149
  results.select();
 
157
 
158
  app.listen(port, () => {
159
  console.log(`Server is running at http://localhost:${port}`);