Skip to content
This repository was archived by the owner on Jun 7, 2022. It is now read-only.

Commit 716edd1

Browse files
author
Tarek
committed
utry to fix the cloudflare multi domain bug
1 parent c303a99 commit 716edd1

File tree

4 files changed

+76
-39
lines changed

4 files changed

+76
-39
lines changed

cloudnet-cord/cloudnet-cloudflare/src/main/java/de/dytanic/cloudnet/cloudflare/CloudFlareService.java

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
import de.dytanic.cloudnet.cloudflare.util.DNSType;
1313
import de.dytanic.cloudnet.cloudflare.util.DefaultDNSRecord;
1414
import de.dytanic.cloudnet.cloudflare.util.SRVRecord;
15+
import de.dytanic.cloudnet.lib.MultiValue;
1516
import de.dytanic.cloudnet.lib.NetworkUtils;
16-
import de.dytanic.cloudnet.lib.database.Database;
1717
import de.dytanic.cloudnet.lib.server.ProxyGroup;
1818
import de.dytanic.cloudnet.lib.server.ProxyProcessMeta;
1919
import de.dytanic.cloudnet.lib.service.SimpledWrapperInfo;
20+
import de.dytanic.cloudnet.lib.utility.Acceptable;
2021
import de.dytanic.cloudnet.lib.utility.CollectionWrapper;
2122
import de.dytanic.cloudnet.lib.utility.document.Document;
2223
import lombok.Getter;
@@ -42,9 +43,9 @@ public class CloudFlareService {
4243
private Collection<CloudFlareConfig> cloudFlareConfigs;
4344

4445
// WrapperId DNSRecord
45-
private final Map<String, PostResponse>
46-
ipARecords = NetworkUtils.newConcurrentHashMap(),
47-
bungeeSRVRecords = NetworkUtils.newConcurrentHashMap();
46+
private final Map<String, MultiValue<PostResponse, String>> ipARecords = NetworkUtils.newConcurrentHashMap();
47+
48+
private final Map<String, MultiValue<PostResponse, String>> bungeeSRVRecords = NetworkUtils.newConcurrentHashMap();
4849

4950
/**
5051
* Constructs a new CloudFlare Service module with a given configuration.
@@ -61,12 +62,8 @@ public CloudFlareService(Collection<CloudFlareConfig> cloudFlareConfigs)
6162
@Deprecated
6263
public boolean bootstrap(Map<String, SimpledWrapperInfo> wrapperInfoMap, Map<String, ProxyGroup> groups, CloudFlareDatabase cloudFlareDatabase)
6364
{
64-
for (PostResponse id : cloudFlareDatabase.getAndRemove().values())
65-
this.deleteRecord(id);
66-
67-
for (String key : cloudFlareDatabase.getAll())
68-
if (!key.equalsIgnoreCase(Database.UNIQUE_NAME_KEY) && !wrapperInfoMap.containsKey(key))
69-
deleteRecord(cloudFlareDatabase.getResponse(key));
65+
for (MultiValue<PostResponse, String> id : cloudFlareDatabase.getAndRemove().values())
66+
this.deleteRecord(id.getFirst());
7067

7168
for (CloudFlareConfig cloudFlareConfig : this.cloudFlareConfigs)
7269
if (cloudFlareConfig.isEnabled())
@@ -75,15 +72,15 @@ public boolean bootstrap(Map<String, SimpledWrapperInfo> wrapperInfoMap, Map<Str
7572
ProxyGroup proxyGroup = groups.get(cloudFlareProxyGroup.getName());
7673
for (String wrapper : proxyGroup.getWrapper())
7774
{
78-
if (!cloudFlareDatabase.contains(wrapper))
75+
if (!cloudFlareDatabase.contains(cloudFlareConfig, wrapper)) //TODO
7976
{
8077
String host = wrapperInfoMap.get(wrapper).getHostName();
8178
DNSRecord dnsRecord = new DefaultDNSRecord(DNSType.A, wrapper + "." + cloudFlareConfig.getDomainName(), host, new Document().obj());
8279
if (!ipARecords.containsKey(wrapper))
8380
{
8481
PostResponse postResponse = this.createRecord(cloudFlareConfig, dnsRecord);
85-
ipARecords.put(wrapper, postResponse);
86-
cloudFlareDatabase.putPostResponse(wrapper, postResponse);
82+
ipARecords.put(postResponse.getId(), new MultiValue<>(postResponse, wrapper));
83+
cloudFlareDatabase.putPostResponse(new MultiValue<>(postResponse, wrapper));
8784
NetworkUtils.sleepUninterruptedly(400);
8885
}
8986
}
@@ -94,16 +91,22 @@ public boolean bootstrap(Map<String, SimpledWrapperInfo> wrapperInfoMap, Map<Str
9491

9592
public boolean shutdown(CloudFlareDatabase cloudFlareDatabase)
9693
{
97-
for (PostResponse postResponse : this.bungeeSRVRecords.values())
94+
for (MultiValue<PostResponse, String> postResponse : this.bungeeSRVRecords.values())
9895
{
99-
this.deleteRecord(postResponse);
100-
NetworkUtils.sleepUninterruptedly(450);
96+
try
97+
{
98+
this.deleteRecord(postResponse.getFirst());
99+
NetworkUtils.sleepUninterruptedly(500);
100+
} catch (Exception ex)
101+
{
102+
ex.printStackTrace();
103+
}
101104
}
102105

103-
for (PostResponse id : cloudFlareDatabase.getAndRemove().values())
106+
for (MultiValue<PostResponse, String> id : cloudFlareDatabase.getAndRemove().values())
104107
{
105-
this.deleteRecord(id);
106-
NetworkUtils.sleepUninterruptedly(450);
108+
this.deleteRecord(id.getFirst());
109+
NetworkUtils.sleepUninterruptedly(500);
107110
}
108111
return true;
109112
}
@@ -163,9 +166,9 @@ public void addProxy(ProxyProcessMeta proxyServer, CloudFlareDatabase cloudFlare
163166
proxyServer.getServiceId().getWrapperId() + "." + cloudFlareConfig.getDomainName()
164167
);
165168
}
166-
PostResponse postResponse1 = this.createRecord(cloudFlareConfig, srvRecord);
167-
cloudFlareDatabase.add(postResponse1);
168-
this.bungeeSRVRecords.put(proxyServer.getServiceId().getServerId(), postResponse1);
169+
PostResponse postResponse = this.createRecord(cloudFlareConfig, srvRecord);
170+
cloudFlareDatabase.add(postResponse);
171+
this.bungeeSRVRecords.put(postResponse.getId(), new MultiValue<>(postResponse, proxyServer.getServiceId().getServerId()));
169172
}
170173
}
171174
}
@@ -178,13 +181,24 @@ public void addProxy(ProxyProcessMeta proxyServer, CloudFlareDatabase cloudFlare
178181
*/
179182
public void removeProxy(ProxyProcessMeta proxyServer, CloudFlareDatabase cloudFlareDatabase)
180183
{
181-
if (!bungeeSRVRecords.containsKey(proxyServer.getServiceId().getServerId())) return;
184+
//if (!bungeeSRVRecords.containsKey(proxyServer.getServiceId().getServerId())) return;
182185

183-
PostResponse postResponse = bungeeSRVRecords.get(proxyServer.getServiceId().getServerId());
184-
if (postResponse != null)
186+
while (true)
185187
{
186-
cloudFlareDatabase.remove(postResponse.getId());
187-
deleteRecord(postResponse);
188+
MultiValue<PostResponse, String> postResponse = CollectionWrapper.filter(bungeeSRVRecords.values(), new Acceptable<MultiValue<PostResponse, String>>() {
189+
@Override
190+
public boolean isAccepted(MultiValue<PostResponse, String> postResponseStringMultiValue)
191+
{
192+
return postResponseStringMultiValue.getSecond().equalsIgnoreCase(proxyServer.getServiceId().getServerId());
193+
}
194+
});
195+
196+
//MultiValue<PostResponse, String> postResponse = bungeeSRVRecords.get(proxyServer.getServiceId().getServerId());
197+
if (postResponse != null)
198+
{
199+
cloudFlareDatabase.remove(postResponse.getFirst().getId());
200+
deleteRecord(postResponse.getFirst());
201+
} else break;
188202
}
189203
}
190204

@@ -231,6 +245,7 @@ public PostResponse createRecord(CloudFlareConfig cloudFlareConfig, DNSRecord dn
231245
{
232246
e.printStackTrace();
233247
}
248+
234249
return null;
235250
}
236251

cloudnet-cord/cloudnet-cloudflare/src/main/java/de/dytanic/cloudnet/cloudflare/database/CloudFlareDatabase.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
package de.dytanic.cloudnet.cloudflare.database;
66

77
import com.google.gson.reflect.TypeToken;
8+
import de.dytanic.cloudnet.cloudflare.CloudFlareConfig;
89
import de.dytanic.cloudnet.cloudflare.PostResponse;
910
import de.dytanic.cloudnet.database.DatabaseUsable;
11+
import de.dytanic.cloudnet.lib.MultiValue;
1012
import de.dytanic.cloudnet.lib.database.Database;
1113
import de.dytanic.cloudnet.lib.database.DatabaseDocument;
14+
import de.dytanic.cloudnet.lib.utility.Acceptable;
15+
import de.dytanic.cloudnet.lib.utility.CollectionWrapper;
1216
import de.dytanic.cloudnet.lib.utility.MapWrapper;
1317
import de.dytanic.cloudnet.lib.utility.Return;
1418
import de.dytanic.cloudnet.lib.utility.document.Document;
@@ -42,17 +46,28 @@ public Collection<String> getAll()
4246
return collection;
4347
}
4448

45-
public void putPostResponse(String wrapper, PostResponse postResponse)
49+
public void putPostResponse(MultiValue<PostResponse, String> postResponse)
4650
{
4751
Document document = database.getDocument(CLOUDFLARE_CACHE);
48-
document.append(wrapper, postResponse);
52+
document.append(postResponse.getFirst().getId(), postResponse);
4953
database.insert(document);
5054
}
5155

52-
public boolean contains(String wrapper)
56+
public boolean contains(CloudFlareConfig cloudFlareConfig, String wrapper)
5357
{
5458
Document document = database.getDocument(CLOUDFLARE_CACHE);
55-
return document.contains(wrapper);
59+
Map<String, MultiValue<PostResponse, String>> responses = document.getObject("requests", new TypeToken<Map<String, MultiValue<PostResponse, String>>>() {
60+
}.getType());
61+
62+
return CollectionWrapper.filter(responses.values(), new Acceptable<MultiValue<PostResponse, String>>() {
63+
@Override
64+
public boolean isAccepted(MultiValue<PostResponse, String> value)
65+
{
66+
return value.getSecond().equalsIgnoreCase(wrapper) && value.getFirst().getCloudFlareConfig().getDomainName().equalsIgnoreCase(cloudFlareConfig.getDomainName());
67+
}
68+
}) != null;
69+
70+
//return document.contains(wrapper);
5671
}
5772

5873
public void remove(String wrapper)
@@ -102,18 +117,17 @@ public void remove(PostResponse postResponse)
102117
database.insert(document);
103118
}
104119

105-
public Map<String, PostResponse> getAndRemove()
120+
public Map<String, MultiValue<PostResponse, String>> getAndRemove()
106121
{
107122
Document document = database.getDocument(CLOUDFLARE_CACHE_REQ);
108123
if (document.contains("requests"))
109124
{
110-
Map<String, PostResponse> responses = document.getObject("requests", new TypeToken<Map<String, PostResponse>>() {
125+
Map<String, MultiValue<PostResponse, String>> responses = document.getObject("requests", new TypeToken<Map<String, MultiValue<PostResponse, String>>>() {
111126
}.getType());
112-
document.append("requests", new HashMap<>(0));
127+
document.append("requests", Collections.EMPTY_MAP);
113128
database.insert(document);
114129
return responses;
115130
}
116131
return Collections.EMPTY_MAP;
117132
}
118-
119-
}
133+
}

cloudnet-modules/cloudnet-modules-cloudflare/src/main/java/de/dytanic/cloudnetcore/cloudflare/CloudFlareModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void onLoad()
4545
public void onBootstrap()
4646
{
4747
configCloudFlare = new ConfigCloudFlare();
48-
cloudFlareDatabase = new CloudFlareDatabase(getCloud().getDatabaseManager().getDatabase("cloud_internal_cfg"));
48+
cloudFlareDatabase = new CloudFlareDatabase(getCloud().getDatabaseManager().getDatabase("cloudnet_internal_cfg"));
4949
try
5050
{
5151

cloudnet-modules/cloudnet-modules-cloudflare/src/main/java/de/dytanic/cloudnetcore/cloudflare/config/ConfigCloudFlare.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
import java.io.File;
1515
import java.nio.file.Paths;
16-
import java.util.Arrays;
1716
import java.util.Collection;
17+
import java.util.Collections;
1818

1919
/**
2020
* Created by Tareko on 26.08.2017.
@@ -23,7 +23,15 @@ public class ConfigCloudFlare extends ConfigAbstract implements ILoader<Collecti
2323

2424
public ConfigCloudFlare()
2525
{
26-
super(new Document("configurations", Arrays.asList(new CloudFlareConfig(false, "example@gmail.com", "token", "example.com", "zone", Arrays.asList(new CloudFlareProxyGroup("Bungee", "server"))))),
26+
super(new Document("configurations", Collections.singletonList(
27+
new CloudFlareConfig(
28+
false,
29+
"example@gmail.com",
30+
"token",
31+
"example.com",
32+
"zone",
33+
Collections.singletonList(new CloudFlareProxyGroup("Bungee", "server"))))
34+
),
2735
Paths.get("local/cloudflare_cfg.json"));
2836
}
2937

0 commit comments

Comments
 (0)