Skip to content

Commit

Permalink
feat: proxy post
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff-Tian committed Oct 3, 2021
1 parent 140438d commit e58dcf0
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 34 deletions.
17 changes: 17 additions & 0 deletions app/controller/proxy/proxy.ts
Expand Up @@ -21,6 +21,23 @@ export default class ProxyController extends Controller {
}
}

public async post() {
const { ctx } = this;

const { data } = (await ctx.curl(ctx.query.url, {
streaming: false,
retry: 3,
timeout: [ 3000, 30000 ],
method: 'POST',
type: 'POST',
contentType: 'json',
data: ctx.request.body,
dataType: 'json',
}));

ctx.body = data;
}

public async pipeFile() {
const { ctx } = this;
const { url } = ctx.query;
Expand Down
3 changes: 1 addition & 2 deletions app/router/proxy/proxy.ts
Expand Up @@ -4,8 +4,6 @@ const cacheResult = async (ctx, next) => {
const cache = await ctx.app.redis.get(ctx.query.url);

if (cache) {
ctx.logger.info('got cache = ', cache);

const final = new Buffer(cache, 'hex').toString();
try {
ctx.type = 'json';
Expand All @@ -25,5 +23,6 @@ export default (app: Application) => {
const subRouter = router.namespace('/proxy');

subRouter.get('/', cacheResult, controller.proxy.proxy.get);
subRouter.post('/', controller.proxy.proxy.post);
subRouter.get('/pipe-file', controller.proxy.proxy.pipeFile);
};
55 changes: 28 additions & 27 deletions config/config.prod.ts
@@ -1,16 +1,16 @@
import { EggAppConfig, PowerPartial } from 'egg';
import redisUrlParse from 'redis-url-parse';
import { v4 as uuid } from 'uuid';
import { EggAppConfig, PowerPartial } from 'egg'
import redisUrlParse from 'redis-url-parse'
import { v4 as uuid } from 'uuid'

export default () => {
const config: PowerPartial<EggAppConfig> = {};
const config: PowerPartial<EggAppConfig> = {}

config.passportGithub = {
key: process.env['passport-github-key']!,
secret: process.env['passport-github-secret']!,
callbackURL: 'https://uniheart.herokuapp.com/passport/github/callback',
proxy: false,
};
}

config.passportWechat = {
clients: {
Expand All @@ -31,7 +31,7 @@ export default () => {
state: ctx => ctx.traceId,
},
},
};
}

config.passportWeapp = {
clients: {
Expand All @@ -47,69 +47,70 @@ export default () => {
successReturnToOrRedirect: '',
},
},
};
}

config.passportCiti = {
key: process.env['passport-citi-key']!,
secret: process.env['passport-citi-secret']!,
callbackURL: 'https://uniheart.herokuapp.com/passport/citi/callback',
state: app => {
return req => {
const state = uuid();
const referer = req.query.redirect_uri || req.headers.referer;
const state = uuid()
const referer = req.query.redirect_uri || req.headers.referer

if (referer) {
app.logger.info('referer = ', referer);
app.logger.info('referer = ', referer)

app.refererCache.save(state, referer).then();
app.refererCache.save(state, referer)
.then()

app.logger.info('state = ', {
state,
referer: req.headers.referer,
headers: req.headers,
query: req.query,
url: req.url,
});
})
}

return state;
};
return state
}
},
};
}

config.sequelize = {
// eslint-disable-next-line @typescript-eslint/no-var-requires
Sequelize: require('sequelize-typescript').Sequelize,
dialect: 'mysql',
connectionUri: process.env.CLEARDB_DATABASE_URL,
};
}

config.alinode = {
enable: true,
appid: process.env['alinode-appid']!,
secret: process.env['alinode-secret']!,
};
}

config.logger = {
disableConsoleAfterReady: !process.env.EGG_LOGGER,
};
}

config.security = {
csrf: {
queryName: process.env.EGG_SECURITY_CSRF_NAME,
bodyName: process.env.EGG_SECURITY_CSRF_CONTENT,
ignore: ctx =>
ctx.path.startsWith('/endpoints/wechat/message') ||
ctx.path.startsWith('/citi-dev'),
ctx.path.startsWith('/citi-dev') || ctx.path.startsWith('/proxy'),
},
};
}

config.jwt = {
secret: process.env.EGG_JWT_SECRET || 'uniheart',
};
}

const redisUri = process.env.REDIS_URI;
const parsed = redisUrlParse(redisUri);
const redisUri = process.env.REDIS_URI
const parsed = redisUrlParse(redisUri)

config.redis = {
client: {
Expand All @@ -119,7 +120,7 @@ export default () => {
db: parsed.database,
},
agent: true,
};
}

config.oss = {
client: {
Expand All @@ -129,14 +130,14 @@ export default () => {
endpoint: process.env.OSS_ENDPOINT || 'oss-cn-shanghai.aliyuncs.com',
timeout: process.env.OSS_TIMEOUT || '60s',
},
};
}

config.errorDisplay = {
isProd: () => false,
// eslint-disable-next-line @typescript-eslint/no-empty-function
serializer: () => {
},
};
}

return config;
return config
}
11 changes: 10 additions & 1 deletion config/config.unittest.ts
Expand Up @@ -58,7 +58,8 @@ export default () => {
successReturnToOrRedirect: '/passport/citi/passport-relay',
state: app => {
return () => {
app.refererCache.save('4321', '1234').then()
app.refererCache.save('4321', '1234')
.then()

return '4321'
}
Expand All @@ -83,5 +84,13 @@ export default () => {
consoleLevel: 'DEBUG',
}

config.security = {
csrf: {
ignore: ctx =>
ctx.path.startsWith('/endpoints/wechat/message') ||
ctx.path.startsWith('/citi-dev') || ctx.path.startsWith('/proxy'),
},
}

return config
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -17,7 +17,7 @@
"debug": "egg-bin debug",
"test-with-full-trace": "egg-bin test TESTS test/app/passport-wechat-hardway.test.ts --full-trace",
"test": "npm run migrate&&egg-bin test",
"test-local-changed": "bash -c \"export DOCKER_HOST='tcp://0.0.0.0:2375'&&test/mysql-up.sh&&test/redis-up.sh\"&&npm run migrate&&egg-bin test --changed",
"test-local-changed": "npm run migrate&&egg-bin test --changed",
"test-local": "npm run lint -- --fix && npm test",
"start:dev-env": "docker-compose up -d --remove-orphans",
"stop:dev-env": "docker-compose down",
Expand Down
21 changes: 18 additions & 3 deletions test/app/controller/proxy.test.ts
@@ -1,8 +1,8 @@
import assert = require('assert')
import assert = require('assert');
// tslint:disable-next-line:no-submodule-imports
import { app } from 'egg-mock/bootstrap';

describe.skip('test/app/controller/proxy.test.ts', () => {
describe('test/app/controller/proxy.test.ts', () => {
const getBaidu = async () => {
const res = await app
.httpRequest()
Expand All @@ -12,15 +12,30 @@ describe.skip('test/app/controller/proxy.test.ts', () => {
assert(res.text.includes('</html>'));
};

const graphql = async () => {
const res = await app.httpRequest()
.post(`/proxy?url=${encodeURIComponent('https://jqp5j170i6.execute-api.us-east-1.amazonaws.com/dev/gatsby/graphql')}`)
.type('application/json')
.send({ query: '{ \n yuque(id: "53296538") {\n id\n title\n description\n \n }\n \n allYuque {\n nodes {\n id\n title\n }\n }\n}', variables: null })
.expect(200);

assert.strictEqual(res.body.errors, undefined);
assert(res.body.data.yuque.title === '快速下载 GitHub 上项目下的子目录');
};

it('should proxy html', async () => {
await app.redis.flushall();
await getBaidu();
});

it('should get from cache', getBaidu);

it('should proxy graphql', graphql);

it.skip('pipes file', async () => {
const res = await app.httpRequest().get(`/proxy/pipe-file?url=${encodeURIComponent('http://libre-office.oss-cn-shanghai.aliyuncs.com/%25E4%25BD%259C%25E4%25B8%259A1581528201606.pdf')}`).expect(200);
const res = await app.httpRequest()
.get(`/proxy/pipe-file?url=${encodeURIComponent('http://libre-office.oss-cn-shanghai.aliyuncs.com/%25E4%25BD%259C%25E4%25B8%259A1581528201606.pdf')}`)
.expect(200);

assert(res.status === 200);
assert(res.body.length > 0);
Expand Down

0 comments on commit e58dcf0

Please sign in to comment.