summaryrefslogtreecommitdiffhomepage
path: root/tunnel/src/main/java/com/wireguard/config/HttpProxy.java
blob: d45914f8e68c8a32634cf0f97f86fc88e5a11078 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright © 2022 WireGuard LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package com.wireguard.config;

import com.wireguard.config.BadConfigException.Location;
import com.wireguard.config.BadConfigException.Section;
import com.wireguard.util.NonNullForAll;

import java.net.MalformedURLException;
import java.net.URL;

import android.net.ProxyInfo;
import android.net.Uri;

@NonNullForAll
public final class HttpProxy {
    public static final int DEFAULT_PROXY_PORT = 8080;

    private ProxyInfo pi;

    protected HttpProxy(ProxyInfo pi) {
        this.pi = pi;
    }

    public ProxyInfo getProxyInfo() {
        return pi;
    }

    public String getHost() {
        return pi.getHost();
    }

    public Uri getPacFileUrl() {
        return pi.getPacFileUrl();
    }

    public int getPort() {
        return pi.getPort();
    }

    public static HttpProxy parse(final String httpProxy) throws BadConfigException {
        try {
            if (httpProxy.startsWith("pac:")) {
                return new HttpProxy(ProxyInfo.buildPacProxy(Uri.parse(httpProxy.substring(4))));
            } else {
                final String urlStr;
                if (!httpProxy.contains("://")) {
                    urlStr = "http://" + httpProxy;
                } else {
                    urlStr = httpProxy;
                }
                URL url = new URL(urlStr);
                return new HttpProxy(ProxyInfo.buildDirectProxy(url.getHost(), url.getPort() <= 0 ? DEFAULT_PROXY_PORT : url.getPort()));
            }
        } catch (final MalformedURLException e) {
            throw new BadConfigException(Section.INTERFACE, Location.HTTP_PROXY, httpProxy, e);
        }
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        if (pi.getPacFileUrl() != null && pi.getPacFileUrl() != Uri.EMPTY)
            sb.append("pac:").append(pi.getPacFileUrl());
        else {
            sb.append("http://").append(pi.getHost()).append(':');
            if (pi.getPort() <= 0)
                sb.append(DEFAULT_PROXY_PORT);
            else
                sb.append(pi.getPort());
        }

        return sb.toString();
    }
}