Skip to content

Instantly share code, notes, and snippets.

@zjx20
Last active February 7, 2024 02:04
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zjx20/25dff5a670ca55d8aba223f508691dfb to your computer and use it in GitHub Desktop.
Save zjx20/25dff5a670ca55d8aba223f508691dfb to your computer and use it in GitHub Desktop.
Convert v2ray config.json (client side) to a subscription content
#!/bin/bash
# brew install jq
# brew install qrencode
config_file=v2rayx*.json
rss_file=rss.txt
cat $config_file | \
jq --raw-output '
def cred:
"\(.settings.vnext[0].users[0].security):\(.settings.vnext[0].users[0].id)@\(.settings.vnext[0].address):\(.settings.vnext[0].port)"
;
def network:
if .streamSettings.network == "ws" then
"path=\(.streamSettings.wsSettings.path)&obfs=websocket&obfsParam=\(.streamSettings.tlsSettings.serverName)"
else
"obfs=none"
end
;
def tls:
if .streamSettings.security == "tls" then
"tls=1&peer=\(.streamSettings.tlsSettings.serverName)"
else
"tls=0"
end
;
def mux:
if .mux?.enabled? then "mux=1" else "mux=0" end
;
def alterid:
"\(.settings.vnext[0].users[0].alterId)"
;
.outbounds | map(select(.protocol == "vmess")) | .[] |
cred as $cred |
network as $network |
tls as $tls |
mux as $mux |
alterid as $alterid |
{ cred: $cred, remarks: .tag, network: $network, tls: $tls, mux: $mux, alterid: $alterid } |
.cred = @base64 "\(.cred)" | .remarks = @uri "\(.remarks)" |
"vmess://\(.cred)?remarks=\(.remarks)&\(.network)&\(.tls)&\(.mux)&alterId=\(.alterid)"' | \
base64 > $rss_file
# put $rss_file to somewhere, then your qr code for the subscribe link is
rss_url="https://yourserver/rss.txt"
qrencode -t ANSI $(echo "sub://$(echo "$rss_url" | base64)")
@agn-7
Copy link

agn-7 commented Nov 2, 2022

Does this script convert a V2ray config.json to a vmess URL?

@zjx20
Copy link
Author

zjx20 commented Nov 3, 2022

Does this script convert a V2ray config.json to a vmess URL?

@agn-7 Yes, if you base64 decode the output file.

@agn-7
Copy link

agn-7 commented Nov 3, 2022

@zjx20 But I don't why neither the generated QRcode nor rss.txt content work as an imported config file.

Here's my config file:

{
	"log": {
		"loglevel": "warning"
	},
	"inbounds": [{
			"port": 1080,
			"protocol": "socks",
			"settings": {
				"udp": true
			}
		},
		{
			"port": 8880,
			"protocol": "http",
			"allowTransparent": false,
			"userLevel": 0
		}
	],
	"outbounds": [{
			"protocol": "vmess",
			"settings": {
				"vnext": [{
					"address": "xxxx",
					"port": 18345,
					"users": [{
						"id": "xxxx",
						"alterId": 0,
						"security": "aes-128-gcm"

					}]
				}]
			},
			"streamSettings": {
				"network": "ws",
				"wsSettings": {
					"path": "/vmess"

				}
			}
		},
		{
			"protocol": "freedom",
			"tag": "direct"
		}
	],
	"routing": {
		"domainStrategy": "IPOnDemand",
		"rules": [

			{

				"domain": ["ext:iran.dat:ir"],
				"outboundTag": "direct",
				"type": "field"
			},
			{

				"domain": ["regexp:.*\\.ir$"],
				"outboundTag": "direct",
				"type": "field"
			},
			{
				"domain": ["regexp:.*\\.cab$"],
				"outboundTag": "direct",
				"type": "field"
			},
			{

				"domain": ["ext:iran.dat:other"],
				"outboundTag": "direct",
				"type": "field"
			},
			{
				"domain": ["ext:iran.dat:other"],
				"outboundTag": "direct",
				"type": "field"
			}
		]
	}
}

But this config file does work without any problem as a configuration on v2ray-core.

@zjx20
Copy link
Author

zjx20 commented Nov 3, 2022

@agn-7
shadowrocket can recognize the QR code and the content. Since there isn't a standard for the vmess URL schema and the subscription, don't expect it works for every app.

After converting your config file, we get:

$ cat rss.txt | base64 -d
vmess://YWVzLTEyOC1nY206eHh4eEB4eHh4OjE4MzQ1?remarks=null&path=/vmess&obfs=websocket&obfsParam=null&tls=0&mux=0&alterId=0

Since you're not enabling TLS, the script produced a null value for the obfsParam param, which might cause problems.

@agn-7
Copy link

agn-7 commented Nov 3, 2022

@zjx20
Thank you for your instant response. However, I couldn't import this vmess URL into Nekoray or V2rayNG apps.

@zjx20
Copy link
Author

zjx20 commented Nov 3, 2022

@agn-7
I don't use both of them, but I found a URL example from here. It's a totally different format than the one used in this script, so it won't work. I do not intend to add support for that, you can try it yourself.

@agn-7
Copy link

agn-7 commented Nov 4, 2022

@zjx20
ooh, thanks. I didn't know there are different types of vmess URL !

@mulezic
Copy link

mulezic commented Feb 6, 2024

@zjx20
I would also love to know how to convert a JSON file to a URL, but the English of "base64 decode output file" is just way too much for me, I'm not an IT guy.

@zjx20
Copy link
Author

zjx20 commented Feb 6, 2024

@mulezic Sorry, I can't help you if you are not familiar with this IT stuff (e.g. shell scripts, base64 encoding, etc.). Maybe you can say more about what you're trying to do, and maybe it can be solved in a different way.

@mulezic
Copy link

mulezic commented Feb 6, 2024 via email

@zjx20
Copy link
Author

zjx20 commented Feb 7, 2024

@mulezic OK, I understand. You can tell me what server software or script you are using, what client you are planning to use, and also a sample json config (without sensitive information). I can help you determine the feasibility of the situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment