Skip to content

Commit 9293c40

Browse files
committed
app_key is now generated automatically upon install
1 parent dfe9fad commit 9293c40

File tree

4 files changed

+80
-22
lines changed

4 files changed

+80
-22
lines changed

composer.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
{
22
"name": "athlon1600/php-proxy-app",
33
"type": "project",
4-
"version": "1.0.0",
5-
"keywords": ["php proxy", "php proxy application", "php proxy web", "proxy script", "php web proxy", "web proxy"],
4+
"version": "2.0.0",
5+
"license": "MIT",
6+
"description": "Web proxy application project powered by PHP-Proxy library",
7+
"keywords": ["php proxy application", "php proxy web", "proxy script", "php web proxy", "web proxy"],
68
"homepage": "https://www.php-proxy.com/",
79
"require": {
810
"athlon1600/php-proxy": "@dev"
9-
}
11+
},
12+
"post-create-project-cmd": [
13+
"php -f setup.txt"
14+
]
1015
}

config.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@
33
// all possible options will be stored
44
$config = array();
55

6-
// make it as long as possible for extra security... secret key is being used when encrypting urls
7-
$config['secret_key'] = '';
6+
// a unique key that identifies application - DO NOT LEAVE THIS EMPTY!
7+
$config['app_key'] = '';
8+
9+
// a secret key to be used during encryption
10+
$config['encryption_key'] = '';
11+
12+
/*
13+
how unique is each URL that is generated by this proxy app?
14+
0 - no encryption, people can hotlink to your proxy
15+
1 - unique to the IP address that generated it. A person that generated that URL, can bookmark it and visit it and any point
16+
2 - unique to that session and IP address - URL no longer valid anywhere when browser session ends
17+
*/
18+
19+
$config['url_mode'] = 1;
820

921
// plugins to load - plugins will be loaded in this exact order as in array
1022
$config['plugins'] = array(

index.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
22

3-
require("vendor/autoload.php");
4-
53
define('PROXY_START', microtime(true));
6-
define('SCRIPT_BASE', (!empty($_SERVER['HTTPS']) ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
7-
define('SCRIPT_DIR', pathinfo(SCRIPT_BASE, PATHINFO_DIRNAME).'/');
4+
5+
require("vendor/autoload.php");
86

97
use Symfony\Component\HttpFoundation\Request;
108
use Symfony\Component\HttpFoundation\Response;
@@ -15,17 +13,31 @@
1513
use Proxy\Config;
1614
use Proxy\Proxy;
1715

16+
// start the session
17+
session_start();
18+
1819
// load config...
1920
Config::load('./config.php');
2021

22+
if(!Config::get('app_key')){
23+
die("app_key inside config.php cannot be empty!");
24+
}
25+
26+
// how are our URLs be generated from this point? this must be set here so the proxify_url function below can make use of it
27+
if(Config::get('url_mode') == 1){
28+
Config::set('encryption_key', md5(Config::get('app_key').$_SERVER['REMOTE_ADDR']));
29+
} else if(Config::get('url_mode') == 2){
30+
Config::set('encryption_key', md5(Config::get('app_key').session_id()));
31+
}
32+
2133
// form submit in progress...
2234
if(isset($_POST['url'])){
2335

2436
$url = $_POST['url'];
2537
$url = add_http($url);
2638

2739
header("HTTP/1.1 302 Found");
28-
header('Location: '.SCRIPT_BASE.'?q='.encrypt_url($url));
40+
header('Location: '.proxify_url($url));
2941
exit;
3042

3143
} else if(!isset($_GET['q'])){
@@ -38,21 +50,17 @@
3850
header("Location: ".Config::get('index_redirect'));
3951

4052
} else {
41-
echo render_template("./templates/main.php", array('script_base' => SCRIPT_BASE, 'version' => Proxy::VERSION));
53+
echo render_template("./templates/main.php", array('version' => Proxy::VERSION));
4254
}
4355

4456
exit;
4557
}
4658

47-
48-
// get real URL
49-
$url = decrypt_url($_GET['q']);
50-
define('URL', $url);
51-
59+
// decode q parameter to get the real URL
60+
$url = base64_decrypt($_GET['q']);
5261

5362
$proxy = new Proxy();
5463

55-
5664
// load plugins
5765
foreach(Config::get('plugins', array()) as $plugin){
5866

@@ -63,12 +71,13 @@
6371
// use user plugin from /plugins/
6472
require_once('./plugins/'.$plugin_class.'.php');
6573

66-
} else {
74+
} else if(class_exists('\\Proxy\\Plugin\\'.$plugin_class)){
6775

68-
// use native plugin from php-proxy - it was already loaded into namespace automatically through composer
76+
// does the native plugin from php-proxy package with such name exist?
6977
$plugin_class = '\\Proxy\\Plugin\\'.$plugin_class;
7078
}
7179

80+
// otherwise plugin_class better be loaded already and match namespace exactly \\Vendor\\Plugin\\SuperPlugin
7281
$proxy->getEventDispatcher()->addSubscriber(new $plugin_class());
7382
}
7483

@@ -86,8 +95,7 @@
8695
}
8796

8897
$url_form = render_template("./templates/url_form.php", array(
89-
'url' => $url,
90-
'script_base' => SCRIPT_BASE
98+
'url' => $url
9199
));
92100

93101
$output = $response->getContent();
@@ -131,7 +139,6 @@
131139

132140
echo render_template("./templates/main.php", array(
133141
'url' => $url,
134-
'script_base' => SCRIPT_BASE,
135142
'error_msg' => $ex->getMessage(),
136143
'version' => Proxy::VERSION
137144
));

setup.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
function generate_random_key(){
4+
5+
if(function_exists('openssl_random_pseudo_bytes')){
6+
$random = openssl_random_pseudo_bytes(100);
7+
} else {
8+
$random = rand().microtime().rand();
9+
}
10+
11+
return md5($random);
12+
}
13+
14+
$path_config = './config.php';
15+
16+
// config.php won't be writable if ran from within web server
17+
if(!is_writable($path_config)){
18+
exit;
19+
}
20+
21+
$key = generate_random_key();
22+
23+
// open config.php
24+
$config = file_get_contents($path_config);
25+
26+
// replace blank app_key with new generated key
27+
$config = str_replace('$config[\'app_key\'] = \'\';', '$config[\'app_key\'] = \''.$key.'\';', $config);
28+
29+
// write to config.php
30+
file_put_contents($path_config, $config);
31+
32+
echo "New Key: {$key}\r\n";
33+
34+
?>

0 commit comments

Comments
 (0)