UNPKG

site

Version:

Site Engine is the world's first all-in-one engine to build a modern web site, game, or blog. No HTML or CSS knowledge required.

1 lines 11.4 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./shared.manifest"),t=m(require("http")),r=m(require("https")),s=(m(require("crypto")),m(require("url"))),i=m(require("fs")),n=m(require("path")),o=m(require("bluebird")),a=m(require("./shared.main")),l=f(require("./shared.log")),u=f(require("./server.common-responses")),h=m(require("./server.request")),d=m(require("./server.response")),c=m(require("./server.o-auth")),p=m(require("ws"));function f(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t.default=e,t}function m(e){return e&&e.__esModule?e:{default:e}}var y,v,g,S,x,q,w,b,k,O,P,A,E,H,T,D=function(e){return null!=e},R=function(e,t,r,s){return e(t)[r](t,...s)},U=function(e){var t=e.call(this);return new Promise(function(e,r){!function s(i,n){try{var o=t[i](n),a=o.value}catch(e){return void r(e)}o.done?e(a):Promise.resolve(a).then(function(e){s("next",e)},function(e){s("throw",e)})}("next")})};y=!1,v=null,g=null,x=Symbol(),S=class{constructor(t){(t=>(()=>{this[x]=t,this.onOpen=(0,e.eventField)(),this.onClose=(0,e.eventField)(),this.onMessage=(0,e.eventField)(),this.onError=(0,e.eventField)()})())(t)}close(){return(()=>(()=>this[x].close())())()}forceClose(){return(()=>(()=>this[x].terminate())())()}send(e){return(e=>(()=>this[x].send(e))())(e)}},exports.default=(q=Symbol(),w=Symbol(),b=Symbol(),k=Symbol(),O=Symbol(),P=Symbol(),A=Symbol(),E=Symbol(),H=Symbol(),(T=class{static get password(){return(()=>{return(()=>this[q])()})()}static set password(t){return(()=>{var t=arguments;return(()=>{(0,e.assert)(null===t[0]||-1!==t[0].search(new RegExp("^[A-Za-z0-9]{1,50}$"))),this[q]=t[0]})()})()}static start(){return(()=>{return(()=>new Promise((o,a)=>(()=>{var a,h,d,c;return y?(()=>o())():(()=>(y=!0,R(e.dotDot_573300145710716007,this.onRequest.getListeners(),"isEmpty",[])&&(()=>this.onRequest(e=>(()=>e.response.handleFile())()))(),a=(t=>(()=>(r,n)=>(()=>{var o,a,h;return this.inInternalTest&&(()=>(()=>{try{return(()=>{return void 0!==this[w]&&null!==this[w]||(this[w]=i.default.readFileSync("test-password.txt","utf8").trim()),(0,e.assert)(-1!==this[w].search(new RegExp("^[A-Za-z0-9]{6,50}$"))),(0,e.assert)(r.headers["internal-test-password-705357537593286848"]===this[w])})()}catch(e){return(()=>(console.error("Internal test validation error."),process.exit(1)))()}})())(),o=this[b](t,r),a=Buffer.alloc(0),h=!1,r.on("data",e=>(()=>h?void 0:(()=>(a=Buffer.concat([a,e]),D(this.maxRequestBodySize)&&a.length>this.maxRequestBodySize?(()=>(h=!0,r.socket.destroy()))():void 0))())()),r.on("end",()=>{return U.call(this,function*(){var t,i,d,c;return yield U.call(this,function*(){try{return yield U.call(this,function*(){return h?void 0:yield U.call(this,function*(){return l.info({method:r.method,pathAndQuery:r.url.substr(0,1e3),authority:r.headers.host,scheme:o,clientIp:(D(r.headers["x-forwarded-for"])&&r.headers["x-forwarded-for"].length>0?yield U.call(this,function*(){return r.headers["x-forwarded-for"].split(",").map(e=>(()=>e.trim())())}):yield U.call(this,function*(){return[]})).concat([void 0===(c=r.socket.remoteAddress)?null:c])},"_server-http-request"),this[k](o,r),t=s.default.parse(r.url).pathname,i=t.match(new RegExp("^/\\.password/(.*)$")),D(i)&&(yield U.call(this,function*(){i=i[1]})),D(i)?yield U.call(this,function*(){return(0,e.assert)("https"===o),(0,e.assert)(-1!==i.search(new RegExp("^[A-Za-z0-9]{0,50}$"))),u.sendNewPasswordOk(i,n)}):yield U.call(this,function*(){return-1!==t.search(new RegExp("^/\\.browse$"))?yield U.call(this,function*(){return(0,e.assert)("https"===o),u.sendBrowsePage(n)}):yield U.call(this,function*(){return this[O](o,r,!1),yield this[P](a,o,r,n)})})})})}catch(e){return d=e,yield U.call(this,function*(){return D(d)&&"string"==typeof d.message?yield U.call(this,function*(){return d.message.startsWith("PWD:")?yield U.call(this,function*(){return u.sendPasswordMismatchError(n)}):yield U.call(this,function*(){return u.send500(n)})}):yield U.call(this,function*(){return u.send500(n)})})}})})})})())()),h=0,this.httpEnabled&&(h+=1),this.httpsEnabled&&(h+=1),0===h?(()=>o())():(()=>(d=0,this.httpEnabled&&(()=>((v=t.default.createServer(a("http"))).keepAliveTimeout=72e5,this[A]("http",v),D(this.httpUnixDomainSocket)?(()=>(i.default.existsSync(this.httpUnixDomainSocket)&&i.default.statSync(this.httpUnixDomainSocket).isSocket()&&(()=>i.default.unlinkSync(this.httpUnixDomainSocket))(),v.listen(this.httpUnixDomainSocket,()=>{return(()=>{var e;return(d+=1)===h&&(()=>o())(),(e=v.address()).startsWith("/")||(e=n.default.resolve(e)),l.info('HTTP started at Unix domain socket "'+e+'".')})()})))():(()=>v.listen(this.httpPort,this.host,()=>{return(()=>{var e;return(d+=1)===h&&(()=>o())(),e=v.address(),l.info('HTTP started at "'+e.address+'" ('+e.family+") port "+e.port+".")})()}))()))(),this.httpsEnabled?(()=>(c=R(e.dotDot_573300145710716007,Object,"clone",[this.httpsCredential]),D(this.httpsCredentialFunction)&&(()=>{c.SNICallback=(e=>(()=>crypto.createCredentials(this.httpsCredentialFunction(e)).context)())})(),(g=r.default.createServer(c,a("https"))).keepAliveTimeout=72e5,this[A]("https",g),D(this.httpsUnixDomainSocket)?(()=>(i.default.existsSync(this.httpsUnixDomainSocket)&&i.default.statSync(this.httpsUnixDomainSocket).isSocket()&&(()=>i.default.unlinkSync(this.httpsUnixDomainSocket))(),g.listen(this.httpsUnixDomainSocket,()=>{return(()=>{var e;return(d+=1)===h&&(()=>o())(),(e=g.address()).startsWith("/")||(e=n.default.resolve(e)),l.info('HTTPS started at Unix domain socket "'+e+'".')})()})))():(()=>g.listen(this.httpsPort,this.host,()=>{return(()=>{var e;return(d+=1)===h&&(()=>o())(),e=g.address(),l.info('HTTPS started at "'+e.address+'" ('+e.family+") port "+e.port+".")})()}))()))():void 0))()))()})()))()})()}static stop(){return(()=>{return(()=>new Promise((e,t)=>(()=>{var t,r;return y?(()=>(t=0,this.httpEnabled&&(t+=1),this.httpsEnabled&&(t+=1),0===t?(()=>e())():(()=>(r=0,D(v)&&(v.close(()=>(()=>((r+=1)===t&&(()=>e())(),l.info("HTTP stopped.")))()),v=null),D(g)?(g.close(()=>(()=>((r+=1)===t&&(()=>e())(),l.info("HTTPS stopped.")))()),void(g=null)):void 0))()))():(()=>e())()})()))()})()}static[P](e,t,r,s){return((e,t,r,s)=>U.call(this,function*(){var i;return i={},new h.default(["GET","HEAD"].indexOf(r.method)>=0?yield U.call(this,function*(){}):yield U.call(this,function*(){return"application/json"===r.headers["content-type"]?yield U.call(this,function*(){return JSON.parse(e.toString())}):yield U.call(this,function*(){return new Uint8Array(e)})}),t,r,i),new d.default(s,i),yield this.onRequest.asyncFire(i)}))(e,t,r,s)}static[E](e,t,r){return((e,t,r)=>(()=>(this[k](e,t),this[O](e,t,r)))())(e,t,r)}static[k](t,r){return((t,r)=>(()=>((0,e.assert)(D(t)),["GET","HEAD","POST"].indexOf(r.method)<0&&(()=>(0,e.fail)())(),this.debugMustHavePassword&&a.default.isDebug()&&!D(this[q])?(()=>(0,e.fail)())():void 0))())(t,r)}static[O](t,r,i){return((t,r,i)=>(()=>{var n,o;return-1!==(n=s.default.parse(r.url).pathname).search(new RegExp("^(/|\\\\)\\.[A-Za-z0-9]+"))&&-1===n.search(new RegExp("^(/|\\\\)\\.well-known(/|\\\\)"))&&-1===n.search(new RegExp("^/\\.password/"))&&-1===n.search(new RegExp("^/\\.browse$"))&&(()=>(0,e.fail)())(),D(this.requestOriginHost)&&(()=>i?(()=>(0,e.assert)(D(r.headers.origin)&&r.headers.origin.length>0&&"https:"===s.default.parse(r.headers.origin).protocol&&("string"==typeof this.requestOriginHost?(()=>s.default.parse(r.headers.origin).hostname===this.requestOriginHost)():(()=>this.requestOriginHost.indexOf(s.default.parse(r.headers.origin).hostname)>=0)())))():(()=>(0,e.assert)("/"===n||D(r.headers.referer)&&r.headers.referer.length>0&&"https:"===s.default.parse(r.headers.referer).protocol&&("string"==typeof this.requestOriginHost?(()=>s.default.parse(r.headers.referer).hostname===this.requestOriginHost)():(()=>this.requestOriginHost.indexOf(s.default.parse(r.headers.referer).hostname)>=0)())))())(),D(this[q])?(()=>((0,e.assert)("https"===t),(0,e.assert)(D(this.requestOriginHost)&&("string"==typeof this.requestOriginHost&&this.requestOriginHost.length>0||this.requestOriginHost instanceof Array&&this.requestOriginHost.every(e=>(()=>e.length>0)()))),(()=>{try{return(()=>(o=r.headers.cookie.match(new RegExp("(^|; )serverPassword_705357537593286848=([A-Za-z0-9]*)\\b")),D(o)&&(o=o[2]),o!==this[q]?(()=>(0,e.fail)())():void 0))()}catch(e){return(()=>{throw new Error("PWD:")})()}})()))():void 0})())(t,r,i)}static[H](t,r,i){return((t,r,i)=>(()=>{var n;return D(this.webSocketOrigins)?(()=>(n=s.default.parse(t),this.webSocketOrigins.forEach(t=>(()=>{var o,a,l,u,h,d;return"same"===t?(()=>(o=s.default.parse(r+"://"+i),(0,e.assert)(n.protocol===o.protocol&&n.hostname===o.hostname&&(void 0===(l=n.port)||null===l?"http"===r?"80":"443":l)===(void 0===(u=o.port)||null===u?"http"===r?"80":"443":u))))():(()=>(a=s.default.parse(t),(0,e.assert)(n.protocol===a.protocol&&n.hostname===a.hostname&&(void 0===(h=n.port)||null===h?"http"===r?"80":"443":h)===(void 0===(d=a.port)||null===d?"http"===r?"80":"443":d))))()})())))():void 0})())(t,r,i)}static[b](t,r){return((t,r)=>(()=>{var s;return s=this.behindProxy?(()=>(()=>{try{return(()=>R(e.dotDot_573300145710716007,r.headers["x-forwarded-proto"].split(","),"last",[]).trim().toLowerCase())()}catch(e){return null}})())():(()=>t)(),["http","https"].indexOf(s)<0&&(s=null),s})())(t,r)}static[A](e,t){return((e,t)=>(()=>{var r;return new p.default.Server({server:t,verifyClient:t=>(()=>{var r;return(()=>{try{return(()=>(r=this[b](e,t.req),this[E](r,t.req,!0),this[H](t.origin,r,t.req.headers.host),!0))()}catch(e){return!1}})()})(),maxPayload:void 0===(r=this.maxWebSocketMessageSize)||null===r?0:r}).on("connection",(t,r)=>(()=>{var s,i,n;return s=new S(t),i=this[b](e,r),n=new h.default(void 0,i,r,{}),this.onWebSocketConnect.fire({socket:s,request:n}),t.on("open",()=>{return(()=>s.onOpen.fire())()}),t.on("close",()=>{return(()=>s.onClose.fire())()}),t.on("message",e=>(()=>s.onMessage.fire({data:e}))()),t.on("error",e=>(()=>s.onError.fire(e))())})())})())(e,t)}}).promisify=o.default.promisify,T.promisifyAll=o.default.promisifyAll,T.inInternalTest=!1,T.behindProxy=!1,T.host=void 0,T.httpPort=80,T.httpsPort=443,T.httpUnixDomainSocket=null,T.httpsUnixDomainSocket=null,T.httpEnabled=!0,T.httpsEnabled=!1,T.httpsCredential=null,T.httpsCredentialFunction=null,T.maxRequestBodySize=16777216,T.maxWebSocketMessageSize=1048576,T.debugMustHavePassword=!1,T.requestOriginHost=null,T.webSocketOrigins=["same"],T.homePageHtmlAttributes="",T.homePageBeforeScript="",T.homePageAfterScript="",T.onRequest=(0,e.eventField)(),T.onWebSocketConnect=(0,e.eventField)(),T.OAuth=c.default,function(){return(()=>{this.promisifyAll(require("child_process")),this.promisifyAll(require("crypto")),this.promisifyAll(require("dgram")),this.promisifyAll(require("dns")),this.promisifyAll(require("fs")),this.promisifyAll(require("http")),this.promisifyAll(require("https")),this.promisifyAll(require("net")),this.promisifyAll(process),this.promisifyAll(require("tls")),this.promisifyAll(require("zlib")),this[q]=null})()}.call(T),T),process.on("uncaughtException",function(e){return(()=>(l.switchToSync(),l.error(e,"_server-uncaught-exception"),process.exit(1)))()}),process.on("unhandledRejection",function(e){return(()=>(l.switchToSync(),l.error(e,"_server-unhandled-rejection"),process.exit(1)))()}),l.info('"site/server" loaded.');