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

Function fetch

_worker.js:36–143  ·  view source on GitHub ↗

* @param {import("@cloudflare/workers-types").Request} request * @param {{UUID: string, PROXYIP: string, DNS_RESOLVER_URL: string, NODE_ID: int, API_HOST: string, API_TOKEN: string}} env * @param {import("@cloudflare/workers-types").ExecutionContext} ctx * @returns {Promise }

(request, env, ctx)

Source from the content-addressed store, hash-verified

34 * @returns {Promise<Response>}
35 */
36 async fetch(request, env, ctx) {
37 try {
38 userID = env.UUID || userID;
39 proxyIP = env.PROXYIP || proxyIP;
40 dohURL = env.DNS_RESOLVER_URL || dohURL;
41 nodeId = env.NODE_ID || nodeId;
42 apiToken = env.API_TOKEN || apiToken;
43 apiHost = env.API_HOST || apiHost;
44 let userID_Path = userID;
45 if (userID.includes(',')) {
46 userID_Path = userID.split(',')[0];
47 }
48 const upgradeHeader = request.headers.get('Upgrade');
49 if (!upgradeHeader || upgradeHeader !== 'websocket') {
50 const url = new URL(request.url);
51 switch (url.pathname) {
52 case '/cf':
53 return new Response(JSON.stringify(request.cf, null, 4), {
54 status: 200,
55 headers: {
56 "Content-Type": "application/json;charset=utf-8",
57 },
58 });
59 case '/connect': // for test connect to cf socket
60 const [hostname, port] = ['cloudflare.com', '80'];
61 console.log(`Connecting to ${hostname}:${port}...`);
62
63 try {
64 const socket = await connect({
65 hostname: hostname,
66 port: parseInt(port, 10),
67 });
68
69 const writer = socket.writable.getWriter();
70
71 try {
72 await writer.write(new TextEncoder().encode('GET / HTTP/1.1\r\nHost: ' + hostname + '\r\n\r\n'));
73 } catch (writeError) {
74 writer.releaseLock();
75 await socket.close();
76 return new Response(writeError.message, { status: 500 });
77 }
78
79 writer.releaseLock();
80
81 const reader = socket.readable.getReader();
82 let value;
83
84 try {
85 const result = await reader.read();
86 value = result.value;
87 } catch (readError) {
88 await reader.releaseLock();
89 await socket.close();
90 return new Response(readError.message, { status: 500 });
91 }
92
93 await reader.releaseLock();

Callers 3

getPlanetScaleConnectionFunction · 0.85
fetchApiResponseFunction · 0.85
writeFunction · 0.85

Calls 3

getVLESSConfigFunction · 0.85
createVLESSSubFunction · 0.85
vlessOverWSHandlerFunction · 0.85

Tested by

no test coverage detected