Skip to content

Commit

Permalink
Merge pull request #49 from vvbbnn00/dev
Browse files Browse the repository at this point in the history
feat: Add support for selecting 'proxy & proxy group' and 'only proxy…
  • Loading branch information
vvbbnn00 authored Feb 22, 2024
2 parents ab6ddca + ae6d43d commit b87f7af
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
18 changes: 11 additions & 7 deletions services/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
def generateClashSubFile(account: Account = None,
logger=logging.getLogger(__name__),
best=False,
only_proxies=False,
proxy_format='full',
random_name=False):
"""
Generate Clash subscription file
:param random_name: Whether to use random name
:param only_proxies: If this is True, only generate proxies
:param proxy_format: full - full config, with_groups - only proxies and proxy-groups, only_proxies - only proxies
:param account:
:param logger:
:param best: Whether to use the best entrypoints
Expand Down Expand Up @@ -74,8 +74,12 @@ def generateClashSubFile(account: Account = None,
proxy_group["proxies"] += [proxy["name"] for proxy in user_config]

# Generate YAML file
if only_proxies:
clash_yaml = yaml.dump({'proxies': clash_json['proxies'], 'proxy-groups': clash_json['proxy-groups']},
if proxy_format == 'only_proxies':
clash_yaml = yaml.dump({'proxies': clash_json['proxies']},
allow_unicode=True)
elif proxy_format == 'with_groups':
clash_yaml = yaml.dump({'proxies': clash_json['proxies'],
'proxy-groups': clash_json['proxy-groups']},
allow_unicode=True)
else:
clash_yaml = yaml.dump(clash_json, allow_unicode=True)
Expand Down Expand Up @@ -115,12 +119,12 @@ def generateWireguardSubFile(account: Account = None,
def generateSurgeSubFile(account: Account = None,
logger=logging.getLogger(__name__),
best=False,
only_proxies=False,
proxy_format='full',
random_name=False):
"""
Generate Surge subscription file
:param random_name: Whether to use random name
:param only_proxies: If this is True, only generate proxies
:param proxy_format: full - full config, with_groups - only proxies and proxy-groups, only_proxies - only proxies
:param account:
:param logger:
:param best: Whether to use the best entrypoints
Expand Down Expand Up @@ -174,7 +178,7 @@ def generateSurgeSubFile(account: Account = None,
surge_ini = temp_file.read()

# Generate INI file
if only_proxies:
if proxy_format == 'with_groups' or proxy_format == 'only_proxies':
pass
else:
surge_ini += SURGE_RULE
Expand Down
17 changes: 14 additions & 3 deletions services/web_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,14 @@ def httpSubscription(sub_type: str):
account = getCurrentAccount(logger)
best = request.args.get('best', 'false').lower() == "true" or False
random_name = request.args.get('randomName', 'false').lower() == "true" or False
proxy_format = request.args.get('proxyFormat', 'full').lower()

if sub_type == "clash": # Clash
fileData = generateClashSubFile(account, logger, best=best, only_proxies=False, random_name=random_name)
fileData = generateClashSubFile(account,
logger,
best=best,
proxy_format=proxy_format,
random_name=random_name)
headers = {
'Content-Type': 'application/x-yaml; charset=utf-8',
'Content-Disposition': f'attachment; filename=Clash-{fake.color_name()}.yaml',
Expand All @@ -137,7 +142,11 @@ def httpSubscription(sub_type: str):
'Content-Disposition': f'attachment; filename={fake.lexify("????????????").lower()}.conf'
}
elif sub_type == "surge": # Surge
fileData = generateSurgeSubFile(account, logger, best=best, random_name=random_name)
fileData = generateSurgeSubFile(account,
logger,
best=best,
random_name=random_name,
proxy_format=proxy_format)
headers = {
'Content-Type': 'text/plain; charset=utf-8',
'Content-Disposition': 'attachment; filename=surge.conf',
Expand All @@ -152,8 +161,10 @@ def httpSubscription(sub_type: str):
"Subscription-Userinfo": f"upload=0; download={account.usage}; total={account.quota}; "
f"expire=253388144714"
}
# This might be deprecated in the future.
elif sub_type == "only_proxies": # Only proxies
fileData = generateClashSubFile(account, logger, best=best, only_proxies=True, random_name=random_name)
fileData = generateClashSubFile(account, logger, best=best, proxy_format='with_groups',
random_name=random_name)
headers = {
'Content-Type': 'application/x-yaml; charset=utf-8',
'Content-Disposition': f'attachment; filename=Clash-{fake.color_name()}.yaml',
Expand Down
49 changes: 21 additions & 28 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@
border: none;
text-align: center;
width: 100%;
max-width: 500px;
font-size: 16px;
margin-bottom: 10px;
word-break: break-all;
}

textarea:focus {
Expand Down Expand Up @@ -101,10 +103,10 @@
</div>

<h1>WARP Clash 订阅地址生成器</h1>
<form>
<form action="javascript:generateLink()">
<div>
<label for="passwordInput"></label>
<input type="password" id="passwordInput" placeholder="输入您的SECRET_KEY" oninput="generateLink()">
<input type="password" name="password" id="passwordInput" placeholder="输入您的SECRET_KEY">
</div>
<div>
<label>
Expand All @@ -114,30 +116,32 @@ <h1>WARP Clash 订阅地址生成器</h1>
<input type="checkbox" id="randomName" onchange="generateLink()" checked> 随机节点名称
</label>
</div>
<div>
<br/>
<label for="proxyFormat">订阅格式:</label>
<select id="proxyFormat" onchange="generateLink()">
<option value="full">完整订阅</option>
<option value="with_groups">Proxies & Proxy Groups</option>
<option value="only_proxies">仅包含Proxies</option>
</select>
</div>
</form>
<center>
<h2>自动判断订阅类型</h2>
<textarea id="autoSubscriptionLink" rows="1" readonly onclick="copyText('autoSubscriptionLink')"></textarea>
<textarea id="autoSubscriptionLink" rows="3" readonly onclick="copyText('autoSubscriptionLink')"></textarea>
<div id="qrcode_auto"></div>
<h2>Clash</h2>
<textarea id="subscriptionLink" rows="1" readonly onclick="copyText('subscriptionLink')"></textarea>
<textarea id="subscriptionLink" rows="3" readonly onclick="copyText('subscriptionLink')"></textarea>
<div id="qrcode_clash"></div>
<h2>ShadowRocket</h2>
<textarea id="subShadowRocketSubscriptionLink" rows="1" readonly
<textarea id="subShadowRocketSubscriptionLink" rows="3" readonly
onclick="copyText('subShadowRocketSubscriptionLink')"></textarea>
<div id="qrcode_sr"></div>
<h2>Surge</h2>
<textarea id="subSurgeScriptionLink" rows="1" readonly onclick="copyText('subSurgeScriptionLink')"></textarea>
<textarea id="subSurgeScriptionLink" rows="3" readonly onclick="copyText('subSurgeScriptionLink')"></textarea>
<h2>Wireguard</h2>
<textarea id="WireguardLink" rows="1" readonly onclick="copyText('WireguardLink')"></textarea>
<textarea id="WireguardLink" rows="3" readonly onclick="copyText('WireguardLink')"></textarea>
<div id="qrcode_wg"></div>
<h2>Only Proxies & Proxies Group</h2>
<div class="tip">
该链接仅包含节点信息,不包含规则信息,适用于自定义规则的用户。
</div>
<textarea id="onlyProxyLink" rows="1" readonly onclick="copyText('onlyProxyLink')"></textarea>
<a onclick="getOnlyProxy()" href="javascript:">点击复制节点信息</a>
<textarea id="onlyProxyInfo" rows="0" style="height: 0; width: 0;"></textarea>
<h2>账户信息</h2>
<a href="#" title="账户信息" target="_blank" id="AccountLink">点击查看</a>
</center>
Expand All @@ -155,10 +159,12 @@ <h2>账户信息</h2>
const password = document.getElementById('passwordInput').value;
const best = document.getElementById('alwaysBest').checked;
const randomName = document.getElementById('randomName').checked;
const proxyFormat = document.getElementById('proxyFormat').value;
const baseUrl = location.protocol + '//' + location.host + '/';
const queryParams = new URLSearchParams({
best,
randomName
randomName,
proxyFormat
});

if (password.length) {
Expand All @@ -171,7 +177,6 @@ <h2>账户信息</h2>
document.getElementById('subSurgeScriptionLink').textContent = baseUrl + "api/surge?" + queryParams.toString();
document.getElementById('WireguardLink').textContent = baseUrl + "api/wireguard?" + queryParams.toString();
document.getElementById('AccountLink').href = baseUrl + "api/account?" + queryParams.toString();
document.getElementById('onlyProxyLink').textContent = baseUrl + "api/only_proxies?" + queryParams.toString();

const qrcodeContainerClash = document.getElementById('qrcode_clash');
qrcodeContainerClash.innerHTML = '';
Expand Down Expand Up @@ -217,18 +222,6 @@ <h2>账户信息</h2>
}, 1000);
}

function getOnlyProxy() {
const url = document.getElementById('onlyProxyLink').textContent;
fetch(url, {
method: 'GET',
}).then(res => res.text()).then(res => {
document.getElementById('onlyProxyInfo').textContent = res;
copyText('onlyProxyInfo');
}).catch(err => {
showToast(`Error ${err.status}: ${err.statusText}`)
});
}

function getQrCodeWg() {
const qrcodeContainerWg = document.getElementById('qrcode_wg');
const url = document.getElementById('WireguardLink').textContent;
Expand Down

0 comments on commit b87f7af

Please sign in to comment.