Update public source

This commit is contained in:
2026-10-06 21:08:12 +03:00
commit f231cc5b1f
102 changed files with 48249 additions and 0 deletions
+704
View File
@@ -0,0 +1,704 @@
import sqlite3
from pathlib import Path
class VendorResolver:
def __init__(
self,
database
):
self.db_path = Path(database)
self.db_path.parent.mkdir(
parents=True,
exist_ok=True
)
self.conn = sqlite3.connect(
self.db_path
)
self.create_tables()
# =====================================================
# Создание структуры
# =====================================================
def create_tables(self):
self.conn.execute(
"""
CREATE TABLE IF NOT EXISTS oui
(
prefix TEXT PRIMARY KEY,
vendor TEXT NOT NULL,
confidence TEXT NOT NULL,
prefix_length INTEGER NOT NULL
)
"""
)
self.conn.execute(
"""
CREATE TABLE IF NOT EXISTS metadata
(
key TEXT PRIMARY KEY,
value TEXT NOT NULL
)
"""
)
self.conn.execute(
"""
CREATE TABLE IF NOT EXISTS metadata
(
key TEXT PRIMARY KEY,
value TEXT
)
"""
)
self.conn.execute(
"""
CREATE INDEX IF NOT EXISTS idx_prefix
ON oui(prefix)
"""
)
self.conn.execute(
"""
CREATE TABLE IF NOT EXISTS metadata
(
key TEXT PRIMARY KEY,
value TEXT
)
"""
)
self.conn.commit()
# =====================================================
# Определение производителя
# =====================================================
def lookup(
self,
mac
):
if not mac:
return None
mac = self.normalize(
mac
)
for length in (
9,
7,
6
):
prefix = mac[:length]
cursor = self.conn.execute(
"""
SELECT vendor
FROM oui
WHERE prefix=?
""",
(
prefix,
)
)
row = cursor.fetchone()
if row:
return row[0]
return "Unknown"
# =====================================================
# Добавление производителя
# =====================================================
def add_vendor(
self,
prefix,
vendor,
confidence="IEEE"
):
prefix = self.normalize(prefix)
if not prefix:
return False
prefix_length = len(prefix)
if prefix_length not in (6,7,9):
return False
self.conn.execute(
"""
INSERT OR REPLACE INTO oui
(
prefix,
vendor,
confidence,
prefix_length
)
VALUES (?,?,?,?)
""",
(
prefix,
vendor,
confidence,
prefix_length
)
)
return True
# =====================================================
# Сохранение изменений
# =====================================================
def commit(self):
self.conn.commit()
# =====================================================
# Количество записей
# =====================================================
def count(self):
cursor = self.conn.execute(
"SELECT COUNT(*) FROM oui"
)
return cursor.fetchone()[0]
# =====================================================
# Metadata
# =====================================================
def get_metadata(
self,
key,
default=None
):
cursor = self.conn.execute(
"""
SELECT value
FROM metadata
WHERE key=?
""",
(
key,
)
)
row = cursor.fetchone()
if row:
return row[0]
return default
def set_metadata(
self,
key,
value
):
self.conn.execute(
"""
INSERT OR REPLACE INTO metadata
(
key,
value
)
VALUES (?,?)
""",
(
key,
str(value)
)
)
# =====================================================
# Получить значение metadata
# =====================================================
def get_metadata(
self,
key
):
cursor = self.conn.execute(
"""
SELECT value
FROM metadata
WHERE key=?
""",
(
key,
)
)
row = cursor.fetchone()
if row:
return row[0]
return None
# =====================================================
# Сохранение metadata
# =====================================================
def set_metadata(
self,
key,
value
):
self.conn.execute(
"""
INSERT OR REPLACE INTO metadata
(
key,
value
)
VALUES (?,?)
""",
(
key,
str(value)
)
)
# =====================================================
# Записать значение metadata
# =====================================================
def set_metadata(
self,
key,
value
):
self.conn.execute(
"""
INSERT OR REPLACE INTO metadata
(
key,
value
)
VALUES
(
?,
?
)
""",
(
key,
str(value)
)
)
# =====================================================
# Удаление metadata
# =====================================================
def delete_metadata(
self,
key
):
self.conn.execute(
"""
DELETE
FROM metadata
WHERE key=?
""",
(
key,
)
)
# =====================================================
# Есть ли база производителей
# =====================================================
def has_database(self):
cursor = self.conn.execute(
"""
SELECT COUNT(*)
FROM oui
"""
)
return cursor.fetchone()[0] > 0
# =====================================================
# Требуется обновление
# =====================================================
def update_available(self):
value = self.get_metadata(
"update_available"
)
return value == "1"
# =====================================================
# Получение параметра metadata
# =====================================================
def get_metadata(
self,
key
):
cursor = self.conn.execute(
"""
SELECT value
FROM metadata
WHERE key=?
""",
(
key,
)
)
row = cursor.fetchone()
if row:
return row[0]
return None
# =====================================================
# Запись параметра metadata
# =====================================================
def set_metadata(
self,
key,
value
):
self.conn.execute(
"""
INSERT OR REPLACE INTO metadata
(
key,
value
)
VALUES
(
?,
?
)
""",
(
key,
str(value)
)
)
# =====================================================
# Получение всех параметров metadata
# =====================================================
def get_all_metadata(
self
):
cursor = self.conn.execute(
"""
SELECT
key,
value
FROM metadata
"""
)
result = {}
for row in cursor.fetchall():
result[
row[0]
] = row[1]
return result
# =====================================================
# Удаление параметра metadata
# =====================================================
def delete_metadata(
self,
key
):
self.conn.execute(
"""
DELETE
FROM metadata
WHERE key=?
""",
(
key,
)
)
# =====================================================
# Проверка metadata
# =====================================================
def has_metadata(
self,
key
):
cursor = self.conn.execute(
"""
SELECT 1
FROM metadata
WHERE key=?
LIMIT 1
""",
(
key,
)
)
return cursor.fetchone() is not None
# =====================================================
# Приведение MAC
# =====================================================
def normalize(
self,
mac
):
if not mac:
return None
mac = mac.replace(
":",
""
)
mac = mac.replace(
"-",
""
)
mac = mac.replace(
".",
""
)
mac = mac.upper()
return mac
# =====================================================
# Получение metadata
# =====================================================
def get_metadata(
self,
key,
default=None
):
cursor = self.conn.execute(
"""
SELECT value
FROM metadata
WHERE key=?
""",
(
key,
)
)
row = cursor.fetchone()
if row:
return row[0]
return default
# =====================================================
# Закрытие
# =====================================================
def close(self):
self.conn.close()