Skip to content

Commit 797ac36

Browse files
committed
Extremely WIP
1 parent dbac749 commit 797ac36

File tree

6 files changed

+231
-10
lines changed

6 files changed

+231
-10
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package io.github.techstreet.dfscript.features;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
6+
import com.google.gson.JsonParser;
7+
import com.mojang.authlib.exceptions.AuthenticationException;
8+
import io.github.techstreet.dfscript.DFScript;
9+
import io.github.techstreet.dfscript.loader.Loadable;
10+
import net.minecraft.client.util.Session;
11+
import org.apache.commons.codec.digest.DigestUtils;
12+
13+
import java.io.BufferedReader;
14+
import java.io.InputStream;
15+
import java.io.InputStreamReader;
16+
import java.io.OutputStream;
17+
import java.net.HttpURLConnection;
18+
import java.net.URL;
19+
import java.nio.charset.Charset;
20+
import java.util.Objects;
21+
import java.util.UUID;
22+
23+
import static io.github.techstreet.dfscript.screen.script.ScriptAddScreen.readAll;
24+
25+
public class AuthHandler implements Loadable {
26+
private static String authCode = null;
27+
private static boolean staff = false;
28+
29+
@Override
30+
public void load() {
31+
regen();
32+
}
33+
34+
public static void regen() {
35+
URL url;
36+
HttpURLConnection con;
37+
String commonSecret;
38+
JsonObject obj;
39+
40+
try {
41+
// Authorization step one - Create a random clientcode
42+
url = new URL("https://DFScript-Server.techstreetdev.repl.co/auth/secret/");
43+
con = (HttpURLConnection) url.openConnection();
44+
con.setRequestMethod("POST");
45+
con.setRequestProperty("Content-Type", "application/json");
46+
con.setRequestProperty("Accept", "application/json");
47+
con.setDoOutput(true);
48+
con.setReadTimeout(5000);
49+
con.setConnectTimeout(5000);
50+
51+
String clientCode = UUID.randomUUID().toString();
52+
53+
obj = new JsonObject();
54+
obj.addProperty("uuid", DFScript.PLAYER_UUID);
55+
obj.addProperty("clientcode", clientCode);
56+
57+
try (OutputStream os = con.getOutputStream()) {
58+
byte[] input = obj.toString().getBytes("utf-8");
59+
os.write(input, 0, input.length);
60+
}
61+
62+
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) {
63+
StringBuilder response = new StringBuilder();
64+
String responseLine;
65+
66+
while ((responseLine = br.readLine()) != null) {
67+
response.append(responseLine.trim());
68+
}
69+
70+
// ServercodeResponse servercodeResponse = DFScript.GSON.fromJson(response.toString(), ServercodeResponse.class);
71+
// commonSecret = DigestUtils.sha256Hex(servercodeResponse.getServercode() + clientCode);
72+
// commonSecret = commonSecret.substring(0, 30);
73+
}
74+
75+
// Authorization step two - Fake server connect
76+
// try {
77+
// Session session = DFScript.MC.getSession();
78+
// DFScript.MC.getSessionService().joinServer(session.getProfile(), session.getAccessToken(), commonSecret);
79+
// } catch (AuthenticationException e) {
80+
// DFScript.LOGGER.error(e.getMessage());
81+
// e.printStackTrace();
82+
// }
83+
84+
// Authorization step two - Generate the authcode
85+
url = new URL("https://DFScript-Server.techstreetdev.repl.co/auth/auth/");
86+
con = (HttpURLConnection) url.openConnection();
87+
con.setRequestMethod("POST");
88+
con.setRequestProperty("Content-Type", "application/json");
89+
con.setRequestProperty("Accept", "application/json");
90+
con.setDoOutput(true);
91+
con.setReadTimeout(5000);
92+
con.setConnectTimeout(5000);
93+
94+
obj = new JsonObject();
95+
// obj.addProperty("secret", commonSecret);
96+
obj.addProperty("uuid", DFScript.PLAYER_UUID);
97+
98+
try (OutputStream os = con.getOutputStream()) {
99+
byte[] input = obj.toString().getBytes("utf-8");
100+
os.write(input, 0, input.length);
101+
}
102+
103+
try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))) {
104+
StringBuilder response = new StringBuilder();
105+
String responseLine;
106+
107+
while ((responseLine = br.readLine()) != null) {
108+
response.append(responseLine.trim());
109+
}
110+
111+
// AuthcodeResponse authcodeResponse = DFScript.GSON.fromJson(response.toString(), AuthcodeResponse.class);
112+
// authCode = authcodeResponse.getAuthcode();
113+
DFScript.LOGGER.info("Server authorization code successfully generated!");
114+
}
115+
} catch (Exception e) {
116+
e.printStackTrace();
117+
}
118+
119+
try {
120+
InputStream is = new URL("https://dfscript-server.techstreetdev.repl.co/staff/").openStream();
121+
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
122+
obj = JsonParser.parseString(readAll(rd)).getAsJsonObject();
123+
124+
JsonArray array = obj.get("staff").getAsJsonArray();
125+
boolean localStaff = false;
126+
127+
for (JsonElement staffMember : array) {
128+
if (Objects.equals(staffMember.getAsString(), DFScript.PLAYER_UUID)) {
129+
staff = true;
130+
localStaff = true;
131+
}
132+
}
133+
134+
if (!localStaff) {
135+
staff = false;
136+
}
137+
} catch (Exception e) {
138+
e.printStackTrace();
139+
}
140+
}
141+
142+
public static String getAuthCode() {
143+
return authCode;
144+
}
145+
146+
public static boolean getStaffMember() {
147+
return staff;
148+
}
149+
}

src/main/java/io/github/techstreet/dfscript/network/AuthHandler.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.github.techstreet.dfscript.DFScript;
66
import io.github.techstreet.dfscript.loader.Loadable;
77
import io.github.techstreet.dfscript.network.request.ForbiddenException;
8+
import io.github.techstreet.dfscript.network.request.ReadBody;
89
import io.github.techstreet.dfscript.network.request.ServerCodeResponse;
910
import net.minecraft.client.util.Session;
1011
import org.apache.commons.codec.digest.DigestUtils;
@@ -19,13 +20,16 @@
1920
import java.util.UUID;
2021

2122
public class AuthHandler implements Loadable {
22-
protected String commonSecret;
23+
public static AuthHandler instance;
24+
25+
private String commonSecret;
2326
protected boolean valid = false;
2427

2528
@Override
2629
public void load() {
2730
try {
2831
regen();
32+
instance = this;
2933
}
3034
catch (IOException e) {
3135
DFScript.LOGGER.error(e);
@@ -81,16 +85,9 @@ private String getCommonSecret() throws IOException {
8185
os.write(input, 0, input.length);
8286
}
8387

84-
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
85-
StringBuilder response = new StringBuilder();
86-
String responseLine;
88+
ServerCodeResponse servercodeResponse = DFScript.GSON.fromJson(ReadBody.getResponse(connection.getInputStream()), ServerCodeResponse.class);
89+
return DigestUtils.sha256Hex(clientCode + servercodeResponse.getServerCode());
8790

88-
while ((responseLine = br.readLine()) != null) {
89-
response.append(responseLine.trim());
90-
}
91-
ServerCodeResponse servercodeResponse = DFScript.GSON.fromJson(response.toString(), ServerCodeResponse.class);
92-
return DigestUtils.sha256Hex(clientCode + servercodeResponse.getServerCode());
93-
}
9491
}
9592

9693
/**
@@ -123,4 +120,8 @@ private void validateCode() throws AuthenticationException, IOException, Forbidd
123120
}
124121
// It worked.
125122
}
123+
124+
public void addAuthorization(HttpURLConnection connection) {
125+
connection.addRequestProperty("Authorization",commonSecret);
126+
}
126127
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package io.github.techstreet.dfscript.network;
2+
3+
public class RemoteScriptManager {
4+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.github.techstreet.dfscript.network.remote;
2+
3+
import io.github.techstreet.dfscript.DFScript;
4+
import io.github.techstreet.dfscript.network.AuthHandler;
5+
import io.github.techstreet.dfscript.network.request.ForbiddenException;
6+
7+
import java.net.MalformedURLException;
8+
import java.net.URL;
9+
10+
public class RemoteScript {
11+
private String ID;
12+
private String name;
13+
private String owner;
14+
private String description = "N/A";
15+
private int version = 0;
16+
private boolean verified;
17+
18+
private URL url;
19+
20+
private RemoteScript(String ID, boolean verified) {
21+
this.ID = ID;
22+
this.verified = verified;
23+
try {this.url = new URL(DFScript.BACKEND + "/script/" + ID);}
24+
catch (MalformedURLException e) {DFScript.LOGGER.error("Backend is invalid:" + DFScript.BACKEND);}
25+
}
26+
27+
public boolean isVerified() {
28+
return this.verified;
29+
}
30+
31+
public void setVerified(boolean verified) throws ForbiddenException {
32+
try {URL url = new URL(this.url.toString() + "/verify");}
33+
catch (MalformedURLException e) {DFScript.LOGGER.error("Backend is invalid:" + DFScript.BACKEND); return;}
34+
// AuthHandler.instance.addAuthorization();
35+
this.verified = verified;
36+
}
37+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.github.techstreet.dfscript.network.remote;
2+
3+
public class RemoteUser {
4+
5+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.github.techstreet.dfscript.network.request;
2+
3+
import io.github.techstreet.dfscript.DFScript;
4+
import org.apache.commons.codec.digest.DigestUtils;
5+
6+
import java.io.BufferedReader;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.io.InputStreamReader;
10+
import java.nio.charset.StandardCharsets;
11+
12+
public class ReadBody {
13+
public static String getResponse(InputStream inputStream) throws IOException {
14+
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
15+
StringBuilder response = new StringBuilder();
16+
String responseLine;
17+
18+
while ((responseLine = br.readLine()) != null) {
19+
response.append(responseLine.trim());
20+
}
21+
22+
return response.toString();
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)