MCPcopy Index your code
hub / github.com/Ptechgithub/pp-worker / write

Function write

_worker.js:192–243  ·  view source on GitHub ↗
(chunk, controller)

Source from the content-addressed store, hash-verified

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

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