-
Notifications
You must be signed in to change notification settings - Fork 0
/
R-Balancer.py
121 lines (88 loc) · 4.12 KB
/
R-Balancer.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
#!/usr/bin/python3
print(
r'''
$$$$$$$\ $$$$$$$\ $$\
$$ __$$\ $$ __$$\ $$ |
$$ | $$ | $$ | $$ | $$$$$$\ $$ | $$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$\ $$$$$$\
$$$$$$$ |$$$$$$\ $$$$$$$\ | \____$$\ $$ | \____$$\ $$ __$$\ $$ _____|$$ __$$\ $$ __$$\
$$ __$$< \______|$$ __$$\ $$$$$$$ |$$ | $$$$$$$ |$$ | $$ |$$ / $$$$$$$$ |$$ | \__|
$$ | $$ | $$ | $$ |$$ __$$ |$$ |$$ __$$ |$$ | $$ |$$ | $$ ____|$$ |
$$ | $$ | $$$$$$$ |\$$$$$$$ |$$ |\$$$$$$$ |$$ | $$ |\$$$$$$$\ \$$$$$$$\ $$ |
\__| \__| \_______/ \_______|\__| \_______|\__| \__| \_______| \_______|\__|
============================================================================================
[*] R-Balancer - Load Balancer ( Round-Robin ) | R&D incrustwerush.org - Afrizal F.A
============================================================================================
''')
import os, json, socket
import threading
from argparse import ArgumentParser
class RBalancer:
def __init__(self, servers):
self.servers = self.listServer(servers)
self.current_server = 0
def listServer(self, servers):
try:
server_list = []
for entry in servers.split(","):
host, port = entry.split(":")
server_list.append((host, int(port)))
return server_list
except Exception as E:
print(f"[!] [Error: {E}]")
def handle_client(self, client_socket):
try:
server = self.servers[self.current_server]
self.current_server = (self.current_server + 1) % len(self.servers)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.connect(server)
threading.Thread(target=self.forward_data, args=(client_socket, server_socket)).start()
threading.Thread(target=self.forward_data, args=(server_socket, client_socket)).start()
except Exception as E:
print(f"[!] [Error: {E}]")
def forward_data(self, source, destination):
try:
while True:
try:
data = source.recv(4096)
if not data:
break
destination.sendall(data)
except:
break
source.close()
destination.close()
except Exception as E:
print(f"[!] [Error: {E}]")
def start(self, bind_ip, bind_port):
try:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(5)
print(f"[*] Listening on {bind_ip}:{bind_port}")
while True:
try:
client_socket, addr = server.accept()
print(f"[*] Accepted connection from {addr}")
client_handler = threading.Thread(target=self.handle_client, args=(client_socket,))
client_handler.start()
except Exception as E:
print(f"[!] [Error: {E}]")
except Exception as E:
print(f"[!] [Error: {E}]")
if __name__ == "__main__":
configFile = "R-Balancer.conf"
if os.path.isfile(configFile):
opt = json.loads(open(configFile, "r").read())
else:
parser = ArgumentParser()
parser.add_argument("-s", "--server", help="Host Server Balancer", required=True, type=str)
parser.add_argument("-p", "--port", help="PORT Server Balancer", required=True, type=int)
parser.add_argument("-l", "--list", help="List Backend Server", required=True, type=str)
args = parser.parse_args()
opt = {
"server": args.server,
"port": args.port,
"list_server": args.list
}
run = RBalancer(opt["list_server"])
run.start(opt["server"], int(opt["port"]))