From 99fe6d291896d47d2763ecdc8fa61dfa461297de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Thu, 14 Aug 2025 23:57:48 +0300 Subject: [PATCH 01/15] =?UTF-8?q?=D0=BF=D1=83=D1=81=D1=82=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nodejs.setup | 2 ++ 1 file changed, 2 insertions(+) create mode 100755 nodejs.setup diff --git a/nodejs.setup b/nodejs.setup new file mode 100755 index 0000000..a82a5b3 --- /dev/null +++ b/nodejs.setup @@ -0,0 +1,2 @@ +#!/bin/bash +sudo apt install nodejs -- 2.34.1 From 46f65da61d4d7c07c9e9d7687cf578fe3bef60cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sat, 16 Aug 2025 23:30:57 +0300 Subject: [PATCH 02/15] add srv --- nodejs.setup | 2 -- srv.setup | 4 +++ srv/srv.consult.js | 89 ++++++++++++++++++++++++++++++++++++++++++++++ srv/test | 3 ++ 4 files changed, 96 insertions(+), 2 deletions(-) delete mode 100755 nodejs.setup create mode 100755 srv.setup create mode 100644 srv/srv.consult.js create mode 100755 srv/test diff --git a/nodejs.setup b/nodejs.setup deleted file mode 100755 index a82a5b3..0000000 --- a/nodejs.setup +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -sudo apt install nodejs diff --git a/srv.setup b/srv.setup new file mode 100755 index 0000000..c900e34 --- /dev/null +++ b/srv.setup @@ -0,0 +1,4 @@ +#!/bin/bash +apt install nodejs +mkdir -p /var/log/consult +chgrp www-data /var/log/consult diff --git a/srv/srv.consult.js b/srv/srv.consult.js new file mode 100644 index 0000000..ea2d03a --- /dev/null +++ b/srv/srv.consult.js @@ -0,0 +1,89 @@ +let fs = require("fs"); +let http = require("http"); + +//let DST_DIR = "/var/log"; +let DST_DIR = "/tmp"; +let TEMPLATE_FILE_NAME = "%DIR%/consult_%UUID%"; +let URL_CONSULT = "/api/consult"; + +let srv = http.createServer((req, res) => { + // POST + if (req.method == "POST") { + var dat = ""; + req.on("data", (chunk) => { + dat += chunk; + }); + req.on("end", () => { + // /api/consult + if (isConsultationRequestValid(req, dat)) { + saveUserContacts(dat); + returnSuccess(res); + } else { + returnError(res); + } + }); + } else { + returnError(res); + } +}); + +function isConsultationRequestValid(req, dat) { + var json = {}; + try { + json = JSON.parse(dat); + } catch (e) { + //console.log("ERR isCRV error:", e); + } + + if ( + req.url == URL_CONSULT && + req.method == "POST" && + "name" in json && + "phone" in json + ) { + return true; + } + + return false; +} + +function returnError(res) { + res.writeHead(404, { "Content-Type": "text/html" }); + res.end(` +404 Not Found + +

404 Not Found

+
nginx/1.24.0 (Ubuntu)
+ +`); +} + +function returnSuccess(res) { + res.writeHead(200, { "Content-Type": "application/json;charset=UTF-8" }); + let dat = { code: 4 }; + res.end(JSON.stringify(dat)); +} + +function saveUserContacts(dat) { + let fileName = TEMPLATE_FILE_NAME + .replaceAll("%DIR%", DST_DIR) + .replaceAll("%UUID%", uuidString()); + fs.writeFile(fileName, dat, (err) => { + console.log("ИГР saveUC fileN/err:", fileName, err); + }); +} + +// https://stackoverflow.com/a/2117523 +function uuidString() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( + /[xy]/g, + function(c) + { + var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); + return v.toString(16); + } + ); +} + +// Launch. +srv.listen(8001); diff --git a/srv/test b/srv/test new file mode 100755 index 0000000..50f9a3c --- /dev/null +++ b/srv/test @@ -0,0 +1,3 @@ +#!/bin/bash +curl -X POST -d '.' http://localhost:8001/api/consult +curl -X POST -d '{"name":"test-name","phone":"+79001002030"}' http://localhost:8001/api/consult -- 2.34.1 From c2109d39229596fe4f1121fc59c4ace8f0674737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sat, 16 Aug 2025 23:33:17 +0300 Subject: [PATCH 03/15] install pm2 --- srv.setup | 1 + srv/srv.consult.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/srv.setup b/srv.setup index c900e34..eca664f 100755 --- a/srv.setup +++ b/srv.setup @@ -2,3 +2,4 @@ apt install nodejs mkdir -p /var/log/consult chgrp www-data /var/log/consult +npm install -g pm2 diff --git a/srv/srv.consult.js b/srv/srv.consult.js index ea2d03a..0f35362 100644 --- a/srv/srv.consult.js +++ b/srv/srv.consult.js @@ -1,8 +1,8 @@ let fs = require("fs"); let http = require("http"); -//let DST_DIR = "/var/log"; -let DST_DIR = "/tmp"; +let DST_DIR = "/var/log/consult"; +//let DST_DIR = "/tmp"; let TEMPLATE_FILE_NAME = "%DIR%/consult_%UUID%"; let URL_CONSULT = "/api/consult"; -- 2.34.1 From 745dcfb1e5c70b3c76d1f32030a68ff1ada004ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sat, 16 Aug 2025 23:35:06 +0300 Subject: [PATCH 04/15] install npm --- srv.setup | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/srv.setup b/srv.setup index eca664f..df5b01d 100755 --- a/srv.setup +++ b/srv.setup @@ -1,5 +1,5 @@ #!/bin/bash -apt install nodejs +apt install nodejs npm mkdir -p /var/log/consult chgrp www-data /var/log/consult npm install -g pm2 -- 2.34.1 From 36a345e8de540b5f619b0d164c8e664e92c69278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sat, 16 Aug 2025 23:37:48 +0300 Subject: [PATCH 05/15] only install pm2 if not installed --- srv.setup | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/srv.setup b/srv.setup index df5b01d..ac3a225 100755 --- a/srv.setup +++ b/srv.setup @@ -2,4 +2,4 @@ apt install nodejs npm mkdir -p /var/log/consult chgrp www-data /var/log/consult -npm install -g pm2 +npm list | grep pm2 || npm install -g pm2 -- 2.34.1 From 40ccd2b7c2b7da989b6f2bf3ff4c3eeafe7fb34c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sat, 16 Aug 2025 23:38:36 +0300 Subject: [PATCH 06/15] g --- srv.setup | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/srv.setup b/srv.setup index ac3a225..677fa18 100755 --- a/srv.setup +++ b/srv.setup @@ -2,4 +2,4 @@ apt install nodejs npm mkdir -p /var/log/consult chgrp www-data /var/log/consult -npm list | grep pm2 || npm install -g pm2 +npm list -g | grep pm2 || npm install -g pm2 -- 2.34.1 From 4e98ece400b3c4dacca15c538065dfb3a4bc5c20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sat, 16 Aug 2025 23:40:35 +0300 Subject: [PATCH 07/15] start --- srv.setup | 1 + 1 file changed, 1 insertion(+) diff --git a/srv.setup b/srv.setup index 677fa18..1a3ed90 100755 --- a/srv.setup +++ b/srv.setup @@ -3,3 +3,4 @@ apt install nodejs npm mkdir -p /var/log/consult chgrp www-data /var/log/consult npm list -g | grep pm2 || npm install -g pm2 +pm2 start srv/srv.consult.js -- 2.34.1 From 309602ffbceb9c3e5dffcb2b63f7e69958089b28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sat, 16 Aug 2025 23:42:22 +0300 Subject: [PATCH 08/15] auto --- srv.setup | 1 + 1 file changed, 1 insertion(+) diff --git a/srv.setup b/srv.setup index 1a3ed90..8e2da5e 100755 --- a/srv.setup +++ b/srv.setup @@ -4,3 +4,4 @@ mkdir -p /var/log/consult chgrp www-data /var/log/consult npm list -g | grep pm2 || npm install -g pm2 pm2 start srv/srv.consult.js +pm2 startup -- 2.34.1 From aef61b2ff4dc91ce69a79706030cbb6baaf94520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sat, 16 Aug 2025 23:44:12 +0300 Subject: [PATCH 09/15] save --- srv.setup | 1 + 1 file changed, 1 insertion(+) diff --git a/srv.setup b/srv.setup index 8e2da5e..9e6fcb3 100755 --- a/srv.setup +++ b/srv.setup @@ -5,3 +5,4 @@ chgrp www-data /var/log/consult npm list -g | grep pm2 || npm install -g pm2 pm2 start srv/srv.consult.js pm2 startup +pm2 save -- 2.34.1 From 1eb4d7e2d2339920ff89e57bfcc626797c409d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sat, 16 Aug 2025 23:54:19 +0300 Subject: [PATCH 10/15] srv to api --- srv.setup => api.setup | 4 +++- {srv => api}/srv.consult.js | 0 {srv => api}/test | 0 nginx/cfg | 46 +++++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) rename srv.setup => api.setup (82%) rename {srv => api}/srv.consult.js (100%) rename {srv => api}/test (100%) create mode 100644 nginx/cfg diff --git a/srv.setup b/api.setup similarity index 82% rename from srv.setup rename to api.setup index 9e6fcb3..088a69b 100755 --- a/srv.setup +++ b/api.setup @@ -1,8 +1,10 @@ #!/bin/bash + apt install nodejs npm mkdir -p /var/log/consult chgrp www-data /var/log/consult + npm list -g | grep pm2 || npm install -g pm2 -pm2 start srv/srv.consult.js +pm2 restart api/srv.consult.js pm2 startup pm2 save diff --git a/srv/srv.consult.js b/api/srv.consult.js similarity index 100% rename from srv/srv.consult.js rename to api/srv.consult.js diff --git a/srv/test b/api/test similarity index 100% rename from srv/test rename to api/test diff --git a/nginx/cfg b/nginx/cfg new file mode 100644 index 0000000..e141283 --- /dev/null +++ b/nginx/cfg @@ -0,0 +1,46 @@ +server { + listen 80 default_server; + listen [::]:80 default_server; + + # SSL configuration + # + # listen 443 ssl default_server; + # listen [::]:443 ssl default_server; + # + # Note: You should disable gzip for SSL traffic. + # See: https://bugs.debian.org/773332 + # + # Read up on ssl_ciphers to ensure a secure configuration. + # See: https://bugs.debian.org/765782 + # + # Self signed certs generated by the ssl-cert package + # Don't use them in a production server! + # + # include snippets/snakeoil.conf; + + root /var/www/html; + + # Add index.php to the list if you are using PHP + index index.html; + + server_name _; + + location / { + # First attempt to serve request as file, then + # as directory, then fall back to displaying a 404. + try_files $uri $uri/ =404; + add_header "Access-Control-Allow-Origin" "*"; + # Remove CORS. + if ($request_method = "OPTIONS") { + add_header "Access-Control-Allow-Origin" "*"; + add_header "Access-Control-Allow-Headers" "Content-Type"; + add_header "Content-Type" "text/plain charset=UTF-8"; + add_header "Content-Length" 0; + return 204; + } + } + + location /api { + proxy_pass http://localhost:8001; + } +} -- 2.34.1 From c9ac11e7f774b00beb48a98b07eb3e6f7fd114fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sat, 16 Aug 2025 23:55:50 +0300 Subject: [PATCH 11/15] nginx --- nginx.setup | 5 +++++ 1 file changed, 5 insertions(+) create mode 100755 nginx.setup diff --git a/nginx.setup b/nginx.setup new file mode 100755 index 0000000..4fc31be --- /dev/null +++ b/nginx.setup @@ -0,0 +1,5 @@ +#!/bin/bash + +apt install nginx +cp nginx/cfg /etc/nginx/sites-enabled/default +systemctl restart nginx -- 2.34.1 From bed76399914055b703bf76e1f1209e8535e336c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sat, 16 Aug 2025 23:59:22 +0300 Subject: [PATCH 12/15] add nginx test --- api/test-nginx | 2 ++ 1 file changed, 2 insertions(+) create mode 100755 api/test-nginx diff --git a/api/test-nginx b/api/test-nginx new file mode 100755 index 0000000..506536f --- /dev/null +++ b/api/test-nginx @@ -0,0 +1,2 @@ +#!/bin/bash +curl -X POST -d '{"name":"test-name","phone":"+79001002030"}' http://167.17.178.89/api/consult -- 2.34.1 From 4c4a46ce28102b70fe61ae51b9100d88c32b8330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sun, 17 Aug 2025 00:07:40 +0300 Subject: [PATCH 13/15] re-ad --- api.setup | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api.setup b/api.setup index 088a69b..72b6161 100755 --- a/api.setup +++ b/api.setup @@ -5,6 +5,8 @@ mkdir -p /var/log/consult chgrp www-data /var/log/consult npm list -g | grep pm2 || npm install -g pm2 -pm2 restart api/srv.consult.js +pm2 stop api/srv.consult.js +pm2 delete api/srv.consult.js +pm2 start api/srv.consult.js pm2 startup pm2 save -- 2.34.1 From 81f39d238fa141b49f50bbf855a9ce61d2f3de5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sun, 17 Aug 2025 23:38:37 +0300 Subject: [PATCH 14/15] overcome CORS --- api/srv.consult.js | 16 +++++++++++++++- nginx/cfg | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/api/srv.consult.js b/api/srv.consult.js index 0f35362..607a681 100644 --- a/api/srv.consult.js +++ b/api/srv.consult.js @@ -7,7 +7,6 @@ let TEMPLATE_FILE_NAME = "%DIR%/consult_%UUID%"; let URL_CONSULT = "/api/consult"; let srv = http.createServer((req, res) => { - // POST if (req.method == "POST") { var dat = ""; req.on("data", (chunk) => { @@ -22,6 +21,8 @@ let srv = http.createServer((req, res) => { returnError(res); } }); + } else if (req.method == "OPTIONS") { + returnNoCORS(res); } else { returnError(res); } @@ -58,6 +59,19 @@ function returnError(res) { `); } +function returnNoCORS(res) { + res.writeHead( + 204, + { + "Access-Control-Allow-Headers": "Content-Type", + "Access-Control-Allow-Origin": "*", + "Content-Length": "0", + "Content-Type": "text/plain;charset=UTF-8", + } + ); + res.end(); +} + function returnSuccess(res) { res.writeHead(200, { "Content-Type": "application/json;charset=UTF-8" }); let dat = { code: 4 }; diff --git a/nginx/cfg b/nginx/cfg index e141283..079181e 100644 --- a/nginx/cfg +++ b/nginx/cfg @@ -34,7 +34,7 @@ server { if ($request_method = "OPTIONS") { add_header "Access-Control-Allow-Origin" "*"; add_header "Access-Control-Allow-Headers" "Content-Type"; - add_header "Content-Type" "text/plain charset=UTF-8"; + add_header "Content-Type" "text/plain;charset=UTF-8"; add_header "Content-Length" 0; return 204; } -- 2.34.1 From 9755c477892c163c542971e373852fb03b42555f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9A=D0=B0=D0=BF?= =?UTF-8?q?=D0=B5=D0=BB=D1=8C=D0=BA=D0=BE?= Date: Sun, 17 Aug 2025 23:47:44 +0300 Subject: [PATCH 15/15] allow cors for api --- api/srv.consult.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/api/srv.consult.js b/api/srv.consult.js index 607a681..dc16c7b 100644 --- a/api/srv.consult.js +++ b/api/srv.consult.js @@ -73,7 +73,13 @@ function returnNoCORS(res) { } function returnSuccess(res) { - res.writeHead(200, { "Content-Type": "application/json;charset=UTF-8" }); + res.writeHead( + 200, + { + "Access-Control-Allow-Origin": "*", + "Content-Type": "application/json;charset=UTF-8", + } + ); let dat = { code: 4 }; res.end(JSON.stringify(dat)); } -- 2.34.1