This repository has been archived by the owner on Feb 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support sync runtime config to github gist
- Loading branch information
pompurin404
committed
Sep 22, 2024
1 parent
1402a28
commit edab0fb
Showing
7 changed files
with
147 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
### Features | ||
|
||
- 支持在特定WiFi SSID下直连 | ||
- 支持同步运行时配置到GitHub Gist | ||
|
||
### Bug Fixes | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import axios from 'axios' | ||
import { getAppConfig, getControledMihomoConfig } from '../config' | ||
import { getRuntimeConfigStr } from '../core/factory' | ||
|
||
interface GistInfo { | ||
id: string | ||
description: string | ||
files: Record<string, { filename: string; raw_url: string }> | ||
} | ||
|
||
async function listGists(token: string): Promise<GistInfo[]> { | ||
const { 'mixed-port': port = 7890 } = await getControledMihomoConfig() | ||
const res = await axios.get('https://api.github.com/gists', { | ||
headers: { | ||
Accept: 'application/vnd.github+json', | ||
Authorization: `Bearer ${token}`, | ||
'X-GitHub-Api-Version': '2022-11-28' | ||
}, | ||
proxy: { | ||
protocol: 'http', | ||
host: '127.0.0.1', | ||
port | ||
} | ||
}) | ||
return res.data as GistInfo[] | ||
} | ||
|
||
async function createGist(token: string, content: string): Promise<void> { | ||
const { 'mixed-port': port = 7890 } = await getControledMihomoConfig() | ||
return await axios.post( | ||
'https://api.github.com/gists', | ||
{ | ||
description: 'Auto Synced Mihomo Party Runtime Config', | ||
public: false, | ||
files: { 'mihomo-party.yaml': { content } } | ||
}, | ||
{ | ||
headers: { | ||
Accept: 'application/vnd.github+json', | ||
Authorization: `Bearer ${token}`, | ||
'X-GitHub-Api-Version': '2022-11-28' | ||
}, | ||
proxy: { | ||
protocol: 'http', | ||
host: '127.0.0.1', | ||
port | ||
} | ||
} | ||
) | ||
} | ||
|
||
async function updateGist(token: string, id: string, content: string): Promise<void> { | ||
const { 'mixed-port': port = 7890 } = await getControledMihomoConfig() | ||
return await axios.patch( | ||
`https://api.github.com/gists/${id}`, | ||
{ | ||
description: 'Auto Synced Mihomo Party Runtime Config', | ||
files: { 'mihomo-party.yaml': { content } } | ||
}, | ||
{ | ||
headers: { | ||
Accept: 'application/vnd.github+json', | ||
Authorization: `Bearer ${token}`, | ||
'X-GitHub-Api-Version': '2022-11-28' | ||
}, | ||
proxy: { | ||
protocol: 'http', | ||
host: '127.0.0.1', | ||
port | ||
} | ||
} | ||
) | ||
} | ||
|
||
export async function getGistUrl(): Promise<string> { | ||
const { githubToken } = await getAppConfig() | ||
if (!githubToken) return '' | ||
const gists = await listGists(githubToken) | ||
const gist = gists.find((gist) => gist.description === 'Auto Synced Mihomo Party Runtime Config') | ||
if (gist) { | ||
return gist.files['mihomo-party.yaml'].raw_url | ||
} else { | ||
throw new Error('Gist not found') | ||
} | ||
} | ||
|
||
export async function uploadRuntimeConfig(): Promise<void> { | ||
const { githubToken } = await getAppConfig() | ||
if (!githubToken) return | ||
const gists = await listGists(githubToken) | ||
console.log(gists) | ||
const gist = gists.find((gist) => gist.description === 'Auto Synced Mihomo Party Runtime Config') | ||
const config = await getRuntimeConfigStr() | ||
if (gist) { | ||
await updateGist(githubToken, gist.id, config) | ||
} else { | ||
await createGist(githubToken, config) | ||
} | ||
} |
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 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