@hermenics/deepseek-code 0.13.3 → 0.13.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser-egress-worker.mjs +12 -12
- package/dist/cli.mjs +2588 -1306
- package/dist/runtime.mjs +3 -3
- package/dist/shell-supervisor.mjs +19 -20
- package/package.json +1 -1
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import{createSecureContext as
|
|
1
|
+
import{createSecureContext as re}from"node:tls";import{createServer as U,request as G}from"node:http";import{request as X}from"node:https";import{TLSSocket as V}from"node:tls";import{connect as J,createServer as Y,isIP as P}from"node:net";import{randomBytes as C,timingSafeEqual as _}from"node:crypto";import{mkdtempSync as Q,rmSync as Z}from"node:fs";import{tmpdir as ee}from"node:os";import{join as b}from"node:path";import{lookup as F}from"dns/promises";import{isIP as k}from"node:net";import{BlockList as H,isIP as N}from"node:net";var O=20000,se=O*4;var z=[["0.0.0.0",8],["10.0.0.0",8],["100.64.0.0",10],["127.0.0.0",8],["169.254.0.0",16],["172.16.0.0",12],["192.0.0.0",24],["192.0.2.0",24],["192.31.196.0",24],["192.52.193.0",24],["192.88.99.0",24],["192.168.0.0",16],["192.175.48.0",24],["198.18.0.0",15],["198.51.100.0",24],["203.0.113.0",24],["224.0.0.0",4],["240.0.0.0",4]],W=[["::",96],["::1",128],["::ffff:0:0",96],["64:ff9b::",96],["64:ff9b:1::",48],["100::",64],["2001::",23],["2001:db8::",32],["2002::",16],["3fff::",20],["fc00::",7],["fe80::",10],["ff00::",8]];function R(t,e){let o=new H;for(let[n,r]of t)o.addSubnet(n,r,e);return o}var j=R(z,"ipv4"),q=R(W,"ipv6");function w(t){let e=N(t);if(e===4)return j.check(t,"ipv4");if(e===6&&/^::ffff:[0-9a-f]{1,4}:[0-9a-f]{1,4}$/i.test(t))return!0;if(e===6)return q.check(t,"ipv6");return!0}var K=2000,S=2048,h=async(t)=>{let e;try{return(await Promise.race([F(t,{all:!0}),new Promise((n,r)=>{e=setTimeout(()=>r(Error("DNS timeout")),K)})])).map((n)=>n.address)}finally{clearTimeout(e)}};function L(t){return t.replace(/^\[|\]$/g,"").replace(/\.$/,"").toLowerCase()}function v(t){let e=L(t);if(e==="localhost"||e.endsWith(".localhost"))return!0;if(k(e)===4)return e.startsWith("127.");return e==="::1"}async function x(t,e=h){if(t==="about:blank")return{kind:"loopback",origin:"about:blank"};if(t.length>S)return{kind:"blocked",reason:`URL longer than ${S} characters`};let o;try{o=new URL(t)}catch{return{kind:"blocked",reason:"not a valid URL"}}if(o.protocol!=="http:"&&o.protocol!=="https:")return{kind:"blocked",reason:`${o.protocol} URLs are not allowed; use http or https`};if(o.username||o.password)return{kind:"blocked",reason:"URLs with embedded credentials are not allowed"};let n=o.origin,r=L(o.hostname);if(v(r))return{kind:"loopback",origin:n};if(r==="metadata.google.internal")return{kind:"blocked",origin:n,reason:"cloud metadata endpoints are blocked"};if(k(r))return w(r)?{kind:"blocked",origin:n,reason:`${r} is not a public address`}:{kind:"public",origin:n};let i;try{i=await e(r)}catch{return{kind:"blocked",origin:n,reason:`could not resolve ${r}`}}if(i.length===0)return{kind:"blocked",origin:n,reason:`could not resolve ${r}`};let s=i.find((a)=>w(a));return s?{kind:"blocked",origin:n,reason:`${r} resolves to non-public address ${s}`}:{kind:"public",origin:n}}var te=["connection","proxy-connection","proxy-authorization","proxy-authenticate","keep-alive","te","trailer","transfer-encoding","upgrade"];function T(t){let e={...t},o=String(t.connection??"").split(",").map((n)=>n.trim().toLowerCase());for(let n of[...te,...o])delete e[n];return e}function I(t){return t.replace(/^\[|\]$/g,"").replace(/\.$/,"")}class f{certificate;authorize;resolve;certificateAuthority;username="codimium";password=C(32).toString("hex");realm=C(16).toString("hex");authorization=Buffer.from(`Basic ${Buffer.from(`${this.username}:${this.password}`).toString("base64")}`);sockets=new Set;tunnel=new WeakMap;authenticated=new WeakSet;server=U();inner=U();innerTls=Y((t)=>this.acceptTls(t));privateDirectory=Q(b(ee(),"deepseek-egress-tls-"));stopped=!1;_url="";constructor(t,e,o,n){this.certificate=t;this.authorize=e;this.resolve=o;this.certificateAuthority=n;this.inner.setTimeout(30000);for(let[r,i]of[[this.server,!1],[this.inner,!0]])r.maxHeadersCount=100,r.headersTimeout=15000,r.requestTimeout=30000,r.on("request",(s,a)=>{this.forward(s,a,i).catch(()=>{if(!a.headersSent)a.writeHead(502);a.end()})}),r.on("upgrade",(s,a,c)=>{this.upgrade(s,a,c,i).catch(()=>a.destroy())}),r.on("connection",(s)=>this.track(s)),r.on("clientError",(s,a)=>a.destroy());this.server.on("connect",(r,i,s)=>{this.connect(r,i,s).catch(()=>i.destroy())})}static async start(t,e,o=h,n){let r=new f(t,e,o,n);try{await new Promise((s,a)=>{r.innerTls.once("error",a),r.innerTls.listen(b(r.privateDirectory,"tls"),()=>{r.innerTls.off("error",a),s()})}),await new Promise((s,a)=>{r.server.once("error",a),r.server.listen(0,"127.0.0.1",()=>{r.server.off("error",a),s()})});let i=r.server.address();if(!i||typeof i==="string")throw r.stop(),Error("Codimium network protection did not bind");return r._url=`http://127.0.0.1:${i.port}`,r}catch(i){throw r.stop(),i}}get url(){return this._url}get cleanupDirectory(){return this.privateDirectory}launchOptions(){return{proxyServer:this.url,proxyBypassList:"<-loopback>",certificateSPKI:this.certificate.spki}}stop(){if(this.stopped)return;this.stopped=!0,this.server.close(),this.innerTls.close();for(let t of this.sockets)t.destroy();this.sockets.clear(),Z(this.privateDirectory,{recursive:!0,force:!0})}track(t){this.sockets.add(t),t.once("close",()=>this.sockets.delete(t)),t.on("error",()=>t.destroy())}hasAuth(t,e=!1){if(e&&this.authenticated.has(t.socket))return!0;let o=Buffer.from(String(t.headers["proxy-authorization"]??""));return o.length===this.authorization.length&&_(o,this.authorization)}challenge(t){t.end(`HTTP/1.1 407 Proxy Authentication Required\r
|
|
2
2
|
Proxy-Authenticate: Basic realm="${this.realm}"\r
|
|
3
3
|
Connection: close\r
|
|
4
4
|
Content-Length: 0\r
|
|
5
5
|
\r
|
|
6
|
-
`)}destination(
|
|
6
|
+
`)}destination(t){let e=this.tunnel.get(t.socket),o=e?new URL(t.url??"/",e):new URL(t.url??"");if(!["http:","https:","ws:","wss:"].includes(o.protocol)||o.username||o.password)throw Error("Unsupported proxy URL");if(e&&(o.host!==e.host||o.protocol!==e.protocol))throw Error("Tunnel authority changed");if(e&&new URL(`${e.protocol}//${t.headers.host??""}`).host!==e.host)throw Error("Tunnel Host changed");return o}async endpoint(t){let e=new URL(t);e.protocol=t.protocol==="ws:"?"http:":t.protocol==="wss:"?"https:":t.protocol;let o=[];if((await x(e.href,async(a)=>(o=await this.resolve(a),o))).kind==="blocked")throw Error("Blocked egress address");let r=I(t.hostname);if(P(r))o=[r];else if(v(r))o=["127.0.0.1"];if(!o.length)throw Error("No verified egress address");let i=o[0],s=P(i);if(!s||i==="127.0.0.1"&&Number(t.port)===Number(new URL(this.url).port))throw Error("Invalid or recursive egress address");return{address:i,family:s}}async connect(t,e,o){if(this.stopped){e.destroy();return}if(!this.hasAuth(t)){this.challenge(e);return}let n=new URL(`https://${t.url}`);if(n.username||n.password||n.pathname!=="/"||n.search||n.hash)throw Error("Invalid CONNECT authority");if(await this.endpoint(n),this.stopped){e.destroy();return}e.write(`HTTP/1.1 200 Connection Established\r
|
|
7
7
|
\r
|
|
8
|
-
`),
|
|
9
|
-
`));let
|
|
10
|
-
`.startsWith(
|
|
11
|
-
`)return
|
|
12
|
-
`))return;
|
|
13
|
-
`);let
|
|
8
|
+
`),e.setTimeout(1e4,()=>e.destroy());let r=(i)=>{if(e.pause(),e.setTimeout(0),i[0]===22){let s=J(b(this.privateDirectory,"tls"));this.track(s),s.once("connect",()=>s.write(JSON.stringify({password:this.password,target:n.href})+`
|
|
9
|
+
`));let a="",c=(l)=>{if(a+=l.toString(),!`OK
|
|
10
|
+
`.startsWith(a)){s.destroy();return}if(a!==`OK
|
|
11
|
+
`)return;s.off("data",c),s.write(i),e.pipe(s),s.pipe(e)};s.on("data",c),e.once("close",()=>s.destroy()),s.once("close",()=>e.destroy())}else{let s=new URL(`http://${t.url}`);this.authenticated.add(e),this.tunnel.set(e,s),e.unshift(i),this.inner.emit("connection",e),e.resume()}};if(o.length)r(o);else e.once("data",r),e.resume()}acceptTls(t){this.track(t),t.setTimeout(5000,()=>t.destroy());let e="",o=(n)=>{if(e+=n.toString(),e.length>4096){t.destroy();return}if(!e.endsWith(`
|
|
12
|
+
`))return;t.off("data",o);try{let r=JSON.parse(e),i=Buffer.from(r.password??""),s=Buffer.from(this.password);if(i.length!==s.length||!_(i,s)||typeof r.target!=="string")throw Error("Unowned TLS bridge");let a=new URL(r.target);if(a.protocol!=="https:"||a.username||a.password)throw Error("Invalid TLS authority");t.pause(),t.write(`OK
|
|
13
|
+
`);let c=new V(t,{isServer:!0,secureContext:this.certificate.context,ALPNProtocols:["http/1.1"]});this.track(c),this.authenticated.add(c),this.tunnel.set(c,a),c.setTimeout(15000,()=>c.destroy()),c.once("secure",()=>c.setTimeout(30000)),this.inner.emit("connection",c),t.resume()}catch{t.destroy()}};t.on("data",o)}async forward(t,e,o){if(this.stopped){e.writeHead(503),e.end();return}if(!this.hasAuth(t,o)){e.writeHead(407,{"proxy-authenticate":`Basic realm="${this.realm}"`}),e.end();return}let n=this.destination(t);if(!["http:","https:"].includes(n.protocol)){e.writeHead(400),e.end();return}let r=await this.endpoint(n);if(!await this.authorize(n.href)||this.stopped){e.writeHead(403),e.end();return}let i=this.outgoing(t,n,r,!1);i.on("response",(s)=>{e.writeHead(s.statusCode??502,T(s.headers)),s.on("error",()=>e.destroy()),s.pipe(e)}),i.on("error",()=>{if(!e.headersSent)e.writeHead(502);e.end()}),t.on("aborted",()=>i.destroy()),e.on("close",()=>i.destroy()),t.pipe(i)}outgoing(t,e,o,n){let r=T(t.headers);if(r.host=e.host,n)r.connection="Upgrade",r.upgrade="websocket";let i=["https:","wss:"].includes(e.protocol),a=(i?X:G)({hostname:I(e.hostname),port:e.port||(i?443:80),path:e.pathname+e.search,method:t.method,headers:r,agent:!1,rejectUnauthorized:!0,ca:this.certificateAuthority,lookup:(c,l,p)=>{if(l.all)p(null,[o]);else p(null,o.address,o.family)}});return a.setTimeout(30000,()=>a.destroy()),a}async upgrade(t,e,o,n){if(this.stopped){e.destroy();return}if(!this.hasAuth(t,n)){this.challenge(e);return}if(String(t.headers.upgrade).toLowerCase()!=="websocket")throw Error("Unsupported upgrade");let r=this.destination(t),i=t.headers.origin;if(typeof i!=="string"||!await this.authorize(r.href,i)){e.end(`HTTP/1.1 403 Forbidden\r
|
|
14
14
|
Connection: close\r
|
|
15
15
|
Content-Length: 0\r
|
|
16
16
|
\r
|
|
17
|
-
`);return}let
|
|
18
|
-
${
|
|
17
|
+
`);return}let s=await this.endpoint(r);if(this.stopped){e.destroy();return}let a=this.outgoing(t,r,s,!0);a.on("upgrade",(c,l,p)=>{if(a.setTimeout(0),l.setTimeout(0),e.setTimeout(0),this.track(l),this.stopped||e.destroyed){l.destroy();return}let m=T(c.headers);m.connection="Upgrade",m.upgrade="websocket";let A=Object.entries(m).flatMap(([M,g])=>(Array.isArray(g)?g:[g]).filter((y)=>y!==void 0).map((y)=>`${M}: ${y}`));if(e.write(`HTTP/1.1 101 Switching Protocols\r
|
|
18
|
+
${A.join(`\r
|
|
19
19
|
`)}\r
|
|
20
20
|
\r
|
|
21
|
-
`),
|
|
21
|
+
`),p.length)e.write(p);if(o.length)l.write(o);e.once("close",()=>l.destroy()),l.once("close",()=>e.destroy()),l.pipe(e),e.pipe(l)}),a.on("response",(c)=>{c.resume(),e.end(`HTTP/1.1 502 Bad Gateway\r
|
|
22
22
|
Connection: close\r
|
|
23
23
|
Content-Length: 0\r
|
|
24
24
|
\r
|
|
25
|
-
`)}),
|
|
25
|
+
`)}),a.on("error",()=>e.destroy()),e.once("close",()=>a.destroy()),a.end()}}var d,B=!1,oe=0,u=new Map;function D(t,e){if(!process.connected||u.size>=512)return Promise.reject(Error("Network owner unavailable"));let o=++oe;return new Promise((n,r)=>{let i=setTimeout(()=>{u.delete(o),r(Error("Network owner timed out"))},5000);u.set(o,{done(s){clearTimeout(i),n(s)},fail(){clearTimeout(i),r(Error("Network owner disconnected"))}}),process.send({type:t,id:o,...e})})}function E(){d?.stop();for(let t of u.values())t.fail();u.clear(),process.exit(0)}process.on("disconnect",E);process.on("SIGTERM",E);process.on("SIGINT",E);process.on("message",(t)=>{let e=t;if(e.type==="reply"&&typeof e.id==="number"){let o=u.get(e.id);u.delete(e.id),o?.done(e.value);return}if(e.type!=="init"||B)return;B=!0,(async()=>{if(!e.key||!e.cert||!e.spki)throw Error("Missing private network identity");let o={key:e.key,cert:e.cert,spki:e.spki,context:re({key:e.key,cert:e.cert})};d=await f.start(o,async(n,r)=>await D("authorize",{url:n,origin:r})===!0,e.customResolver?async(n)=>{let r=await D("resolve",{host:n});if(!Array.isArray(r)||!r.every((i)=>typeof i==="string"))throw Error("Invalid resolver result");return r}:h,e.certificateAuthority),process.send({type:"ready",url:d.url,username:d.username,password:d.password,realm:d.realm,cleanupDirectory:d.cleanupDirectory})})().catch(()=>{process.send?.({type:"error"}),d?.stop(),process.exit(1)})});
|