-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add createShortSub function for generating short subscription link
- Loading branch information
Showing
4 changed files
with
115 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { encode } from 'js-base64'; | ||
|
||
export const createShortSub = async (subLink: string) => { | ||
if (!subLink) throw Error('请在生成订阅链接后再试'); | ||
|
||
const formData = new FormData(); | ||
formData.append('longUrl', encode(subLink)); | ||
|
||
const res = await fetch(`${process.env.NEXT_PUBLIC_SHORTURL}short`,{ | ||
method: 'POST', | ||
body: formData, | ||
}); | ||
|
||
if (res.status === 200) { | ||
const data = await res.json(); | ||
if (data.Code !== 1) throw new Error(data.Message); | ||
return data.ShortUrl | ||
} | ||
} |
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,34 @@ | ||
import { config as cfg } from '@/config' | ||
|
||
const backends = process.env.NEXT_PUBLIC_BACKENDS?.split('|') ?? ["http://127.0.0.1:25500/sub?"] | ||
|
||
export const createSub = (params: Params) => { | ||
const { url, target, backend, mode, config, include, exclude, tfo, udp, scv, append_type, emoji, list } = params; | ||
|
||
if (!url) throw Error('请在输入订阅链接后再试'); | ||
if (!target) throw Error('请在选择客户端后再试'); | ||
|
||
const flow = [ | ||
backend || backends[0], | ||
`target=${cfg.clients[target as keyof typeof cfg.clients]}`, | ||
`&url=${encodeURIComponent(url.replace(/\n/g, '|'))}`, | ||
'&insert=false', | ||
]; | ||
|
||
if (params.mode === 'hard') { | ||
const configItem = cfg.remoteConfig.flatMap(category => category.items).find(item => item.label === config); | ||
const configValue = configItem ? configItem.value : config; | ||
|
||
if (config) flow.push(`&config=${encodeURIComponent(configValue)}`); | ||
if (include) flow.push(`&include=${encodeURIComponent(include)}`); | ||
if (exclude) flow.push(`&exclude=${encodeURIComponent(exclude)}`); | ||
if (tfo) flow.push('&tfo=true'); | ||
if (udp) flow.push('&udp=true'); | ||
if (scv) flow.push('&scv=true'); | ||
if (append_type) flow.push('&append_type=true'); | ||
if (emoji) flow.push('&emoji=true'); | ||
if (list) flow.push('&list=true'); | ||
} | ||
|
||
return flow.join('') | ||
} |
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