This commit is contained in:
parent
5f54220113
commit
2b69254416
1 changed files with 17 additions and 10 deletions
19
src/gw.nim
19
src/gw.nim
|
@ -2,12 +2,13 @@ import prologue, prologue/middlewares/staticfile, prologue/middlewares/cors, std
|
||||||
|
|
||||||
const key_len = 128
|
const key_len = 128
|
||||||
const refer_len = 16
|
const refer_len = 16
|
||||||
const debug = true
|
const debug = false
|
||||||
|
|
||||||
let db = open("real.db", "", "", "")
|
let db = open("real.db", "", "", "")
|
||||||
|
|
||||||
db.exec(
|
db.exec(
|
||||||
sql"""CREATE TABLE IF NOT EXISTS keys(
|
sql"""CREATE TABLE IF NOT EXISTS keys(
|
||||||
|
referred_by VARCHAR(255) NOT NULL,
|
||||||
referral VARCHAR(255) NOT NULL,
|
referral VARCHAR(255) NOT NULL,
|
||||||
key VARCHAR(255) NOT NULL,
|
key VARCHAR(255) NOT NULL,
|
||||||
ip VARCHAR(255) NOT NULL
|
ip VARCHAR(255) NOT NULL
|
||||||
|
@ -25,21 +26,27 @@ proc referral_exists*(refer: string): bool =
|
||||||
let results = db.getAllRows(sql"SELECT * FROM keys WHERE referral=?", refer)
|
let results = db.getAllRows(sql"SELECT * FROM keys WHERE referral=?", refer)
|
||||||
return len(results) != 0
|
return len(results) != 0
|
||||||
|
|
||||||
proc generate_referral*(key, referral: var string, ip: string): void =
|
proc table_empty*(): bool =
|
||||||
|
return len(db.getAllRows(sql"SELECT * FROM keys")) == 0
|
||||||
|
|
||||||
|
proc generate_referral*(key, referral: var string, ip: string, referred_by: string): void =
|
||||||
key = "K-" & getRandomString(key_len)
|
key = "K-" & getRandomString(key_len)
|
||||||
referral = "R-" & getRandomString(refer_len)
|
referral = "R-" & getRandomString(refer_len)
|
||||||
db.exec(sql"INSERT INTO keys (referral, key, ip) VALUES (?, ?, ?)", referral, key, ip)
|
db.exec(sql"INSERT INTO keys (referral, key, ip, referred_by) VALUES (?, ?, ?, ?)", referral, key, ip, referred_by)
|
||||||
|
|
||||||
proc gentry*(ctx: Context) {.async.} =
|
proc gentry*(ctx: Context) {.async.} =
|
||||||
var key: string = ""
|
var key: string = ""
|
||||||
var referral: string = ""
|
var referral: string = ""
|
||||||
var err: bool = true
|
var err: bool = true
|
||||||
if (not ctx.request.hasHeader("referral") or
|
|
||||||
|
echo table_empty()
|
||||||
|
|
||||||
|
if ((not ctx.request.hasHeader("referral") or
|
||||||
not referral_exists($ctx.request.getHeader("referral")) or
|
not referral_exists($ctx.request.getHeader("referral")) or
|
||||||
ip_exists(ctx.request.hostName)) and not debug:
|
ip_exists(ctx.request.hostName)) and not table_empty()) and not debug:
|
||||||
err = true
|
err = true
|
||||||
else:
|
else:
|
||||||
generate_referral(key,referral, ctx.request.hostName)
|
generate_referral(key,referral, ctx.request.hostName, $ctx.request.getHeader("header"))
|
||||||
err = false
|
err = false
|
||||||
|
|
||||||
var info = %*
|
var info = %*
|
||||||
|
|
Loading…
Reference in a new issue