Skip to content

Commit cb0f358

Browse files
committed
прокси
1 parent faabbc6 commit cb0f358

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

src/main/java/com/temnenkov/mz/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Main {
1212
public static void main(String[] args) {
1313
try {
1414
final Config config = ConfigLoader.loadConfig(args[0]);
15-
new TgInbound(config.getToken()).loop();
15+
new TgInbound(config.getToken(), config.getProxyHost(), config.getProxyPort()).loop();
1616
} catch (InterruptedException e) {
1717
Thread.currentThread().interrupt();
1818
logger.error("Interrupted", e);

src/main/java/com/temnenkov/mz/config/Config.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@
22

33
public class Config {
44
private final String token;
5+
private final String proxyHost;
6+
private final Integer proxyPort;
57

6-
public Config(String token) {
8+
public Config(String token, String proxyHost, Integer proxyPort) {
79
this.token = token;
10+
this.proxyHost = proxyHost;
11+
this.proxyPort = proxyPort;
12+
}
13+
14+
public Integer getProxyPort() {
15+
return proxyPort;
16+
}
17+
18+
public String getProxyHost() {
19+
return proxyHost;
820
}
921

1022
public String getToken() {

src/main/java/com/temnenkov/mz/config/ConfigLoader.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@
44
import org.jetbrains.annotations.NotNull;
55

66
import java.io.FileInputStream;
7-
import java.io.FileNotFoundException;
87
import java.io.IOException;
98
import java.util.Properties;
109

1110
public class ConfigLoader {
11+
12+
private ConfigLoader() {
13+
}
14+
1215
@Contract("_ -> new")
1316
public static @NotNull Config loadConfig(@NotNull String fileName) throws IOException {
1417
final Properties properties = new Properties();
1518
try (FileInputStream input = new FileInputStream(fileName)) {
1619
properties.load(input);
17-
return new Config(properties.getProperty("token"));
20+
return new Config(
21+
properties.getProperty("token"),
22+
properties.getProperty("proxy.host"),
23+
Integer.parseInt(properties.getProperty("proxy.port", "0"))
24+
);
1825
}
1926
}
2027
}

src/main/java/com/temnenkov/mz/ports/tg/TgInbound.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import com.temnenkov.mz.Main;
44
import com.temnenkov.mz.ports.tg.dto.GetUpdatesRequest;
55
import com.temnenkov.mz.utils.JsonUtils;
6+
import org.jetbrains.annotations.Nullable;
67
import org.slf4j.Logger;
78
import org.slf4j.LoggerFactory;
89

910
import java.io.IOException;
11+
import java.net.InetSocketAddress;
12+
import java.net.ProxySelector;
1013
import java.net.URI;
1114
import java.net.http.HttpClient;
1215
import java.net.http.HttpRequest;
@@ -17,12 +20,20 @@ public class TgInbound {
1720
private static final Logger logger = LoggerFactory.getLogger(TgInbound.class);
1821
private static final String CONTENT_TYPE_VALUE = "application/json";
1922
private static final String CONTENT_TYPE = "Content-Type";
20-
private final HttpClient httpClient =
21-
HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).connectTimeout(Duration.ofSeconds(5L)).build();
23+
private final HttpClient httpClient;
2224
private final String url;
2325

24-
public TgInbound(String token) {
25-
url = "https://api.telegram.org/bot" + token + "/sendMessage";
26+
public TgInbound(String token, @Nullable String proxyHost, int proxyPort) {
27+
28+
final HttpClient.Builder builder = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).connectTimeout(Duration.ofSeconds(5L));
29+
30+
if (proxyHost != null) {
31+
builder.proxy(ProxySelector.of(new InetSocketAddress(proxyHost, proxyPort)));
32+
}
33+
34+
httpClient = builder.build();
35+
36+
url = "https://api.telegram.org/bot" + token + "/getUpdates";
2637
}
2738

2839
public void loop() throws IOException, InterruptedException {

0 commit comments

Comments
 (0)