package K1;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Properties;
import java.util.Random;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import okhttp3.HttpUrl;

/* JADX INFO: loaded from: classes2.dex */
public class e extends K1.c implements K1.a {

    /* JADX INFO: renamed from: f0, reason: collision with root package name */
    private static final Pattern f1215f0 = Pattern.compile("(\\d{1,3},\\d{1,3},\\d{1,3},\\d{1,3}),(\\d{1,3}),(\\d{1,3})");

    /* JADX INFO: renamed from: A, reason: collision with root package name */
    private Duration f1216A;

    /* JADX INFO: renamed from: B, reason: collision with root package name */
    private int f1217B;

    /* JADX INFO: renamed from: C, reason: collision with root package name */
    private String f1218C;

    /* JADX INFO: renamed from: D, reason: collision with root package name */
    private final Random f1219D;

    /* JADX INFO: renamed from: E, reason: collision with root package name */
    private int f1220E;

    /* JADX INFO: renamed from: F, reason: collision with root package name */
    private int f1221F;

    /* JADX INFO: renamed from: G, reason: collision with root package name */
    private InetAddress f1222G;

    /* JADX INFO: renamed from: H, reason: collision with root package name */
    private InetAddress f1223H;

    /* JADX INFO: renamed from: I, reason: collision with root package name */
    private InetAddress f1224I;

    /* JADX INFO: renamed from: J, reason: collision with root package name */
    private int f1225J;

    /* JADX INFO: renamed from: K, reason: collision with root package name */
    private int f1226K;

    /* JADX INFO: renamed from: L, reason: collision with root package name */
    private int f1227L;

    /* JADX INFO: renamed from: M, reason: collision with root package name */
    private int f1228M;

    /* JADX INFO: renamed from: N, reason: collision with root package name */
    private boolean f1229N;

    /* JADX INFO: renamed from: O, reason: collision with root package name */
    private long f1230O;

    /* JADX INFO: renamed from: P, reason: collision with root package name */
    private L1.d f1231P;

    /* JADX INFO: renamed from: Q, reason: collision with root package name */
    private int f1232Q;

    /* JADX INFO: renamed from: R, reason: collision with root package name */
    private int f1233R;

    /* JADX INFO: renamed from: S, reason: collision with root package name */
    private int f1234S;

    /* JADX INFO: renamed from: T, reason: collision with root package name */
    private boolean f1235T;

    /* JADX INFO: renamed from: U, reason: collision with root package name */
    private boolean f1236U;

    /* JADX INFO: renamed from: V, reason: collision with root package name */
    private String f1237V;

    /* JADX INFO: renamed from: W, reason: collision with root package name */
    private j f1238W;

    /* JADX INFO: renamed from: X, reason: collision with root package name */
    private String f1239X;

    /* JADX INFO: renamed from: Y, reason: collision with root package name */
    private f f1240Y;

    /* JADX INFO: renamed from: c0, reason: collision with root package name */
    private boolean f1244c0;

    /* JADX INFO: renamed from: d0, reason: collision with root package name */
    private HashMap f1245d0;

    /* JADX INFO: renamed from: z, reason: collision with root package name */
    private int f1247z;

    /* JADX INFO: renamed from: Z, reason: collision with root package name */
    private Duration f1241Z = Duration.ZERO;

    /* JADX INFO: renamed from: a0, reason: collision with root package name */
    private Duration f1242a0 = Duration.ofSeconds(1);

    /* JADX INFO: renamed from: b0, reason: collision with root package name */
    private a f1243b0 = new b(this);

    /* JADX INFO: renamed from: e0, reason: collision with root package name */
    private boolean f1246e0 = Boolean.getBoolean("org.apache.commons.net.ftp.ipAddressFromPasvResponse");

    public interface a {
        String a(String str);
    }

    public static class b implements a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        private final e f1248a;

        public b(e eVar) {
            this.f1248a = eVar;
        }

        @Override // K1.e.a
        public String a(String str) {
            if (!InetAddress.getByName(str).isSiteLocalAddress()) {
                return str;
            }
            InetAddress inetAddressN = this.f1248a.n();
            return !inetAddressN.isSiteLocalAddress() ? inetAddressN.getHostAddress() : str;
        }
    }

    private static class c {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        static final Properties f1249a;

        static {
            Properties properties;
            InputStream resourceAsStream = e.class.getResourceAsStream("/systemType.properties");
            if (resourceAsStream != null) {
                properties = new Properties();
                try {
                    properties.load(resourceAsStream);
                } catch (IOException unused) {
                } catch (Throwable th) {
                    try {
                        resourceAsStream.close();
                    } catch (IOException unused2) {
                    }
                    throw th;
                }
                try {
                    resourceAsStream.close();
                } catch (IOException unused3) {
                }
            } else {
                properties = null;
            }
            f1249a = properties;
        }
    }

    public e() {
        n0();
        this.f1216A = Duration.ofMillis(-1L);
        this.f1229N = true;
        this.f1231P = new L1.c();
        this.f1240Y = null;
        this.f1235T = false;
        this.f1236U = false;
        this.f1219D = new Random();
        this.f1224I = null;
    }

    private InputStream f0(InputStream inputStream) {
        return this.f1232Q > 0 ? new BufferedInputStream(inputStream, this.f1232Q) : new BufferedInputStream(inputStream);
    }

    private static Properties j0() {
        return c.f1249a;
    }

    private void n0() {
        this.f1247z = 0;
        this.f1218C = null;
        this.f1217B = -1;
        this.f1222G = null;
        this.f1223H = null;
        this.f1220E = 0;
        this.f1221F = 0;
        this.f1225J = 0;
        this.f1227L = 7;
        this.f1226K = 4;
        this.f1228M = 10;
        this.f1230O = 0L;
        this.f1237V = null;
        this.f1238W = null;
        this.f1239X = HttpUrl.FRAGMENT_ENCODE_SET;
        this.f1245d0 = null;
    }

    private boolean o0() {
        String strSubstring;
        String strSubstring2;
        if (this.f1245d0 == null) {
            int iW = w();
            if (iW == 530) {
                return false;
            }
            boolean zA = t.a(iW);
            this.f1245d0 = new HashMap();
            if (!zA) {
                return false;
            }
            for (String str : this.f1206q) {
                if (str.startsWith(" ")) {
                    int iIndexOf = str.indexOf(32, 1);
                    if (iIndexOf > 0) {
                        strSubstring = str.substring(1, iIndexOf);
                        strSubstring2 = str.substring(iIndexOf + 1);
                    } else {
                        strSubstring = str.substring(1);
                        strSubstring2 = HttpUrl.FRAGMENT_ENCODE_SET;
                    }
                    ((Set) this.f1245d0.computeIfAbsent(strSubstring.toUpperCase(Locale.ENGLISH), new Function() { // from class: K1.d
                        @Override // java.util.function.Function
                        public final Object apply(Object obj) {
                            return e.t0((String) obj);
                        }
                    })).add(strSubstring2);
                }
            }
        }
        return true;
    }

    private s p0(j jVar, String str) {
        Socket socketV = V(g.LIST, h0(str));
        s sVar = new s(jVar, this.f1240Y);
        if (socketV == null) {
            return sVar;
        }
        try {
            sVar.g(socketV.getInputStream(), x());
            M1.d.a(socketV);
            b0();
            return sVar;
        } catch (Throwable th) {
            M1.d.a(socketV);
            throw th;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ Set t0(String str) {
        return new HashSet();
    }

    public void A0(int i2) {
        this.f1216A = Duration.ofMillis(i2);
    }

    public boolean B0(int i2) {
        if (!t.a(F(i2))) {
            return false;
        }
        this.f1228M = i2;
        return true;
    }

    public boolean C0(int i2) {
        if (!t.a(S(i2))) {
            return false;
        }
        this.f1225J = i2;
        this.f1226K = 4;
        return true;
    }

    public void D0(long j2) {
        if (j2 >= 0) {
            this.f1230O = j2;
        }
    }

    protected Socket V(g gVar, String str) {
        return W(gVar.getCommand(), str);
    }

    protected Socket W(String str, String str2) throws IOException {
        Socket socketAccept;
        int i2 = this.f1247z;
        if (i2 != 0 && i2 != 2) {
            return null;
        }
        boolean z2 = n() instanceof Inet6Address;
        int iA = K1.b.a(this.f1216A);
        if (this.f1247z == 0) {
            ServerSocket serverSocketCreateServerSocket = this.f1164i.createServerSocket(e0(), 1, g0());
            try {
                if (z2) {
                    if (!t.a(u(k0(), serverSocketCreateServerSocket.getLocalPort()))) {
                        serverSocketCreateServerSocket.close();
                        return null;
                    }
                } else if (!t.a(I(k0(), serverSocketCreateServerSocket.getLocalPort()))) {
                    serverSocketCreateServerSocket.close();
                    return null;
                }
                long j2 = this.f1230O;
                if (j2 > 0 && !y0(j2)) {
                    if (serverSocketCreateServerSocket != null) {
                        serverSocketCreateServerSocket.close();
                    }
                    return null;
                }
                if (!t.c(O(str, str2))) {
                    if (serverSocketCreateServerSocket != null) {
                        serverSocketCreateServerSocket.close();
                    }
                    return null;
                }
                if (iA >= 0) {
                    serverSocketCreateServerSocket.setSoTimeout(iA);
                }
                socketAccept = serverSocketCreateServerSocket.accept();
                if (iA >= 0) {
                    socketAccept.setSoTimeout(iA);
                }
                int i3 = this.f1234S;
                if (i3 > 0) {
                    socketAccept.setReceiveBufferSize(i3);
                }
                int i4 = this.f1233R;
                if (i4 > 0) {
                    socketAccept.setSendBufferSize(i4);
                }
                serverSocketCreateServerSocket.close();
            } catch (Throwable th) {
                if (serverSocketCreateServerSocket != null) {
                    try {
                        serverSocketCreateServerSocket.close();
                    } catch (Throwable th2) {
                        th.addSuppressed(th2);
                    }
                }
                throw th;
            }
        } else {
            if ((s0() || z2) && v() == 229) {
                X((String) this.f1206q.get(0));
            } else {
                if (z2 || H() != 227) {
                    return null;
                }
                Y((String) this.f1206q.get(0));
            }
            Socket socketCreateSocket = this.f1163h.createSocket();
            int i5 = this.f1234S;
            if (i5 > 0) {
                socketCreateSocket.setReceiveBufferSize(i5);
            }
            int i6 = this.f1233R;
            if (i6 > 0) {
                socketCreateSocket.setSendBufferSize(i6);
            }
            if (this.f1224I != null) {
                socketCreateSocket.bind(new InetSocketAddress(this.f1224I, 0));
            }
            if (iA >= 0) {
                socketCreateSocket.setSoTimeout(iA);
            }
            socketCreateSocket.connect(new InetSocketAddress(this.f1218C, this.f1217B), this.f1165j);
            long j3 = this.f1230O;
            if (j3 > 0 && !y0(j3)) {
                socketCreateSocket.close();
                return null;
            }
            if (!t.c(O(str, str2))) {
                socketCreateSocket.close();
                return null;
            }
            socketAccept = socketCreateSocket;
        }
        if (!this.f1229N || q(socketAccept)) {
            return socketAccept;
        }
        InetAddress inetAddress = socketAccept.getInetAddress();
        socketAccept.close();
        throw new IOException("Host attempting data connection " + inetAddress.getHostAddress() + " is not same as server " + n().getHostAddress());
    }

    protected void X(String str) throws J1.a {
        String strTrim = str.substring(str.indexOf(40) + 1, str.indexOf(41)).trim();
        char cCharAt = strTrim.charAt(0);
        char cCharAt2 = strTrim.charAt(1);
        char cCharAt3 = strTrim.charAt(2);
        char cCharAt4 = strTrim.charAt(strTrim.length() - 1);
        if (cCharAt != cCharAt2 || cCharAt2 != cCharAt3 || cCharAt3 != cCharAt4) {
            throw new J1.a("Could not parse extended passive host information.\nServer Reply: " + strTrim);
        }
        try {
            int i2 = Integer.parseInt(strTrim.substring(3, strTrim.length() - 1));
            this.f1218C = n().getHostAddress();
            this.f1217B = i2;
        } catch (NumberFormatException unused) {
            throw new J1.a("Could not parse extended passive host information.\nServer Reply: " + strTrim);
        }
    }

    protected void Y(String str) throws J1.a {
        Matcher matcher = f1215f0.matcher(str);
        if (!matcher.find()) {
            throw new J1.a("Could not parse passive host information.\nServer Reply: " + str);
        }
        String hostAddress = "0,0,0,0".equals(matcher.group(1)) ? this.f1157b.getInetAddress().getHostAddress() : matcher.group(1).replace(',', '.');
        try {
            int i2 = Integer.parseInt(matcher.group(3)) | (Integer.parseInt(matcher.group(2)) << 8);
            if (r0()) {
                a aVar = this.f1243b0;
                if (aVar != null) {
                    try {
                        String strA = aVar.a(hostAddress);
                        if (!hostAddress.equals(strA)) {
                            k(0, "[Replacing PASV mode reply address " + this.f1218C + " with " + strA + "]\n");
                            hostAddress = strA;
                        }
                    } catch (UnknownHostException unused) {
                        throw new J1.a("Could not parse passive host information.\nServer Reply: " + str);
                    }
                }
            } else {
                Socket socket = this.f1157b;
                hostAddress = socket == null ? null : socket.getInetAddress().getHostAddress();
            }
            this.f1218C = hostAddress;
            this.f1217B = i2;
        } catch (NumberFormatException unused2) {
            throw new J1.a("Could not parse passive port information.\nServer Reply: " + str);
        }
    }

    protected InputStream Z(String str, String str2) throws IOException {
        Socket socketW = W(str, str2);
        if (socketW == null) {
            return null;
        }
        return new M1.c(socketW, this.f1225J == 0 ? new M1.b(f0(socketW.getInputStream())) : socketW.getInputStream());
    }

    public boolean a0(String str) {
        return t.a(t(str));
    }

    @Override // J1.d
    protected void b() throws SocketException {
        r(null);
    }

    public boolean b0() {
        return t.a(y());
    }

    void c0(String str) throws IOException {
        String property;
        if (this.f1238W == null || !(str == null || this.f1239X.equals(str))) {
            if (str != null) {
                this.f1238W = this.f1231P.b(str);
                this.f1239X = str;
                return;
            }
            f fVar = this.f1240Y;
            if (fVar != null && fVar.e().length() > 0) {
                this.f1238W = this.f1231P.a(this.f1240Y);
                this.f1239X = this.f1240Y.e();
                return;
            }
            String property2 = System.getProperty("org.apache.commons.net.ftp.systemType");
            if (property2 == null) {
                property2 = l0();
                Properties propertiesJ0 = j0();
                if (propertiesJ0 != null && (property = propertiesJ0.getProperty(property2)) != null) {
                    property2 = property;
                }
            }
            if (this.f1240Y != null) {
                this.f1238W = this.f1231P.a(new f(property2, this.f1240Y));
            } else {
                this.f1238W = this.f1231P.b(property2);
            }
            this.f1239X = property2;
        }
    }

    @Override // K1.a
    public void d(f fVar) {
        this.f1240Y = fVar;
    }

    public void d0() {
        this.f1247z = 2;
        this.f1218C = null;
        this.f1217B = -1;
    }

    int e0() {
        int i2;
        int i3 = this.f1220E;
        if (i3 <= 0 || (i2 = this.f1221F) < i3) {
            return 0;
        }
        return i2 == i3 ? i2 : this.f1219D.nextInt((i2 - i3) + 1) + this.f1220E;
    }

    InetAddress g0() {
        InetAddress inetAddress = this.f1222G;
        return inetAddress != null ? inetAddress : m();
    }

    protected String h0(String str) {
        if (!i0()) {
            return str;
        }
        if (str == null) {
            return "-a";
        }
        StringBuilder sb = new StringBuilder(str.length() + 3);
        sb.append("-a ");
        sb.append(str);
        return sb.toString();
    }

    @Override // K1.c, J1.d
    public void i() {
        super.i();
        n0();
    }

    public boolean i0() {
        return this.f1235T;
    }

    InetAddress k0() {
        InetAddress inetAddress = this.f1223H;
        return inetAddress != null ? inetAddress : g0();
    }

    public String l0() throws IOException {
        if (this.f1237V == null) {
            if (t.a(R())) {
                this.f1237V = ((String) this.f1206q.get(r0.size() - 1)).substring(4);
            } else {
                String property = System.getProperty("org.apache.commons.net.ftp.systemType.default");
                if (property == null) {
                    throw new IOException("Unable to determine system type - response: " + B());
                }
                this.f1237V = property;
            }
        }
        return this.f1237V;
    }

    public boolean m0(String str) {
        if (o0()) {
            return this.f1245d0.containsKey(str.toUpperCase(Locale.ENGLISH));
        }
        return false;
    }

    public s q0(String str, String str2) throws IOException {
        c0(str);
        return p0(this.f1238W, str2);
    }

    @Override // K1.c
    protected void r(Reader reader) throws SocketException {
        super.r(reader);
        n0();
        if (this.f1244c0) {
            ArrayList arrayList = new ArrayList(this.f1206q);
            int i2 = this.f1205p;
            if (m0("UTF8") || m0("UTF-8")) {
                P("UTF-8");
                this.f1213x = new M1.a(new InputStreamReader(this.f1161f, x()));
                this.f1214y = new BufferedWriter(new OutputStreamWriter(this.f1162g, x()));
            }
            this.f1206q.clear();
            this.f1206q.addAll(arrayList);
            this.f1205p = i2;
            this.f1207r = true;
        }
    }

    public boolean r0() {
        return this.f1246e0;
    }

    public boolean s0() {
        return this.f1236U;
    }

    public i[] u0() {
        return v0(null);
    }

    public i[] v0(String str) {
        return q0(null, str).c();
    }

    public boolean w0(String str, String str2) {
        T(str);
        if (t.a(this.f1205p)) {
            return true;
        }
        if (t.b(this.f1205p)) {
            return t.a(G(str2));
        }
        return false;
    }

    public boolean x0() {
        return t.a(J());
    }

    protected boolean y0(long j2) {
        this.f1230O = 0L;
        return t.b(K(Long.toString(j2)));
    }

    public InputStream z0(String str) {
        return Z(g.RETR.getCommand(), str);
    }
}
