add comments to code
This commit is contained in:
parent
daddb2bbc4
commit
1f73faa7e4
@ -9,11 +9,17 @@
|
|||||||
# Copyright 2019 - Thomas T. Jarløv
|
# Copyright 2019 - Thomas T. Jarløv
|
||||||
# --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-- #
|
# --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
|
||||||
# import db_sqlite, os, parsecfg, strutils, logging
|
import os
|
||||||
import db_sqlite, os, parsecfg, logging
|
import logging
|
||||||
|
import parsecfg
|
||||||
|
import db_sqlite
|
||||||
|
|
||||||
import ../code/password_utils
|
import ../code/password_utils
|
||||||
|
|
||||||
|
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# PROC : Generate Database
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
proc generateDB*() =
|
proc generateDB*() =
|
||||||
echo "Generating database"
|
echo "Generating database"
|
||||||
|
|
||||||
@ -69,6 +75,9 @@ proc generateDB*() =
|
|||||||
echo " - Database: session table already exists"
|
echo " - Database: session table already exists"
|
||||||
|
|
||||||
|
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# PROC : Create Administrator User
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
proc createAdminUser*(db: DbConn, args: seq[string]) =
|
proc createAdminUser*(db: DbConn, args: seq[string]) =
|
||||||
## Create new admin user
|
## Create new admin user
|
||||||
|
|
||||||
|
@ -8,7 +8,9 @@
|
|||||||
# [TODO] joplin_json_utils : need to create many procedures
|
# [TODO] joplin_json_utils : need to create many procedures
|
||||||
# --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-- #
|
# --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
|
||||||
# Data structure for TAGS
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# TYPE : Data structure for TAGS
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
type Tag = object
|
type Tag = object
|
||||||
id : string
|
id : string
|
||||||
parent_id : string
|
parent_id : string
|
||||||
@ -19,3 +21,4 @@ type Tags = object
|
|||||||
has_more: string
|
has_more: string
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
# joplin_utils : procedure related joplin application
|
# joplin_utils : procedure related joplin application
|
||||||
#
|
#
|
||||||
# --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-- #
|
# --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
|
||||||
import net
|
import net
|
||||||
import times
|
import times
|
||||||
import osproc
|
import osproc
|
||||||
@ -19,29 +18,37 @@ import std/options
|
|||||||
from os import sleep
|
from os import sleep
|
||||||
import std/[asyncdispatch, httpclient]
|
import std/[asyncdispatch, httpclient]
|
||||||
|
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
# Setup joplin_ping data
|
# TYPE : Setup joplin_ping data
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
type
|
type
|
||||||
joplin_ping* = object
|
joplin_ping* = object
|
||||||
ping_result*: seq[string]
|
ping_result*: seq[string]
|
||||||
ping_status*: bool
|
ping_status*: bool
|
||||||
req*: Request
|
req*: Request
|
||||||
|
|
||||||
# Setup joplin_tags data
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# TYPE : Setup joplin_tags data
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
type
|
type
|
||||||
joplin_tags* = object
|
joplin_tags* = object
|
||||||
id*, parent_id*, title*: seq[string]
|
id*, parent_id*, title*: seq[string]
|
||||||
created_time*, updated_time*, user_created_time*, user_updated_time*, is_shared*: seq[int]
|
created_time*, updated_time*, user_created_time*, user_updated_time*, is_shared*: seq[int]
|
||||||
req*: Request
|
req*: Request
|
||||||
|
|
||||||
# Setup joplin_notebooks data
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# TYPE : Setup joplin_notebooks data
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
type
|
type
|
||||||
joplin_notebooks* = object
|
joplin_notebooks* = object
|
||||||
id*, parent_id*, title*, share_id*, icon*: seq[string]
|
id*, parent_id*, title*, share_id*, icon*: seq[string]
|
||||||
created_time*, updated_time*, user_created_time*, user_updated_time*, is_shared*: seq[int]
|
created_time*, updated_time*, user_created_time*, user_updated_time*, is_shared*: seq[int]
|
||||||
req*: Request
|
req*: Request
|
||||||
|
|
||||||
# Setup joplin_notes data
|
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# TYPE : Setup joplin_notes data
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
type
|
type
|
||||||
joplin_notes* = object
|
joplin_notes* = object
|
||||||
id*, parent_id*, title*, body*, author*, source_url*, source*, source_application*, application_data*, share_id*, conflict_original_id*, body_html*, base_url*: seq[string]
|
id*, parent_id*, title*, body*, author*, source_url*, source*, source_application*, application_data*, share_id*, conflict_original_id*, body_html*, base_url*: seq[string]
|
||||||
@ -49,15 +56,25 @@ type
|
|||||||
latitude*, longitude*, altitude*, order*:seq[float]
|
latitude*, longitude*, altitude*, order*:seq[float]
|
||||||
req*: Request
|
req*: Request
|
||||||
|
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# DURATION : duration variables
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
let
|
let
|
||||||
resetDuration = initDuration(seconds=2)
|
resetDuration = initDuration(seconds=2)
|
||||||
deciSecondDuration* = initDuration(milliseconds = 100)
|
deciSecondDuration* = initDuration(milliseconds = 100)
|
||||||
qtrsecondDuration* = initDuration(milliseconds = 250)
|
qtrsecondDuration* = initDuration(milliseconds = 250)
|
||||||
|
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# VAR : connexion http variables
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
var
|
var
|
||||||
client = newHttpClient()
|
client = newHttpClient()
|
||||||
lastConnection = getTime().utc
|
lastConnection = getTime().utc
|
||||||
|
|
||||||
|
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# PROC : reset HTTP client
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
proc resetHttpClient() =
|
proc resetHttpClient() =
|
||||||
if (getTime().utc - lastConnection) > resetDuration:
|
if (getTime().utc - lastConnection) > resetDuration:
|
||||||
# Probably a new timeout. We have not yet experienced a long outage.
|
# Probably a new timeout. We have not yet experienced a long outage.
|
||||||
@ -71,8 +88,9 @@ proc resetHttpClient() =
|
|||||||
echo("Current exception: ", getCurrentExceptionMsg())
|
echo("Current exception: ", getCurrentExceptionMsg())
|
||||||
client = newHttpClient(timeout=500)
|
client = newHttpClient(timeout=500)
|
||||||
|
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==--==--==--==-==--==--==- #
|
||||||
# [TODO] procédure Ping à finir qui ne fonctionne pas.
|
# PROC : use the joplin API ping to validate service availibility
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==--==--==--==-==--==--==- #
|
||||||
proc ping_joplin*(token:string): Future[joplin_ping] {.async.} =
|
proc ping_joplin*(token:string): Future[joplin_ping] {.async.} =
|
||||||
|
|
||||||
var j_p: joplin_ping
|
var j_p: joplin_ping
|
||||||
@ -108,7 +126,9 @@ proc ping_joplin*(token:string): Future[joplin_ping] {.async.} =
|
|||||||
#let joplin_notes_Json = parseJson(json)
|
#let joplin_notes_Json = parseJson(json)
|
||||||
|
|
||||||
|
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# PROC : get all joplin NOTEBOOKS
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
proc get_joplin_notebooks*(token:string): Future[joplin_notebooks] {.async.} =
|
proc get_joplin_notebooks*(token:string): Future[joplin_notebooks] {.async.} =
|
||||||
|
|
||||||
# Variables
|
# Variables
|
||||||
@ -152,6 +172,10 @@ proc get_joplin_notebooks*(token:string): Future[joplin_notebooks] {.async.} =
|
|||||||
|
|
||||||
return j_nb
|
return j_nb
|
||||||
|
|
||||||
|
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# PROC : get all joplin NOTES
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
proc get_joplin_notes*(token:string): Future[joplin_notes] {.async.} =
|
proc get_joplin_notes*(token:string): Future[joplin_notes] {.async.} =
|
||||||
|
|
||||||
# Variables
|
# Variables
|
||||||
@ -195,6 +219,9 @@ proc get_joplin_notes*(token:string): Future[joplin_notes] {.async.} =
|
|||||||
return j_notes
|
return j_notes
|
||||||
|
|
||||||
|
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# PROC : get all joplin TAGS
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
proc get_joplin_tags*(token:string): Future[joplin_tags] {.async.} =
|
proc get_joplin_tags*(token:string): Future[joplin_tags] {.async.} =
|
||||||
|
|
||||||
# Variables
|
# Variables
|
||||||
@ -238,7 +265,9 @@ proc get_joplin_tags*(token:string): Future[joplin_tags] {.async.} =
|
|||||||
return j_tags
|
return j_tags
|
||||||
|
|
||||||
|
|
||||||
# get the token from Joplin Terminal
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# PROC : get the token from Joplin Terminal
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
proc get_joplin_cli_token*(): string =
|
proc get_joplin_cli_token*(): string =
|
||||||
var flagName: string = ""
|
var flagName: string = ""
|
||||||
var flagValue: string = ""
|
var flagValue: string = ""
|
||||||
@ -253,44 +282,55 @@ proc get_joplin_cli_token*(): string =
|
|||||||
return flagValue
|
return flagValue
|
||||||
|
|
||||||
|
|
||||||
# start Joplin Terminal
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# PROC : start Joplin Terminal
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
proc joplin_cli_start*(): bool =
|
proc joplin_cli_start*(): bool =
|
||||||
var rc = false
|
var rc = false
|
||||||
var result = execCmdEx("joplin server start &")
|
var result = execCmdEx("joplin server start &")
|
||||||
|
|
||||||
if result.exitCode == 0:
|
if result.exitCode == 0:
|
||||||
echo result.output
|
# echo result.output
|
||||||
rc = true
|
rc = true
|
||||||
else:
|
else:
|
||||||
rc = false
|
rc = false
|
||||||
return rc
|
return rc
|
||||||
|
|
||||||
|
|
||||||
# check Joplin Terminal staus
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# PROC : check Joplin Terminal staus
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
proc joplin_cli_status*(): bool =
|
proc joplin_cli_status*(): bool =
|
||||||
var rc = false
|
var rc = false
|
||||||
var result = execCmdEx("joplin server status")
|
var result = execCmdEx("joplin server status")
|
||||||
|
|
||||||
if result.exitCode == 0:
|
if result.exitCode == 0:
|
||||||
echo result.output
|
# echo result.output
|
||||||
rc = true
|
rc = true
|
||||||
else:
|
else:
|
||||||
rc = false
|
rc = false
|
||||||
return rc
|
return rc
|
||||||
|
|
||||||
|
|
||||||
# stop Joplin Terminal
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# PROC : stop Joplin Terminal
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
proc joplin_cli_stop*(): bool =
|
proc joplin_cli_stop*(): bool =
|
||||||
var rc = false
|
var rc = false
|
||||||
var result = execCmdEx("joplin server stop")
|
var result = execCmdEx("joplin server stop")
|
||||||
|
|
||||||
if result.exitCode == 0:
|
if result.exitCode == 0:
|
||||||
echo result.output
|
# echo result.output
|
||||||
rc = true
|
rc = true
|
||||||
else:
|
else:
|
||||||
rc = false
|
rc = false
|
||||||
return rc
|
return rc
|
||||||
|
|
||||||
|
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# PROC :
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
|
||||||
# proc get_joplin_tags_json*(token:string): Future[] {.async.} =
|
# proc get_joplin_tags_json*(token:string): Future[] {.async.} =
|
||||||
|
|
||||||
# # url = fmt"http://localhost:41184/notes/{id}?fields=id,parent_id,title,body,created_time,updated_time,is_conflict,latitude,longitude,altitude,author,source_url,is_todo,todo_due,todo_completed,source,source_application,application_data,order,user_created_time,user_updated_time,encryption_cipher_text,encryption_applied,markup_language,is_shared,share_id,conflict_original_id,master_key_id&token={token}"
|
# # url = fmt"http://localhost:41184/notes/{id}?fields=id,parent_id,title,body,created_time,updated_time,is_conflict,latitude,longitude,altitude,author,source_url,is_todo,todo_due,todo_completed,source,source_application,application_data,order,user_created_time,user_updated_time,encryption_cipher_text,encryption_applied,markup_language,is_shared,share_id,conflict_original_id,master_key_id&token={token}"
|
||||||
|
@ -8,19 +8,24 @@
|
|||||||
# Inspiration of : https://ttj.dk/blog/2019/01/20/setup-a-website-with-nim
|
# Inspiration of : https://ttj.dk/blog/2019/01/20/setup-a-website-with-nim
|
||||||
# Copyright 2019 - Thomas T. Jarløv
|
# Copyright 2019 - Thomas T. Jarløv
|
||||||
# --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-- #
|
# --==--==--==--==--==--==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
|
||||||
# import md5, bcrypt, math, random, os
|
|
||||||
import md5
|
import md5
|
||||||
import bcrypt
|
import bcrypt
|
||||||
import random
|
import random
|
||||||
randomize()
|
randomize()
|
||||||
|
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# Let and VAR : for random
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
var urandom: File
|
var urandom: File
|
||||||
let useUrandom = urandom.open("/dev/urandom")
|
let useUrandom = urandom.open("/dev/urandom")
|
||||||
|
|
||||||
proc makeSalt*(): string =
|
|
||||||
## Generate random salt. Uses cryptographically secure /dev/urandom
|
# --==--==--==--==--==--==--==--==--==--==--=--==--==--=--==--==--=--==--==-- #
|
||||||
## on platforms where it is available, and Nim's random module in other cases.
|
# PROC : Make Salt
|
||||||
|
## : Generate random salt. Uses cryptographically secure /dev/urandom
|
||||||
|
## : on platforms where it is available, and Nim's random module in other cases.
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==--=--==--==--=--==--==--=--==--==-- #
|
||||||
|
proc makeSalt*(): string =
|
||||||
result = ""
|
result = ""
|
||||||
if useUrandom:
|
if useUrandom:
|
||||||
var randomBytes: array[0..127, char]
|
var randomBytes: array[0..127, char]
|
||||||
@ -32,13 +37,20 @@ proc makeSalt*(): string =
|
|||||||
for i in 0..127:
|
for i in 0..127:
|
||||||
result.add(chr(rand(94) + 32)) # Generate numbers from 32 to 94 + 32 = 126
|
result.add(chr(rand(94) + 32)) # Generate numbers from 32 to 94 + 32 = 126
|
||||||
|
|
||||||
proc makeSessionKey*(): string =
|
|
||||||
## Creates a random key to be used to authorize a session.
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# PROC : Make session key
|
||||||
|
## Creates a random key to be used to authorize a session.
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
proc makeSessionKey*(): string =
|
||||||
let random = makeSalt()
|
let random = makeSalt()
|
||||||
return bcrypt.hash(random, genSalt(8))
|
return bcrypt.hash(random, genSalt(8))
|
||||||
|
|
||||||
|
|
||||||
proc makePassword*(password, salt: string, comparingTo = ""): string =
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
## Creates an MD5 hash by combining password and salt
|
# PROC : make password
|
||||||
|
## Creates an MD5 hash by combining password and salt
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
proc makePassword*(password, salt: string, comparingTo = ""): string =
|
||||||
let bcryptSalt = if comparingTo != "": comparingTo else: genSalt(8)
|
let bcryptSalt = if comparingTo != "": comparingTo else: genSalt(8)
|
||||||
result = hash(getMD5(salt & getMD5(password)), bcryptSalt)
|
result = hash(getMD5(salt & getMD5(password)), bcryptSalt)
|
@ -1,3 +1,6 @@
|
|||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# Database : information to setup DB
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
[Database]
|
[Database]
|
||||||
folder = "data"
|
folder = "data"
|
||||||
host = "data/website.db"
|
host = "data/website.db"
|
||||||
@ -5,14 +8,24 @@ name = "website"
|
|||||||
user = "user"
|
user = "user"
|
||||||
pass = ""
|
pass = ""
|
||||||
|
|
||||||
|
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# Server : informations related to server side
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
[Server]
|
[Server]
|
||||||
website = "https://127.0.0.1:7000"
|
website = "https://127.0.0.1:7000"
|
||||||
title = "Joplin The New Web"
|
title = "Joplin The New Web"
|
||||||
url = "127.0.0.1"
|
url = "127.0.0.1"
|
||||||
port = "7000"
|
port = "7000"
|
||||||
|
|
||||||
|
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
|
# Joplin : Informations related to Joplin app
|
||||||
|
# --==--==--==--==--==--==--==--==--==--==-- #
|
||||||
[Joplin]
|
[Joplin]
|
||||||
|
#Joplin DESKTOP token
|
||||||
#token = "e5f6644fbf6a97ddc55648dae72b11caecda6c6642d8ce0d3b20129b89b196385737eb908923542c3343649ebbf865b55bda031ab4c3a16edc7723ef2ad77d8f"
|
#token = "e5f6644fbf6a97ddc55648dae72b11caecda6c6642d8ce0d3b20129b89b196385737eb908923542c3343649ebbf865b55bda031ab4c3a16edc7723ef2ad77d8f"
|
||||||
|
# Joplin CLI token
|
||||||
token = "5b05f489016ce8a001ec83a7968419368eb9206340a18f73119c79e2154ab267ddec424658920bb6f88961c170a2680cd07fbd83f38e1c0c8df907a9aed22427"
|
token = "5b05f489016ce8a001ec83a7968419368eb9206340a18f73119c79e2154ab267ddec424658920bb6f88961c170a2680cd07fbd83f38e1c0c8df907a9aed22427"
|
||||||
joplin_server = "https://joplinlab.bcmaison.cf"
|
joplin_server = "https://joplinlab.bcmaison.cf"
|
||||||
joplin_server_user = "joplinlab@zohomail.com"
|
joplin_server_user = "joplinlab@zohomail.com"
|
||||||
|
Loading…
Reference in New Issue
Block a user