2929public class GodzillaManager implements Closeable {
3030 private final OkHttpClient client ;
3131 private static final List <String > CLASS_NAMES ;
32- private String J_SESSION_ID = "" ;
32+ private String cookie = "" ;
3333 private String entrypoint ;
3434 private String key ;
3535 private String pass ;
@@ -96,18 +96,18 @@ public GodzillaManager() {
9696 }
9797
9898 private Response post (byte [] bytes ) throws IOException {
99- byte [] aes = aes (bytes , true );
99+ byte [] aes = aes (this . key , bytes , true );
100100 assert aes != null ;
101101 String base64String = Base64 .encodeBase64String (aes );
102102 RequestBody requestBody = new FormBody .Builder ()
103- .add (" pass" , base64String )
103+ .add (this . pass , base64String )
104104 .build ();
105105 Request .Builder builder = new Request .Builder ()
106106 .url (this .entrypoint )
107107 .post (requestBody )
108108 .headers (Headers .of (this .headers ));
109- if (StringUtils .isNotBlank (J_SESSION_ID )) {
110- builder .header ("Cookie" , J_SESSION_ID );
109+ if (StringUtils .isNotBlank (cookie )) {
110+ builder .header ("Cookie" , cookie );
111111 }
112112 return client .newCall (builder .build ()).execute ();
113113 }
@@ -128,7 +128,7 @@ public boolean start() {
128128 try (Response response = post (bytes )) {
129129 String setCookie = response .header ("Set-Cookie" );
130130 if (setCookie != null && setCookie .contains ("JSESSIONID=" )) {
131- J_SESSION_ID = setCookie .substring (setCookie .indexOf ("JSESSIONID=" ), setCookie .indexOf (";" ));
131+ cookie = setCookie .substring (setCookie .indexOf ("JSESSIONID=" ), setCookie .indexOf (";" ));
132132 }
133133 return response .code () == 200 ;
134134 } catch (IOException e ) {
@@ -148,7 +148,6 @@ public boolean test() {
148148 }
149149 return false ;
150150 } catch (IOException e ) {
151- e .printStackTrace ();
152151 return false ;
153152 }
154153 }
@@ -172,39 +171,48 @@ public void close() throws IOException {
172171 * @param encoding 是否为加密,true 为加密,false 解密
173172 * @return 返回加解密后的字节数组
174173 */
175- public byte [] aes (byte [] bytes , boolean encoding ) {
176- System .out .println (key );
174+ public static byte [] aes (String key , byte [] bytes , boolean encoding ) {
177175 try {
178176 Cipher c = Cipher .getInstance ("AES" );
179- c .init (encoding ? 1 : 2 , new SecretKeySpec (this . key .getBytes (), "AES" ));
177+ c .init (encoding ? 1 : 2 , new SecretKeySpec (key .getBytes (), "AES" ));
180178 return c .doFinal (bytes );
181179 } catch (Exception e ) {
182- e .printStackTrace ();
183- return null ;
180+ return new byte [0 ];
184181 }
185182 }
186183
187184 private boolean isValidResponse (String response ) {
188185 if (StringUtils .isEmpty (response )) {
189186 return false ;
190187 }
191- return response .startsWith (md5 .substring (0 , 16 )) && response .endsWith (md5 .substring (16 ));
188+ return response .length () > 32 && response . startsWith (md5 .substring (0 , 16 )) && response .endsWith (md5 .substring (16 ));
192189 }
193190
194191 public String getResultFromRes (String responseBody ) throws IOException {
192+ if (!isValidResponse (responseBody )) {
193+ return responseBody ;
194+ }
195195 String result = responseBody .substring (16 );
196196 result = result .substring (0 , result .length () - 16 );
197197 byte [] bytes = Base64 .decodeBase64 (result );
198- byte [] x = aes (bytes , false );
198+ byte [] x = aes (this . key , bytes , false );
199199 GZIPInputStream gzipInputStream = new GZIPInputStream (new ByteArrayInputStream (x ));
200200 return IOUtils .toString (gzipInputStream , StandardCharsets .UTF_8 );
201201 }
202202
203- Map <String , String > restorePayload (String payload ) throws IOException {
204- String p = URLDecoder .decode (payload , "UTF-8" );
203+ public static Map <String , String > restorePayload (String key , String payload ) {
204+ String p = payload ;
205+ try {
206+ String urlDecoded = URLDecoder .decode (payload , "UTF-8" );
207+ if (StringUtils .isNoneBlank (urlDecoded )) {
208+ p = urlDecoded ;
209+ }
210+ } catch (UnsupportedEncodingException ignored ) {
211+
212+ }
205213 Map <String , String > map = new HashMap <>();
206214 byte [] bytes = Base64 .decodeBase64 (p );
207- byte [] x = aes (bytes , false );
215+ byte [] x = aes (key , bytes , false );
208216 ByteArrayInputStream tStream = new ByteArrayInputStream (x );
209217 ByteArrayOutputStream tp = new ByteArrayOutputStream ();
210218 byte [] lenB = new byte [4 ];
@@ -215,16 +223,16 @@ Map<String, String> restorePayload(String payload) throws IOException {
215223 byte t = (byte ) inputStream .read ();
216224 if (t != -1 ) {
217225 if (t == 2 ) {
218- String key = tp .toString ();
219- int read1 = inputStream .read (lenB );
226+ String dataKey = tp .toString ();
227+ inputStream .read (lenB );
220228 int len = bytesToInt (lenB );
221229 byte [] data = new byte [len ];
222230 int readOneLen = 0 ;
223231 do {
224232 read = readOneLen + inputStream .read (data , readOneLen , data .length - readOneLen );
225233 readOneLen = read ;
226234 } while (read < data .length );
227- map .put (key , new String (data ));
235+ map .put (dataKey , new String (data ));
228236 tp .reset ();
229237 } else {
230238 tp .write (t );
@@ -249,7 +257,7 @@ public static int bytesToInt(byte[] bytes) {
249257 private byte [] generateMethodCallBytes (String methodName ) {
250258 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream ();
251259 try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream (byteArrayOutputStream );) {
252- byte [] value = "close" .getBytes ();
260+ byte [] value = methodName .getBytes ();
253261 gzipOutputStream .write ("methodName" .getBytes ());
254262 gzipOutputStream .write (2 );
255263 gzipOutputStream .write (intToBytes (value .length ));
0 commit comments