fork(1) download
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedReader;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintStream;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import java.security.InvalidKeyException;
  10. import java.security.Key;
  11. import java.security.NoSuchAlgorithmException;
  12. import java.util.zip.GZIPInputStream;
  13.  
  14. import javax.crypto.Mac;
  15. import javax.crypto.spec.SecretKeySpec;
  16.  
  17. class API2ch {
  18. private static final String HMKey = "DgQ3aNpoluV1cl3GFJAqitBg5xKiXZ";
  19. private static final String AppKey = "xxfvFQcOzpTBvwuwPMwwzLZxiCSaGb";
  20.  
  21. private String sid;
  22.  
  23. String CT = "1234567890";
  24. String message = AppKey + CT;
  25. String HB = dumpToHex(getHash(message));
  26. URL url = new URL("https://a...content-available-to-author-only...h.net/v1/auth/");
  27. String param = "ID=&PW=&KY=" + AppKey + "&CT=" + CT + "&HB=" + HB;
  28.  
  29. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  30. PrintStream out = null;
  31. BufferedReader buff = null;
  32. try {
  33. con.setDoOutput(true);
  34. con.setRequestMethod("POST");
  35. con.setRequestProperty("User-Agent", "");
  36. con.setRequestProperty("X-2ch-UA", "JaneStyle/3.80");
  37. out = new PrintStream(con.getOutputStream());
  38. out.print(param);
  39. out.flush();
  40.  
  41. con.getInputStream()));
  42. String line;
  43. while ((line = buff.readLine()) != null) {
  44. System.out.println(line);
  45. if (line.startsWith("SESSION-ID")) {
  46. return line.split(":")[1];
  47. }
  48. }
  49. return null;
  50. } finally {
  51. try {
  52. out.close();
  53. } catch (Exception ignore) {
  54. }
  55. try {
  56. buff.close();
  57. } catch (Exception ignore) {
  58. }
  59. }
  60. }
  61.  
  62. private static byte[] getHash(String message) throws InvalidKeyException,
  63. Key key = new SecretKeySpec(HMKey.getBytes(), "HmacSHA256");
  64. Mac mac = Mac.getInstance(key.getAlgorithm());
  65. mac.init(key);
  66. return mac.doFinal(message.getBytes());
  67. }
  68.  
  69. private static String dumpToHex(byte[] data) {
  70. for (byte b : data) {
  71. String s = Integer.toHexString(0xff & b);
  72. if (s.length() == 1) {
  73. sb.append("0");
  74. }
  75. sb.append(s);
  76. }
  77. return sb.toString();
  78. }
  79.  
  80. public String getSid() {
  81. if (null == sid) {
  82. try {
  83. sid = authenticate();
  84. } catch (Exception e) {
  85. throw new RuntimeException(e);
  86. }
  87. }
  88. return sid;
  89. }
  90.  
  91. public byte[] getDATData(String serverName, String boardName,
  92. String threadID, long startPos) throws InvalidKeyException,
  93. String URI = "/v1/" + serverName + "/" + boardName + "/" + threadID;
  94. String message = URI + getSid() + AppKey;
  95. String hobo = dumpToHex(getHash(message));
  96. String param = "sid=" + getSid() + "&hobo=" + hobo + "&appkey="
  97. + AppKey;
  98. URL url = new URL("https://a...content-available-to-author-only...h.net" + URI);
  99. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  100. PrintStream out = null;
  101. GZIPInputStream gzIn = null;
  102. BufferedInputStream bis = null;
  103. try {
  104. con.setDoOutput(true);
  105. con.setRequestMethod("POST");
  106. con.setRequestProperty("User-Agent",
  107. "Mozilla/3.0 (compatible; JaneStyle/3.80..)");
  108. con.setRequestProperty("Accept-Encoding", "gzip");
  109. con.setRequestProperty("Connection", "close");
  110. con.setRequestProperty("Content-Type",
  111. "application/x-www-form-urlencoded");
  112. con.setRequestProperty("Host", "api.2ch.net");
  113. if (startPos > 0) {
  114. con.setRequestProperty("Range", "bytes=" + startPos + "-");
  115. }
  116. out = new PrintStream(con.getOutputStream());
  117. out.print(param);
  118. out.flush();
  119. bis = new BufferedInputStream(con.getInputStream());
  120. if(startPos > 0) {
  121. byte[] buff = new byte[1024];
  122. int i;
  123. while ((i = bis.read(buff)) > 0) {
  124. baos.write(buff, 0, i);
  125. }
  126. return baos.toByteArray();
  127. } else {
  128. gzIn = new GZIPInputStream(bis);
  129. byte[] buff = new byte[1024];
  130. int i;
  131. while ((i = gzIn.read(buff)) > 0) {
  132. baos.write(buff, 0, i);
  133. }
  134. return baos.toByteArray();
  135. }
  136. } finally {
  137. try {
  138. out.close();
  139. } catch (Exception ignore) {
  140. }
  141. try {
  142. gzIn.close();
  143. } catch (Exception ignore) {
  144. }
  145. try {
  146. bis.close();
  147. } catch (Exception ignore) {
  148. }
  149. }
  150. }
  151.  
  152. public static void main(String[] args) throws Exception {
  153. API2ch a2ch = new API2ch();
  154. a2ch.getSid();
  155. System.out.println(a2ch.getSid());
  156.  
  157. byte[] result = a2ch.getDATData("anago", "software", "1424315959", 0);
  158. System.out.println(new String(result, "MS932"));
  159. }
  160. }
  161.  
Runtime error #stdin #stdout #stderr 0.57s 321920KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.RuntimeException: java.net.UnknownHostException: api.2ch.net
	at API2ch.getSid(Main.java:90)
	at API2ch.main(Main.java:162)
Caused by: java.net.UnknownHostException: api.2ch.net
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
	at java.net.Socket.connect(Socket.java:589)
	at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:649)
	at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
	at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
	at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
	at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:275)
	at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371)
	at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1103)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:997)
	at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
	at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1281)
	at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1256)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
	at API2ch.authenticate(Main.java:39)
	at API2ch.getSid(Main.java:88)
	... 1 more