-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure_server.py
146 lines (127 loc) · 5.3 KB
/
configure_server.py
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
import time
from server_creator import server_creator
import paramiko
import re
from SECRETS import SHADOWSOCKSR_PASSWORD, SSH_PRIVATE_KEY_PASS, EMAIL_SENDER
from sendemail import get_email_info
# 3.Configure servers
def configure_server(list, server_name):
hetzner_or_not = int(input("If you want to create a new Hetzner server press 1, if you want to configure your own "
"server press 0 and then press Enter. (0/1) "))
if hetzner_or_not == 1:
# 3.2.Login to server
server_access_info = server_creator(server_name)
# Connection details
host = server_access_info['server_ip']
host_ir = input('Tunnel host IP:(if you have if not press Enter) ')
if host_ir == '':
host_ir = host
username = 'root'
# The path to the private key file
private_key_file = "./file.key"
# The password to unlock the private key
private_key_password = SSH_PRIVATE_KEY_PASS
# Load the private key
private_key = paramiko.RSAKey.from_private_key_file(private_key_file, password=private_key_password)
elif hetzner_or_not == 0:
host = input("Please enter your host's IP: ")
host_ir = input('Tunnel host IP:(if you have if not press Enter) ')
if host_ir == '':
host_ir = host
username = 'root'
password = input("Enter your server's password: ")
ssh_port = input("Enter your server's SSH port: ")
# Create an instance of the SSH client
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
time.sleep(20)
if hetzner_or_not == 1:
ssh.connect(host, username=username, pkey=private_key)
elif hetzner_or_not == 0:
ssh.connect(host, username=username, password=password)
# Successful connection message
print("Connected to {} as {}".format(host, username))
except Exception as e:
print("Error: {}".format(e), "Sleep time was 20 seconds. Now going to try 30 seconds.")
time.sleep(30)
if hetzner_or_not == 1:
ssh.connect(host, username=username, pkey=private_key)
elif hetzner_or_not == 0:
ssh.connect(host, username=username, password=password)
# Successful connection message
print("Connected to {} as {}".format(host, username))
except Exception as e:
print("Error: {}".format(e), "Sleep time was 30 seconds. Now going to try 40 seconds.")
time.sleep(40)
if hetzner_or_not == 1:
ssh.connect(host, username=username, pkey=private_key)
elif hetzner_or_not == 0:
ssh.connect(host, username=username, password=password)
# Successful connection message
print("Connected to {} as {}".format(host, username))
# Run a command on the VPS
ssh.exec_command("yes" + "\n")
ssh.exec_command("\n")
ssh.exec_command("\n")
ssh.exec_command("\n")
# wget ShadowsocksR
try:
stdin, stdout, stderr = ssh.exec_command('wget -N --no-check-certificate https://raw.githubusercontent.com/ToyoDAdoubi/doubi/master/ssrmu.sh && chmod +x ssrmu.sh')
exit_status = stdout.channel.recv_exit_status() # Blocking call
if exit_status == 0:
print("ssrmu.sh downloaded")
else:
print("Error", exit_status)
print("ssrmu.sh download is ok.")
except Exception as e:
print("Error: {}".format(e))
exit()
# Install ShadowsocksR
port = 2333
try:
stdin, stdout, stderr = ssh.exec_command(f"./ssrmu.sh <<< $'1\n{host_ir}\nAdmin\n{port}\n{SHADOWSOCKSR_PASSWORD}\n10\n5\n2\nn\n\n\n\n\n\n'")
exit_status = stdout.channel.recv_exit_status() # Blocking call
if exit_status == 0:
print("ShadowsocksR installed.")
else:
print("Error", exit_status)
output = stdout.read().decode()
print(output)
print("ShadowsocksR installation is ok.")
except Exception as e:
print("Error: {}".format(e))
exit()
# Get the user link that starts from ssr://
pattern = r"ssr:\/\/.*[a-zA-Z]\w"
match = re.search(pattern, output)
if match:
user_link = match.group()
print('This is Admin\'s user link: ')
print(user_link)
print('Going to send it.')
get_email_info("admin", EMAIL_SENDER, user_link)
# Create users
for user in list:
name = user["name"]
email = user["email"]
port += 1
stdin, stdout, stderr = ssh.exec_command(f"./ssrmu.sh <<< $'7\n1\n{name}\n{port}\n{SHADOWSOCKSR_PASSWORD}\n10\n5\n2\nn\n1\n\n\n\n\nn'")
exit_status = stdout.channel.recv_exit_status() # Blocking call
if exit_status == 0:
print(f"user {name} created successfully.")
else:
print("Error", exit_status)
# Print the output of the command
output = stdout.read().decode()
# Get the user link that starts from ssr://
pattern = r"ssr:\/\/.*[a-zA-Z]\w"
match = re.search(pattern, output)
if match:
user_link = match.group()
print(f'This is {name}\'s user link: ')
print(user_link)
print('Going to send it.')
get_email_info(name, email, user_link)
# Close the connection
ssh.close()