Advertisement
Guest User

Untitled

a guest
May 14th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. package sock;
  2. import java.io.*;
  3. import java.net.*;
  4. import java.util.*;
  5. import java.sql.*;
  6. public class Serv extends Thread
  7. { static StringBuffer st;
  8.  
  9. static Socket s,s1;
  10. static ServerSocket ss;
  11. static DataInputStream ins,ins1;
  12. static DataOutputStream dos,dos1;
  13. static byte[] request = new byte[1024];
  14. static byte[] reply = new byte[4096];
  15. public static void main(String args[]) throws Exception
  16. {
  17. ss=new ServerSocket(8080);
  18. s=ss.accept();
  19. System.out.println("1");
  20. ins=new DataInputStream(s.getInputStream());
  21. dos=new DataOutputStream(s.getOutputStream());
  22. java.lang.String req;
  23. Integer flag1=0;
  24. Integer flag=0;
  25. while(true)
  26. {
  27. req=ins.readLine();
  28. flag1++;
  29.  
  30. if(flag1==1)
  31. {
  32. StringTokenizer st=new StringTokenizer(req," ");
  33. st.nextToken();
  34. String filename=st.nextToken();
  35. Integer i1=filename.indexOf("/",7);
  36.  
  37. String finalhost=filename.substring(7,i1);
  38. try
  39. {
  40. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  41. Connection con=DriverManager.getConnection("jdbc:odbc:locksites");
  42. Statement stat=con.createStatement();
  43. ResultSet rs=stat.executeQuery("select * from sites where name="+"'"+finalhost+"'");
  44. if(rs.next())
  45. { flag=1;
  46. }
  47. else
  48. { flag=0;
  49. }
  50. }
  51. catch(Exception e)
  52. {System.out.println(e);}
  53.  
  54.  
  55. if(flag==0)
  56. { System.out.println("User has right to access "+finalhost);
  57. s1=new Socket(finalhost,80);
  58. ins1=new DataInputStream(s1.getInputStream());
  59. dos1=new DataOutputStream(s1.getOutputStream());
  60. (new Serv()).start();
  61.  
  62. }
  63. else if(flag==1)
  64. { System.out.println("Restricted access to "+finalhost);
  65. s.close();
  66. }
  67.  
  68.  
  69. }
  70. // req=(char)ins.read();
  71. System.out.println(req);
  72. dos1.writeBytes(req);
  73. dos1.writeBytes("\r\n");
  74.  
  75. }
  76. //System.out.println("finished request");
  77. /* StringTokenizer st=new StringTokenizer(req," ");
  78. st.nextToken();
  79. String filename=st.nextToken();
  80. Integer i1=filename.indexOf("/",7);
  81.  
  82. String finalhost=filename.substring(7,i1);
  83.  
  84. InetAddress address = null;
  85. try {
  86. address = InetAddress.getByName(finalhost);
  87. } catch (UnknownHostException e) {
  88. System.out.println(e);
  89. }
  90. System.out.println(address.getHostName() + "="
  91. + address.getHostAddress());
  92. try
  93. {
  94. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  95. Connection con=DriverManager.getConnection("jdbc:odbclocksites");
  96. Statement stat=con.createStatement();
  97. ResultSet rs=stat.executeQuery("select * from sites where name="+"'"+finalhost+"'");
  98. if(rs.next())
  99. { flag=1;
  100. }
  101. else
  102. { flag=0;
  103. }
  104. }
  105. catch(Exception e)
  106. {System.out.println(e);}
  107.  
  108.  
  109. if(flag==0)
  110. { System.out.println("User has right to access "+finalhost);
  111. }
  112. else if(flag==1)
  113. { System.out.println("Restricted access to "+finalhost);
  114. }
  115. s.close(); */
  116.  
  117. }
  118.  
  119. public void run()
  120. { String res;
  121. System.out.println("response");
  122. int bytesread;
  123. try
  124. {
  125. while((bytesread=ins1.read(reply))!=-1)
  126. {
  127.  
  128. //System.out.println("waiting for response");
  129.  
  130. //System.out.println("got response");
  131. System.out.println(reply.toString());
  132.  
  133. dos.write(reply,0,bytesread);
  134. dos.flush();
  135.  
  136. }
  137.  
  138. }
  139.  
  140. catch(Exception e)
  141. { System.out.println(e);
  142. }
  143.  
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement