-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathShareCommand.php
More file actions
181 lines (143 loc) · 6 KB
/
ShareCommand.php
File metadata and controls
181 lines (143 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace Expose\Client\Commands;
use Expose\Client\Commands\Concerns\DetectsLocalDevelopmentSites;
use Expose\Client\Commands\Concerns\SharesViteServer;
use Expose\Client\Commands\Concerns\TriggersLogin;
use Expose\Client\Factory;
use chillerlan\QRCode\Common\Version;
use chillerlan\QRCode\Data\QRMatrix;
use chillerlan\QRCode\Output\QROutputInterface;
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
use Illuminate\Support\Str;
use React\EventLoop\LoopInterface;
use Symfony\Component\Console\Output\OutputInterface;
use function Expose\Common\banner;
use function Expose\Common\error;
use function Expose\Common\info;
use function Termwind\render;
use function Termwind\terminal;
class ShareCommand extends ServerAwareCommand
{
use DetectsLocalDevelopmentSites;
use SharesViteServer;
use TriggersLogin;
protected $signature = 'share {host} {--subdomain=} {--auth=} {--basicAuth=} {--magic-auth=} {--dns=} {--domain=} {--prevent-cors} {--no-vite-detection} {--qr} {--qr-code}';
protected $description = 'Share a local url with a remote expose server';
protected ?bool $isWindows = null;
public function handle()
{
$this->loadConfigurationFiles();
terminal()->clear();
banner();
$this->ensureExposeSetup();
info("Expose version v" . config('app.version'), options: OutputInterface::VERBOSITY_VERBOSE);
$auth = $this->option('auth') ?? config('expose.auth_token', '');
info("Using auth token: $auth", options: OutputInterface::VERBOSITY_VERBOSE);
info("Using basic auth: ". $this->option('basicAuth'), options: OutputInterface::VERBOSITY_VERBOSE);
if ($this->getMagicAuthValue() !== null) {
$magicAuthPatterns = $this->getMagicAuthValue() ?: 'any email';
info("Using magic auth: ". $magicAuthPatterns, options: OutputInterface::VERBOSITY_VERBOSE);
}
if (strstr($this->argument('host'), 'host.docker.internal')) {
config(['expose.dns' => true]);
}
if ($this->option('dns') !== null) {
config(['expose.dns' => empty($this->option('dns')) ? true : $this->option('dns')]);
}
$domain = config('expose.default_domain');
if (!is_null($this->option('server'))) {
$domain = null;
}
if (!is_null($this->option('domain'))) {
$domain = $this->option('domain');
}
if (!is_null($this->option('subdomain'))) {
$subdomains = explode(',', $this->option('subdomain'));
info("Trying to use custom subdomain $subdomains[0]", options: OutputInterface::VERBOSITY_VERBOSE);
} else {
$host = Str::beforeLast($this->argument('host'), '.');
$host = str_replace('https://', '', $host);
$host = str_replace('http://', '', $host);
$host = Str::beforeLast($host, ':');
$subdomains = [Str::slug($host)];
info("Trying to use custom subdomain: $subdomains[0]", options: OutputInterface::VERBOSITY_VERBOSE);
}
if ($domain) {
info("Using custom domain $domain", options: OutputInterface::VERBOSITY_VERBOSE);
}
if ($this->option('qr-code') || $this->option('qr')) {
$qrDomain = $domain ?? $this->getServerHost();
$subdomain = $subdomains[0];
$link = "https://$subdomain.$qrDomain";
render($this->renderQrCode($link));
}
if (!$this->option('no-vite-detection')) {
$localSitePath = $this->detectSharedSitePathFromHostname($this->argument('host'));
if (!is_null($localSitePath)) {
$this->checkForVite($localSitePath);
}
}
(new Factory())
->setLoop(app(LoopInterface::class))
->setHost($this->getServerHost())
->setPort($this->getServerPort())
->setAuth($auth)
->setBasicAuth($this->option('basicAuth'))
->setMagicAuth($this->getMagicAuthValue())
->setPreventCORS($this->option('prevent-cors'))
->createClient()
->share(
$this->argument('host'),
$subdomains,
$domain
)
->createHttpServer()
->run();
}
protected function renderQrCode(string $link)
{
$options = new QROptions;
$options->outputType = QROutputInterface::STRING_TEXT;
$options->version = Version::AUTO;
$options->quietzoneSize = 1;
$options->eol = "\n";
$options->textLineStart = str_repeat(' ', 2);
$options->textDark = $this->ansi8('▌', 0);
$options->textLight = $this->ansi8(' ', 255);
$options->moduleValues = [
QRMatrix::M_FINDER_DARK => $this->ansi8('██', 0),
QRMatrix::M_FINDER => $this->ansi8('░░', 0),
QRMatrix::M_FINDER_DOT => $this->ansi8('██', 0),
QRMatrix::M_ALIGNMENT_DARK => $this->ansi8('██', 0),
QRMatrix::M_ALIGNMENT => $this->ansi8('░░', 0),
QRMatrix::M_VERSION_DARK => $this->ansi8('██', 0),
QRMatrix::M_VERSION => $this->ansi8('░░', 0),
];
return (new QRCode($options))->render($link);
}
protected function ansi8(string $str, int $color, bool $background = false): string
{
$color = max(0, min($color, 255));
$background = ($background ? 0 : 255);
return sprintf("\x1b[%s;5;%sm%s\x1b[0m", $background, $color, $str);
}
protected function detectOperatingSystem(): void
{
$this->isWindows = strpos(php_uname('s'), 'Windows') !== false;
}
protected function isWindows(): bool
{
if ($this->isWindows === null) {
$this->detectOperatingSystem();
}
return $this->isWindows;
}
protected function getMagicAuthValue(): ?string
{
if (!$this->input->hasParameterOption('--magic-auth')) {
return null;
}
return $this->option('magic-auth') ?? '';
}
}