-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTCpayserverWebsite
234 lines (179 loc) · 7.67 KB
/
BTCpayserverWebsite
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
#!/usr/bin/env python3
import subprocess
import os
import subprocess
# Define the command to create the directory
directory_command = "sudo mkdir -p --mode=0755 /usr/share/keyrings"
# Run the directory command
try:
subprocess.run(directory_command, shell=True, check=True)
print("Directory created successfully.")
except subprocess.CalledProcessError as e:
print(f"Error creating directory: {e}")
# Define the command to download and save the GPG key
gpg_key_command = "curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg > /dev/null"
# Run the GPG key command
try:
subprocess.run(gpg_key_command, shell=True, check=True)
print("GPG key downloaded and saved successfully.")
except subprocess.CalledProcessError as e:
print(f"Error downloading and saving GPG key: {e}")
# Define the line to append to the file
line_to_append = "deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared jammy main"
# Define the file path
file_path = "/etc/apt/sources.list.d/cloudflared.list"
# Construct the echo command
echo_command = f'echo "{line_to_append}"'
# Combine the echo command with sudo and tee to append to the file
append_command = f'{echo_command} | sudo tee -a {file_path}'
# Run the append command
try:
subprocess.run(append_command, shell=True, check=True)
print("Line appended to the file successfully.")
except subprocess.CalledProcessError as e:
print(f"Error appending line to the file: {e}")
# Define the update command
update_command = "sudo apt-get update"
# Define the install command
install_command = "sudo apt-get install cloudflared"
# Run the update command
try:
subprocess.run(update_command, shell=True, check=True)
print("Apt update completed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error running apt-get update: {e}")
# Run the install command
try:
subprocess.run(install_command, shell=True, check=True)
print("Cloudflared installation completed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error running apt-get install cloudflared: {e}")
# Define the install command
install_command = "sudo apt install cloudflared"
# Run the install command
try:
subprocess.run(install_command, shell=True, check=True)
print("Cloudflared installation completed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error running apt install cloudflared: {e}")
# Define the login command
login_command = "cloudflared tunnel login"
# Run the login command
try:
subprocess.run(login_command, shell=True, check=True)
print("Cloudflared tunnel login completed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error running 'cloudflared tunnel login': {e}")
# Define the tunnel create command
create_tunnel_command = "cloudflared tunnel create btcpayserver"
# Run the tunnel create command
try:
tunnel_creation_result = subprocess.run(create_tunnel_command, shell=True, capture_output=True, text=True, check=True)
print("Cloudflared tunnel 'btcpayserver' created successfully.")
# Extract the tunnel id from the command output
tunnel_id = tunnel_creation_result.stdout.strip()
except subprocess.CalledProcessError as e:
print(f"Error running 'cloudflared tunnel create btcpayserver': {e}")
tunnel_id = None
if tunnel_id:
# Define the content of the config.yml file
config_content = f'''url: "http://localhost:23001"
tunnel: {tunnel_id}
credentials-file: /home/humble/.cloudflared/{tunnel_id}.json
'''
# Define the path to the config.yml file
config_file_path = os.path.expanduser("~/.cloudflared/config.yml")
# Create the ~/.cloudflared directory if it doesn't exist
os.makedirs(os.path.dirname(config_file_path), exist_ok=True)
# Write the content to the config.yml file
try:
with open(config_file_path, "w") as config_file:
config_file.write(config_content)
print(f"config.yml file created successfully at {config_file_path}")
except Exception as e:
print(f"Error creating config.yml file: {e}")
import subprocess
# Define the website domain
user_website = "yourwebsitedomain" # Replace with your desired website domain
# Define the tunnel route command
route_command = f"cloudflared tunnel route dns btcpayserver btcpay.{user_website}"
# Run the tunnel route command
try:
subprocess.run(route_command, shell=True, check=True)
print(f"DNS route 'btcpay.{user_website}' added successfully.")
except subprocess.CalledProcessError as e:
print(f"Error running 'cloudflared tunnel route dns': {e}")
# Define the tunnel run command
run_command = "cloudflared tunnel run btcpayserver"
# Run the tunnel run command
try:
subprocess.run(run_command, shell=True, check=True)
print("Cloudflared tunnel 'btcpayserver' is now running.")
except subprocess.CalledProcessError as e:
print(f"Error running 'cloudflared tunnel run btcpayserver': {e}")
# Define the source and destination paths
source_path = "~/.cloudflared/config.yml"
destination_path = "/etc/cloudflared/"
# Expand the user's home directory in the source path
source_path = os.path.expanduser(source_path)
# Define the copy command
copy_command = f"sudo cp {source_path} {destination_path}"
# Run the copy command
try:
subprocess.run(copy_command, shell=True, check=True)
print(f"Config.yml copied to {destination_path} successfully.")
except subprocess.CalledProcessError as e:
print(f"Error copying config.yml: {e}")
# Define the directory path
cloudflared_directory = os.path.expanduser("~/.cloudflared/")
# Change the current working directory to the Cloudflared directory
os.chdir(cloudflared_directory)
# Define the service install command
service_install_command = "sudo cloudflared service install"
# Run the service install command
try:
subprocess.run(service_install_command, shell=True, check=True)
print("Cloudflared service installed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error running 'sudo cloudflared service install': {e}")
import subprocess
def create_cloudflared_service(system_username):
file_path = "/etc/systemd/system/cloudflared.service"
content = f'''[Unit]
Description=Cloudflared Tunnel for BTCPayServer
After=network.target
[Service]
User={system_username}
Group={system_username}
WorkingDirectory=/home/{system_username}/
ExecStart=/usr/bin/cloudflared tunnel run btcpayserver
Restart=always
[Install]
WantedBy=multi-user.target
'''
# Write the content to a temporary file
temp_file_path = "/tmp/cloudflared.service"
with open(temp_file_path, 'w') as file:
file.write(content)
# Use sudo to move the temporary file to the target directory
move_command = ["sudo", "mv", temp_file_path, file_path]
subprocess.run(move_command, check=True)
# Set the correct ownership and permissions for the service file
chown_command = ["sudo", "chown", "root:root", file_path]
subprocess.run(chown_command, check=True)
chmod_command = ["sudo", "chmod", "644", file_path]
subprocess.run(chmod_command, check=True)
# Reload systemd configuration
reload_command = ["sudo", "systemctl", "daemon-reload"]
subprocess.run(reload_command, check=True)
# Enable the cloudflared service
enable_command = ["sudo", "systemctl", "enable", "cloudflared.service"]
subprocess.run(enable_command, check=True)
# Start the cloudflared service
start_command = ["sudo", "systemctl", "start", "cloudflared.service"]
subprocess.run(start_command, check=True)
def main():
system_username = 'humble' # Replace with the desired system username
create_cloudflared_service(system_username)
if __name__ == "__main__":
main()