CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/io/gram_server.rb
Views: 11780
1
# -*- coding: binary -*-
2
require 'thread'
3
4
module Rex
5
module IO
6
7
###
8
#
9
# This mixin provides the framework and interface for implementing a datagram
10
# server that can handle incoming datagrams. Datagram servers include this mixin
11
#
12
###
13
module GramServer
14
15
##
16
#
17
# Abstract methods
18
#
19
##
20
21
##
22
#
23
# Default server monitoring and client management implementation follows
24
# below.
25
#
26
##
27
28
29
#
30
# This callback is notified when a client connection has data that needs to
31
# be processed.
32
#
33
def dispatch_request(client, data)
34
if (dispatch_request_proc)
35
dispatch_request_proc.call(client, data)
36
end
37
end
38
39
#
40
# This callback is notified when data must be returned to the client
41
# @param client [Socket] Client/Socket to receive data
42
# @param data [String] Data to be sent to client/socket
43
def send_response(client, data)
44
if (send_response_proc)
45
send_response_proc.call(client, data)
46
else
47
client.write(data)
48
end
49
end
50
51
#
52
# Start monitoring the listener socket for connections and keep track of
53
# all client connections.
54
#
55
def start
56
self.listener_thread = Rex::ThreadFactory.spawn("GramServerListener", false) {
57
monitor_listener
58
}
59
end
60
61
#
62
# Terminates the listener monitoring threads and closes all active clients.
63
#
64
def stop
65
self.listener_thread.kill
66
end
67
68
#
69
# This method waits on the server listener thread
70
#
71
def wait
72
self.listener_thread.join if self.listener_thread
73
end
74
75
##
76
#
77
# Callback procedures.
78
#
79
##
80
81
#
82
# This callback procedure can be set and will be called when clients
83
# have data to be processed.
84
#
85
attr_accessor :dispatch_request_proc, :send_response_proc
86
87
attr_accessor :listener_thread# :nodoc:
88
89
90
end
91
92
end
93
end
94
95
96