-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
60 lines (47 loc) · 1.28 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from peewee import *
import json
import string
import random
db = SqliteDatabase('data/database.db')
class Tunnel(Model):
name = CharField(unique=True) # subdomain
hostname = CharField()
localaddr = CharField()
remoteport = CharField()
proto = CharField()
auth = CharField()
starttime = IntegerField()
class Meta:
database = db
class Auth(Model):
token = CharField()
class Meta:
database = db
@staticmethod
def token_gen(len):
str = string.ascii_letters + string.digits
keylist = [random.choice(str) for i in range(len)]
return "".join(keylist)
def tunnel_to_dict(row):
d = {}
d['id'] = row.id
d['name'] = row.name
d['hostname'] = row.hostname
d['localaddr'] = row.localaddr
d['remoteport'] = row.remoteport
d['proto'] = row.proto
d['auth'] = row.auth
d['starttime'] = row.starttime
return d
def database_init():
try:
Tunnel.create_table()
except OperationalError:
print("tunnel table already exists!")
try:
Auth.create_table()
random_token = Auth.token_gen(32)
Auth.create(token=random_token)
print("auth token: " + random_token)
except OperationalError:
print("auth table already exists!")