You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
2.4 KiB
64 lines
2.4 KiB
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2014 clowwindy
|
|
#
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
# in the Software without restriction, including without limitation the rights
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
# furnished to do so, subject to the following conditions:
|
|
#
|
|
# The above copyright notice and this permission notice shall be included in
|
|
# all copies or substantial portions of the Software.
|
|
#
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
# SOFTWARE.
|
|
|
|
import socket
|
|
import struct
|
|
import logging
|
|
|
|
|
|
def parse_header(data):
|
|
addrtype = ord(data[0])
|
|
dest_addr = None
|
|
dest_port = None
|
|
header_length = 0
|
|
if addrtype == 1:
|
|
if len(data) >= 7:
|
|
dest_addr = socket.inet_ntoa(data[1:5])
|
|
dest_port = struct.unpack('>H', data[5:7])[0]
|
|
header_length = 7
|
|
else:
|
|
logging.warn('header is too short')
|
|
elif addrtype == 3:
|
|
if len(data) > 2:
|
|
addrlen = ord(data[1])
|
|
if len(data) >= 2 + addrlen:
|
|
dest_addr = data[2:2 + addrlen]
|
|
dest_port = struct.unpack('>H', data[2 + addrlen:4 +
|
|
addrlen])[0]
|
|
header_length = 4 + addrlen
|
|
else:
|
|
logging.warn('header is too short')
|
|
else:
|
|
logging.warn('header is too short')
|
|
elif addrtype == 4:
|
|
if len(data) >= 19:
|
|
dest_addr = socket.inet_ntop(socket.AF_INET6, data[1:17])
|
|
dest_port = struct.unpack('>H', data[17:19])[0]
|
|
header_length = 19
|
|
else:
|
|
logging.warn('header is too short')
|
|
else:
|
|
logging.warn('unsupported addrtype %d, maybe wrong password' % addrtype)
|
|
if dest_addr is None:
|
|
return None
|
|
return (addrtype, dest_addr, dest_port, header_length)
|
|
|