Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CORS and mixed content issue #199

Merged
merged 2 commits into from
Oct 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export default {
server: {
host: '0.0.0.0', // default: localhost
},
serverMiddleware: [
'~/proxy/index.js'
],
head: {
title: `${meta.name} \u2022 ${meta.shortDescription}`,
meta: [
Expand Down
54 changes: 41 additions & 13 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,28 @@
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() {
if (!this.isValidURL) {
this.$toast.error('URL is not formatted properly', {
Expand Down Expand Up @@ -768,13 +790,8 @@

try {
const startTime = Date.now();
const payload = await this.$axios({
method: this.method,
url: this.url + this.pathName + this.queryString,
auth,
headers,
data: requestBody ? requestBody.toString() : null
});

const payload = await this.makeRequest(auth, headers, requestBody);

const duration = Date.now() - startTime;
this.$toast.info(`Finished in ${duration}ms`, {
Expand Down Expand Up @@ -821,13 +838,24 @@
};
this.$refs.historyComponent.addEntry(entry);
return;
} else {
this.response.status = error.message;
this.response.body = "See JavaScript console (F12) for details.";
this.$toast.error('Something went wrong!', {
icon: 'error'
});
if(!this.$store.state.postwoman.settings.PROXY_ENABLED) {
this.$toast.info('Turn on the proxy', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good to me, my only recommendation is that this be renamed to 'Try enabling the proxy' or words to that effect, but it's not that significant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about "Switch to Proxy mode"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - but perhaps with the question mark. The idea being to imply to the user that it's something that might help with the error.

dontClose : true,
action: {
text: 'Go to settings',
onClick : (e, toastObject) => {
this.$router.push({ path: '/settings' });
}
}
});
}
}

this.response.status = error.message;
this.response.body = "See JavaScript console (F12) for details.";
this.$toast.error('Something went wrong!', {
icon: 'error'
});
}
},
getQueryStringFromPath() {
Expand Down
14 changes: 6 additions & 8 deletions pages/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@
</li>
</ul>
</pw-section>

<!--
PROXY SETTINGS
--------------
This feature is currently not finished.

<pw-section class="blue" label="Proxy">
<ul>
<li>
Expand All @@ -46,7 +40,10 @@
</pw-toggle>
</li>
</ul>

<!--
PROXY SETTINGS URL AND KEY
--------------
This feature is currently not finished.
<ul>
<li>
<label for="url">URL</label>
Expand All @@ -57,8 +54,9 @@
<input id="key" type="password" v-model="settings.PROXY_KEY" :disabled="!settings.PROXY_ENABLED" @change="applySetting('PROXY_KEY', $event)">
</li>
</ul>
-->
</pw-section>
-->

</div>
</template>
<script>
Expand Down
50 changes: 50 additions & 0 deletions proxy/index.js
Original file line number Diff line number Diff line change
@@ -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
}