import io, json, re, ssl, sys, zipfile, urllib.request
from concurrent.futures import ThreadPoolExecutor

CTX = ssl._create_unverified_context()
BASE = 'https://10.112.205.15'
LIM = int(sys.argv[1]) if len(sys.argv) > 1 else 600
PAT = re.compile(r'(?i)(password|passwd|secret|token|api[_-]?key)[^A-Za-z0-9]{1,4}[=:][ \t]*["\']?[A-Za-z0-9!@#$%^&*_\-\.\+/]{5,}|jdbc:[a-z0-9]+://[^\s"\'<>]+|ldaps?://[^\s"\'<>]+|(?:postgres(?:ql)?|mysql|mongodb(?:\+srv)?)://[^ /@]+:[^ /@]+@[^\s"\'<>]+')
NAME = re.compile(r'(\.properties|\.ya?ml|\.xml|\.json|\.conf)$', re.I)


def get(u):
    req = urllib.request.Request(u, headers={'User-Agent': 'Mozilla/5.0'})
    return urllib.request.urlopen(req, timeout=30, context=CTX).read()


def scan(row):
    url, name, size = row
    if size and int(size) > 4000000:
        return []
    try:
        blob = get(url)
    except Exception:
        return []
    out = []
    try:
        z = zipfile.ZipFile(io.BytesIO(blob))
    except Exception:
        return []
    for zi in z.infolist():
        if zi.file_size > 200000 or not NAME.search(zi.filename):
            continue
        try:
            txt = z.read(zi).decode('utf-8', 'replace')
        except Exception:
            continue
        for m in PAT.finditer(txt):
            s = txt[max(0, m.start() - 50):m.start() + 120].replace('\n', ' ')
            out.append('%s :: %s :: %s' % (name, zi.filename, ' '.join(s.split())[:200]))
    return out


def main():
    rows = []
    for repo in ('maven-releases', 'maven-snapshots'):
        tok = None
        while len(rows) < 4000:
            u = BASE + '/service/rest/v1/search/assets?repository=%s' % repo
            if tok:
                u += '&continuationToken=' + tok
            try:
                d = json.loads(get(u))
            except Exception as e:
                print('ERR', repo, type(e).__name__, str(e)[:50], flush=True)
                break
            for it in d.get('items', []):
                p = it.get('path', '')
                if p.endswith(('.jar', '.war')) and '.sources' not in p and '.javadoc' not in p:
                    rows.append((it.get('downloadUrl'), p, it.get('fileSize')))
            tok = d.get('continuationToken')
            if not tok:
                break
        print('collected', repo, len(rows), flush=True)
    sel = rows[:LIM]
    print('selected', len(sel), flush=True)
    f = open('/tmp/.jv_hits.txt', 'w')
    n = 0
    with ThreadPoolExecutor(max_workers=6) as ex:
        for res in ex.map(scan, sel):
            n += 1
            for line in res:
                f.write(line + '\n')
            if n % 100 == 0:
                f.flush()
                print('done', n, flush=True)
    f.close()
    print('finished', n, flush=True)

main()
