mirror of
https://github.com/9001/copyparty.git
synced 2025-08-17 09:02:15 -06:00
slight tweaks before merge
Signed-off-by: ed <s@ocv.me>
This commit is contained in:
parent
f55b938d28
commit
56e3b1e787
|
@ -25,8 +25,8 @@ from .util import (
|
||||||
termsize,
|
termsize,
|
||||||
)
|
)
|
||||||
|
|
||||||
if True:
|
if True: # pylint: disable=using-constant-test
|
||||||
from typing import Generator, Union
|
from typing import Generator, Optional, Union
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .svchub import SvcHub
|
from .svchub import SvcHub
|
||||||
|
@ -245,7 +245,7 @@ class TcpSrv(object):
|
||||||
|
|
||||||
def _listen(self, ip: str, port: int) -> None:
|
def _listen(self, ip: str, port: int) -> None:
|
||||||
uds_perm = uds_gid = -1
|
uds_perm = uds_gid = -1
|
||||||
bound = False
|
bound: Optional[socket.socket] = None
|
||||||
tcp = False
|
tcp = False
|
||||||
|
|
||||||
if "unix:" in ip:
|
if "unix:" in ip:
|
||||||
|
@ -274,10 +274,7 @@ class TcpSrv(object):
|
||||||
tcp = True
|
tcp = True
|
||||||
ipv = socket.AF_INET
|
ipv = socket.AF_INET
|
||||||
|
|
||||||
if not bound:
|
srv = bound or socket.socket(ipv, socket.SOCK_STREAM)
|
||||||
srv = socket.socket(ipv, socket.SOCK_STREAM)
|
|
||||||
else:
|
|
||||||
srv = bound
|
|
||||||
|
|
||||||
if not ANYWIN or self.args.reuseaddr:
|
if not ANYWIN or self.args.reuseaddr:
|
||||||
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
@ -295,31 +292,34 @@ class TcpSrv(object):
|
||||||
if getattr(self.args, "freebind", False):
|
if getattr(self.args, "freebind", False):
|
||||||
srv.setsockopt(socket.SOL_IP, socket.IP_FREEBIND, 1)
|
srv.setsockopt(socket.SOL_IP, socket.IP_FREEBIND, 1)
|
||||||
|
|
||||||
try:
|
if bound:
|
||||||
if not bound:
|
self.srv.append(srv)
|
||||||
if tcp:
|
return
|
||||||
srv.bind((ip, port))
|
|
||||||
else:
|
|
||||||
if ANYWIN or self.args.rm_sck:
|
|
||||||
if os.path.exists(ip):
|
|
||||||
os.unlink(ip)
|
|
||||||
srv.bind(ip)
|
|
||||||
else:
|
|
||||||
tf = "%s.%d" % (ip, os.getpid())
|
|
||||||
if os.path.exists(tf):
|
|
||||||
os.unlink(tf)
|
|
||||||
srv.bind(tf)
|
|
||||||
if uds_gid != -1:
|
|
||||||
os.chown(tf, -1, uds_gid)
|
|
||||||
if uds_perm != -1:
|
|
||||||
os.chmod(tf, uds_perm)
|
|
||||||
atomic_move(self.nlog, tf, ip, VF_CAREFUL)
|
|
||||||
|
|
||||||
sport = srv.getsockname()[1] if tcp else port
|
try:
|
||||||
if port != sport:
|
if tcp:
|
||||||
# linux 6.0.16 lets you bind a port which is in use
|
srv.bind((ip, port))
|
||||||
# except it just gives you a random port instead
|
else:
|
||||||
raise OSError(E_ADDR_IN_USE[0], "")
|
if ANYWIN or self.args.rm_sck:
|
||||||
|
if os.path.exists(ip):
|
||||||
|
os.unlink(ip)
|
||||||
|
srv.bind(ip)
|
||||||
|
else:
|
||||||
|
tf = "%s.%d" % (ip, os.getpid())
|
||||||
|
if os.path.exists(tf):
|
||||||
|
os.unlink(tf)
|
||||||
|
srv.bind(tf)
|
||||||
|
if uds_gid != -1:
|
||||||
|
os.chown(tf, -1, uds_gid)
|
||||||
|
if uds_perm != -1:
|
||||||
|
os.chmod(tf, uds_perm)
|
||||||
|
atomic_move(self.nlog, tf, ip, VF_CAREFUL)
|
||||||
|
|
||||||
|
sport = srv.getsockname()[1] if tcp else port
|
||||||
|
if port != sport:
|
||||||
|
# linux 6.0.16 lets you bind a port which is in use
|
||||||
|
# except it just gives you a random port instead
|
||||||
|
raise OSError(E_ADDR_IN_USE[0], "")
|
||||||
self.srv.append(srv)
|
self.srv.append(srv)
|
||||||
except (OSError, socket.error) as ex:
|
except (OSError, socket.error) as ex:
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Reference in a new issue