From 51d6736d5c89b1ca9420beb7664a847eee7b8b45 Mon Sep 17 00:00:00 2001 From: CncAnon Date: Mon, 28 Aug 2023 14:04:54 +0300 Subject: [PATCH] Add proper token checking, just have .env directly in here --- .env | 1 + .env.example | 1 - .gitignore | 1 - README.md | 11 +---------- main.js | 55 ++++++++++++++++++++++++++++++++++++++++++---------- 5 files changed, 47 insertions(+), 22 deletions(-) create mode 100644 .env delete mode 100644 .env.example diff --git a/.env b/.env new file mode 100644 index 0000000..2244a11 --- /dev/null +++ b/.env @@ -0,0 +1 @@ +API_TOKEN="replace_this_with_your_token" \ No newline at end of file diff --git a/.env.example b/.env.example deleted file mode 100644 index ec4bc45..0000000 --- a/.env.example +++ /dev/null @@ -1 +0,0 @@ -API_TOKEN="sgp_replace" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 97aca2e..b512c09 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -.env node_modules \ No newline at end of file diff --git a/README.md b/README.md index 60dbd60..c1c4d08 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,3 @@ # SG-proxy -Newer info on here: https://rentry.org/sg_proxy - -1) Create a SourceGraph account -2) Go to https://sourcegraph.com/, click your profile icon in upper-right, then Settings, then go to Account->Access tokens, and generate a new token -3) Create a `.env` and put your token there -4) `npm install` -5) `node main.js` -6) Change completion to Claude in ST, set any API key, then set the proxy URL as `http://localhost:3000/v1` and set any password. - -Enjoy. \ No newline at end of file +For instructions see https://rentry.org/sg_proxy \ No newline at end of file diff --git a/main.js b/main.js index 6f65f3a..63f6d15 100644 --- a/main.js +++ b/main.js @@ -2,7 +2,6 @@ require("dotenv").config(); const express = require('express'); const axios = require('axios'); -const { exit } = require("process"); const app = express(); app.use(express.json()); @@ -116,14 +115,50 @@ process.on('unhandledRejection', (reason, promise) => { console.error('Unhandled Promise Rejection:', reason); }); -if (!API_TOKEN) { - console.error("SourceGraph API token not found! Create a file named '.env' and put your token there as an API_TOKEN. See .env.example for an example."); - exit(); -} -else if (API_TOKEN.indexOf("sgp_") == -1) { - console.error("Invalid SourceGraph API token! Make sure you copied the whole token starting with sgp_, like 'sgp-blablabla'."); - exit(); +async function checkToken(token) { + const data = { + query: 'query { currentUser { username } }' + }; + + const config = { + method: 'post', + url: 'https://sourcegraph.com/.api/graphql', + headers: { + 'Authorization': `token ${token}` + }, + data: data + }; + + try { + const response = await axios(config); + if(response.data && response.data.data && response.data.data.currentUser) { + console.log(`Token works, username: ${response.data.data.currentUser.username}`); + return true; + } else { + return false; + } + } catch (error) { + return false; + } } -const port = process.env.PORT || 3000; -app.listen(port, () => console.log(`Server listening on port ${port}`)); \ No newline at end of file +// Two basic checks +if (!API_TOKEN) { + console.error("SourceGraph API token not found! Create a file named '.env' and put your token there as an API_TOKEN. See .env.example for an example."); + process.exit(1); +} +else if (API_TOKEN.indexOf("sgp_") == -1) { + console.error("Invalid SourceGraph API token! Make sure you copied the whole token starting with sgp_, like 'sgp_blablabla'."); + process.exit(1); +} + +// Check token validity +checkToken(API_TOKEN).then(isValid => { + if (!isValid) { + console.error("Invalid SourceGraph API token! The token is not valid. Make sure you copied the whole token."); + process.exit(1); + } + + const port = process.env.PORT || 3000; + app.listen(port, () => console.log(`Server listening on port ${port}`)); +}); \ No newline at end of file