-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package de.samply.proxy; | ||
|
||
import de.samply.app.ProjectManagerConst; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@ConfigurationProperties(prefix = ProjectManagerConst.HTTP_PROXY_PREFIX) | ||
public class HttpProxyConfiguration extends ProxyConfiguration { | ||
|
||
public HttpProxyConfiguration() { | ||
this.setSchema(ProjectManagerConst.HTTP_PROTOCOL_SCHEMA); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/de/samply/proxy/HttpsProxyConfiguration.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package de.samply.proxy; | ||
|
||
import de.samply.app.ProjectManagerConst; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@ConfigurationProperties(prefix = ProjectManagerConst.HTTPS_PROXY_PREFIX) | ||
public class HttpsProxyConfiguration extends ProxyConfiguration { | ||
|
||
public HttpsProxyConfiguration() { | ||
this.setSchema(ProjectManagerConst.HTTPS_PROTOCOL_SCHEMA); | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package de.samply.proxy; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
@Data | ||
public class ProxyConfiguration { | ||
|
||
private String host; | ||
private Integer port; | ||
private String schema; | ||
private String username; | ||
private String password; | ||
private String url; | ||
|
||
// Comma-separated list of hosts or IPs to bypass the proxy | ||
private String noProxy; | ||
private String noProxyPattern; | ||
|
||
// Convenience method to get noProxy as a List | ||
public List<String> getNoProxyList() { | ||
return noProxy != null ? Arrays.asList(noProxy.split(",")) : List.of(); | ||
} | ||
|
||
@PostConstruct | ||
public void init() throws MalformedURLException { | ||
if (StringUtils.hasText(this.url)) { | ||
URL url = new URL(this.url); | ||
this.schema = url.getProtocol(); | ||
this.host = url.getHost(); | ||
this.port = url.getPort(); | ||
setProxyUserAndPassword(url.getUserInfo()); | ||
if (this.noProxy != null) { | ||
this.noProxyPattern = createNonProxyPattern(this.noProxy); | ||
} | ||
} | ||
if (isConfigured()) { | ||
String schema = (this.schema != null) ? this.schema : ""; | ||
log.info(schema + " Proxy configured:"); | ||
log.info("\t-Host: " + this.host); | ||
log.info("\t-Port: " + this.port); | ||
if (username != null && password != null) { | ||
log.info("\t-Username: " + username); | ||
} | ||
if (noProxy != null) { | ||
log.info("\t-NoProxy: " + noProxy); | ||
} | ||
} | ||
} | ||
|
||
private void setProxyUserAndPassword(String userInfo) { | ||
if (userInfo != null) { | ||
String[] credentials = userInfo.split(":"); | ||
if (credentials.length == 2) { | ||
this.username = credentials[0]; | ||
this.password = credentials[1]; | ||
} | ||
} | ||
} | ||
|
||
public boolean isConfigured() { | ||
return StringUtils.hasText(this.host) && this.port != null; | ||
} | ||
|
||
// Method to create a regex pattern for non-proxy hosts | ||
private static String createNonProxyPattern(String nonProxyHosts) { | ||
// Split the comma-separated list into individual host patterns and build the regex | ||
return Arrays.stream(nonProxyHosts.split(",")) | ||
.map(String::trim) // Trim whitespace | ||
.map(host -> host.replace(".", "\\.") // Escape dots to literal | ||
.replace("*", ".*")) // Convert '*' to '.*' for wildcard matching | ||
.collect(Collectors.joining("|")); // Join with '|' to match any pattern | ||
} | ||
|
||
|
||
} |
This file contains 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