Skip to content

Instantly share code, notes, and snippets.

/GUI

Created December 23, 2014 18:35
Show Gist options
  • Select an option

  • Save anonymous/925c90e6152bf03ca719 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/925c90e6152bf03ca719 to your computer and use it in GitHub Desktop.
samin_proxy
package proxyserver;
import java.util.Calendar;
import java.util.HashMap;
/**
*
* @author Samin_Labaf
*/
public class Cache {
private static final long EXPIRATION_TIME = 300000;
private static Cache instance = null;
public Cache() {
cache = new HashMap<>(128 * 128 * 32);
expirations = new HashMap<>(128);
}
public static Cache getInstance() {
if (instance == null) {
instance = new Cache();
}
return instance;
}
public boolean isCached(String url) {
long currentTime = Calendar.getInstance().getTimeInMillis();
if (cache.containsKey(url)) {
if (currentTime - expirations.get(url) < EXPIRATION_TIME) {
return true;
}
}
return false;
}
public byte[] getCached(String url) {
System.out.println("[Cache] URL \"" + url + "\" sent from cache.");
return cache.get(url);
}
public void save(String url, byte[] data) {
long currentTime = Calendar.getInstance().getTimeInMillis();
cache.put(url, data);
expirations.put(url, currentTime);
System.out.println("[Cache] URL \"" + url + "\" saved.");
}
private final HashMap<String, byte[]> cache;
private final HashMap<String, Long> expirations;
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package proxyserver;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
/**
*
* @author Samin_Labaf
*/
public class GUI extends JFrame{
ProxyServer ps = new ProxyServer();
JButton connecting ;
JTextField port ;
JList list ;
int input ;
private LayoutManager FlowLayout;
private final Image MyIcon ;
String [] BlockingList = new String [25] ;
public GUI()
{
super("Samin Proxy Server");
setSize(600, 400);
setResizable(true);
MyIcon = new ImageIcon("imported.png").getImage();
this.setIconImage(MyIcon);
connecting = new JButton("Connect");
setLayout(FlowLayout);
this.add(connecting);
connecting.setBounds(460, 310 ,85, 35);
ClickListener cl = new ClickListener();
connecting.addActionListener(cl);
list = new JList();
JScrollPane pane = new JScrollPane(list);
this.add(pane);
pane.setBounds(340, 80, 150, 190);
JButton addUrl = new JButton("Add");
this.add(addUrl);
addUrl.setBounds(495, 90, 80, 35);
port = new JTextField();
this.add(port);
port.setEditable(true);
port.setBounds(460, 20, 75, 30);
JLabel portLabel = new JLabel("Port : ");
portLabel.setBounds(425, 20, 40, 30);
this.add(portLabel);
addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing( WindowEvent exit )
{
exit();
}//end method
});//end method addWindowListener
}
public void exit()
{
setVisible(false); //hide the Frame
dispose(); //tell windowing system to free resources
System.exit(0); //exit
}
private class ClickListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == connecting){
input = Integer.parseInt(port.getText());
System.out.println("connecting on port : " + input);
}
}
}
}
package proxyserver;
import java.net.*;
import java.io.*;
public class ProxyServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
GUI MyGUI = new GUI();
MyGUI.setVisible(true);
try {
int port = MyGUI.input ;
serverSocket = new ServerSocket(port);
System.out.println("Started on: " + port);
}
catch (IOException e) {
System.exit(-1);
}
while (true) {
new ProxyThread(serverSocket.accept()).start();
}
//sserverSocket.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment