vpn integration
This commit is contained in:
+63
-37
@@ -1,24 +1,38 @@
|
||||
import os
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
import traceback
|
||||
import base64
|
||||
import io
|
||||
import os
|
||||
import traceback
|
||||
|
||||
from django.conf import settings
|
||||
from transmission_rpc import Client
|
||||
from transmission_rpc.error import TransmissionError
|
||||
|
||||
# from app.utils import send_sync_channel_message
|
||||
from .models import Torrent, File
|
||||
from user.models import User
|
||||
|
||||
# from app.utils import send_sync_channel_message
|
||||
from .models import File, Torrent
|
||||
|
||||
|
||||
class Transmission:
|
||||
trpc_args = [
|
||||
"id", "percentDone", "uploadRatio", "rateUpload", "rateDownload", "hashString", "status", "sizeWhenDone",
|
||||
"leftUntilDone", "name", "eta", "totalSize", "uploadedEver", "peersGettingFromUs", "peersSendingToUs",
|
||||
"tracker", "trackerStats", "activityDate"
|
||||
"id",
|
||||
"percentDone",
|
||||
"uploadRatio",
|
||||
"rateUpload",
|
||||
"rateDownload",
|
||||
"hashString",
|
||||
"status",
|
||||
"sizeWhenDone",
|
||||
"leftUntilDone",
|
||||
"name",
|
||||
"eta",
|
||||
"totalSize",
|
||||
"uploadedEver",
|
||||
"peersGettingFromUs",
|
||||
"peersSendingToUs",
|
||||
"tracker",
|
||||
"trackerStats",
|
||||
"activityDate",
|
||||
]
|
||||
|
||||
def __init__(self):
|
||||
@@ -31,7 +45,12 @@ class Transmission:
|
||||
if os.path.exists(port_file):
|
||||
try:
|
||||
with open(port_file) as f:
|
||||
vpn_port = int(f.read().strip())
|
||||
content = f.read().strip()
|
||||
if (
|
||||
not content
|
||||
): # Si le fichier est vide, on attend la prochaine itération
|
||||
return
|
||||
vpn_port = int(content)
|
||||
|
||||
# Récupère le port actuel configuré dans Transmission
|
||||
current_settings = self.client.get_session()
|
||||
@@ -55,15 +74,15 @@ class Transmission:
|
||||
def get_data(self, hash_string):
|
||||
data = self.client.get_torrent(hash_string, self.trpc_args)
|
||||
|
||||
return {
|
||||
"progress": data.progress,
|
||||
"status_str": data.status,
|
||||
**data.fields
|
||||
}
|
||||
return {"progress": data.progress, "status_str": data.status, **data.fields}
|
||||
|
||||
def get_all_data(self, hash_strings=None):
|
||||
return {
|
||||
data.hashString: {"progress": data.progress, "status_str": data.status, **data.fields}
|
||||
data.hashString: {
|
||||
"progress": data.progress,
|
||||
"status_str": data.status,
|
||||
**data.fields,
|
||||
}
|
||||
for data in self.client.get_torrents(hash_strings, self.trpc_args)
|
||||
}
|
||||
|
||||
@@ -84,7 +103,7 @@ class Transmission:
|
||||
port_file = "/tmp/gluetun/forwarded_port"
|
||||
vpn_port = None
|
||||
if os.path.exists(port_file):
|
||||
with open(port_file, "r") as f:
|
||||
with open(port_file) as f:
|
||||
vpn_port = f.read().strip()
|
||||
|
||||
# 2. Test de connectivité du port (via l'API Transmission)
|
||||
@@ -106,11 +125,7 @@ transmission_handler = Transmission()
|
||||
|
||||
|
||||
def torrent_proceed(user, file, file_mode="file_object"):
|
||||
r = {
|
||||
"torrent": None,
|
||||
"status": "error",
|
||||
"message": "Unexpected error"
|
||||
}
|
||||
r = {"torrent": None, "status": "error", "message": "Unexpected error"}
|
||||
|
||||
user: User
|
||||
if user.size_used > user.max_size:
|
||||
@@ -149,16 +164,18 @@ def torrent_proceed(user, file, file_mode="file_object"):
|
||||
name=data["name"],
|
||||
user=user,
|
||||
size=data["totalSize"],
|
||||
transmission_data=data
|
||||
transmission_data=data,
|
||||
)
|
||||
File.objects.bulk_create(
|
||||
[
|
||||
File(
|
||||
torrent=torrent,
|
||||
rel_name=file.name,
|
||||
size=file.size,
|
||||
)
|
||||
for file in transmission_handler.get_files(torrent.id)
|
||||
]
|
||||
)
|
||||
File.objects.bulk_create([
|
||||
File(
|
||||
torrent=torrent,
|
||||
rel_name=file.name,
|
||||
size=file.size,
|
||||
)
|
||||
for file in transmission_handler.get_files(torrent.id)
|
||||
])
|
||||
|
||||
r["torrent"] = torrent
|
||||
r["status"] = "success"
|
||||
@@ -167,13 +184,22 @@ def torrent_proceed(user, file, file_mode="file_object"):
|
||||
|
||||
|
||||
def torrent_share(torrent, current_user, target_user_id):
|
||||
from .models import Torrent, SharedUser
|
||||
from .models import SharedUser
|
||||
|
||||
torrent: Torrent
|
||||
|
||||
if (torrent.user_id != target_user_id and
|
||||
any([torrent.user == current_user, torrent.shared_users.filter(id=current_user.id)]) and
|
||||
not SharedUser.objects.filter(torrent_id=torrent.id, user_id=target_user_id).exists()):
|
||||
if (
|
||||
torrent.user_id != target_user_id
|
||||
and any(
|
||||
[
|
||||
torrent.user == current_user,
|
||||
torrent.shared_users.filter(id=current_user.id),
|
||||
]
|
||||
)
|
||||
and not SharedUser.objects.filter(
|
||||
torrent_id=torrent.id, user_id=target_user_id
|
||||
).exists()
|
||||
):
|
||||
torrent.shared_users.add(target_user_id)
|
||||
return True
|
||||
return False
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user