-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
165 lines (150 loc) · 5.78 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// GITHUB CODE e698a3349647cf6ff5be3cd676a8dbad900d363d
const express = require('express')
const next = require('next')
const cors = require('cors')
const dev = process.env.NODE_ENV !== 'production'
const hostn = "localhost";
const app = next({ dev })
const handle = app.getRequestHandler()
const { createProxyMiddleware } = require('http-proxy-middleware');
const helmet = require('helmet')
const canvasProxy = require('html2canvas-proxy');
const corsOrigin = [/\.portalnesia\.com$/, "https://portalnesia.com"];
const useDataProxy = (req, res, next) => {
const exampleProxy = createProxyMiddleware({
target: 'https://datas.portalnesia.com',
changeOrigin: true,
headers: {
host: 'https://datas.portalnesia.com'
},
onProxyReq: function (proxy) {
if (process.env.NODE_ENV === 'production') proxy.setHeader('X-Local-Ip', req.headers["cf-connecting-ip"]);
},
onProxyRes: function (proxy) {
const type = proxy.headers['content-type'];
if (typeof type === 'string' && (type.startsWith('image') || type.startsWith('audio') || type.startsWith('video'))) {
proxy.headers['Cache-Control'] = 'public, max-age=86400';
}
},
logLevel: 'error',
});
return exampleProxy(req, res, next);
}
const useApiProxy = (req, res, next) => {
const apiProxy = createProxyMiddleware({
target: process.env.API_URL,
changeOrigin: true,
headers: {
host: process.env.NEXT_PUBLIC_API_URL
},
onProxyReq: function (proxy) {
if (process.env.NODE_ENV === 'production') proxy.setHeader('X-Local-Ip', req.headers["cf-connecting-ip"]);
},
followRedirects: true,
logLevel: 'error',
});
return apiProxy(req, res, next);
}
const useContentProxy = (req, res, next) => {
const apiProxy = createProxyMiddleware({
target: 'https://content.portalnesia.com',
changeOrigin: true,
pathRewrite: {
'^/content': `/`,
},
headers: {
host: 'https://content.portalnesia.com'
},
onProxyRes: function (proxy) {
const type = proxy.headers['content-type'];
if (typeof type === 'string' && (type.startsWith('image') || type.startsWith('audio') || type.startsWith('video'))) {
proxy.headers['Cache-Control'] = 'public, max-age=86400';
}
},
logLevel: 'error',
followRedirects: true
});
return apiProxy(req, res, next);
}
const accountRedirect = (path) => (req, res) => {
const url = new URL(req.url, 'https://accounts.portalnesia.com');
url.pathname = path;
url.searchParams.set('utm_source', 'portalnesia web')
url.searchParams.set('utm_medium', 'redirect')
url.searchParams.set('utm_campaign', 'v5')
res.redirect(301, url.toString());
}
app.prepare().then(() => {
const port = process.env.PORT;
const server = express()
server.use('/canvas-proxy', canvasProxy());
server.options('*', cors({ origin: corsOrigin }))
server.use(cors({ origin: corsOrigin }))
server.use(helmet.dnsPrefetchControl());
server.use(helmet.expectCt());
server.use(helmet.frameguard());
server.use(helmet.hidePoweredBy());
server.use(helmet.hsts());
server.use(helmet.ieNoOpen());
server.use(helmet.noSniff());
server.use(helmet.permittedCrossDomainPolicies());
server.use(helmet.referrerPolicy({
policy: "strict-origin-when-cross-origin"
}));
server.use(helmet.xssFilter());
server.use('/user/*/photo_profile', useApiProxy)
server.use('/email/preview/*', useDataProxy);
server.get('/sitemap.xml', useApiProxy);
server.use('/news/feed', useApiProxy);
server.use('/blog/feed', useApiProxy);
server.use('/chord/feed', useApiProxy);
server.use('/content', useContentProxy);
// server.use('/telegram/oauth', useDataProxy);
server.use('/line', useDataProxy);
// server.use('/print', useDataProxy);
//server.use('/file-manager/images', exampleProxy);
// server.use('/media/embed', useDataProxy);
server.use('/login', accountRedirect('/login'));
server.use('/logout', accountRedirect('/logout'));
server.use('/active', accountRedirect('active'));
server.use('/forgot', accountRedirect('/forgot'));
server.use('/unlock', accountRedirect('/unlock'));
server.get('/ln', (req, res) => {
res.writeHead(302, { Location: "https://line.me/R/ti/p/%40540ytcnc" });
res.end();
})
server.get('/tw', (req, res) => {
res.writeHead(302, { Location: "https://twitter.com/Portalnesia1" });
res.end();
})
server.get('/fb', (req, res) => {
res.writeHead(302, { Location: "https://www.facebook.com/portalnesia" });
res.end();
})
server.get('/tg', (req, res) => {
res.writeHead(302, { Location: "https://t.me/portalnesia_bot" });
res.end();
})
server.get('/ig', (req, res) => {
res.writeHead(302, { Location: "https://instagram.com/portalnesia.id" });
res.end();
})
server.get("/setting/:slug?", (req, res) => {
const slug = req.params.slug;
let url = 'https://accounts.portalnesia.com';
if (slug) url += '/' + slug;
const new_url = new URL(url);
new_url.searchParams.set('utm_source', 'portalnesia web')
new_url.searchParams.set('utm_medium', 'redirect')
new_url.searchParams.set('utm_campaign', 'v5')
res.redirect(301, new_url.toString());
})
server.use(express.json());
server.use(express.urlencoded({ extended: true }));
server.use(handle);
server.listen(port, hostn, (err) => {
if (err) throw err
console.log(`>> Ready on http://${hostn}:${port}`)
if (process.send) process.send('ready')
})
})