예제 #1
0
	def _tls_init(self):
		# Set up members for using tls, if requested.
		if self.tls in (False, '-'):
			self.tls = False
			return
		if self.tls in (None, True, ''):
			self.tls = fhs.module_get_config('network')['tls']
		if self.tls == '':
			self.tls = socket.getfqdn()
		elif self.tls == '-':
			self.tls = False
			return
		# Use tls.
		fc = fhs.read_data(os.path.join('certs', self.tls + os.extsep + 'pem'), opened = False, packagename = 'network')
		fk = fhs.read_data(os.path.join('private', self.tls + os.extsep + 'key'), opened = False, packagename = 'network')
		if fc is None or fk is None:
			# Create new self-signed certificate.
			certfile = fhs.write_data(os.path.join('certs', self.tls + os.extsep + 'pem'), opened = False, packagename = 'network')
			csrfile = fhs.write_data(os.path.join('csr', self.tls + os.extsep + 'csr'), opened = False, packagename = 'network')
			for p in (certfile, csrfile):
				path = os.path.dirname(p)
				if not os.path.exists(path):
					os.makedirs(path)
			keyfile = fhs.write_data(os.path.join('private', self.tls + os.extsep + 'key'), opened = False, packagename = 'network')
			path = os.path.dirname(keyfile)
			if not os.path.exists(path):
				os.makedirs(path, 0o700)
			os.system('openssl req -x509 -nodes -days 3650 -newkey rsa:4096 -subj "/CN=%s" -keyout "%s" -out "%s"' % (self.tls, keyfile, certfile))
			os.system('openssl req -subj "/CN=%s" -new -key "%s" -out "%s"' % (self.tls, keyfile, csrfile))
			fc = fhs.read_data(os.path.join('certs', self.tls + os.extsep + 'pem'), opened = False, packagename = 'network')
			fk = fhs.read_data(os.path.join('private', self.tls + os.extsep + 'key'), opened = False, packagename = 'network')
		self._tls_cert = fc
		self._tls_key = fk
예제 #2
0
	def _tls_init(self):
		# Set up members for using tls, if requested.
		if self.tls in (False, '-'):
			self.tls = False
			return
		if self.tls in (None, True, ''):
			self.tls = fhs.module_get_config('network')['tls']
		if self.tls == '':
			self.tls = socket.getfqdn()
		elif self.tls == '-':
			self.tls = False
			return
		# Use tls.
		fc = fhs.read_data(os.path.join('certs', self.tls + os.extsep + 'pem'), opened = False, packagename = 'network')
		fk = fhs.read_data(os.path.join('private', self.tls + os.extsep + 'key'), opened = False, packagename = 'network')
		if fc is None or fk is None:
			# Create new self-signed certificate.
			certfile = fhs.write_data(os.path.join('certs', self.tls + os.extsep + 'pem'), opened = False, packagename = 'network')
			csrfile = fhs.write_data(os.path.join('csr', self.tls + os.extsep + 'csr'), opened = False, packagename = 'network')
			for p in (certfile, csrfile):
				path = os.path.dirname(p)
				if not os.path.exists(path):
					os.makedirs(path)
			keyfile = fhs.write_data(os.path.join('private', self.tls + os.extsep + 'key'), opened = False, packagename = 'network')
			path = os.path.dirname(keyfile)
			if not os.path.exists(path):
				os.makedirs(path, 0o700)
			os.system('openssl req -x509 -nodes -days 3650 -newkey rsa:4096 -subj "/CN=%s" -keyout "%s" -out "%s"' % (self.tls, keyfile, certfile))
			os.system('openssl req -subj "/CN=%s" -new -key "%s" -out "%s"' % (self.tls, keyfile, csrfile))
			fc = fhs.read_data(os.path.join('certs', self.tls + os.extsep + 'pem'), opened = False, packagename = 'network')
			fk = fhs.read_data(os.path.join('private', self.tls + os.extsep + 'key'), opened = False, packagename = 'network')
		self._tls_cert = fc
		self._tls_key = fk
예제 #3
0
파일: readini.py 프로젝트: wijnen/pydink
def loaddinkfile(f): # {{{
	'''Load a file from dinkdir.'''
	if not is_setup:
		setup(fhs.module_get_config('pydink')['dinkdir'])
	p, walk, f, from_dmod = makedinkpath(f)
	if p is None:
		return None, None, False
	if f[-1] in walk:
		path = os.path.join(p, walk[f[-1]][0])
		ret = open(os.path.join(p, walk[f[-1]][0]), 'rb')
		ret.seek(0, 2)
		l = ret.tell()
		ret.seek(0)
		return ret, (path, 0, l), from_dmod
	if 'dir.ff' not in walk:
		return None, None, False
	path = os.path.join(p, walk['dir.ff'][0])
	dirfile = open(path, 'rb')
	n = read_lsb(dirfile) - 1
	for i in range(n):
		offset = read_lsb(dirfile)
		name = dirfile.read(13).rstrip('\0').lower()
		if name == f[-1]:
			break
		offset = None
	if offset == None:
		return None, None, False
	end = read_lsb(dirfile)
	dirfile.seek(offset)
	data = dirfile.read(end - offset)
	return StringIO.StringIO(data), (path, offset, end - offset), from_dmod
예제 #4
0
def svg(*parts, **kwargs):
    if 'sep' in kwargs:
        sep = kwargs['sep']
    else:
        sep = 5
    sep *= unit
    total_bb = [None] * 4
    offset = []
    for p in parts:
        bb = p.bbox(True)
        if total_bb[0] is None:
            total_bb = bb
            offset.append((0, 0))
            continue
        if bb[3] - bb[1] > total_bb[3] - total_bb[1]:
            total_bb[3] = bb[3] - bb[1] + total_bb[1]
        # Add the new bounding box to the current total and set offset.
        offset.append((total_bb[2] + sep - bb[0], total_bb[1] - bb[1]))
        total_bb[2] += sep + (bb[2] - bb[0])
    w, h = ((total_bb[2] - total_bb[0] + 2 * sep) / unit,
            (total_bb[3] - total_bb[1] + 2 * sep) / unit)
    ret = '''\
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width='%fmm' height='%fmm' viewBox='%f %f %f %f' xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink">
''' % (w, h, (total_bb[0] - sep) / unit, (-total_bb[3] - sep) / unit, w, h)
    s = fhs.module_get_config('polygon')['style']
    style = '' if len(s) == 0 else " style='" + s + "'"
    for p, o in zip(parts, offset):
        current_path = ''
        for s in p.segment:
            if len(current_path) > 0 and s.is_hole is False:
                ret += '<path d="' + current_path + '"' + style + '/>\n'
                current_path = ''
            current_path += s.path(o)
        if len(current_path) > 0:
            ret += '<path d="' + current_path + '"' + style + '/>\n'
            current_path = ''
    if len(debug_show) > 0:
        ret += "<g style='fill:none;stroke:red'>\n"
        for p in debug_show:
            for s in p.segment:
                ret += '<path d="' + s.path((0, 0)) + '"/>\n'
        ret += "</g>\n"
    ret += '</svg>\n'
    if 'file' in kwargs:
        with open(kwargs['file'], 'w') as f:
            f.write(ret)
    return ret
예제 #5
0
def Game(): # Main function to start a game. {{{
	global server, title_game, have_2d, have_3d, _num_players
	# Set up the game name.
	if not hasattr(__main__, 'name') or __main__.name is None:
		__main__.name = os.path.basename(sys.argv[0]).capitalize()
	# Initialize fhs module.
	if not fhs.initialized:
		fhs.init({}, packagename = __main__.name.lower(), game = True)
	# Set up other constants.
	if not hasattr(__main__, 'autokill'):
		__main__.autokill = True
	have_2d = fhs.read_data(os.path.join('html', '2d'), dir = True, opened = False) is not None
	have_3d = fhs.read_data(os.path.join('html', '3d'), dir = True, opened = False) is not None or not have_2d
	# Fill in min and max if not specified.
	assert hasattr(__main__, 'num_players')
	if isinstance(__main__.num_players, int):
		_num_players = (__main__.num_players, __main__.num_players)
	else:
		_num_players = __main__.num_players
	assert 1 <= _num_players[0] and (_num_players[1] is None or _num_players[0] <= _num_players[1])
	# Build asset string for inserting in js.
	for subdir, use_3d in (('2d', False), ('3d', True)):
		targets = []
		for base in ('img', 'jta', 'gani', 'audio', 'text'):
			for d in (os.path.join('html', base), os.path.join('html', subdir, base)):
				for p in fhs.read_data(d, dir = True, multiple = True, opened = False):
					targets.extend(f.encode('utf-8') for f in os.listdir(p) if not f.startswith('.') and not os.path.isdir(os.path.join(p, f)))
		if len(targets) > 0:
			loader_js[use_3d] = b'\n'.join(b"\tplease.load('" + f + b"');" for f in targets)
		else:
			# Nothing to load, but force the "finished loading" event to fire anyway.
			loader_js[use_3d] = b'\twindow.dispatchEvent(new CustomEvent("mgrl_media_ready"));'
	# Set up commands.
	cmds['leave'] = {None: leave}
	if hasattr(__main__, 'commands'):
		for c in __main__.commands:
			cmds[c] = {None: __main__.commands[c]}
	# Start up websockets server.
	config = fhs.module_get_config('webgame')
	httpdirs = [fhs.read_data(x, opened = False, multiple = True, dir = True) for x in ('html', os.path.join('html', '2d'), os.path.join('html', '3d'))]
	server = websocketd.RPChttpd(config['port'], Connection, tls = config['tls'], httpdirs = httpdirs[0] + httpdirs[1] + httpdirs[2])
	server.handle_ext('png', 'image/png')
	server.handle_ext('jpg', 'image/jpeg')
	server.handle_ext('jpeg', 'image/jpeg')
	server.handle_ext('gif', 'image/gif')
	server.handle_ext('gani', 'text/plain')
	server.handle_ext('wav', 'audio/wav')
	server.handle_ext('ogg', 'audio/ogg')
	server.handle_ext('mp3', 'audio/mp3')
	server.handle_ext('jta', 'application/octet-stream')
	server.handle_ext('txt', 'text/plain')
	server.handle_ext('frag', 'text/plain')
	server.handle_ext('vert', 'text/plain')
	server.handle_ext('glsl', 'text/plain')
	# Set up title page.
	if hasattr(__main__, 'Title'):
		title_game = Instance(__main__.Title, '')
	else:
		title_game = Instance(Title, '')
	log('Game "%s" started' % __main__.name)
	# Main loop.
	websocketd.fgloop()