SMTP Proxy to help with wrong date time shown in camera emails

Have some questions or having issues with your IP Camera(s), Post them here for the mods and other users to assist you with.
Post Reply
Bill48105
Posts: 8
Joined: Tue Oct 18, 2016 2:54 pm

SMTP Proxy to help with wrong date time shown in camera emails

Post by Bill48105 »

Hey All,
Frustrated by the date/time being off in emails from my Amcrest Pro camera. I researched & found numerous others running into the same problem. In digging deeper I found the cause is that the time zone offset in the emails from the camera are hard coded at +0800 no matter what timezone you pick in System->General->Date/Time. Amcrest has known about this issue for some time & no idea how long before it'll be fixed so I whipped up 2 scripts to help solve the problem. All they do is act as a proxy watching each line & changing the +0800 to whatever time zone offset you wise. Beyond that it does nothing & anyone familiar with coding can review the source codes to confirm that.

I did 2 versions. One in Python which should run on any OS that python exists for (any modern OS really including Windows, linix, BSD, ubuntu, OSX etc) which was about as bare bones simple as possible but I also adapted an HTTP proxy written in AutoIT which can compile to a Windows EXE, run in tray etc which is a bit more user friendly while really being quite simple still. For now I'm posting just the python version for those interested in trying.

You need python installed. Most nix-based OS's have it or use YUM, APT-GET etc as needed. For Windows you can download here:
https://www.python.org/downloads/windows/

Create a text based py file using the following code:

Code: Select all


# ProxySMTP.py - SMTP Proxy by Bill48105 
import thread, select, socket

ListenOn = "192.168.1.17"
ListenOnPort = 2525
ConnectTo = "mail.somewhere"
ConnectToPort = 587
StartsWith = "Date:"
ReplaceFrom = "+0800"
ReplaceTo = "-0400"

ShowRaw = False
Timeout = 90
MaxBuffer = 4096
class ConnectionHandler:
def __init__(self, Connection, Address, Timeout):
print "Incoming: %s Port %d" % (Address[0], Address[1])
self.OneClient = Connection
self.ClientBuffer = ""
self.Timeout = Timeout
self.ConnectTo = ConnectTo
self.ConnectToPort = ConnectToPort
self.Connect()
print "Disconnecting: %s Port %d" % (Address[0], Address[1])
self.OneTarget.close()
self.OneClient.close()

def Connect(self):
print "Connecting to %s Port %d" % (self.ConnectTo, self.ConnectToPort)
(MySocket, SocketType, Protocol, CanonName, OneAddress) = socket.getaddrinfo(self.ConnectTo, self.ConnectToPort, 0, 0)[0]
self.OneTarget = socket.socket(MySocket)
self.OneTarget.connect(OneAddress)
self.ClientBuffer = ""
MySockets = [self.OneClient, self.OneTarget]
IdleCount = 0
DataChunk = ""
MaxWait = int(self.Timeout/10)
Done = False
while not Done:
IdleCount += 1
ReadBuff, _, SocketException = select.select(MySockets, [ ], MySockets, 10)
if SocketException:
break
if ReadBuff:
for ReadChunk in ReadBuff:
try:
DataChunk += ReadChunk.recv(MaxBuffer)
if DataChunk:
try:
Lines = DataChunk.splitlines(True)
except:
Lines = ["\r\n"]
for OneLine in Lines:
if len(OneLine) > 0:
if OneLine.startswith(StartsWith):
print "Line starts with " + StartsWith + " replacing " + ReplaceFrom + " with " + ReplaceTo
OneLine = OneLine.replace(ReplaceFrom,ReplaceTo)
if ReadChunk is self.OneClient:
if ShowRaw:
print "-> " + OneLine.replace('\r\n','<CR><LF>')
self.OneTarget.send(OneLine)
else:
if ShowRaw:
print "<- " + OneLine.replace('\r\n','<CR><LF>')
self.OneClient.send(OneLine)
IdleCount = 0
DataChunk = ""
else:
Done = True
except:
return
if IdleCount >= MaxWait:
print "Timeout waiting for response.."
break

Daemon = socket.socket()
Daemon.bind((ListenOn, ListenOnPort))
print "Listening on %s Port %d" % (ListenOn, ListenOnPort)
Daemon.listen(0)
MyHandler = ConnectionHandler
while True:
thread.start_new_thread(MyHandler, Daemon.accept() + (Timeout,))
Edit the settings in the code being careful with quotes so they stay the same to avoid changing the variable types. I avoided commenting in the script to keep it short as possible but the variable names should be pretty obvious what they are but I'll give specifics below:
ListenOn = "192.168.1.17" # IP address of the computer you're running it on. This is just example you must change.
ListenOnPort = 2525 # Port number to listen on. 25 is smtp so I just did 2525 as default
ConnectTo = "mail.somewhere" # host name or IP address of remote SMTP server you normally send mail thru
ConnectToPort = 587 # Port number you send thru. 587 is most common for non public or 25 for public
StartsWith = "Date:" # Search all lines for this string to match lines you want to do a search/replace on. Leave Date:
ReplaceFrom = "+0800" # String to search for that you want to replace. Leave this +0800 in this case
ReplaceTo = "-0400" # String to replace with. CHANGE THIS TO YOUR TIME ZONE OFFET.

(Odds are you won't need to change the other 3 settings unless you are curious you can change ShowRaw to "watch" email lines sending & receiving live)

If you're not familiar with how a proxy works you'll need a real world example. Normally when you setup SMTP mail in the camera you enter in the ip/host & port of your mail server somewhere such as mail.yourisp.com etc. (Don't use that it's just an example). So the flow looks like this:

Cam->YourISP

In other words your camera talks to your isp's mail server directly. With a proxy you are running a "man in the middle" system where the camera talks to YOU and then YOU relay that info to your mail server such as:

Cam->YOU->YourISP

It sounds way more complicated than it sounds but does require some technical know how to get setup & working. The gist of it is find your computer's IP address so you can put it on ListenOn in the script. (I didn't try it but 0.0.0.0 should work. 127.0.0.1 or localhost won't work though since your camera needs to be able to talk to your computer & that would indicate only programs on your computer could connect) ListenOnPort of 2525 should be fine in most cases. Set ConnectTo & ConnectToPort to your ISP's SMTP server settings like you normally would in the camera or any email program. NOTE: The script does not support STARTTLS/TLS/SSL only plain text. Sorry about that but it would just complicate things a lot so hopefully your ISP does not require those or this script won't be helpful without modification. Leave StartsWith & ReplaceFrom alone since those match what comes from the camera. You'll need to find your time zone offset. You can find it under date/time on Windows or with date command on nix then lookup offset from the time zone abbreviation. Or of course if you set it correctly in your camera, the offset is shown there. (In fact the whole reason we even have to bother with this proxy hack is because Amcrest doesn't use that offset that you choose & instead always use +0800). Be sure you "pad" your offset to 4 digits (prefix with 0 if needed) and the + or - sign with no spaces. (Do not put the GMT as shown on Time Zone in camera settings.) So once you have the script settings right save the py file & run it. For nix you should be able to just type # python ProxySMTP.py (or whatever name you gave it assuming it's in current directory). On windows if it's in your path you'd do the same otherwise say you installed python 2.7 in default folder you'd do something like C:\Python27\python.exe ProxySMTP.py

Assuming everything is good you should see something like:
Listening on 192.168.1.17
(obviously with your IP that's just example)

Now you need to setup SMTP on your camera under Network. Choose Other then for SMTP Server enter the IP address of your computer (that's on same LAN as your camera), set Authentication to None (as I said earlier this proxy does not support SSL or TLS anyway) For port put 2626 or whatever post you set in the script. For the rest of the settings on the camera set them however you would if you were sending direct to your ISP. Odds are you don't send anonymously & odds are you need username & password & a valid sender (your email address) and of course valid recipient(s). Hit save then hit Email test & cross your fingers. Hopefully the camera says the test succeeded then you're good.

An example session looks like this:

Code: Select all


C:\Python27\python.exe ProxySMTP.py
Listening on 192.168.1.17 Port 2626
Incoming: 192.168.1.129 Port 60550
Connecting to mail.someisp.com Port 587
Line starts with Date: replacing +0800 with -0400
Disconnecting: 192.168.129 Port 60550
And voila! The email was sent from the camera thru your computer where the line starting with Date: was detected then +0800 on that line was replace with -0400 or whatever timezone offset you specified.

Assuming the email went check your mailbox & see if it shows the right date/time. If not you might need to set your date/time & timzone on the camera properly and/or adjust the ReplaceTo value in the script. You might need to do a little trial & error to get it right.

Btw if you're curious this is all that is done by the proxy script:

This: Date: Thu, 20 Oct 2016 23:16:35 +0800
is change to This: Date: Thu, 20 Oct 2016 23:16:35 -0400

That's all that is wrong with the emails causing all this grief! Hopefully Amcrest fixes the camera firmware properly but in the meantime hopefully this script proves helpful.

If there is interest in the Windows only AutoIT one I'll clean that up & post that one with instructions as well.
Bill
eej75
Posts: 1
Joined: Sun May 21, 2017 5:29 pm

Re: SMTP Proxy to help with wrong date time shown in camera emails

Post by eej75 »

Are you freaking kidding me? What happens if you're not a programmer? Why doesn't this stuff just work? Like how am I supposed to go to court with my camera footage and explain to the judge, Well sir this company has a known issue with time zones and so I know it says 16:00, but I assure you it occurred at 15:00 hours." What hacks. I'm going to have to move to another camera for all my other locations.
Melvin
Site Admin
Posts: 2210
Joined: Wed Oct 14, 2015 2:20 pm

Re: SMTP Proxy to help with wrong date time shown in camera emails

Post by Melvin »

Hello eej75,

This issue has been fixed in the latest firmware update.
Can't find your answer on the forum? Try our Knowledge Base! https://amcrest.zendesk.com/hc/en-us

Image
Post Reply