EC2 Default User

set SSL

Showing 1 changed file with 99 additions and 0 deletions
1 +const express = require('express');
2 +const fs = require('fs');
3 +const path = require('path');
4 +const HTTPS = require('https');
5 +
6 +const app = express();
7 +const domain = "2020105659.osschatbot.cf"
8 +const sslport = 23023;
9 +
10 +
11 +app.get('/', function (req, res) {
12 + res.send('Hello World');
13 +})
14 +
15 +try {
16 + const option = {
17 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
18 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
19 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
20 + };
21 +
22 + HTTPS.createServer(option, app).listen(sslport, () => {
23 + console.log(`[HTTPS] Server is started on port ${sslport}`);
24 + });
25 + } catch (error) {
26 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
27 + console.log(error);
28 + }
29 +
30 +
31 +
32 +/*
33 +var express = require('express');
34 +const request = require('request');
35 +const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
36 +const TOKEN = 'P5+e9JkPRckzKu1Fv7ghZl1MfP0wrYwYcgBVzHWe1OHO4Gtv1PIa9BARYqxoAThaXPSf8MyVKcpDE88V5jOnoS4o2xCNcgH9Ne4wcrPuNX6AT4gsfYEz0Y7n/A4+rT4vXHYARK34YPbkzzQbiXiyiQdB04t89/1O/w1cDnyilFU='
37 +const fs = require('fs');
38 +const path = require('path');
39 +const HTTPS = require('https');
40 +const domain = "2020105659.osschatbot.cf"
41 +const sslport = 23023;
42 +
43 +const bodyParser = require('body-parser');
44 +var app = express();
45 +app.use(bodyParser.json());
46 +app.post('/hook', function (req, res) {
47 +
48 + var eventObj = req.body.events[0];
49 + var source = eventObj.source;
50 + var message = eventObj.message;
51 +
52 + // request log
53 + console.log('======================', new Date() ,'======================');
54 + console.log('[request]', req.body);
55 + console.log('[request source] ', eventObj.source);
56 + console.log('[request message]', eventObj.message);
57 +
58 + request.post(
59 + {
60 + url: TARGET_URL,
61 + headers: {
62 + 'Authorization': `Bearer ${TOKEN}`
63 + },
64 + json: {
65 + "replyToken":eventObj.replyToken,
66 + "messages":[
67 + {
68 + "type":"text",
69 + "text":"Hello, user"
70 + },
71 + {
72 + "type":"text",
73 + "text":"May I help you?"
74 + }
75 + ]
76 + }
77 + },(error, response, body) => {
78 + console.log(body)
79 + });
80 +
81 +
82 + res.sendStatus(200);
83 +});
84 +
85 +try {
86 + const option = {
87 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
88 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
89 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
90 + };
91 +
92 + HTTPS.createServer(option, app).listen(sslport, () => {
93 + console.log(`[HTTPS] Server is started on port ${sslport}`);
94 + });
95 + } catch (error) {
96 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
97 + console.log(error);
98 + }
99 + */
...\ No newline at end of file ...\ No newline at end of file