-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.ps1
executable file
·259 lines (224 loc) · 6.43 KB
/
setup.ps1
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/pwsh
function Get-IsRoot {
return ((id -u) -eq 0)
}
function Enter-Settings {
$port = Read-Host -Prompt "输入监听端口 (默认: 443)"
if ($port -eq "" -or $null -eq $port) {
$port = 443
}
$port = [int]$port
Write-Host "选择: $port"
$dest = Read-Host -Prompt "输入伪装域名 (例: example.com:443)"
if ($dest -eq "" -or $null -eq $dest) {
Throw "伪装域名不能为空"
}
Write-Host "选择: $dest"
$serverNames = Read-Host -Prompt "输入客户端可用的 ServerName 列表, 以英文逗号分隔"
if ($serverNames -eq "" -or $null -eq $serverNames) {
Throw "客户端可用的 ServerName 列表不能为空"
}
$serverNames = $serverNames.Split(",")
return @{
port = $port
dest = $dest
serverNames = $serverNames
}
}
function Install-Xray {
systemctl stop xray
Write-Host "开始安装 Xray..."
curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh | bash -s -- install
if ($LASTEXITCODE -ne 0) {
Throw "Xray 安装失败"
}
systemctl stop xray
chown root:root -R /var/log/xray
"[Unit]
Description=Xray Service
Documentation=https://github.com/xtls
After=network.target nss-lookup.target
[Service]
User=root
Group=root
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/usr/local/bin/xray run -config /usr/local/etc/xray/config.json
Restart=on-failure
RestartPreventExitStatus=23
LimitNPROC=10000
LimitNOFILE=1000000
[Install]
WantedBy=multi-user.target" | Out-File /etc/systemd/system/xray.service
systemctl daemon-reload
Write-Host "Xray 安装成功"
}
function New-x25519-KeyPair {
$out = xray x25519 | Out-String
$out.Split("`n") | ForEach-Object {
if ($_ -match "Private key: (.*)") {
$privateKey = $Matches[1]
}
if ($_ -match "Public key: (.*)") {
$publicKey = $Matches[1]
}
}
if ($privateKey -eq $null -or $publicKey -eq $null -or
$privateKey -eq "" -or $publicKey -eq "") {
Throw "生成密钥对失败"
}
return $privateKey, $publicKey
}
function Get-First-N-Primes {
param (
[Parameter(Mandatory = $true)]
[int]
$n
)
$primes = @()
$i = 2
while ($primes.Count -lt $n) {
$isPrime = $true
for ($j = 2; $j -lt $i; $j++) {
if ($i % $j -eq 0) {
$isPrime = $false
break;
}
}
if ($isPrime) {
$primes += $i
}
$i++
}
return $primes
}
function ConvertTo-String-List {
param (
[Parameter(Mandatory = $true)]
[int[]]
$numbers
)
$list = @()
foreach ($number in $numbers) {
$list += $number.ToString("D2")
}
return $list
}
function New-Short-Id-List {
param (
[Parameter(Mandatory = $true)]
[int]
$n
)
return ConvertTo-String-List (Get-First-N-Primes $n)
}
function Initialize-Xray {
param (
[Parameter(Mandatory = $true)]
$settings
)
$uuid = xray uuid
$privateKey, $publicKey = New-x25519-KeyPair
$config = @{
log = @{
loglevel = "warning"
error = "/var/log/xray/error.log"
}
routing = @{
domainStrategy = "IPIfNonMatch"
rules = @(
@{
type = "field"
ip = @("geoip:private")
outboundTag = "block"
}
)
}
outbounds = @(
@{
protocol = "freedom"
tag = "direct"
}
@{
protocol = "blackhole"
tag = "block"
}
)
inbounds = @(
@{
listen = "0.0.0.0"
port = $settings["port"]
protocol = "vless"
settings = @{
clients = @(
@{
id = $uuid
flow = "xtls-rprx-vision"
}
)
decryption = "none"
}
streamSettings = @{
network = "tcp"
security = "reality"
realitySettings = @{
dest = $settings["dest"]
serverNames = $settings["serverNames"]
privateKey = $privateKey
shortIds = New-Short-Id-List 4
}
}
}
)
}
if (-not (Test-Path /usr/local/etc/xray)) {
mkdir /usr/local/etc/xray
}
ConvertTo-Json $config -Depth 5 | Out-File /usr/local/etc/xray/config.json
systemctl enable xray --now
Write-Host "Xray 配置成功"
return $uuid, $publicKey
}
function New-VLESS-ShareLink {
param (
[Parameter(Mandatory = $true)]
$settings,
[Parameter(Mandatory = $true)]
$uuid,
[Parameter(Mandatory = $true)]
$publicKey
)
$ip = (ip route get 8.8.8.8 | sed -n '/src/{s/.*src *\([^ ]*\).*/\1/p;q}' | Out-String).Trim()
$link = "vless://"
$link += [uri]::EscapeUriString($uuid)
$link += "@"
$link += $ip
$link += ":" + $settings["port"]
$link += "?"
$link += "encryption=none&type=tcp"
$link += "&security=reality"
$link += "&fp=chrome"
$link += "&sni=" + [uri]::EscapeUriString($settings["dest"].Split(":")[0])
$link += "&pbk=" + [uri]::EscapeUriString($publicKey)
$link += "&sid=02"
$link += "&flow=xtls-rprx-vision"
$link += "&headerType=none"
$link += "#" + [uri]::EscapeUriString("rn7s2-" + $ip)
return $link
}
function Main {
if (-not (Get-IsRoot)) {
Throw "请使用 root 用户运行此脚本"
}
$settings = Enter-Settings
Read-Host -Prompt "按 Enter 键开始安装"
Install-Xray
$uuid, $publicKey = Initialize-Xray $settings
$shareLink = New-VLESS-ShareLink $settings $uuid $publicKey
Write-Host "VLESS 分享链接:`n$shareLink"
$shareLink | Out-File sharelink.txt
Write-Host "已将分享链接保存到 sharelink.txt"
Write-Host "安装完成"
}
Main