· 4 years ago · Dec 20, 2020, 08:14 AM
1<?php
2
3namespace App\Http\Controllers;
4
5use Illuminate\Http\Request;
6use App\User;
7use App\Vpn;
8use App\Ssh;
9use App\Server;
10use App\Trial;
11use App\Log;
12use Auth;
13use Validator;
14use phpseclib\Net\SSH2;
15use App\Pesan;
16use App\Notif;
17use \Carbon\Carbon;
18
19class AccountController extends Controller
20{
21
22 /**
23 * @return account type
24 */
25
26 public $type;
27
28 /**
29 * @return current authenticated user
30 */
31
32 public $user;
33
34 /**
35 * @return server resource.
36 */
37
38 public $server;
39
40 /**
41 * @return ssh / vpn username.
42 */
43
44 public $username;
45
46 /**
47 * @return ssh / vpn password.
48 */
49
50 public $password;
51
52 /**
53 * @return ssh / vpn duration.
54 */
55
56 public $duration;
57
58 /**
59 * @return ssh / vpn duration.
60 */
61
62 public $durationDate;
63
64 /**
65 * @return message,
66 */
67
68 public $message;
69
70 /**
71 * @return User trial account.
72 */
73
74 public $trialCount;
75
76 /**
77 * @return trial percent.
78 */
79
80 public $trialPercent;
81
82 /**
83 * @return curent date time.
84 */
85
86 public $now;
87
88 /**
89 * @return price.
90 */
91
92 public $price;
93
94 /**
95 * @return ssh resources.
96 */
97
98 public $ssh;
99
100 /**
101 * @return vpn resources.
102 */
103
104 public $vpn;
105
106 public function __construct()
107 {
108 $this->middleware(function ($request, $next) {
109 $this->user = Auth::user();
110
111 return $next($request);
112 });
113 // return dd($this->middleware('auth'));
114 // $this->middleware('auth');
115 // $this->user = User::where('email', Auth::user()->email)->first();
116 $this->message = Pesan::where('id', 1)->first();
117 $this->now = Carbon::now();
118 }
119
120 public function indexSSH()
121 {
122 $server = Server::where('limit', 0)->where('type','ssh')->orWhere('type', 'both')->get();
123 return view('global.choose-ssh-server')->with('servers', $server);
124 }
125
126 public function indexVPN()
127 {
128 $server = Server::where('limit', 0)->where('type','vpn')->orWhere('type', 'both')->get();
129 return view('global.choose-vpn-server')->with('servers', $server);
130 }
131
132 public function ssh($ip)
133 {
134 $server = Server::where('ip', $ip)->first();
135 if(!$server)
136 {
137 return abort(404);
138 }
139
140 $trialAccount = Trial::where('reseller_email', Auth::user()->email)->where('create_date', date('d-m-Y'))->get();
141
142 if(Auth::user()->role == 'admin') {
143 $ssh = Ssh::where('status', '!=', 'trial')->get();
144 $vpn = Vpn::where('status', '!=', 'trial')->get();
145
146 }
147 else
148 {
149 $ssh = Ssh::where('reseller_email', Auth::user()->email)->where('status', '!=', 'trial')->get();
150 $vpn = Vpn::where('reseller_email', Auth::user()->email)->where('status', '!=', 'trial')->get();
151
152 }
153
154 return view('global.create-ssh')->with('server', $server)
155 -> with('user', Auth::user())
156 -> with('trial',$trialAccount)
157 -> with('vpn', $vpn)
158 -> with('ssh', $ssh);
159 }
160
161 public function vpn($ip)
162 {
163 $server = Server::where('ip', $ip)->first();
164 if(!$server)
165 {
166 return abort(404);
167 }
168
169 $trialAccount = Trial::where('reseller_email', Auth::user()->email)->where('create_date', date('d-m-Y'))->get();
170 if (Auth::user()->role == 'admin') {
171 $ssh = Ssh::where('status', '!=', 'trial')->get();
172 $vpn = Vpn::where('status', '!=', 'trial')->get();
173
174 } else {
175 $ssh = Ssh::where('reseller_email', Auth::user()->email)->where('status', '!=', 'trial')->get();
176 $vpn = Vpn::where('reseller_email', Auth::user()->email)->where('status', '!=', 'trial')->get();
177
178 }
179
180 return view('global.create-vpn')->with('server', $server)
181 -> with('user', Auth::user())
182 -> with('trial',$trialAccount)
183 -> with('vpn', $vpn)
184 -> with('ssh', $ssh);
185 }
186
187 public function createSSH(Request $request)
188 {
189
190
191 $message = [
192 "captcha.required" => 'The :attribute field is required.',
193 "captcha.captcha" => 'The entering :attribute value does not match server response.'
194 ];
195
196 $validator = Validator::make($request->all(),[
197 'username' => 'required',
198 'password' => 'required',
199 'duration' => 'required'
200 ],$message);
201
202 if($validator->fails())
203 {
204 return response()->json([
205 'response' => 'Validation Error',
206 'data' => [
207 'reason' => 'Some error detected.',
208 'error' => json_decode($validator->errors())
209 ]
210 ],422);
211 }
212
213 if($this->isServerExistsByIP($request->_server))
214 {
215 $this->server = Server::where('ip',$request->_server)->first();
216 }
217 else
218 {
219 return $this->flashMessage('error', [
220 'status' => 'Error',
221 'data' => [
222 'reason' => 'Server you are trying to create account does not exists.'
223 ]
224 ]);
225 }
226
227 if($request->duration == 'trial')
228 {
229 return $this->doTrialTask($request,'ssh');
230 }
231
232 if($this->checkAdmin())
233 {
234 return $this->doPremiumTask($request,'ssh');
235 }
236
237 return $this->doPremiumTask($request,'ssh');
238 }
239
240 private function doCreateSSH()
241 {
242 $server = $this->server;
243
244 $ssh = new SSH2($server->ip);
245 if(!$ssh->login($server->user,decrypt($server->pass)))
246 {
247 return view('global.server-not-connect');
248 }
249
250 if($this->duration == 'trial')
251 {
252 $ssh->exec('useradd ' . $this->username . ' -m -s /bin/false');
253 $ssh->exec('echo ' . $this->username . ':' . $this->password . ' | chpasswd');
254
255 // updating database record.
256
257 // updating trial count.
258 Trial::create([
259 'reseller_email' => $this->user->email,
260 'create_date' => date('d-m-Y'),
261 ]);
262
263 // updating ssh user.
264 Ssh::create([
265 'username' => $this->username,
266 'reseller_email' => $this->user->email,
267 'at_server' => $this->server->ip,
268 'status' => 'trial',
269 'expired_on' => date('d-m-Y'),
270 ]);
271
272 // updating logs table.
273 Log::create([
274 'task_name' => 'Trial SSH Account Successfully created!',
275 'message' => $this->user->name . ' Successfully create trial SSH Account on ' . date('d-m-Y'),
276 'triggerer' => $this->user->email,
277 ]);
278
279 $this->trialCount = Trial::where('reseller_email', $this->user->email)->count();
280 $this->trialPercent = $this->trialCount / 5 * 100;
281
282 return $this->flashMessage('success', [
283 'status' => 'Trial Success',
284 'details' => [
285 'username' => $this->username,
286 'password' => $this->password,
287 'host' => $this->server->ip,
288 ],
289 'message' => ($this->user->role == 'admin') ? $this->message->pesan_trial_sukses_admin : $this->message->pesan_trial_sukses,
290 'trial_account' => $this->trialCount,
291 'percent' => $this->trialPercent,
292 ]);
293 }
294
295 $ssh->exec('useradd -e `date -d "' . $this->duration . ' months" +"%Y-%m-%d"` -m -s /bin/false ' . $this->username);
296 $ssh->exec('echo ' . $this->username . ':' . $this->password . ' | chpasswd');
297
298 // updating database record.
299 Ssh::create([
300 'username' => $this->username,
301 'reseller_email' => $this->user->email,
302 'at_server' => $this->server->ip,
303 'status' => 'active',
304 'expired_on' => $this->now->addMonths($this->duration),
305 ]);
306
307 // updatin servers record.
308 Server::where('ip', $this->server->ip)->increment('user_created');
309
310 // updating logs table.
311 Log::create([
312 'task_name' => 'SSH Account Successfully created!',
313 'message' => $this->user->name . ' Successfully create SSH Account on ' . date('d-m-Y') . ' Active until ' . $this->now->addMonths($this->duration),
314 'triggerer' => $this->user->email,
315 ]);
316
317 // decrements balance & incrementing points.
318 $this->cutBalance($this->price);
319 User::where('id', $this->user->id)->increment('point', $this->server->points * $this->duration);
320 $this->ssh = Ssh::where('reseller_email', $this->user->email);
321 $this->user = User::where('email', $this->user->email)->first();
322
323 return $this->flashMessage('success', [
324 'status' => 'Ok',
325 'details' => [
326 'username' => $this->username,
327 'password' => $this->password,
328 'host' => $this->server->ip,
329 ],
330 'message' => ($this->user->role == 'admin') ? $this->message->pesan_ssh_sukses : $this->message->pesan_ssh_sukses_admin,
331 'curent_ssh_user' => $this->ssh->count(),
332 'curent_user_balance' => $this->user->balance,
333 'curent_user_point' => $this->user->point,
334 ]);
335 }
336
337 private function doPremiumTask($request,$type)
338 {
339 // check if server available to create new user.
340 if($this->isServerAvailableToCreateNewAccount())
341 {
342 // check username available.
343 if($this->isAccountAvailable($request->username,$type,$request->_server))
344 {
345 // check if balance or point meet the account prices.
346 if($request->pay == 'balance')
347 {
348 $price = $this->server->price * $request->duration;
349 if($this->isBalanceMeet($price))
350 {
351 // create account!
352 $this->price = $price;
353 $this->username = $request->username;
354 $this->password = $request->password;
355 $this->duration = $request->duration;
356
357 if($type == 'vpn')
358 {
359 return $this->doCreateVPN();
360 }
361
362 return $this->doCreateSSH();
363 }
364
365 return $this->flashMessage('error', [
366 'status' => 'Error Balance',
367 'data' => [
368 'reason' => 'Your Balance not meet the account price'
369 ],
370 'message' => ($this->user->admin) ? $this->message->pesan_saldo_tidak_cukup_admin : $this->message->pesan_saldo_tidak_cukup,
371 ]);
372 }
373
374 $price = $this->server->price_point * $request->duration;
375 if($this->isPointMeet($price))
376 {
377 // create account!
378 $this->price = $price;
379 $this->username = $request->username;
380 $this->password = $request->password;
381 $this->duration = $request->duration;
382
383 if($type == 'vpn')
384 {
385 return $this->doCreateVPN();
386 }
387
388 return $this->doCreateSSH();
389 }
390
391 return $this->flashMessage('error', [
392 'status' => 'Error Point',
393 'data' => [
394 'reason' => 'Your Point not meet the account price'
395 ]
396 ]);
397 }
398
399 return $this->flashMessage('error', [
400 'status' => 'Error',
401 'data' => [
402 'reason' => 'Username already used!'
403 ]
404 ]);
405 }
406
407 return $this->flashMessage('error', [
408 'status' => 'Error',
409 'data' => [
410 'reason' => 'Server daily limit reached!'
411 ]
412 ]);
413 }
414
415 private function doTrialTask($request,$type)
416 {
417 // set the duration.
418 $this->duration = 'trial';
419
420 if($this->isAllowedTrial())
421 {
422 if($this->isServerAvailableToCreateNewAccount())
423 {
424 if($this->isAccountAvailable($request->username,$type,$request->_server))
425 {
426 // set the username and password.
427 $this->username = $request->username;
428 $this->password = $request->password;
429
430 if($type == 'vpn')
431 {
432 return $this->doCreateVPN();
433 }
434
435 return $this->doCreateSSH();
436 }
437
438 return $this->flashMessage('error', [
439 'status' => 'Error',
440 'data' => [
441 'reason' => 'Username already used!'
442 ]
443 ]);
444 }
445
446