Skip to content

Instantly share code, notes, and snippets.

@SimonTheCoder
Created May 12, 2022 05:38
Show Gist options
  • Save SimonTheCoder/7b8e1aa02e5c7a91d74772f8c161f9b6 to your computer and use it in GitHub Desktop.
Save SimonTheCoder/7b8e1aa02e5c7a91d74772f8c161f9b6 to your computer and use it in GitHub Desktop.
n8n scirpt to auto switch clash node
{
"name": "Clash auto switch",
"nodes": [
{
"parameters": {},
"name": "Start",
"type": "n8n-nodes-base.start",
"typeVersion": 1,
"position": [
240,
320
]
},
{
"parameters": {
"url": "={{$node[\"Server Config\"].parameter[\"values\"][\"string\"][0][\"value\"]}}/proxies",
"options": {}
},
"name": "HTTP Request",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
560,
320
]
},
{
"parameters": {
"functionCode": "// Code here will run only once, no matter how many input items there are.\n// More info and help: https://docs.n8n.io/nodes/n8n-nodes-base.function\n// Tip: You can use luxon for dates and $jmespath for querying JSON structures\n\n// Loop over inputs and add a new field called 'myNewField' to the JSON of each one\nproxies = items[0].json.proxies\nconsole.log(proxies)\n\nlet proxy_list = []\nfor(let proxy in proxies){\n //console.log(proxy)\n let proxy_obj = proxies[proxy]\n if(proxy_obj.name.match(/-/))\n {\n proxy_list.push({\n json:proxy_obj\n })\n }\n}\n\nconsole.log(items)\n// You can write logs to the browser console\nconsole.log('Done!');\n\nreturn proxy_list;"
},
"name": "Function",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
720,
320
]
},
{
"parameters": {
"url": "={{$node[\"Server Config\"].parameter[\"values\"][\"string\"][0][\"value\"]}}/proxies/{{$node[\"Function\"].json[\"name\"]}}/delay",
"options": {},
"queryParametersUi": {
"parameter": [
{
"name": "url",
"value": "http://www.gstatic.com/generate_204"
},
{
"name": "timeout",
"value": "10000"
}
]
}
},
"name": "HTTP Request1 delay",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
720,
540
],
"alwaysOutputData": false,
"continueOnFail": true
},
{
"parameters": {
"values": {
"string": [
{
"name": "name",
"value": "={{$node[\"Function\"].json[\"name\"]}}"
}
],
"number": [
{
"name": "delay",
"value": "={{$json[\"delay\"]}}"
}
]
},
"options": {}
},
"name": "Set",
"type": "n8n-nodes-base.set",
"typeVersion": 1,
"position": [
900,
540
]
},
{
"parameters": {
"values": {
"string": [
{
"name": "server_url",
"value": "http://YOUR_CLASH_EXTERNEL_CONTROL_IP:PORT"
}
]
},
"options": {}
},
"name": "Server Config",
"type": "n8n-nodes-base.set",
"typeVersion": 1,
"position": [
400,
320
]
},
{
"parameters": {
"requestMethod": "PUT",
"url": "={{$node[\"Server Config\"].parameter[\"values\"][\"string\"][0][\"value\"]}}/proxies/Proxy",
"responseFormat": "string",
"options": {},
"bodyParametersUi": {
"parameter": [
{
"name": "name",
"value": "={{$json[\"json\"][\"name\"]}}"
}
]
}
},
"name": "HTTP Request switch",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
1280,
540
]
},
{
"parameters": {
"functionCode": "// Code here will run only once, no matter how many input items there are.\n// More info and help: https://docs.n8n.io/nodes/n8n-nodes-base.function\n// Tip: You can use luxon for dates and $jmespath for querying JSON structures\n\n// Loop over inputs and add a new field called 'myNewField' to the JSON of each one\nlet list = []\nfor (item of items) {\n console.log(item)\n server = item.json\n if(server.delay){\n list.push(server)\n }\n}\n\nlist.sort((a,b)=>a.delay-b.delay)\n\n// You can write logs to the browser console\nconsole.log('Done!');\n\nreturn { json:list[0] };"
},
"name": "Function_sort_by_delay",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
1080,
540
]
},
{
"parameters": {
"interval": 10,
"unit": "minutes"
},
"name": "Interval",
"type": "n8n-nodes-base.interval",
"typeVersion": 1,
"position": [
240,
500
]
}
],
"connections": {
"HTTP Request": {
"main": [
[
{
"node": "Function",
"type": "main",
"index": 0
}
]
]
},
"Function": {
"main": [
[
{
"node": "HTTP Request1 delay",
"type": "main",
"index": 0
}
]
]
},
"Start": {
"main": [
[
{
"node": "Server Config",
"type": "main",
"index": 0
}
]
]
},
"HTTP Request1 delay": {
"main": [
[
{
"node": "Set",
"type": "main",
"index": 0
}
]
]
},
"Set": {
"main": [
[
{
"node": "Function_sort_by_delay",
"type": "main",
"index": 0
}
]
]
},
"Server Config": {
"main": [
[
{
"node": "HTTP Request",
"type": "main",
"index": 0
}
]
]
},
"Function_sort_by_delay": {
"main": [
[
{
"node": "HTTP Request switch",
"type": "main",
"index": 0
}
]
]
},
"Interval": {
"main": [
[
{
"node": "Server Config",
"type": "main",
"index": 0
}
]
]
}
},
"active": true,
"settings": {},
"id": 9
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment