Spaces:
jiome
/
Running

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

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +17 -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);
@@ -76,6 +75,7 @@ const testAll = async (startIP, endIP) => {
76
 
77
  let count = 0;
78
  let stop = false;
 
79
  while (true) {
80
  while (count >= 16) {
81
  await new Promise((t) => { setTimeout(t, 100) });
@@ -91,13 +91,14 @@ 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 +124,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 +136,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 +159,4 @@ 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);
 
75
 
76
  let count = 0;
77
  let stop = false;
78
+ res.write("[\n"); // Start of the array
79
  while (true) {
80
  while (count >= 16) {
81
  await new Promise((t) => { setTimeout(t, 100) });
 
91
  break;
92
  }
93
  }
94
+ res.write("];\n"); // End of the array
95
+ res.end();
96
  }
97
 
98
  app.post("/test", async (req, res) => {
99
  const { startIP, endIP } = req.body;
100
+ res.setHeader('Content-Type', 'text/plain');
101
+ await testAll(startIP, endIP, res);
102
  });
103
 
104
  app.get("/", (req, res) => {
 
124
  <textarea id="results" rows="10" cols="50"></textarea>
125
  <br>
126
  <button id="copyButton">复制</button>
 
127
  <script>
128
  document.getElementById('ipForm').addEventListener('submit', async (event) => {
129
  event.preventDefault();
 
136
  },
137
  body: JSON.stringify({ startIP, endIP })
138
  });
139
+ const reader = response.body.getReader();
140
+ const decoder = new TextDecoder();
141
+ let results = '';
142
+ while (true) {
143
+ const { done, value } = await reader.read();
144
+ if (done) break;
145
+ results += decoder.decode(value);
146
+ document.getElementById('results').value = results;
147
+ }
148
  });
 
149
  document.getElementById('copyButton').addEventListener('click', () => {
150
  const results = document.getElementById('results');
151
  results.select();
 
159
 
160
  app.listen(port, () => {
161
  console.log(`Server is running at http://localhost:${port}`);
162
+ });