Skip to content

Commit

Permalink
Add option only_proxies and random_name.
Browse files Browse the repository at this point in the history
- `only_proxies`: This option determines whether the API returns built-in Clash rules.
- `random_name`: This option controls whether the API generates random node names. When set to false, the node name will take the form of `CF-WARP-1`.
  • Loading branch information
vvbbnn00 committed Nov 23, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 9097ab3 commit 53b0de6
Showing 3 changed files with 55 additions and 28 deletions.
17 changes: 11 additions & 6 deletions services/subscription.py
Original file line number Diff line number Diff line change
@@ -12,9 +12,15 @@
CLASH = json.load(open("./config/clash.json", "r", encoding="utf8"))


def generate_Clash_subFile(account: Account = None, logger=logging.getLogger(__name__), best=False, only_proxies=False):
def generate_Clash_subFile(account: Account = None,
logger=logging.getLogger(__name__),
best=False,
only_proxies=False,
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 account:
:param logger:
:param best: Whether to use the best entrypoints
@@ -30,13 +36,12 @@ def generate_Clash_subFile(account: Account = None, logger=logging.getLogger(__n
user_config = []

# Use len() instead of RANDOM_COUNT because the entrypoints may be less than RANDOM_COUNT
j = 1
for i in range(len(random_points)):
point = random_points[i]
name = f"{fake.emoji()} CF-{fake.color_name()}" if random_name else f"CF-WARP-{i + 1}"
user_config.append(
{
# "name": f"{fake.emoji()} CF-{fake.color_name()}",
"name": f"CF-WARP-{j}",
"name": name,
"type": "wireguard",
"server": point.ip,
"port": point.port,
@@ -46,15 +51,15 @@ def generate_Clash_subFile(account: Account = None, logger=logging.getLogger(__n
"remote-dns-resolve": False,
"udp": False
})
j += 1
clashJSON = copy.deepcopy(CLASH)
clashJSON["proxies"] = user_config
for proxyGroup in clashJSON["proxy-groups"]:
proxyGroup["proxies"] += [proxy["name"] for proxy in user_config]

# Generate YAML file
if only_proxies:
clashYAML = yaml.dump({'proxies': clashJSON['proxies'], 'proxy-groups': clashJSON['proxy-groups']}, allow_unicode=True)
clashYAML = yaml.dump({'proxies': clashJSON['proxies'], 'proxy-groups': clashJSON['proxy-groups']},
allow_unicode=True)
else:
clashYAML = yaml.dump(clashJSON, allow_unicode=True)
return clashYAML
18 changes: 14 additions & 4 deletions services/web_service.py
Original file line number Diff line number Diff line change
@@ -96,7 +96,9 @@ def http_account():
def http_clash():
account = getCurrentAccount(logger)
best = request.args.get('best') or False
fileData = generate_Clash_subFile(account, logger, best=best)
random_name = request.args.get('randomName').lower() == "true" or False

fileData = generate_Clash_subFile(account, logger, best=best, random_name=random_name)

headers = {
'Content-Type': 'application/x-yaml; charset=utf-8',
@@ -115,6 +117,7 @@ def http_clash():
def http_wireguard():
account = getCurrentAccount(logger)
best = request.args.get('best') or False

fileData = generate_Wireguard_subFile(account, logger, best=best)

headers = {
@@ -133,13 +136,20 @@ def http_wireguard():
def only_proxies():
account = getCurrentAccount(logger)
best = request.args.get('best') or False
fileData = generate_Clash_subFile(account, logger, best=best, only_proxies=True)
random_name = request.args.get('randomName').lower() == "true" or False

fileData = generate_Clash_subFile(account, logger, best=best, only_proxies=True, random_name=random_name)

response = make_response(fileData)
# response.headers = headers
headers = {
'Content-Type': 'application/x-yaml; charset=utf-8',
'Content-Disposition': f'attachment; filename=Clash-{fake.color_name()}.yaml',
"Subscription-Userinfo": f"upload=0; download={account.usage}; total={account.quota}; expire=253388144714"
}

response.headers = headers

return response



def create_app(app_name: str = "web", logger: logging.Logger = None) -> Flask:
48 changes: 30 additions & 18 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@
left: 50%;
transform: translateX(-50%);
padding: 10px 20px;
background-color: rgba(110, 255, 114, 0.8);
background-color: rgba(110, 255, 114);
color: #4caf50;
border-radius: 4px;
font-size: 16px;
@@ -85,6 +85,12 @@
.show {
opacity: 1;
}

.tip {
color: #666;
font-size: 14px;
margin-bottom: 10px;
}
</style>
</head>
<body>
@@ -104,6 +110,9 @@ <h1>WARP Clash 订阅地址生成器</h1>
<label>
<input type="checkbox" id="alwaysBest" onchange="generateLink()"> 始终选择最优节点
</label>
<label>
<input type="checkbox" id="randomName" onchange="generateLink()" checked> 随机节点名称
</label>
</div>
</form>
<center>
@@ -113,8 +122,12 @@ <h2>Clash</h2>
<h2>Wireguard</h2>
<textarea id="WireguardLink" rows="1" readonly onclick="copyText('WireguardLink')"></textarea>
<h2>Only Proxies & Proxies Group</h2>
<textarea id="proxy_info" rows="0"></textarea>
<span readonly onclick="copyText('proxy_info')">点击复制节点信息</span>
<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>
@@ -131,9 +144,11 @@ <h2>账户信息</h2>
function generateLink() {
const password = document.getElementById('passwordInput').value;
const best = document.getElementById('alwaysBest').checked;
const randomName = document.getElementById('randomName').checked;
const baseUrl = location.protocol + '//' + location.host + '/api/';
const queryParams = new URLSearchParams({
best
best,
randomName
});

if (password.length) {
@@ -143,6 +158,7 @@ <h2>账户信息</h2>
document.getElementById('subscriptionLink').textContent = baseUrl + "clash?" + queryParams.toString();
document.getElementById('WireguardLink').textContent = baseUrl + "wireguard?" + queryParams.toString();
document.getElementById('AccountLink').href = baseUrl + "account?" + queryParams.toString();
document.getElementById('onlyProxyLink').textContent = baseUrl + "only_proxies?" + queryParams.toString();

const qrcodeContainer = document.getElementById('qrcode');
qrcodeContainer.innerHTML = '';
@@ -151,8 +167,6 @@ <h2>账户信息</h2>
width: 200,
height: 200
});
const url = baseUrl + "only_proxies?" + queryParams.toString();
getOnlyProxy(url)
}

function copyText(elem) {
@@ -172,18 +186,16 @@ <h2>账户信息</h2>
}, 1000);
}

function getOnlyProxy(url) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url)
xhr.send()
xhr.onload = function() {
if (xhr.status != 200) {
showToast(`Error ${xhr.status}: ${xhr.statusText}`)
} else {
document.getElementById('proxy_info').textContent = '';
document.getElementById('proxy_info').textContent = xhr.response;
}
}
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}`)
});
}

generateLink()

0 comments on commit 53b0de6

Please sign in to comment.