/
gatebreaker.py
executable file
·299 lines (215 loc) · 8.32 KB
/
gatebreaker.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Authors: Caner Candan <caner@candan.fr>
#
"""A script to easily activate proxy ports
"""
__author__ = 'Caner Candan'
__version__ = '0.2'
############################
# gate breaker configuration
############################
RUN_PATH = './run'
############################
############################
# LOGGER
############################
import logging
LEVELS = {'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL}
def logger(level_name, filename='gatebreaker.log'):
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
filename=filename, filemode='a'
)
console = logging.StreamHandler()
console.setLevel(LEVELS.get(level_name, logging.NOTSET))
console.setFormatter(logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s'))
logging.getLogger('').addHandler(console)
############################
############################
# PARSER
############################
import optparse
def parser(parser=optparse.OptionParser()):
parser.add_option('-v', '--verbose', choices=LEVELS.keys(), default='info', help='set a verbose level [default: info]')
parser.add_option('-o', '--output', help='give an output filename for logging [default: gatebreaker.log]', default='gatebreaker.log')
parser.add_option('-c', '--config', help='give a file path containing the connection information of the proxy server you want to use [default: proxy.lst]', default='proxy.lst')
parser.add_option('-l', '--list', help='give a file path containing the list of ports you want to use', default='')
parser.add_option('-p', '--port', help='give a specific port number', default='')
parser.add_option('-s', '--start', action='store_true', help='start the given port number process or all gate breaker processes [default: False]', default=False)
parser.add_option('-r', '--restart', action='store_true', help='restart the given port number process or all gate breaker processes [default: False]', default=False)
parser.add_option('-k', '--kill', action='store_true', help='kill the given port number process or all gate breaker processes [default: False]', default=False)
parser.add_option('-m', '--monitor', action='store_true', help='check the status for the given port number process or all gate breaker processes [default: False]', default=False)
parser.add_option('-a', '--available', action='store_true', help='give the list of all ports that you can activate from the given list file [default: True]', default=True)
parser.add_option('-V', '--version', action='store_true', help='current version [default: False]', default=False)
options, args = parser.parse_args()
logger(options.verbose, options.output)
return options
options = parser()
############################
############################
# GateBreaker begins
############################
import sys, os, subprocess
def getrunfiles():
filelist = []
try:
filelist = os.listdir(RUN_PATH)
except OSError, e:
logging.error(e)
return filelist
def kill(port=options.port):
if port == '': return
logger = logging.getLogger('kill(%s)' % port)
logger.debug('killing...')
try:
path = '%s/%s' % (RUN_PATH, port)
pid = int(open(path).readline())
logger.debug('pid to kill: %s', pid)
try:
os.kill(pid, 2)
except OSError, e:
logger.error(e)
try:
os.unlink(path)
except OSError, e:
logger.error(e)
logger.debug('done')
except IOError, e:
logger.error(e)
def killall():
if not options.kill: return
for x in getrunfiles():
kill(x)
def monitor(port=options.port):
if port == '': return
logger = logging.getLogger('monitor(%s)' % port)
try:
path = '%s/%s' % (RUN_PATH, port)
pid = int(open(path).readline())
logger.debug('pid to check: %s', pid)
os.getsid(pid)
logger.info('pid %d seems ok' % pid)
except IOError, e:
logger.error(e)
except OSError, e:
logger.error(e)
def monitorall():
if not options.monitor: return
for x in getrunfiles():
monitor(x)
def start(ports, port=options.port):
logger = logging.getLogger('start(%s)' % port)
logger.debug('starting...')
if int(port) not in ports:
logger.info('%s not available in the list' % port)
return
exist = True
try:
path = '%s/%s' % (RUN_PATH, port)
open(path)
logger.warning('port already activated')
except IOError:
exist = False
if exist: return
mode, remotehost, remoteport, description = ports[int(port)]
pid = subprocess.Popen(['autossh',
'-M', '0', '-N',
'-p', SERVER_PORT, '%s@%s' % (SERVER_USERNAME, SERVER_HOST),
'-R' if mode == 'IN' else '-L',
'*:%s:%s:%s' % (port, remotehost, remoteport)
]).pid
logger.debug('pid = %d', pid)
logger.debug('port number = %s', port)
try:
os.mkdir(RUN_PATH)
except OSError, e:
logger.info(e)
open('%s/%s' % (RUN_PATH, port), 'w').write(str(pid))
logger.debug('done')
def restart(ports, port=options.port):
if port == '': return
logger = logging.getLogger('restart(%s)' % port)
logger.debug('restarting...')
if int(port) not in ports:
logger.info('%s not available in the list' % port)
return
kill(port)
start(ports, port)
logger.debug('done')
def available(ports, port=options.port):
logger = logging.getLogger('available(%s)' % port)
logger.debug('availability...')
if int(port) not in ports:
logger.info('%s not available in the list' % port)
return
mode, remotehost, remoteport, description = ports[int(port)]
logger.info('\t%s,\t%s,\t%s,\t\t\t%s,\t%s' % (port, mode, remotehost, remoteport, description))
logger.debug('done')
def applytoall(ports, fct):
for port in ports.iterkeys():
fct(ports, port)
if __name__ == '__main__':
if options.version:
print 'Version: %s' % __version__
sys.exit()
if options.kill:
if options.port == '':
killall() # with -k
else:
kill() # with -k and -p PORT
sys.exit()
if options.monitor:
if options.port == '':
monitorall() # with -m
else:
monitor() # with -m and -p PORT
sys.exit()
config = {'USERNAME': 'root', ### Please define the SSH username
'HOST': '127.0.0.1', ### Please enter your server ip address used like ssh tunnel
'PORT': '443', ### And now the SSH port
}
try:
config = eval(''.join(open(options.config).readlines()))
except IOError:
pass
if not options.list:
print 'No list file defined ... exit.'
sys.exit()
# with -l FILE_PATH
ports = eval(''.join(open(options.list).readlines()))
if options.restart:
if options.port == '':
applytoall(ports, restart) # with -r
else:
restart(ports) # with -r and -p PORT
sys.exit()
if options.start:
if options.port == '':
applytoall(ports, start) # with -s
else:
start(ports) # with -s and -p PORT
if options.available:
if options.port == '':
applytoall(ports, available) # with -a
else:
available(ports) # with -a and -p PORT
############################