Create and use proxy server middleware
Because - Get over of CORS and mixed content browser errors
This commit is contained in:
@@ -27,6 +27,9 @@ export default {
|
|||||||
server: {
|
server: {
|
||||||
host: '0.0.0.0', // default: localhost
|
host: '0.0.0.0', // default: localhost
|
||||||
},
|
},
|
||||||
|
serverMiddleware: [
|
||||||
|
'~/proxy/index.js'
|
||||||
|
],
|
||||||
head: {
|
head: {
|
||||||
title: `${meta.name} \u2022 ${meta.shortDescription}`,
|
title: `${meta.name} \u2022 ${meta.shortDescription}`,
|
||||||
meta: [
|
meta: [
|
||||||
|
|||||||
@@ -700,6 +700,28 @@
|
|||||||
behavior: 'smooth'
|
behavior: 'smooth'
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
async makeRequest(auth, headers, requestBody) {
|
||||||
|
const config = this.$store.state.postwoman.settings.PROXY_ENABLED ? {
|
||||||
|
method: 'POST',
|
||||||
|
url: '/proxy',
|
||||||
|
data: {
|
||||||
|
method: this.method,
|
||||||
|
url: this.url + this.pathName + this.queryString,
|
||||||
|
auth,
|
||||||
|
headers,
|
||||||
|
data: requestBody ? requestBody.toString() : null
|
||||||
|
}
|
||||||
|
} : {
|
||||||
|
method: this.method,
|
||||||
|
url: this.url + this.pathName + this.queryString,
|
||||||
|
auth,
|
||||||
|
headers,
|
||||||
|
data: requestBody ? requestBody.toString() : null
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await this.$axios(config);
|
||||||
|
return this.$store.state.postwoman.settings.PROXY_ENABLED ? response.data : response;
|
||||||
|
},
|
||||||
async sendRequest() {
|
async sendRequest() {
|
||||||
if (!this.isValidURL) {
|
if (!this.isValidURL) {
|
||||||
this.$toast.error('URL is not formatted properly', {
|
this.$toast.error('URL is not formatted properly', {
|
||||||
@@ -768,13 +790,8 @@
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
const payload = await this.$axios({
|
|
||||||
method: this.method,
|
const payload = await this.makeRequest(auth, headers, requestBody);
|
||||||
url: this.url + this.pathName + this.queryString,
|
|
||||||
auth,
|
|
||||||
headers,
|
|
||||||
data: requestBody ? requestBody.toString() : null
|
|
||||||
});
|
|
||||||
|
|
||||||
const duration = Date.now() - startTime;
|
const duration = Date.now() - startTime;
|
||||||
this.$toast.info(`Finished in ${duration}ms`, {
|
this.$toast.info(`Finished in ${duration}ms`, {
|
||||||
@@ -821,13 +838,21 @@
|
|||||||
};
|
};
|
||||||
this.$refs.historyComponent.addEntry(entry);
|
this.$refs.historyComponent.addEntry(entry);
|
||||||
return;
|
return;
|
||||||
}
|
} else {
|
||||||
|
|
||||||
this.response.status = error.message;
|
this.response.status = error.message;
|
||||||
this.response.body = "See JavaScript console (F12) for details.";
|
this.response.body = "See JavaScript console (F12) for details.";
|
||||||
this.$toast.error('Something went wrong!', {
|
this.$toast.error('Something went wrong!', {
|
||||||
icon: 'error'
|
icon: 'error'
|
||||||
});
|
});
|
||||||
|
if(!this.$store.state.postwoman.settings.PROXY_ENABLED) {
|
||||||
|
this.$toast.info('Turn on the proxy', {
|
||||||
|
duration: 4000,
|
||||||
|
action: {
|
||||||
|
text: 'Click',
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getQueryStringFromPath() {
|
getQueryStringFromPath() {
|
||||||
|
|||||||
@@ -32,12 +32,6 @@
|
|||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</pw-section>
|
</pw-section>
|
||||||
|
|
||||||
<!--
|
|
||||||
PROXY SETTINGS
|
|
||||||
--------------
|
|
||||||
This feature is currently not finished.
|
|
||||||
|
|
||||||
<pw-section class="blue" label="Proxy">
|
<pw-section class="blue" label="Proxy">
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
@@ -46,7 +40,10 @@
|
|||||||
</pw-toggle>
|
</pw-toggle>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
<!--
|
||||||
|
PROXY SETTINGS URL AND KEY
|
||||||
|
--------------
|
||||||
|
This feature is currently not finished.
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<label for="url">URL</label>
|
<label for="url">URL</label>
|
||||||
@@ -57,8 +54,9 @@
|
|||||||
<input id="key" type="password" v-model="settings.PROXY_KEY" :disabled="!settings.PROXY_ENABLED" @change="applySetting('PROXY_KEY', $event)">
|
<input id="key" type="password" v-model="settings.PROXY_KEY" :disabled="!settings.PROXY_ENABLED" @change="applySetting('PROXY_KEY', $event)">
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</pw-section>
|
|
||||||
-->
|
-->
|
||||||
|
</pw-section>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
|
|||||||
50
proxy/index.js
Normal file
50
proxy/index.js
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
import express from 'express';
|
||||||
|
import bodyParser from 'body-parser';
|
||||||
|
import axios from 'axios';
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
|
app.use((req, res, next) => {
|
||||||
|
res.header('Access-Control-Allow-Origin', '*');
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post('/', async function(req, res) {
|
||||||
|
const {method, url, auth, headers, data} = req.body;
|
||||||
|
try {
|
||||||
|
const payload = await axios({
|
||||||
|
method,
|
||||||
|
url,
|
||||||
|
auth,
|
||||||
|
headers,
|
||||||
|
data
|
||||||
|
})
|
||||||
|
|
||||||
|
return res.json({
|
||||||
|
data: payload.data,
|
||||||
|
status: payload.status,
|
||||||
|
statusText: payload.statusText,
|
||||||
|
headers: payload.headers,
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch(error) {
|
||||||
|
if(error.response) {
|
||||||
|
const errorResponse = error.response;
|
||||||
|
return res.json({
|
||||||
|
data: errorResponse.data,
|
||||||
|
status: errorResponse.status,
|
||||||
|
statusText: errorResponse.statusText,
|
||||||
|
headers: errorResponse.headers,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return res.status(500).send();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default {
|
||||||
|
path: '/proxy',
|
||||||
|
handler: app
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user