aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/dev/msfjarvis/aps/util/proxy/ProxyUtils.kt
blob: d21ee24d3a9745a2d053c764abfdee2638cf6bf5 (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
/*
 * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
 * SPDX-License-Identifier: GPL-3.0-only
 */

package dev.msfjarvis.aps.util.proxy

import dev.msfjarvis.aps.util.settings.GitSettings
import java.io.IOException
import java.net.Authenticator
import java.net.InetSocketAddress
import java.net.PasswordAuthentication
import java.net.Proxy
import java.net.ProxySelector
import java.net.SocketAddress
import java.net.URI
import javax.inject.Inject
import javax.inject.Singleton

/** Utility class for [Proxy] handling. */
@Singleton
class ProxyUtils @Inject constructor(private val gitSettings: GitSettings) {

  /** Set the default [Proxy] and [Authenticator] for the app based on user provided settings. */
  fun setDefaultProxy() {
    ProxySelector.setDefault(
      object : ProxySelector() {
        override fun select(uri: URI?): MutableList<Proxy> {
          val host = gitSettings.proxyHost
          val port = gitSettings.proxyPort
          return if (host == null || port == -1) {
            mutableListOf(Proxy.NO_PROXY)
          } else {
            mutableListOf(Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(host, port)))
          }
        }

        override fun connectFailed(uri: URI?, sa: SocketAddress?, ioe: IOException?) {
          if (uri == null || sa == null || ioe == null) {
            throw IllegalArgumentException("Arguments can't be null.")
          }
        }
      }
    )
    val user = gitSettings.proxyUsername ?: ""
    val password = gitSettings.proxyPassword ?: ""
    if (user.isEmpty() || password.isEmpty()) {
      System.clearProperty(HTTP_PROXY_USER_PROPERTY)
      System.clearProperty(HTTP_PROXY_PASSWORD_PROPERTY)
    } else {
      System.setProperty(HTTP_PROXY_USER_PROPERTY, user)
      System.setProperty(HTTP_PROXY_PASSWORD_PROPERTY, password)
    }
    Authenticator.setDefault(
      object : Authenticator() {
        override fun getPasswordAuthentication(): PasswordAuthentication? {
          return if (requestorType == RequestorType.PROXY) {
            PasswordAuthentication(user, password.toCharArray())
          } else {
            null
          }
        }
      }
    )
  }

  companion object {
    private const val HTTP_PROXY_USER_PROPERTY = "http.proxyUser"
    private const val HTTP_PROXY_PASSWORD_PROPERTY = "http.proxyPassword"
  }
}