Skip to content

Commit

Permalink
add create azure vm socks5 proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
azpanel committed Dec 30, 2023
1 parent cc7a885 commit 9b1cc22
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 6 deletions.
36 changes: 36 additions & 0 deletions app/controller/ProxyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace app\controller;

use GuzzleHttp\Client;

class ProxyController extends UserBase
{
public function test()
{
$addr = input('socks5_address/s');
$port = input('socks5_port/d');

try {
if ($addr === '' || $port === '' || $port < 1 || $port > 65535) {
throw new \Exception("代理服务器参数不正确");
}
$client = new Client([
'proxy' => "socks5://{$addr}:{$port}",
'timeout' => 5,
]);
$response = $client->request('GET', 'https://myip.ipip.net');
$statusCode = (int) $response->getStatusCode();

return json([
'status' => $statusCode !== 204 ? true : false,
'msg' => $response->getBody()->getContents(),
]);
} catch (\Exception $e) {
return json([
'status' => false,
'msg' => $e->getMessage(),
]);
}
}
}
28 changes: 24 additions & 4 deletions app/controller/UserAzureServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,21 @@ public function save()
return json(Tools::msg('0', '创建失败', '维护中'));
} */

// 创建http会话
if (input('socks5_switch') === 'true') {
$socks5_addr = input('socks5_address/s');
$socks5_port = input('socks5_port/d');

$client = new Client([
'proxy' => "socks5://{$socks5_addr}:{$socks5_port}",
'timeout' => 5,
]);
} else {
$client = new Client();
}

// 初始化创建任务
$progress = 0;
$client = new Client();
$steps = ($vm_number * 6) + 6;
$task_id = UserTask::create(session('user_id'), '创建虚拟机', $params, $task_uuid);

Expand Down Expand Up @@ -474,7 +486,7 @@ public function save()
$vm_location
);
} catch (\Exception $e) {
$error = $e->getResponse()->getBody()->getContents();
$error = $e->getMessage();
UserTask::end($task_id, true, $error);
return json(Tools::msg('0', '创建失败', $error));
}
Expand All @@ -498,7 +510,11 @@ public function save()
if ((int) session('user_id') === (int) Config::obtain('ali_whitelist')) {
if (Config::obtain('sync_immediately_after_creation')) {
foreach ($names as $vm_name) {
$server = AzureServer::where('name', $vm_name)->order('id', 'desc')->limit(1)->find();
$server = AzureServer::where('user_id', session('user_id'))
->where('name', $vm_name)
->order('id', 'desc')
->limit(1)
->find();
try {
Ali::createOrUpdate($server->name, $server->ip_address);
} catch (\Exception $e) {
Expand All @@ -511,7 +527,11 @@ public function save()
// 将设置的备注应用
$pointer = 0;
foreach ($names as $name) {
$server = AzureServer::where('name', $name)->order('id', 'desc')->limit(1)->find();
$server = AzureServer::where('user_id', session('user_id'))
->where('name', $name)
->order('id', 'desc')
->limit(1)
->find();
$server->user_remark = $remarks[$pointer];
$server->rule = $vm_traffic_rule;
$server->save();
Expand Down
80 changes: 79 additions & 1 deletion app/view/user/azure/server/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

<div class="mdui-typo">
<div class="mdui-panel" mdui-panel>

<div class="mdui-panel-item">
<div class="mdui-panel-item-header">
<div class="mdui-panel-item-title">使用说明</div>
Expand Down Expand Up @@ -185,6 +184,42 @@
</div>
</div>
</div>
<div class="mdui-typo">
<div class="mdui-panel" mdui-panel>
<div class="mdui-panel-item">
<div class="mdui-panel-item-header">
<div class="mdui-panel-item-title">SOCKS5代理</div>
<i class="mdui-panel-item-arrow mdui-icon material-icons">keyboard_arrow_down</i>
</div>
<div class="mdui-panel-item-body">
<div class="mdui-col-sm-12 mdui-col-md-12">
<div class="mdui-switch mdui-m-t-2">
<label>
创建时使用以下代理&nbsp;&nbsp;
<input type="checkbox" id="socks5_switch" />
<i class="mdui-switch-icon"></i>
</label>
</div>
</div>
<div class="mdui-col-sm-12 mdui-col-md-6">
<div class="mdui-textfield">
<label class="mdui-textfield-label">地址</label>
<input class="mdui-textfield-input" type="text" id="socks5_address" required />
</div>
</div>
<div class="mdui-col-sm-12 mdui-col-md-6">
<div class="mdui-textfield">
<label class="mdui-textfield-label">端口</label>
<input class="mdui-textfield-input" type="number" id="socks5_port" min="1"
max="65535" step="1" required />
</div>
</div>
<button id="test_socks5"
class="mdui-btn mdui-btn-raised mdui-ripple mdui-color-indigo mdui-m-p-2">测试</button>
</div>
</div>
</div>
</div>
</div>

<div class="mdui-card mdui-m-t-2">
Expand Down Expand Up @@ -233,6 +268,18 @@
var $ = mdui.$;
var inst = new mdui.Select('#vm_size');

document.addEventListener('DOMContentLoaded', function () {
var cache_socks5_port = localStorage.getItem('socks5_port');
var cache_socks5_address = localStorage.getItem('socks5_address');

if (cache_socks5_port !== null) {
document.getElementById('socks5_port').value = cache_socks5_port;
}
if (cache_socks5_address !== null) {
document.getElementById('socks5_address').value = cache_socks5_address;
}
});

function getProgress() {
$.ajax({
method: 'GET',
Expand All @@ -253,6 +300,34 @@
});
}

$('#test_socks5').on('click', function () {
mdui.snackbar({
message: '正在测试代理可用性中...',
position: 'top',
timeout: '500',
});

localStorage.setItem('socks5_port', $('#socks5_port').val());
localStorage.setItem('socks5_address', $('#socks5_address').val());

$.ajax({
method: 'POST',
url: "/proxy/test",
data: {
socks5_port: $('#socks5_port').val(),
socks5_address: $('#socks5_address').val(),
},
dataType: "json",
success: function (data) {
mdui.snackbar({
message: data.status ? data.msg : "代理不可用,请检查后重试:" + data.msg,
position: 'top',
timeout: data.status ? 1500 : 2500,
});
}
});
});

$('#load_available_models').on('click', function () {
mdui.snackbar({
message: '加载中...',
Expand Down Expand Up @@ -367,6 +442,9 @@
vm_ssh_key: $('#vm_ssh_key').val(),
create_check: $('#create_check').val(),
create_ipv6: $('#create_ipv6').val(),
socks5_port: $('#socks5_port').val(),
socks5_address: $('#socks5_address').val(),
socks5_switch: $("#socks5_switch").prop("checked"),
task_uuid: uuid,
},
dataType: "json",
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"topthink/framework": "^6.0.0",
"topthink/think-orm": "^2.0",
"topthink/think-view": "^1.0",
"guzzlehttp/guzzle": "~6.0",
"guzzlehttp/guzzle": "~7.0",
"phpmailer/phpmailer": "^6.5",
"topthink/think-helper": "^3.1",
"nesbot/carbon": "^2.57",
Expand Down
3 changes: 3 additions & 0 deletions route/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,6 @@
// 其他
Route::get('/share', 'Share/getShare');
Route::get('/user/progress/:uuid', 'UserTask/ajaxQuery')->json();

// 代理测试
Route::post('/proxy/test', 'ProxyController/test');

0 comments on commit 9b1cc22

Please sign in to comment.