Skip to content

Commit

Permalink
pony
Browse files Browse the repository at this point in the history
  • Loading branch information
fagci committed Mar 30, 2021
1 parent 51ba7ad commit 73d5c35
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fortune_http_unseen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from lib.files import LOCAL_DIR
from lib.net import HTTPConnection
from lib.scan import generate_ips, process_each
from lib.models import add_result

LOG_FILE = LOCAL_DIR / 'http_unseen.txt'
DISALLOW_RE = re.compile(
Expand Down Expand Up @@ -38,6 +39,7 @@ def check_host(ip, lock):

with lock:
print(ip, title)
add_result(ip, 80, title, ['unseen'])
with LOG_FILE.open('a') as f:
f.write('%s %s\n' % (ip, title))

Expand Down
2 changes: 2 additions & 0 deletions fortune_rtsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from lib.scan import generate_ips, process_each
from lib.net import RTSPConnection
from lib.models import add_result

counter = 0
max_count = 1024
Expand Down Expand Up @@ -33,6 +34,7 @@ def check(ip, pl, out, p, t, i):
counter += 1
print('{:<4} {:<15} ({:>4} ms) {}'.format(
counter, ip, int(dt*1000), server[:20]))
add_result(ip, p, server)
out.write('%s\n' % ip)


Expand Down
51 changes: 51 additions & 0 deletions lib/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from pony.orm import Database, db_session
from pony.orm.core import Entity, Optional, PrimaryKey, Required, Set, commit, sql_debug
from datetime import datetime

from pony.orm.ormtypes import StrArray
from lib.files import LOCAL_DIR

DB_PATH = LOCAL_DIR / 'db.sqlite'

db = Database()


class Target(db.Entity):
ip = PrimaryKey(str)
created_at = Required(datetime, default=datetime.now())
updated_at = Required(datetime, default=datetime.now())
ports = Set('Port')
comment = Optional(str)

def before_update(self):
self.updated_at = datetime.now()


class Port(db.Entity):
num = Required(int)
comment = Optional(str)
targets = Set(Target)
tags = Optional(StrArray)


@db_session
def add_result(ip, port, comment='', tags=None):
try:
if tags is None:
tags = []
t = Target.get(ip=ip)
if not t:
t = Target(ip=ip)
if port not in t.ports.num:
t.ports.add(Port(num=port, comment=comment))
for p in t.ports:
if p.num == port:
for tag in tags:
p.tags.append(tag)
t.updated_at = datetime.now()
except Exception as e:
print('error', repr(e))


db.bind(provider='sqlite', filename=str(DB_PATH), create_db=True)
db.generate_mapping(create_tables=True)
3 changes: 3 additions & 0 deletions lib/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ def fn2(gen, gl, pl, running, *args):
break
try:
fn(item, pl, *args)
except StopIteration:
running = False
return
except:
running = False
raise
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ av
colorama
fire
pillow
pony
pyyaml
requests
tqdm
14 changes: 14 additions & 0 deletions results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env python3
from fire import Fire
from pony.orm.core import select
from pony.utils.utils import count
from lib.models import Port, Target, db_session


@db_session
def main():
select((p.num, p.tags, count(p)) for p in Port).show()


if __name__ == "__main__":
Fire(main)

0 comments on commit 73d5c35

Please sign in to comment.