-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfig.java
More file actions
159 lines (129 loc) · 4.75 KB
/
Config.java
File metadata and controls
159 lines (129 loc) · 4.75 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.arloor.forwardproxy.vo;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class Config {
private static final String TRUE = "true";
public static boolean ask4Authcate = false;
private static final String POUND_SIGN = "\u00A3"; // £
private Ssl ssl;
private Http http;
public Ssl ssl() {
return ssl;
}
public Http http() {
return http;
}
public static Config parse(Properties properties) {
Config config = new Config();
ask4Authcate = TRUE.equals(properties.getProperty("ask4Authcate"));
String httpsEnable = properties.getProperty("https.enable");
if (TRUE.equals(httpsEnable)) {
String httpsPortStr = properties.getProperty("https.port");
Integer port = Integer.parseInt(httpsPortStr);
String auth = properties.getProperty("https.auth");
Map<String, String> users = new HashMap<>();
if (auth != null && auth.length() != 0) {
for (String user : auth.split(",")) {
users.computeIfAbsent(genBasicAuth(user), (cell) -> user);
users.computeIfAbsent(genBasicAuthWithOut£(user), (cell) -> user);
}
}
String fullchain = properties.getProperty("https.fullchain.pem");
String privkey = properties.getProperty("https.privkey.pem");
Ssl ssl = new Ssl(port, users, fullchain, privkey);
config.ssl = ssl;
}
String httpEnable = properties.getProperty("http.enable");
if (TRUE.equals(httpEnable)) {
String httpPortStr = properties.getProperty("http.port");
Integer port = Integer.parseInt(httpPortStr);
String auth = properties.getProperty("http.auth");
Map<String, String> users = new HashMap<>();
if (auth != null && auth.length() != 0) {
for (String user : auth.split(",")) {
users.computeIfAbsent(genBasicAuth(user), (cell) -> user);
users.computeIfAbsent(genBasicAuthWithOut£(user), (cell) -> user);
}
}
Http http = new Http(port, users);
config.http = http;
}
return config;
}
/**
* https://datatracker.ietf.org/doc/html/rfc7617
* The user's name is "test", and the password is the string "123"
* followed by the Unicode character U+00A3 (POUND SIGN). Using the
* character encoding scheme UTF-8, the user-pass becomes:
* <p>
* 't' 'e' 's' 't' ':' '1' '2' '3' pound
* 74 65 73 74 3A 31 32 33 C2 A3
* <p>
* Encoding this octet sequence in Base64 ([RFC4648], Section 4) yields:
* <p>
* dGVzdDoxMjPCow==
*
* @param user
* @return
*/
private static String genBasicAuth(String user) {
user += POUND_SIGN;
return "Basic " + Base64.getEncoder().encodeToString(user.getBytes(StandardCharsets.UTF_8));
}
private static String genBasicAuthWithOut£(String user) {
return "Basic " + Base64.getEncoder().encodeToString(user.getBytes(StandardCharsets.UTF_8));
}
public static class Http {
private Integer port;
private Map<String, String> auth; // base64 - raw
public Http(Integer port, Map<String, String> auth) {
this.port = port;
this.auth = auth;
}
public Integer getPort() {
return port;
}
public String getAuth(String base64Auth) {
return auth.get(base64Auth);
}
public Map<String, String> getAuthMap() {
return auth;
}
public boolean needAuth() {
return auth != null && auth.size() != 0;
}
}
public static class Ssl {
private Integer port;
private Map<String, String> auth; // base64 - raw
private String fullchain;
private String privkey;
public Ssl(Integer port, Map<String, String> auth, String fullchain, String privkey) {
this.port = port;
this.auth = auth;
this.fullchain = fullchain;
this.privkey = privkey;
}
public Integer getPort() {
return port;
}
public String getAuth(String base64Auth) {
return auth.get(base64Auth);
}
public Map<String, String> getAuthMap() {
return auth;
}
public String getFullchain() {
return fullchain;
}
public String getPrivkey() {
return privkey;
}
public boolean needAuth() {
return auth != null && auth.size() != 0;
}
}
}