Need some implementation ideea

  • Thread starter Thread starter Crirus
  • Start date Start date
C

Crirus

Hi there!

This is the problem:
I have a server that accept connections.
Each request span a thread that handle it.
I need a way to store the client IP's.
If the requests made from a single IP pass a number, say 10, I want to drop
that request without processing.
All that request have a timeout so after say 10 sec elapsed, the requests
number made fom a single IP is decreased by one, so the client cant make
more that 10 requests in 10 secs.

Can someone help me?

Thanks, Crirus
 
Hi Crirus,

Because of the relatively long time spans involved compared to the life of
a request and response, and because this is a service supplying all requests,
my thinking tends towards a separate process which acts as a 'permission
broker'. Also, that way you only need one timer to handle everything.

I'd have a list of 'decrement' events which would be managed using the
timer. An event on the list would consist of the time when it is to occur, an
action and the IP. Although such a class is highly generalisable, in your case
there will only be one type of event. The timer interval would be set to the
next event. On the tick it would decrement the count of the associated IP and
set the next interval accordingly.

Each request coming in to the server would pass the IP to this broker
which would check against its internal list. If over the threshold - "no
permission". If under - "ok, off you go".

Each "off you go" would bump up the count for that IP and add a decrement
event to the end of the list, due to go off in so many seconds.

Regards,
Fergus
 
Hi Crirus,

Can you not use "modified" that nice example Armin did send in this week?

\\\ by Armin
Private m_Thread As MyThread
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
m_Thread = New MyThread
AddHandler m_Thread.Progress, AddressOf OnProgress
AddHandler m_Thread.Done, AddressOf OnDone
m_Thread.Start()
End Sub
Public Delegate Sub ProgressDelegate(ByVal Progress As Integer)
Private Sub OnProgress(ByVal Progress As Integer)
If Me.InvokeRequired Then
Me.Invoke(New ProgressDelegate( _
AddressOf OnProgress _
), New Object() {Progress})
Else
Me.Button1.Text = Progress.ToString
End If
End Sub
Private Sub OnDone()
m_Thread = Nothing
End Sub
///
\\\
Class MyThread
Public Event Progress(ByVal Progress As Integer)
Public Event Done()
Private m_Thread As Thread
Public Sub Start()
m_Thread = New Thread(AddressOf ThreadStart)
m_Thread.Start()
End Sub
Private Sub ThreadStart()
Dim i As Integer
For i = 1 To 100
Thread.Sleep(100)
RaiseEvent Progress(i)
Next
RaiseEvent Done()
End Sub
End Class
///

Cor
 
Hi Fergus
Hard to follow your ideea :(
What I was thinking by now is a lockSynk object, called Flood, that handle
the IP's.
Each request handler thread should add the IP to the Flood ip's list that
maintain a number of them in a dictionary (IP, number pair)
The add function should return true if IP is allowed to receipt data.
So, each thread wait for the others previous threads to return from lock and
then test it's own IP .

The question I have right now is how to decrement each IP's number added in
that with it's own timer in that class.

Anyway, can you repost your ideea another way to lem me follow it?

Thanks
Crirus
 
I post below some code

Private Function isBanned(ByVal ip As String) As Boolean
SyncLock myFlood
Return myFlood.isFlooding(ip)
End SyncLock
End Function

'this function is the callback for a receiv thread
Private Sub AcceptCallback(ByVal ar As IAsyncResult)
Try
allDone.Set() ' Signal the listen thread to continue.
Dim listener As Socket = CType(ar.AsyncState, Socket) 'Get the
socket that handles the client request.
Dim handler As Socket = listener.EndAccept(ar) 'Mark the end of
accept and create the new socket for client

Dim clientIP As IPEndPoint = CType(handler.RemoteEndPoint,
IPEndPoint) 'adresa clientului not used here
If isBanned(clientIP.Address.ToString) Then
handler.Shutdown(SocketShutdown.Both)
handler.Close()
Exit Sub
End If
........
end sub

Public Class Flood
Private Const maxConnectionAllowed = 10
Private myIps() As DictionaryEntry
Public Function isFlooding(ByVal ip As String)
For i As Integer = 0 To UBound(myIps)
If myIps(i).Key = ip Then
myIps(i).Value += 1
If myIps(i).Value > maxConnectionAllowed Then
Return True
Else
Return False
End If
End If
Next
End Function


End Class
 
Hi Crirus
So, what is this sample code?
some kind of a timer?

Yes and I thougth when you make your steps for 1 in a second, you are very
close to your goal. But it is not some kind of a timer, it is a timer,
which can keep the other processes going very well and you can make it in a
way I think you need..

But it is just an idea, I am at the moment typical not doing the things you
are doing now

Cor
 
OK, thanks, but my problem is not to set a timer...
Is deeper, in the way that I need to know what to do with that timer.
I need a IP manager that keep track of the IP that meke request to my server
and only allow a certain number of them to be processed

In fact, is a flood protection I want
 
Hi Fergus,
After second read, seems that we go on the same route...
please, detail only the decremental events part :)
 
Hi Crirus
In fact, is a flood protection I want

That was exactly what I was understanding.

And with saving all IP's I thought you can get messed up with a terrible
unmanaged collection, so I thought maybe there is another approach.

But if you have your solution, take that first.

You did ask for idea's I thought

Cor
 
Hi Fergus,
I knew this, just as I said to Scorpion yesterday.
I thougth you was to bed man.
:-))))))
Cor
 
Hi Fergus,

I saw the other thread, and before I started on the image I thought look
what Fergus did made.

I can see from the signature from your writting that it is from you, but I
think it is better not sending vb files. Some will think that it are
virusses and then we have the whole discussion again.

I know why you did it and the hurry you are in now and it was of course an
accidant that can happen.

I thought I was the best to tell you and not someone like Herfried or Armin
and I hope those keep there mouth.

Cor
 
Hi Cor,

LOL. I should be. I just wanted to finish that event queueing sample for
Crirus.

Thanks for talking to Eva for me. I don't have any time at all now (Zzzz),
and I'm not sure I'd be able to do it anyway. I'll have a quick go tonight.

Regards,
Fergus
 
Hi Cor,

'Twas no accident. And now I'm puzzled. What's wrong with vb files -
they're just text? Open one up and it whips you into VS.

Regards,
Fergus
 
Back
Top