Add timeout to script function?

This section contains scripts that hMailServer has contributed with. hMailServer 5 is needed to use these.
Post Reply
insomniac2k2
Normal user
Normal user
Posts: 87
Joined: 2016-08-09 19:47

Add timeout to script function?

Post by insomniac2k2 » 2018-02-21 19:17

I've ran into an issue several times where i will have a bunch of open sessions which are waiting for various lookup sites to reply. These open sessions eventually cause a thread lock on the hmail server. In order to prevent this, i've come to the conclusion that I need to add some sort of timeout in the lookup process. Basically, if lookup doesn't reply in 5 seconds, move on.

Example script function used by many here:

Code: Select all

Sub OnClientConnect(oClient)
        If SpamRatsAuthHacker(oClient.IPAddress) Then
		Set oShell = CreateObject("WScript.Shell") 
		'oShell.Run """C:\Program Files (x86)\hMailServer\Bin\ProxyAuth.exe"" -secret Mangos -ban " & oClient.IPAddress & " -bandays 180"
		 oShell.Run """C:\Program Files (x86)\hMailServer\Bin\ProxyAuth.exe"" -secret Mangos -ban " & oClient.IPAddress & " -bandays 180", 0, True
		Result.Value = 1
		EventLog.Write("Message from: " & oClient.IPAddress & " Blocked as authentication hacker - SpamRatsAuthHacker") 
		Exit Sub
	End If
End Sub	
calls:

Code: Select all

Function SpamRatsAuthHacker(strIP)
	SpamRatsAuthHacker = false
	Dim a : a = Split(strIP, ".")
	On Error Resume Next
	With CreateObject("DNSLibrary.DNSResolver")
		strIP = .DNSLookup(a(3) & "." & a(2) & "." & a(1) & "." & a(0) & ".auth.spamrats.com")
	End With
	On Error Goto 0
	Dim strRegEx : strRegEx = "^(127\.0\.0\.43)$"
	SpamRatsAuthHacker = Lookup(strRegEx, strIP)
End Function
Ideally i would think the best place for timeout to be added to the function itself. Any thoughts on how to do that?

Apologies to moderators. I just realized that I posted this in the wrong place :/

User avatar
SorenR
Senior user
Senior user
Posts: 4169
Joined: 2006-08-21 15:38
Location: Denmark

Re: Add timeout to script function?

Post by SorenR » 2018-02-21 20:34

I know you can't use it in HM script but here goes ...

Code: Select all

WScript.Echo WScript.Timeout
WScript.Timeout = 15
WScript.Echo "Start..."
While True
    WScript.Sleep 5000
    WScript.Echo "Tick..."
Wend
WScript.Echo "Stop..."
He he.....

Hint: perhaps look into calling the exe via PowerShell, IIRC you can set a timeout.

PS. Also came across this...

Exit code is WScript.Quit n where N is a 32 bit integer. It becomes the return code of the shell.
SørenR.

Algorithm (noun.)
Word used by programmers when they do not want to explain what they did.

User avatar
RvdH
Senior user
Senior user
Posts: 1193
Joined: 2008-06-27 14:42
Location: Netherlands

Re: Add timeout to script function?

Post by RvdH » 2018-02-22 13:33

The DNSLibrary.DNSResolver component has a lookup timeout build-in and returns empty string if a timeout occurred.... so my guess the timeout occurs elsewhere, not in the lookup
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

insomniac2k2
Normal user
Normal user
Posts: 87
Joined: 2016-08-09 19:47

Re: Add timeout to script function?

Post by insomniac2k2 » 2018-02-22 16:57

Interesting. What is the default timeout? Because presently, I have a pretty busy server. It's not unusual to have most all threads open at the same time. Is it adjustable by ini?

User avatar
RvdH
Senior user
Senior user
Posts: 1193
Joined: 2008-06-27 14:42
Location: Netherlands

Re: Add timeout to script function?

Post by RvdH » 2018-02-22 17:46

I believe the Resolver in the component uses a timeout of 1000 milliseconds (3 retries)
This is not adjustable
CIDR to RegEx: d-fault.nl/CIDRtoRegEx
DNS Lookup: d-fault.nl/DNSTools
DNSBL Lookup: d-fault.nl/DNSBLLookup
GEOIP Lookup: d-fault.nl/GeoipLookup

insomniac2k2
Normal user
Normal user
Posts: 87
Joined: 2016-08-09 19:47

Re: Add timeout to script function?

Post by insomniac2k2 » 2018-02-22 19:20

Thanks. I'll poke around elsewhere then. 1000ms is definitely fine.
RvdH wrote:I believe the Resolver in the component uses a timeout of 1000 milliseconds (3 retries)
This is not adjustable

Post Reply