From 2c1790301261299b83fc6418df1974505a86a2de Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Thu, 3 Mar 2022 14:42:02 +0300 Subject: [PATCH] init: server workds with tls, needs database models Signed-off-by: HeshamTB --- .gitignore | 6 ++++++ echo_server.py | 25 +++++++++++++++++++++++++ genssl.sh | 10 ++++++++++ mkdb.sh | 26 ++++++++++++++++++++++++++ run-tls | 4 ++++ schema.sql | 8 ++++++++ server.py | 26 ++++++++++++++++++++++++++ 7 files changed, 105 insertions(+) create mode 100644 .gitignore create mode 100755 echo_server.py create mode 100644 genssl.sh create mode 100755 mkdb.sh create mode 100755 run-tls create mode 100644 schema.sql create mode 100755 server.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6f72a58 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +server.crt +server.csr +server.key +server.orig.key +__pycache__ +venv/ diff --git a/echo_server.py b/echo_server.py new file mode 100755 index 0000000..2ea4ce0 --- /dev/null +++ b/echo_server.py @@ -0,0 +1,25 @@ +#/bin/python + +import uvicorn +from typing import Optional +from fastapi import FastAPI + +from pydantic import BaseModel + + +app = FastAPI() + +class EchoType(BaseModel): + val: str + +@app.post("/echo") +def get_echo(echo: EchoType): + return echo + +if __name__ == '__main__': + #uvicorn server:app --port 8000 --ssl-certfile server.crt --ssl-keyfile server.key + uvicorn.run( + 'echo_server:app', port=8000, host='localhost', + reload=False, + ssl_keyfile='server.key', + ssl_certfile='server.crt') diff --git a/genssl.sh b/genssl.sh new file mode 100644 index 0000000..c8f792c --- /dev/null +++ b/genssl.sh @@ -0,0 +1,10 @@ +#/bin/env bash + +openssl genrsa -des3 -out server.orig.key 2048 + +openssl rsa -in server.orig.key -out server.key + +openssl req -new -key server.key -out server.csr + +openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt + diff --git a/mkdb.sh b/mkdb.sh new file mode 100755 index 0000000..ace7bc0 --- /dev/null +++ b/mkdb.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Make a new database +# Mar 3, 2022 - H.B. + +if [ -z ${1} ] +then + echo "Enter a databse name to use this" + exit 1 +fi + +sqlite3 ${1} << EOF +CREATE TABLE user_accounts( + id INTEGER(512) PRIMARY KEY NOT NULL, + username TEXT(64) NOT NULL, + date_created datetime , + digest TEXT NOT NULL, + salt TEXT +); +CREATE TABLE iot_entity( + id INTEGER(512) PRIMARY KEY NOT NULL, + description TEXT, + last_connection datetime, +); +EOF + diff --git a/run-tls b/run-tls new file mode 100755 index 0000000..fd567f2 --- /dev/null +++ b/run-tls @@ -0,0 +1,4 @@ +#!/bin/bash + +uvicorn server:app --ssl-certfile server.crt --ssl-keyfile server.key --port 4433 --host 0.0.0.0 --no-server-header --reload + diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..41aca33 --- /dev/null +++ b/schema.sql @@ -0,0 +1,8 @@ +CREATE TABLE user_accounts( + user_id UNSIGHNED INT PRIMARY KEY NOT NULL, + username NCHAR(64) NOT NULL, + email NCHAR(64) NOT NULL, + date_created DATETIME, + digest NCHAR(512) NOT NULL, + salt NCHAR(64) NOT NULL +); diff --git a/server.py b/server.py new file mode 100755 index 0000000..9e54a22 --- /dev/null +++ b/server.py @@ -0,0 +1,26 @@ +#/bin/python + +import uvicorn +from typing import Optional +from fastapi import FastAPI + +from pydantic import BaseModel + +class UserAuthForEntity(BaseModel): + username: str + passwd: str + entitiy: int + +app = FastAPI() + +@app.post("/isauth") +def is_auth(user_auth_for_entity: UserAuthForEntity): + return user_auth_for_entity + +if __name__ == '__main__': + #uvicorn server:app --port 8000 --ssl-certfile server.crt --ssl-keyfile server.key + uvicorn.run( + 'server:app', port=4433, host='0.0.0.0', + reload=False, + ssl_keyfile='server.key', + ssl_certfile='server.crt')