package defpackage;

import com.hierynomus.mssmb2.SMBApiException;
import com.hierynomus.protocol.transport.TransportException;
import com.hierynomus.smbj.common.SMBRuntimeException;
import com.hierynomus.smbj.paths.PathResolveException;
import com.hierynomus.smbj.session.SMB2GuestSigningRequiredException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/* JADX INFO: renamed from: ᵸᵹᵩᵽ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C5022 implements AutoCloseable {

    /* JADX INFO: renamed from: ᵠˏˏ, reason: contains not printable characters */
    public static final InterfaceC5025 f19029 = AbstractC2626.m6386(C5022.class);

    /* JADX INFO: renamed from: ʿٴˇ, reason: contains not printable characters */
    public boolean f19030;

    /* JADX INFO: renamed from: ˈᵲᵢ, reason: contains not printable characters */
    public final C2170 f19031;

    /* JADX INFO: renamed from: ˌᴵ, reason: contains not printable characters */
    public long f19032;

    /* JADX INFO: renamed from: ˌᵨʠᵫ, reason: contains not printable characters */
    public final ArrayList f19033;

    /* JADX INFO: renamed from: ˑˊᵲ, reason: contains not printable characters */
    public boolean f19034;

    /* JADX INFO: renamed from: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public boolean f19035;

    /* JADX INFO: renamed from: ᵜᵪʾ, reason: contains not printable characters */
    public final C0855 f19036;

    /* JADX INFO: renamed from: ᵨᵼⁱ, reason: contains not printable characters */
    public final C5179 f19037;

    /* JADX INFO: renamed from: ᵯᵷʽ, reason: contains not printable characters */
    public final C3250 f19038;

    /* JADX INFO: renamed from: ᵶˇᵱ, reason: contains not printable characters */
    public final ˈᵷᵵᵞ f19039;

    /* JADX INFO: renamed from: ᵸˆˁ, reason: contains not printable characters */
    public final ᵯᵨᵲ f19040;

    public C5022(C5179 c5179, ᵯᵨᵲ r4, C3250 c3250, ˈᵷᵵᵞ r6, InterfaceC0849 interfaceC0849) {
        C2170 c2170 = new C2170();
        c2170.f8623 = new ReentrantReadWriteLock();
        c2170.f8621 = new HashMap();
        c2170.f8622 = new HashMap();
        this.f19031 = c2170;
        this.f19033 = new ArrayList();
        this.f19037 = c5179;
        this.f19040 = r4;
        this.f19038 = c3250;
        this.f19039 = r6;
        EnumC3519 enumC3519 = (EnumC3519) ((C4349) c5179.f19498.f19206).f16540;
        C0855 c0855 = new C0855();
        c0855.f3766 = enumC3519;
        c0855.f3764 = interfaceC0849;
        this.f19036 = c0855;
        if (c3250 != null) {
            c3250.m7409(this);
        }
    }

    @Override // java.lang.AutoCloseable
    public final void close() throws TransportException {
        m10365();
    }

    /* JADX INFO: renamed from: ˏːˆ, reason: contains not printable characters */
    public final C4479 m10362(AbstractC1899 abstractC1899) throws TransportException {
        boolean z = this.f19035;
        C0855 c0855 = this.f19036;
        if (z && c0855.f3767 == null) {
            throw new TransportException("Message signing is required, but no signing key is negotiated");
        }
        if (c0855.f3767 != null) {
            abstractC1899 = new C3557(c0855, abstractC1899);
        } else {
            C0855.f3763.mo9486(((C2964) abstractC1899.ᵮᵣᵗˈ()).f11406, "Not wrapping {} as signed, as no key is set.");
        }
        return this.f19037.m10721(abstractC1899);
    }

    /* JADX WARN: Removed duplicated region for block: B:57:0x013e A[Catch: TransportException -> 0x0156, TryCatch #2 {TransportException -> 0x0156, blocks: (B:11:0x004a, B:12:0x007b, B:13:0x0081, B:15:0x0086, B:18:0x0096, B:20:0x00a1, B:22:0x00a7, B:27:0x00b2, B:29:0x00b7, B:33:0x00cc, B:35:0x00d6, B:37:0x00eb, B:44:0x0102, B:40:0x00f4, B:43:0x00fd, B:46:0x0106, B:47:0x010d, B:48:0x010e, B:49:0x0115, B:50:0x0116, B:51:0x0136, B:55:0x013a, B:57:0x013e, B:59:0x0147, B:58:0x0141, B:61:0x0149, B:62:0x0155), top: B:72:0x004a, inners: #5, #5 }] */
    /* JADX WARN: Removed duplicated region for block: B:58:0x0141 A[Catch: TransportException -> 0x0156, TryCatch #2 {TransportException -> 0x0156, blocks: (B:11:0x004a, B:12:0x007b, B:13:0x0081, B:15:0x0086, B:18:0x0096, B:20:0x00a1, B:22:0x00a7, B:27:0x00b2, B:29:0x00b7, B:33:0x00cc, B:35:0x00d6, B:37:0x00eb, B:44:0x0102, B:40:0x00f4, B:43:0x00fd, B:46:0x0106, B:47:0x010d, B:48:0x010e, B:49:0x0115, B:50:0x0116, B:51:0x0136, B:55:0x013a, B:57:0x013e, B:59:0x0147, B:58:0x0141, B:61:0x0149, B:62:0x0155), top: B:72:0x004a, inners: #5, #5 }] */
    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final AbstractC1250 m10363(String str) throws TransportException {
        AbstractC1250 c4748;
        C5022 c5022M10366;
        if (str.contains("\\")) {
            ᵕᵩʽᵲ.ᵮᵣᵗˈ(AbstractC3589.m8117("Share name (", str, ") cannot contain '\\' characters."));
            return null;
        }
        C2170 c2170 = this.f19031;
        ReentrantReadWriteLock reentrantReadWriteLock = c2170.f8623;
        reentrantReadWriteLock.readLock().lock();
        try {
            AbstractC1250 abstractC1250 = (AbstractC1250) c2170.f8622.get(str);
            reentrantReadWriteLock.readLock().unlock();
            InterfaceC5025 interfaceC5025 = f19029;
            if (abstractC1250 != null) {
                interfaceC5025.mo9485("Returning cached Share {} for {}", abstractC1250, str);
                return abstractC1250;
            }
            ˈᵷᵵᵞ r8 = this.f19039;
            C5179 c5179 = this.f19037;
            String str2 = c5179.f19495;
            C0974 c0974 = new C0974(str2, str, null);
            interfaceC5025.mo9487("Connecting to {} on session {}", c0974, Long.valueOf(this.f19032));
            try {
                C5102 c5102 = new C5102(9, (EnumC3519) ((C4349) c5179.f19498.f19206).f16540, EnumC4635.f17626, this.f19032, 0L, 1);
                c5102.f19221 = c0974;
                ((C2964) ((InterfaceC2684) ((ᵜᵪʾ) c5102).ˌᴵ)).f11404 = 256;
                try {
                    C5297 c5297 = (C5297) m10362(c5102).f17089.get(c5179.f19496.f15153, TimeUnit.MILLISECONDS);
                    try {
                        C0974 c09742 = r8.ᵛˑﹳ(this, c5297, c0974);
                        boolean z = ᵱˉˈ.ᵱʡˏ(c09742.f4268, str2);
                        String str3 = c09742.f4266;
                        String str4 = c09742.f4268;
                        if (z) {
                            c5022M10366 = this;
                        } else {
                            interfaceC5025.mo9494(str4, "Re-routing the connection to host {}");
                            c5022M10366 = m10366(c09742);
                        }
                        if (!(ᵱˉˈ.ᵱʡˏ(str4, str2) && ᵱˉˈ.ᵱʡˏ(str3, str))) {
                            return c5022M10366.m10363(str3);
                        }
                    } catch (PathResolveException unused) {
                    }
                    InterfaceC2684 interfaceC2684 = (InterfaceC2684) ((ᵜᵪʾ) c5297).ˌᴵ;
                    if ((((C2964) interfaceC2684).f11408 >>> 30) == 3) {
                        interfaceC5025.mo9495(((C2964) interfaceC2684).toString());
                        throw new SMBApiException((C2964) ((InterfaceC2684) ((ᵜᵪʾ) c5297).ˌᴵ), "Could not connect to " + c0974);
                    }
                    if (c5297.f20013.contains(EnumC1748.f6986)) {
                        throw new SMBRuntimeException("ASYMMETRIC capability unsupported");
                    }
                    C5227 c5227 = new C5227(((C2964) ((InterfaceC2684) ((ᵜᵪʾ) c5297).ˌᴵ)).f11413, c0974, this, this.f19037, this.f19038);
                    byte b = c5297.f20014;
                    if (b == 1) {
                        c4748 = new C4675(c0974, c5227, r8);
                    } else if (b == 2) {
                        c4748 = new C2784(c0974, c5227);
                    } else {
                        if (b != 3) {
                            throw new SMBRuntimeException("Unknown ShareType returned in the TREE_CONNECT Response");
                        }
                        c4748 = new C4748(c0974, c5227);
                    }
                    c2170.m5683(c4748);
                    return c4748;
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    throw new TransportException(e);
                } catch (ExecutionException e2) {
                    e = e2;
                    if (e instanceof TransportException) {
                        throw new TransportException(e);
                    }
                    throw ((TransportException) e);
                } catch (TimeoutException e3) {
                    e = e3;
                    if (e instanceof TransportException) {
                    }
                }
            } catch (TransportException e4) {
                throw new SMBRuntimeException(e4);
            }
        } catch (Throwable th) {
            reentrantReadWriteLock.readLock().unlock();
            throw th;
        }
    }

    /* JADX INFO: renamed from: ᵟᵹᵙ, reason: contains not printable characters */
    public final void m10364(C4147 c4147) {
        this.f19034 = c4147.f15824.contains(EnumC3260.f12406);
        boolean zContains = c4147.f15824.contains(EnumC3260.f12405);
        this.f19030 = zContains;
        C5179 c5179 = this.f19037;
        c5179.f19496.getClass();
        C5096 c5096 = c5179.f19498;
        if ((c5096.f19198 & 2) > 0) {
            this.f19035 = true;
        } else {
            this.f19035 = false;
        }
        if (zContains) {
            this.f19035 = false;
        }
        boolean z = this.f19034;
        if (z && this.f19035) {
            throw new SMB2GuestSigningRequiredException("Cannot require message signing when authenticating with a guest account");
        }
        if (z) {
            this.f19035 = false;
        }
        if (((EnumC3519) ((C4349) c5096.f19206).f16540).m8019() && c4147.f15824.contains(EnumC3260.f12407)) {
            this.f19035 = false;
        }
        if (this.f19034 || this.f19030) {
            C0855 c0855 = this.f19036;
            if (c0855.f3766.m8019()) {
                ᵕᵩʽᵲ.ᵪᵹᵥᵻ("Cannot set a signing key (yet) for SMB3.x");
            } else {
                c0855.f3765 = "HmacSHA256";
                c0855.f3767 = null;
            }
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:37:0x00fa A[Catch: all -> 0x0044, TryCatch #4 {all -> 0x0044, blocks: (B:3:0x0006, B:5:0x0029, B:7:0x0038, B:8:0x0040, B:13:0x0048, B:15:0x0057, B:17:0x005f, B:18:0x0078, B:20:0x007c, B:21:0x0088, B:22:0x00a9, B:23:0x00af, B:27:0x00cc, B:28:0x00ef, B:35:0x00f6, B:37:0x00fa, B:39:0x0103, B:38:0x00fd, B:40:0x0104, B:41:0x0110, B:43:0x0112, B:44:0x0119, B:4:0x001e), top: B:53:0x0006, inners: #0, #2, #3, #7, #6 }] */
    /* JADX WARN: Removed duplicated region for block: B:38:0x00fd A[Catch: all -> 0x0044, TryCatch #4 {all -> 0x0044, blocks: (B:3:0x0006, B:5:0x0029, B:7:0x0038, B:8:0x0040, B:13:0x0048, B:15:0x0057, B:17:0x005f, B:18:0x0078, B:20:0x007c, B:21:0x0088, B:22:0x00a9, B:23:0x00af, B:27:0x00cc, B:28:0x00ef, B:35:0x00f6, B:37:0x00fa, B:39:0x0103, B:38:0x00fd, B:40:0x0104, B:41:0x0110, B:43:0x0112, B:44:0x0119, B:4:0x001e), top: B:53:0x0006, inners: #0, #2, #3, #7, #6 }] */
    /* JADX INFO: renamed from: ᵱʡˏ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void m10365() throws TransportException {
        C3250 c3250 = this.f19038;
        C5179 c5179 = this.f19037;
        InterfaceC5025 interfaceC5025 = f19029;
        try {
            interfaceC5025.mo9487("Logging off session {} from host {}", Long.valueOf(this.f19032), c5179.f19495);
            C2170 c2170 = this.f19031;
            ReentrantReadWriteLock reentrantReadWriteLock = c2170.f8623;
            reentrantReadWriteLock.readLock().lock();
            try {
                ArrayList arrayList = new ArrayList(c2170.f8621.values());
                reentrantReadWriteLock.readLock().unlock();
                int size = arrayList.size();
                int i = 0;
                int i2 = 0;
                while (i2 < size) {
                    int i3 = i2 + 1;
                    AbstractC1250 abstractC1250 = (AbstractC1250) arrayList.get(i2);
                    try {
                        abstractC1250.close();
                    } catch (IOException e) {
                        interfaceC5025.mo9491("Caught exception while closing TreeConnect with id: {}", Long.valueOf(abstractC1250.f5278.f19713), e);
                    }
                    i2 = i3;
                }
                ArrayList arrayList2 = this.f19033;
                int size2 = arrayList2.size();
                while (i < size2) {
                    Object obj = arrayList2.get(i);
                    i++;
                    C5022 c5022 = (C5022) obj;
                    interfaceC5025.mo9487("Logging off nested session {} for session {}", Long.valueOf(c5022.f19032), Long.valueOf(this.f19032));
                    try {
                        c5022.m10365();
                    } catch (TransportException unused) {
                        interfaceC5025.mo9482(Long.valueOf(c5022.f19032), "Caught exception while logging off nested session {}");
                    }
                }
                try {
                    C0776 c0776 = (C0776) m10362(new C0776(4, (EnumC3519) ((C4349) c5179.f19498.f19206).f16540, EnumC4635.f17625, this.f19032, 0L)).f17089.get(c5179.f19496.f15153, TimeUnit.MILLISECONDS);
                    if (EnumC0878.m3076(((C2964) ((InterfaceC2684) ((ᵜᵪʾ) c0776).ˌᴵ)).f11408)) {
                        return;
                    }
                    throw new SMBApiException((C2964) ((InterfaceC2684) ((ᵜᵪʾ) c0776).ˌᴵ), "Could not logoff session <<" + this.f19032 + ">>");
                } catch (InterruptedException e2) {
                    Thread.currentThread().interrupt();
                    throw new TransportException(e2);
                } catch (ExecutionException e3) {
                    e = e3;
                    if (!(e instanceof TransportException)) {
                        throw ((TransportException) e);
                    }
                    throw new TransportException(e);
                } catch (TimeoutException e4) {
                    e = e4;
                    if (!(e instanceof TransportException)) {
                    }
                }
            } catch (Throwable th) {
                reentrantReadWriteLock.readLock().unlock();
                throw th;
            }
        } finally {
            c3250.f12363.ˈⁱᵡ(new C4195(this.f19032));
        }
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final C5022 m10366(C0974 c0974) {
        try {
            C5022 c5022M10724 = this.f19037.f19503.m6288(c0974.f4268).m10724(this.f19040);
            this.f19033.add(c5022M10724);
            return c5022M10724;
        } catch (IOException e) {
            EnumC4635 enumC4635 = EnumC4635.f17623;
            throw new SMBApiException(4294967295L, "Could not connect to DFS root " + c0974, e);
        }
    }
}
