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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 

191 lines
6.9 KiB

# imports
import requests
from configparser import ConfigParser
from bottle import route, run, template, error, get, \
post, request, response, redirect, \
static_file
# config
configfile = "factoriocp.conf"
config = ConfigParser()
config.read(configfile)
# functions
# verify session
def logged_in():
username = request.get_cookie(config['DEFAULT']['cookie_name'],
secret=config['DEFAULT']['cookie_secret'])
if username:
return username
else:
return False
# authenticate against factorio.com
def log_in(username, password):
data = { "username": username, "password": password }
l = requests.post("https://auth.factorio.com/api-login", data=data)
if l.status_code == 200:
response.set_cookie(config['DEFAULT']['cookie_name'],
username,
secret=config['DEFAULT']['cookie_secret'],
max_age=config['DEFAULT']['cookie_max_age'])
return True
else:
return False
# get a list of server dicts
def get_servers():
pass
return [{ "name": "Creative fun", "desc": "A creative vanilla server for tests.", "version": "0.14.20", "status": "online", "port": "34197", "mods": [ "base", "" ] },
{ "name": "Vanilla on the rocks", "desc": "Vanilla map with some mods.", "version": "0.14.19", "status": "offline", "port": "34198", "mods": [ "base", "" ] },
{ "name": "Mad enemies", "desc": "Hazardous map. A lot of enemies.", "version": "0.14.19", "status": "offline", "port": "34199", "mods": [ "base", "Factorissimo", "long-reach" ] },
{ "name": "Online Map", "desc": "A modded map for friends.", "version": "0.14.19", "status": "online", "port": "34200", "mods": [ "base", "Factorissimo", "long-reach", "FactorioExtended-Extra", "FactorioExtended-Titan", "FactorioExtended-Military", "FactorioExtended-Core", "Factorissimo" ] },
{ "name": "Mad enemies2", "desc": "Just a copy of Mad enemies.", "version": "0.14.20", "status": "offline", "port": "34201", "mods": [ "base", ] }]
# find a available port for new server
def find_port():
ports = [ server['port'] for server in get_servers()]
for port in range(int(config['factorio']['port']),int(config['factorio']['port'])+9):
if str(port) not in ports:
return port
return False
# create new server
def create_server(name, desc, file):
port = find_port()
if port:
#create server directory in factorio base path
#download and unzip factorio headless to this directory
install_headless(path)
# routing
# render main page
@get('/')
def view_home():
username = logged_in()
message = request.get_cookie(config['DEFAULT']['cookie_name'] + "_message",
secret=config['DEFAULT']['cookie_secret'])
if username:
# render homepage
servers = get_servers()
return template('default',
username=username,
servers=servers,
app_name=config['DEFAULT']['app_name'],
fqdn=config['server']['fqdn'],
message=message)
else:
# render login
return template('login',
app_name=config['DEFAULT']['app_name'],
cookie_max_age=int(config['DEFAULT']['cookie_max_age']),
message=message)
# get login credentials
@post('/login')
def login():
username = request.forms.get('username')
password = request.forms.get('password')
if log_in(username, password):
message = { "message": "You have logged in successfully", "alert": "success" }
response.set_cookie(config['DEFAULT']['cookie_name'] + "_message",
message,
secret=config['DEFAULT']['cookie_secret'],
max_age=5)
redirect('/')
else:
message = { "message": "User or password wrong", "alert": "danger" }
response.set_cookie(config['DEFAULT']['cookie_name'] + "_message",
message,
secret=config['DEFAULT']['cookie_secret'],
max_age=5)
redirect('/')
# delete cookie
@get('/logout')
def logout():
if logged_in():
response.delete_cookie(config['DEFAULT']['cookie_name'])
message = { "message": "You have logged out successfully", "alert": "success" }
response.set_cookie(config['DEFAULT']['cookie_name'] + "_message",
message,
secret=config['DEFAULT']['cookie_secret'],
max_age=5)
redirect('/')
else:
redirect('/')
# serve static files
@get('/static/<filename:path>')
def send_static(filename):
return static_file(filename, root=config['DEFAULT']['static_files'])
# render server page
@get('/edit/<port:int>')
def view_server(port):
username = logged_in()
message = request.get_cookie(config['DEFAULT']['cookie_name'] + "_message",
secret=config['DEFAULT']['cookie_secret'])
if username:
servers = get_servers()
server = [ server for server in servers if int(server['port']) == int(port) ]
if server:
return template('edit',
app_name=config['DEFAULT']['app_name'],
server=server[0],
username=username,
fqdn=config['server']['fqdn'],
message=message)
else:
message = { "message": "Server does not exist", "alert": "danger" }
response.set_cookie(config['DEFAULT']['cookie_name'] + "_message",
message,
secret=config['DEFAULT']['cookie_secret'],
max_age=5,
path="/")
redirect("/")
else:
redirect("/")
# create new server
@get('/create')
def create_server():
username = logged_in()
message = request.get_cookie(config['DEFAULT']['cookie_name'] + "_message",
secret=config['DEFAULT']['cookie_secret'])
if username:
return template('create',
app_name=config['DEFAULT']['app_name'],
username=username,
fqdn=config['server']['fqdn'],
message=message)
else:
redirect("/")
# run webserver
if __name__ == "__main__":
if str(config['server']['debug']) == "True":
run(host=config['server']['host'],
port=config['server']['port'],
debug=True,
reloader=True)
else:
run(host=config['server']['host'],
port=config['server']['port'])