-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.java
More file actions
99 lines (86 loc) · 4.05 KB
/
App.java
File metadata and controls
99 lines (86 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main.java.ElectermSync;
import static spark.Spark.*;
import io.jsonwebtoken.*;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.util.Map;
import java.io.File;
import java.util.Arrays;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class App {
public static void main(String[] args) {
// Configure logging to reduce Jetty and Spark noise
System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog");
System.setProperty("org.eclipse.jetty.LEVEL", "WARN");
System.setProperty("org.eclipse.jetty.util.log.StdErrLog.LEVEL", "WARN");
System.setProperty("spark.embeddedserver.jetty.LEVEL", "WARN");
Gson gson = new Gson();
Config dotenv = new Config();
String secretOri = dotenv.getValue("JWT_SECRET");
byte[] bytesToEncode = secretOri.getBytes(StandardCharsets.UTF_8);
// Encode the bytes using Base64
String secret = Base64.getEncoder().encodeToString(bytesToEncode);
String ids = dotenv.getValue("JWT_USERS");
String[] idArrStrings = ids.split(",");
Jwts.parserBuilder().setSigningKey(secret).build();
port(Integer.parseInt((dotenv.getValue("PORT"))));
ipAddress(dotenv.getValue("HOST"));
before("/api/sync", (request, response) -> {
String authHeader = request.headers("Authorization");
try {
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
throw new JwtException("Missing or invalid token");
} else {
String token = authHeader.substring(7);
Jws<Claims> claimsJws = Jwts.parserBuilder().setSigningKey(secret).build().parseClaimsJws(token);
String id = claimsJws.getBody().get("id").toString();
boolean found = Arrays.stream(idArrStrings).anyMatch(element -> element.equals(id));
if (!found) {
throw new JwtException("Unauthorized access");
}
request.attribute("jwtId", id);
}
} catch (JwtException ex) {
halt(401, "Unauthorized: " + ex.getMessage());
}
});
get("/api/sync", (request, response) -> {
String jwtId = request.attribute("jwtId");
ReadResult r = DataStore.read(jwtId, dotenv);
response.status(r.statusCode);
return r.fileData;
});
post("/api/sync", (request, response) -> {
return "test ok";
});
put("/api/sync", (request, response) -> {
String requestBody = request.body();
String jwtId = request.attribute("jwtId");
response.type("application/json");
WriteResult r = DataStore.write(requestBody, jwtId, dotenv);
response.status(r.statusCode);
return r.message;
});
after((request, response) -> {
response.type("application/json");
response.header("Content-Encoding", "gzip");
});
// Print user-friendly startup information
String host = dotenv.getValue("HOST");
String port = dotenv.getValue("PORT");
String serverUrl = "http://" + host + ":" + port;
System.out.println("===========================================");
System.out.println("Electerm Sync Server started successfully!");
System.out.println("Server running at " + serverUrl);
System.out.println("API endpoint: " + serverUrl + "/api/sync");
System.out.println();
System.out.println("To use this server in Electerm:");
System.out.println("1. Go to Electerm sync settings");
System.out.println("2. Set custom sync server with:");
System.out.println(" - Server URL: " + serverUrl);
System.out.println(" - JWT_SECRET: get it from .env");
System.out.println(" - JWT_USER_NAME: get it from .env");
System.out.println("===========================================");
}
}