-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
196 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from '@iringo/modkit-plugin-loon'; | ||
export type * from '@iringo/modkit-plugin-loon/types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,29 @@ | ||
import type { ModkitPlugin } from '@iringo/modkit-shared'; | ||
|
||
export type LoonArgumentType = 'input' | 'select' | 'switch' | 'exclude'; | ||
|
||
export const pluginLoon = (): ModkitPlugin => { | ||
return { | ||
name: 'loon', | ||
setup() { | ||
return {}; | ||
return { | ||
configurePlatform() { | ||
return { | ||
extension: '.plugin', | ||
template: process.env.TEMP || '', | ||
}; | ||
}, | ||
modifySource({ source }) { | ||
source ??= {}; | ||
source.arguments = source.arguments?.filter((item) => { | ||
if (typeof item.type === 'object' && item.type.loon === 'exclude') { | ||
return false; | ||
} | ||
return true; | ||
}); | ||
return source; | ||
}, | ||
}; | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { Template, logger } from '@iringo/modkit-shared'; | ||
import type { LoonArgumentType } from './index'; | ||
|
||
export class LoonTemplate extends Template { | ||
get General() { | ||
return this.renderKeyValuePairs(this.content.loonGeneral).trim(); | ||
} | ||
|
||
get Rule() { | ||
return this.content.rule | ||
?.map((rule) => { | ||
if (typeof rule === 'string') { | ||
return rule; | ||
} | ||
switch (rule.type) { | ||
case 'RULE-SET': { | ||
let result = `RULE-SET, ${this.utils.getFilePath(rule.assetKey)}`; | ||
if (rule.policyName) { | ||
result += `, ${rule.policyName}`; | ||
} | ||
return result; | ||
} | ||
default: | ||
break; | ||
} | ||
}) | ||
.join('\n') | ||
.trim(); | ||
} | ||
|
||
get Argument() { | ||
return this.source.arguments | ||
?.map((arg) => { | ||
let result = arg.key; | ||
let type: LoonArgumentType = 'input'; | ||
if (arg.type === 'boolean') { | ||
type = 'switch'; | ||
} else if (arg.options && arg.type !== 'array') { | ||
// 只有在有选项且不是数组类型时才使用 select | ||
type = 'select'; | ||
} | ||
result += ` = ${type}`; | ||
result += `,${this.#getDefaultValue(arg.defaultValue)}`; | ||
if (arg.options && type === 'select') { | ||
result += ','; | ||
result += arg.options | ||
.filter((item) => item.key !== arg.defaultValue) | ||
.map((option) => `${this.#getDefaultValue(option.key)}`) | ||
.join(','); | ||
} | ||
if (arg.name) { | ||
result += `,tag=${arg.name}`; | ||
} | ||
if (arg.description) { | ||
result += `,desc=${arg.description}`; | ||
} | ||
return result; | ||
}) | ||
.join('\n') | ||
.trim(); | ||
} | ||
|
||
get Script() { | ||
return this.content.script?.map((script, index) => { | ||
let line = ''; | ||
switch (script.type) { | ||
case 'http-request': | ||
case 'http-response': | ||
line += `${script.type} ${script.pattern},`; | ||
break; | ||
case 'cron': | ||
line += `${script.type} "${script.cronexp}",`; | ||
break; | ||
case 'generic': | ||
line += `${script.type} `; | ||
break; | ||
// case 'network-changed': | ||
case 'event': | ||
line += 'network-changed '; | ||
break; | ||
case 'dns': | ||
logger.warn('[Loon] Unsupported script type: dns'); | ||
break; | ||
} | ||
line += `script-path=${this.utils.getScriptPath(script.scriptKey)},`; | ||
line += this.objectEntries(script) | ||
.map(([key, value]) => { | ||
switch (key) { | ||
case 'name': | ||
return `tag = ${value || `Script${index}`}`; | ||
case 'argument': | ||
return `argument = ${value}`; | ||
case 'injectArgument': { | ||
if (!script.argument && value) { | ||
return `argument = [${this.source.arguments?.map((item) => `{${item.key}}`).join(',')}]`; | ||
} | ||
return ''; | ||
} | ||
case 'type': | ||
case 'pattern': | ||
case 'cronexp': | ||
case 'scriptKey': | ||
return ''; | ||
|
||
default: | ||
return `${key} = ${value}`; | ||
} | ||
}) | ||
.filter(Boolean) | ||
.join(',') | ||
.trim(); | ||
return line; | ||
}); | ||
} | ||
|
||
get MITM() { | ||
return this.content.mitm?.hostname?.join(', '); | ||
} | ||
|
||
#getDefaultValue(defaultValue: any): any { | ||
switch (typeof defaultValue) { | ||
case 'string': | ||
return `"${defaultValue}"`; | ||
case 'number': | ||
case 'boolean': | ||
return defaultValue; | ||
case 'object': | ||
if (Array.isArray(defaultValue) && defaultValue.length > 0) { | ||
return this.#getDefaultValue(defaultValue[0]); | ||
} | ||
return '""'; | ||
default: | ||
return '""'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,5 @@ | |
"outDir": "./dist", | ||
"baseUrl": "./" | ||
}, | ||
"include": ["src"] | ||
"include": ["src", "types"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import '@iringo/modkit-shared'; | ||
import type { LoonArgumentType } from '../dist/index'; | ||
|
||
declare module '@iringo/modkit-shared' { | ||
interface PluginModuleContent { | ||
loonGeneral?: Record<string, string>; | ||
} | ||
|
||
interface PluginArgumentType { | ||
loon?: LoonArgumentType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.