MCPcopy Create free account
hub / github.com/Ridter/cf_workers_proxy / write

Function write

js/worker.js:89–140  ·  view source on GitHub ↗
(chunk, controller)

Source from the content-addressed store, hash-verified

87 // ws --> remote
88 readableWebSocketStream.pipeTo(new WritableStream({
89 async write(chunk, controller) {
90 if (isDns && udpStreamWrite) {
91 return udpStreamWrite(chunk);
92 }
93 if (remoteSocketWapper.value) {
94 const writer = remoteSocketWapper.value.writable.getWriter()
95 await writer.write(chunk);
96 writer.releaseLock();
97 return;
98 }
99
100 const {
101 hasError,
102 message,
103 portRemote = 443,
104 addressRemote = '',
105 rawDataIndex,
106 vlessVersion = new Uint8Array([0, 0]),
107 isUDP,
108 } = processVlessHeader(chunk, userID);
109 address = addressRemote;
110 portWithRandomLog = `${portRemote}--${Math.random()} ${isUDP ? 'udp ' : 'tcp '
111 } `;
112 if (hasError) {
113 // controller.error(message);
114 throw new Error(message); // cf seems has bug, controller.error will not end stream
115 // webSocket.close(1000, message);
116 return;
117 }
118 // if UDP but port not DNS port, close it
119 if (isUDP) {
120 if (portRemote === 53) {
121 isDns = true;
122 } else {
123 // controller.error('UDP proxy only enable for DNS which is port 53');
124 throw new Error('UDP proxy only enable for DNS which is port 53'); // cf seems has bug, controller.error will not end stream
125 return;
126 }
127 }
128 // ["version", "附加信息长度 N"]
129 const vlessResponseHeader = new Uint8Array([vlessVersion[0], 0]);
130 const rawClientData = chunk.slice(rawDataIndex);
131
132 // TODO: support udp here when cf runtime has udp support
133 if (isDns) {
134 const { write } = await handleUDPOutBound(webSocket, vlessResponseHeader, log);
135 udpStreamWrite = write;
136 udpStreamWrite(rawClientData);
137 return;
138 }
139 handleTCPOutBound(remoteSocketWapper, addressRemote, portRemote, rawClientData, webSocket, vlessResponseHeader, log);
140 },
141 close() {
142 log(`readableWebSocketStream is close`);
143 },

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