Configuring V2ray on a Mac

Since simple-obfs is no longer maintained, I have been planning to upgrade the services I use, and recently I started to disconnect from the process of searching for information, so I decided to find time to study it. Here is the quick process of service upgrade and the trampling records.

Introduction

An introduction to V2ray can be found on its official website.
Compared with Shadowsocks, it is not a strict C/S architecture, which means that our installation process will be slightly different.

Step

1. Install V2ray on Server

Take CentOS as an example, assuming that you already have root privileges, first, run the following code on the server

1
bash <(curl -L -s https://install.direct/go.sh)

Remember the version number of the automatic installation, such as v4.10.2

2. Change Config on Server

Edit /etc/v2ray/config.jsonfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
"log":{
"loglevel": "error",
"access": "/var/log/v2ray/access.log",
"error": "/var/log/v2ray/error.log"
},
"inbounds": [{
"port": 8880, // change to your port
"protocol": "vmess",
"listen":"123.123.123.123", // change to your ip
"settings": {
"clients": [{ "id": "12312312-1234-1234-1234-123412341234" }] //change to your custom id
}
}],
"outbounds": [{
"protocol": "freedom",
"settings": {}
}]
}

Run the following command to start v2ray
1
service v2ray start

3. Install V2ray on MacOS

Find the version number recorded in step 1, download the corresponding version of v2ray-core, and download the address here.
After downloading and unzipping, assume that the decompressed path is $V2RAY_PATH.

4. Change Config on MacOS

Create a new file like v2ray_config.json, assuming its path is $V2RAY_CONFIG_PATH.
Edit v2ray_config.json:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
"inbounds": [{
"port": 1081,
"listen": "127.0.0.1",
"protocol": "socks",
"settings": {
"udp": true
}
}],
"outbounds": [{
"protocol": "vmess",
"settings": {
"vnext": [{
"address": "123.123.123.123",
"port": 8880,
"users": [{ "id": "12312312-1234-1234-1234-123412341234" }]
}]
}
},{
"protocol": "freedom",
"settings": {}
}],
"routing": {
"domainStrategy": "IPOnDemand",
"rules": [{
"type": "field",
"ip": ["geoip:private"],
"outboundTag": "direct"
}]
}
}

5. Make V2ray Application Run on Startup

Create a new shell file like v2rayAutoStart.sh, assuming its path is $V2RAY_SHELL_PATH:

1
2
3
#!/bin/bash

$V2RAY_PATH/v2ray -config $V2RAY_CONFIG_PATH/v2rayconfig.json

It is now possible to start v2ray with the shell file. Next we need to configure the “boot from the start”, there are two ways, the common way online is to add the shell file to Login Items in Users & Groups of System Preferences, but this method will lead to There is a command line window popping up at the front desk when booting up, which has an impact on the user experience.
So the method I use is to open Automator, choose to create an Application, then select Run shell Script and enter the following command on the right side.
1
nohup sh $V2RAY_SHELL_PATH/v2rayAutoStart.sh &;

Click Save and add the Application to Login Items in Users & Groups of System Preferences.

At this point, all the settings have been completed, after the boot, there will be a constantly rotating white gear in the status bar, indicating that v2ray is always running.

Other Q & A

Q: Why not use GUI tools like V2rayX?
A: V2ray requires that both versions of the connection must be the same, and the kernel version carried by the GUI tool will usually be slightly older.

Q: Why copy the above configuration but it won’t work?
A: The article uses $ to refer to the real absolute path, so it is recommended to replace it with a complete absolute path during the actual configuration process.

Q: I want to use with shadowsocks / add encryption / add HTTP proxy, how to configure?
A: Modify the corresponding configuration file according to the official website.