Created
October 9, 2014 13:46
-
-
Save MikolajKakol/d32395ad040a67396711 to your computer and use it in GitHub Desktop.
Proxy+gradle+OkHttp config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildTypes { | |
release { | |
buildConfigField "boolean", "OMIT_HTTPS_SECURITY", "false" | |
buildConfigField "String", "HTTP_PROXY_INET", "null" | |
buildConfigField "int", "HTTP_PROXY_PORT", "0" | |
} | |
debug { | |
buildConfigField "boolean", "OMIT_HTTPS_SECURITY", "true" | |
Properties properties = new Properties() | |
properties.load(project.rootProject.file('local.properties').newDataInputStream()) | |
buildConfigField "String", "HTTP_PROXY_INET", properties.getProperty('proxy.inet', 'null') | |
buildConfigField "int", "HTTP_PROXY_PORT", properties.getProperty('proxy.port', '0') | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proxy.inet=192.168.10.134 | |
proxy.port=8888 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Provides | |
@Singleton | |
OkHttpClient okHttpClient() { | |
OkHttpClient client = new OkHttpClient(); | |
if (BuildConfig.OMIT_HTTPS_SECURITY) { | |
ignoreHttpsSecurity(client); | |
} | |
//noinspection ConstantConditions | |
if (BuildConfig.HTTP_PROXY_INET != null) { | |
client.setProxy(new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(BuildConfig.HTTP_PROXY_INET, BuildConfig.HTTP_PROXY_PORT))); | |
} | |
return client; | |
} | |
private void ignoreHttpsSecurity(OkHttpClient client) { | |
TrustManager[] trustAllCerts = new TrustManager[]{ | |
new X509TrustManager() { | |
public X509Certificate[] getAcceptedIssuers() { | |
return null; | |
} | |
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException { | |
} | |
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException { | |
} | |
} | |
}; | |
try { | |
SSLContext sc = SSLContext.getInstance("SSL"); | |
sc.init(null, trustAllCerts, new SecureRandom()); | |
client.setSslSocketFactory(sc.getSocketFactory()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment