forked from johanmeijer/grott
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grottproxy.py
168 lines (148 loc) · 6.63 KB
/
grottproxy.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#Grott Growatt monitor : Proxy
#
# Updated: 2020-10-04
# Version 2.2.1d
import socket
import select
import time
import sys
import struct
import textwrap
from itertools import cycle # to support "cycling" the iterator
import time, json, datetime, codecs
## to resolve errno 32: broken pipe issue (only linux)
if sys.platform != 'win32' :
from signal import signal, SIGPIPE, SIG_DFL
from grottdata import procdata, decrypt
#import mqtt
import paho.mqtt.publish as publish
# Changing the buffer_size and delay, you can improve the speed and bandwidth.
# But when buffer get to high or delay go too down, you can broke things
buffer_size = 4096
#buffer_size = 65535
delay = 0.0002
class Forward:
def __init__(self):
self.forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def start(self, host, port):
try:
self.forward.connect((host, port))
return self.forward
except Exception as e:
print(e)
return False
class Proxy:
input_list = []
channel = {}
def __init__(self, conf):
print("\nGrott proxy mode started")
## to resolve errno 32: broken pipe issue (Linux only)
if sys.platform != 'win32':
signal(SIGPIPE, SIG_DFL)
##
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#set default grottip address
if conf.grottip == "default" : conf.grottip = '0.0.0.0'
self.server.bind((conf.grottip, conf.grottport))
#socket.gethostbyname(socket.gethostname())
try:
hostname = (socket.gethostname())
print("Hostname :", hostname)
print("IP : ", socket.gethostbyname(hostname), ", port : ", conf.grottport, "\n")
except:
print("IP and port information not available")
self.server.listen(200)
self.forward_to = (conf.growattip, conf.growattport)
def main(self,conf):
self.input_list.append(self.server)
while 1:
time.sleep(delay)
ss = select.select
inputready, outputready, exceptready = ss(self.input_list, [], [])
for self.s in inputready:
if self.s == self.server:
self.on_accept(conf)
break
try:
self.data, self.addr = self.s.recvfrom(buffer_size)
except:
if conf.verbose : print("\t - Grott connection error")
if len(self.data) == 0:
self.on_close(conf)
break
else:
self.on_recv(conf)
def on_accept(self,conf):
forward = Forward().start(self.forward_to[0], self.forward_to[1])
clientsock, clientaddr = self.server.accept()
if forward:
if conf.verbose: print("\t -", clientaddr, "has connected")
self.input_list.append(clientsock)
self.input_list.append(forward)
self.channel[clientsock] = forward
self.channel[forward] = clientsock
else:
if conf.verbose:
print("\t - Can't establish connection with remote server."),
print("\t - Closing connection with client side", clientaddr)
clientsock.close()
def on_close(self,conf):
if conf.verbose:
#try / except to resolve errno 107: Transport endpoint is not connected
try:
print("\t -", self.s.getpeername(), "has disconnected")
except:
print("\t -", "peer has disconnected")
#remove objects from input_list
self.input_list.remove(self.s)
self.input_list.remove(self.channel[self.s])
out = self.channel[self.s]
# close the connection with client
self.channel[out].close() # equivalent to do self.s.close()
# close the connection with remote server
self.channel[self.s].close()
# delete both objects from channel dict
del self.channel[out]
del self.channel[self.s]
def on_recv(self,conf):
data = self.data
print("")
print("\t - " + "Growatt packet received:")
print("\t\t ", self.channel[self.s])
# FILTER!!!!!!!! Detect if configure data is sent!
header = "".join("{:02x}".format(n) for n in data[0:8])
if conf.blockcmd :
#standard everything is blocked!
blockflag = True
#partly block configure Shine commands
if header[14:16] == "18" :
if conf.blockcmd :
if header[6:8] == "05" or header[6:8] == "06" : confdata = decrypt(data)
else : confdata = data
#get conf command (location depends on record type), maybe later more flexibility is needed
if header[6:8] == "06" : confcmd = confdata[76:80]
else: confcmd = confdata[36:40]
if header[14:16] == "18" :
#do not block if configure time command of configure IP (if noipf flag set)
if conf.verbose : print("\t - Grott: Shine Configure command detected")
if confcmd == "001f" or (confcmd == "0011" and conf.noipf) :
blockflag = False
if confcmd == "001f": confcmd = "Time"
if confcmd == "0011": confcmd = "Change IP"
if conf.verbose : print("\t - Grott: Configure command not blocked : ", confcmd)
else :
#All configure inverter commands will be blocked
if conf.verbose : print("\t - Grott: Inverter Configure command detected")
#allow records:
if header[12:16] in conf.recwl : blockflag = False
if blockflag :
print("\t - Grott: Record blocked: ", header[12:16])
return
# send data to destination
self.channel[self.s].send(data)
if len(data) > conf.minrecl :
#process received data
procdata(conf,data)
else:
if conf.verbose: print("\t - " + 'Data less then minimum record length, data not processed')