MCPcopy Create free account
hub / github.com/Ptechgithub/configs / write

Function write

worker.js:189–240  ·  view source on GitHub ↗
(chunk, controller)

Source from the content-addressed store, hash-verified

187 // ws --> remote
188 readableWebSocketStream.pipeTo(new WritableStream({
189 async write(chunk, controller) {
190 if (isDns && udpStreamWrite) {
191 return udpStreamWrite(chunk);
192 }
193 if (remoteSocketWapper.value) {
194 const writer = remoteSocketWapper.value.writable.getWriter()
195 await writer.write(chunk);
196 writer.releaseLock();
197 return;
198 }
199
200 const {
201 hasError,
202 message,
203 portRemote = 443,
204 addressRemote = '',
205 rawDataIndex,
206 vlessVersion = new Uint8Array([0, 0]),
207 isUDP,
208 } = processVlessHeader(chunk, userID);
209 address = addressRemote;
210 portWithRandomLog = `${portRemote} ${isUDP ? 'udp' : 'tcp'} `;
211 if (hasError) {
212 // controller.error(message);
213 throw new Error(message); // cf seems has bug, controller.error will not end stream
214 // webSocket.close(1000, message);
215 return;
216 }
217
218 // If UDP and not DNS port, close it
219 if (isUDP && portRemote !== 53) {
220 throw new Error('UDP proxy only enabled for DNS which is port 53');
221 // cf seems has bug, controller.error will not end stream
222 }
223
224 if (isUDP && portRemote === 53) {
225 isDns = true;
226 }
227
228 // ["version", "附加信息长度 N"]
229 const vlessResponseHeader = new Uint8Array([vlessVersion[0], 0]);
230 const rawClientData = chunk.slice(rawDataIndex);
231
232 // TODO: support udp here when cf runtime has udp support
233 if (isDns) {
234 const { write } = await handleUDPOutBound(webSocket, vlessResponseHeader, log);
235 udpStreamWrite = write;
236 udpStreamWrite(rawClientData);
237 return;
238 }
239 handleTCPOutBound(remoteSocketWapper, addressRemote, portRemote, rawClientData, webSocket, vlessResponseHeader, log);
240 },
241 close() {
242 log(`readableWebSocketStream is close`);
243 },

Callers

nothing calls this directly

Calls 5

processVlessHeaderFunction · 0.85
handleUDPOutBoundFunction · 0.85
handleTCPOutBoundFunction · 0.85
fetchFunction · 0.85
logFunction · 0.85

Tested by

no test coverage detected