modif cors
This commit is contained in:
		
							
								
								
									
										72
									
								
								apxtri.js
									
									
									
									
									
								
							
							
						
						
									
										72
									
								
								apxtri.js
									
									
									
									
									
								
							| @@ -8,9 +8,6 @@ const cors = require("cors"); | |||||||
| const express = require("express"); | const express = require("express"); | ||||||
| const process = require("process"); | const process = require("process"); | ||||||
|  |  | ||||||
| const l=require('./tools/log.js'); |  | ||||||
| l.showlog= true; // force log as well in prod and dev |  | ||||||
| l.context="apxtri"; |  | ||||||
|  |  | ||||||
| /******************************************* | /******************************************* | ||||||
|  |  | ||||||
| @@ -117,6 +114,9 @@ if ( | |||||||
| } | } | ||||||
| */ | */ | ||||||
| const conf = require(path.resolve(`../conf/townconf.json`)); | const conf = require(path.resolve(`../conf/townconf.json`)); | ||||||
|  | const currentmod='apxtri'; | ||||||
|  | const log = conf.api.activelog.includes(currentmod) | ||||||
|  |  | ||||||
|  |  | ||||||
| // Create and update ./nationchains plutot que cette fonction  | // Create and update ./nationchains plutot que cette fonction  | ||||||
| // A creuser et voir comment synchro | // A creuser et voir comment synchro | ||||||
| @@ -156,7 +156,7 @@ Object.keys(tribelist).forEach((t) => { | |||||||
|   }); |   }); | ||||||
|   tribeIds.push(t); |   tribeIds.push(t); | ||||||
| }); | }); | ||||||
| l.og("Allowed DOMs to access to this apxtri server:",JSON.stringify(doms)); | if (log) console.log(currentmod," Allowed DOMs to access to this apxtri server:",JSON.stringify(doms)); | ||||||
|  |  | ||||||
| const app = express(); | const app = express(); | ||||||
| // load express parameter from conf | // load express parameter from conf | ||||||
| @@ -170,47 +170,21 @@ app.use(express.json()); | |||||||
| app.use(bodyParser.json(conf.api.bodyparse.json)); | app.use(bodyParser.json(conf.api.bodyparse.json)); | ||||||
| app.disable('x-powered-by');// for security | app.disable('x-powered-by');// for security | ||||||
| app.locals.tribeids = tribeIds; | app.locals.tribeids = tribeIds; | ||||||
| l.og("app.locals.tribeids", app.locals.tribeids); | if (log) console.log(currentmod," app.locals.tribeids", app.locals.tribeids); | ||||||
| // Cors management | // Cors management | ||||||
| const corsOptions = { | app.use((req,res,next)=>{ | ||||||
|   origin: (origin, callback) => { |   console.log("request origin:",req.headers.origin) | ||||||
|     //before modif only origin == undefined  |   console.log(req.headers) | ||||||
|     // ajout d'une condition en dev à voir || (infotown.townId == "devfarm" && (origin == undefined || origin == null)) | }) | ||||||
|     if ( | let origlist = [undefined] | ||||||
|       (origin == undefined || | doms.forEach(d=>{ | ||||||
|       origin.indexOf("chrome-extension") > -1) |   origlist.push(new RegExp(`^http.?:\/\/${d.replace(/\./g,"\\.")}`)) | ||||||
|     ) { | }) | ||||||
|       callback(null, true); | console.log(origlist) | ||||||
|     } else { |  | ||||||
|       const rematch = /^https?:\/\/(.*):.*/g.exec(origin); |  | ||||||
|        |  | ||||||
|       let tmp = origin.replace(/http.?:\/\//g, "").split("."); |  | ||||||
|  |  | ||||||
|       if (rematch && rematch.length > 1) tmp = rematch[1].split("."); |  | ||||||
|       //l.og( 'tmp', tmp ); |  | ||||||
|       let dom = tmp[tmp.length - 1]; |  | ||||||
|       if (tmp.length > 1) { |  | ||||||
|         dom = `${tmp[tmp.length - 2]}.${tmp[tmp.length - 1]}`; |  | ||||||
|       } |  | ||||||
|       l.og( |  | ||||||
|         `origin: ${origin}, dom:${dom}, CORS allowed? : ${doms.includes(dom)}` |  | ||||||
|       ); |  | ||||||
|       if (doms.includes(dom)) { |  | ||||||
|         callback(null, true); |  | ||||||
|       } else { |  | ||||||
|         callback(false); |  | ||||||
|       } |  | ||||||
|     } |  | ||||||
|   }, |  | ||||||
|   exposedHeaders: Object.keys(conf.api.exposedHeaders), |  | ||||||
| }; |  | ||||||
| // CORS | // CORS | ||||||
| app.use(cors(corsOptions)); | //app.use(cors(corsOptions)); | ||||||
| // Static Routes // try to use nginx route instead in comments | app.use(cors({origin:origlist})) | ||||||
| /*app.use( express.static( `${__dirname}/nationchains/tribes/${conf.mayorId}/www/cdn/public`, { |  | ||||||
|   dotfiles: 'allow' |  | ||||||
| } ) ); |  | ||||||
| */ |  | ||||||
| // Routers add any routes from /routes and /plugins | // Routers add any routes from /routes and /plugins | ||||||
| let logroute = "Routes available on this apxtri instance: \n"; | let logroute = "Routes available on this apxtri instance: \n"; | ||||||
| routes.forEach((r) => { | routes.forEach((r) => { | ||||||
| @@ -219,13 +193,13 @@ routes.forEach((r) => { | |||||||
|     app.use(r.url, require(r.route)); |     app.use(r.url, require(r.route)); | ||||||
|   } catch (err) { |   } catch (err) { | ||||||
|     logroute += " (err check it module.exports=router;? or ...)\n======\n "; |     logroute += " (err check it module.exports=router;? or ...)\n======\n "; | ||||||
|     l.og("raise err-:", err); |     console.log("raise err-:", err); | ||||||
|   } |   } | ||||||
| }); | }); | ||||||
| console.log(logroute) | if (log) {  | ||||||
| if (process.env.NODE_MODE=="dev") { |   console.log(currentmod,logroute) | ||||||
|   console.log( |   if (process.env.NODE_MODE=="dev") console.log( | ||||||
|     `\x1b[42m############################################################################################\x1b[0m\n\x1b[42mThis is dev conf accessible in http://devfarm-ants to switch this as production, you must run:\n 1 - 'yarn dev nationId:ants townId:usbfarm dns:usbfarm-ants ' to conf your town and check it.\n 2 - 'yarn startpm2'\n Where:\n\x1b[42m * nationId have to exist in the nationchains\n * townId new or if exist must have the smae current dns,\n * dns domaine that has to redirect 80/443 into this server (example wall-ants.ndda.fr redirect to 213.32.65.213 ).\n Check README's project to learn more.\x1b[0m\n To work with apxweb for the front use http://defarm-ants/apxweb/www/tplname/src/index.html to use the api during dev process\n\x1b[42m############################################################################################\x1b[0m` |     `\x1b[42m############################################################################################\x1b[0m\n\x1b[42mThis is dev conf accessible in http://dev-ants to switch this as production, you must run:\n 1 - 'yarn dev nationId:ants townId:dev dns:dev-ants' to conf your town and check it.\n 2 - 'yarn startpm2'\n Where:\n\x1b[42m * nationId have to exist in the nationchains\n * townId new or if exist must have the same current dns,\n * dns domaine that has to redirect 80/443 into this server.\n Check README's project to learn more.\x1b[0m\n To work with apxweb for the front use http://dev-ants/apxwebapp/www/websitename/src/index.html to use the api during dev process\n\x1b[42m############################################################################################\x1b[0m` | ||||||
|   ); |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -234,7 +208,7 @@ app.listen(conf.api.port, () => { | |||||||
|   conf.dns.forEach((u) => { |   conf.dns.forEach((u) => { | ||||||
|     webaccess += `http://${u}:${conf.api.port} `; |     webaccess += `http://${u}:${conf.api.port} `; | ||||||
|   }); |   }); | ||||||
|   console.log(webaccess); |   if (log) console.log(currentmod,webaccess); | ||||||
| }); | }); | ||||||
| console.log( | console.log( | ||||||
|   "\x1b[42m\x1b[37m", |   "\x1b[42m\x1b[37m", | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user