Skip to content

Commit

Permalink
feat: gateway authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Oct 28, 2019
1 parent bc4e9fc commit 48d5371
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
14 changes: 14 additions & 0 deletions lib/gateway/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ export const createHttpServer = () => {
const config = loadConfig(cwd, configFile);
const surgioServer = new Server(config);

if (config.gateway && config.gateway.auth) {
router.use((() => {
return async (ctx, next) => {
const accessToken = ctx.query.access_token;

if (accessToken === config.gateway.accessToken) {
await next();
} else {
ctx.throw(401);
}
};
})());
}

router.use((() => {
return async (_, next) => {
await surgioServer.init();
Expand Down
16 changes: 5 additions & 11 deletions lib/gateway/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class Server {
const artifactName = ctx.params.name;

if (!artifactName) {
ctx.status = 400;
ctx.throw(400);
return;
}

Expand All @@ -151,7 +151,7 @@ export class Server {

ctx.body = result;
} else {
ctx.status = 404;
ctx.throw(404);
}
}

Expand All @@ -160,18 +160,12 @@ export class Server {
autoescape: false,
});
const artifactListTpl = require('./template/artifact-list').default;
const accessToken = this.config.gateway && this.config.gateway.accessToken;

ctx.body = engine.renderString(artifactListTpl, {
artifactList: this.artifactList,
getPreviewUrl: (name: string) => getDownloadUrl(this.config.urlBase, name),
getDownloadUrl: (name: string) => (
url.format({
pathname: `/get-artifact/${name}`,
query: {
dl: '1',
},
})
),
getPreviewUrl: (name: string) => getDownloadUrl(this.config.urlBase, name, true, accessToken),
getDownloadUrl: (name: string) => getDownloadUrl(this.config.urlBase, name, false, accessToken),
encodeURIComponent,
surgioVersion: require('../../package.json').version,
});
Expand Down
6 changes: 4 additions & 2 deletions lib/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export async function generate(
assert(template, '必须指定 artifact 的 template 属性');
assert(artifact.provider, '必须指定 artifact 的 provider 属性');

const gatewayConfig = config.gateway;
const gatewayHasToken: boolean = !!(gatewayConfig && gatewayConfig.accessToken);
const combineProviders = artifact.combineProviders || [];
const providerList = [artifact.provider].concat(combineProviders);
const nodeList: PossibleNodeConfigType[] = [];
Expand Down Expand Up @@ -164,7 +166,7 @@ export async function generate(

try {
return templateEngine.render(`${template}.tpl`, {
downloadUrl: getDownloadUrl(config.urlBase, artifactName),
downloadUrl: getDownloadUrl(config.urlBase, artifactName, true, gatewayHasToken ? gatewayConfig.accessToken : undefined),
nodes: nodeList,
names: nodeNameList,
remoteSnippets: _.keyBy(remoteSnippetList, item => {
Expand All @@ -174,7 +176,7 @@ export async function generate(
provider: artifact.provider,
providerName: artifact.provider,
artifactName,
getDownloadUrl: (name: string) => getDownloadUrl(config.urlBase, name),
getDownloadUrl: (name: string) => getDownloadUrl(config.urlBase, name, true, gatewayHasToken ? gatewayConfig.accessToken : undefined),
getNodeNames,
getClashNodes,
getClashNodeNames,
Expand Down
5 changes: 5 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface CommandConfig {
readonly providerDir: string;
readonly templateDir: string;
readonly configDir: string;
readonly analytics?: boolean;
readonly upload?: {
readonly prefix: string;
readonly region: string;
Expand All @@ -38,6 +39,10 @@ export interface CommandConfig {
readonly surgeConfig?: {
readonly v2ray: 'native'|'external';
};
readonly gateway?: {
readonly accessToken?: string;
readonly auth?: boolean;
},
}

export interface RemoteSnippetConfig {
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export const validateConfig = (userConfig: Partial<CommandConfig>): void => {
v2ray: Joi.string().valid('native', 'external')
}),
analytics: Joi.boolean(),
gateway: Joi.object({
accessToken: Joi.string(),
auth: Joi.boolean(),
}),
})
.unknown();

Expand Down
14 changes: 13 additions & 1 deletion lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,19 @@ const ConfigCache = new LRU<string, any>({
// istanbul ignore next
export const resolveRoot = (...args: readonly string[]): string => path.join(__dirname, '../../', ...args);

export const getDownloadUrl = (baseUrl: string = '/', artifactName: string): string => `${baseUrl}${artifactName}`;
export const getDownloadUrl = (baseUrl: string = '/', artifactName: string, inline: boolean = true, accessToken?: string): string => {
const urlObject = URL.parse(`${baseUrl}${artifactName}`, true);

if (accessToken) {
urlObject.query.access_token = accessToken;
}

if (!inline) {
urlObject.query.dl = '1';
}

return URL.format(urlObject);
};

export const getBlackSSLConfig = async (config: {
readonly username: string;
Expand Down

0 comments on commit 48d5371

Please sign in to comment.