-
Notifications
You must be signed in to change notification settings - Fork 1
/
hw-start-ngrok.ps1
208 lines (174 loc) · 14.1 KB
/
hw-start-ngrok.ps1
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#Set-ExecutionPolicy RemoteSigned #do not enable this line
#current project directory
$ScriptRoot = Split-Path $MyInvocation.MyCommand.Path
."$ScriptRoot\functions.ps1"
$pinvoke = add-type -name pinvoke -passThru -memberDefinition @'
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool SetWindowText(IntPtr hwnd, String lpString);
'@
#===================================start ngork=========================================
#$ngrok_user= Read-Host 'ngrok user =?';
#$ngrok_pass= Read-Host 'ngrok pass =?';
<#
# added to ngrok.yml configuration file
$diff_port=Read-host "Use one other port behind, ie: 8080, i using VisualSVN Server listening 8080 port."
if ([string]::IsNullOrEmpty($diff_port)) {
$diff_port="80"
echo "Use default port: 80"
}
#>
#Start-Process "cmd.exe" ("/c ngrok -authtoken gwUgKJWPQZH6FW1mz2oZ -httpauth='{0}:{1}' 80" -f ($ngrok_user,$ngrok_pass))
# -subdomain=hoangweb --> read from ngrok.yml
$ngrok=Start-Process "cmd.exe" "/c cd $ScriptRoot&&bin\ngrok_old.exe -config ngrok.yml start hw-xampp hw-vsvn hw-ftp&&timeout /t 5" -passthru
$ngrok_status= questionYesNo 'Do you see tunnel "hw-xampp" working ? if run ngrok failt, try other tunnel name in ngrok.yml file.'
if ($ngrok_status -eq 1){
exit
}
echo $ngrok_status
$ngrok_id="hw-xampp"
$change_ngrok="{0}.ngrok.com" -f $ngrok_id
$remind_phpmyadmin_pass="$ScriptRoot/old-phpmyadmin-pass.txt"
$remind_ngrok_process="$ScriptRoot/ngrok-process.txt"
#---------save ngrok process
For ($i=0; $i -le 5; $i++) {
if (-not [string]::IsNullOrEmpty($ngrok.Id)){
$pinvoke::setWindowText($ngrok.mainWindowHandle, "Hoangweb.COM - Share localhost")
#$pinvoke::setProcessName($notepad.mainWindowHandle, "hoang")
}
}
(""+$ngrok.Id+"").Trim()|set-content $remind_ngrok_process
#===================================config.inc.php=========================================
$config="C:/xampp/phpMyAdmin/config.inc.php"
$pass = randomStr 10
$content=(get-content $config|out-string)
if($content -match ".+'password'.+=(.+)"){
#save old passs
($matches[1] -replace "^(\s+)?('|`")|('|`")[;\s]+?$", "")|set-content $remind_phpmyadmin_pass
#$content=$content -replace $matches[0],"XX"
$content -replace $matches[1],("'{0}';" -f $pass)|Foreach {$_.TrimEnd()} | where {$_ -ne ""}|set-content $config
#$content
}
#===================================edit wp-config.php=======================================
$wp_config="E:\HoangData\xampp_htdocs\wpmultisite/wp-config.php"
$config_str=(get-content $wp_config|out-string)
#find ngrok id
if($config_str -match "define.+?NGROK_ID.+"){
#make sure ngrok ID is match values in wp-config.php
#$config_str=$config_str -replace $matches[1],$ngrok_id
$pattern = "define.+NGROK_ID.+"
$config_str=[regex]::Replace(
$config_str,
$pattern,
{
("define('NGROK_ID', '{0}');" -f $ngrok_id)
}
)
$config_str=$config_str | Foreach {$_.TrimEnd()} | where {$_ -ne ""}
$config_str|set-content $wp_config
}
#change $ngrok_url
if($config_str -match "(\$)ngrok_url.+"){
#make sure ngrok ID is match values in wp-config.php
$config_str -replace ($matches[0] -replace '\$','\$'),'$ngrok_url="http://".NGROK_ID.".ngrok.com";' |set-content $wp_config
}
#================================change domain in wp_blogs table=====================================
$changeurl=("/c curl -sS `"http://localhost/wpmultisite/hoang/hw-changeurl.php?do=public_site&domain={0}`"" -f $change_ngrok)
#Start-Process "cmd.exe" "$changeurl" -passthru -wait
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "cmd.exe"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = $changeurl
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
Write-Host "stdout: $stdout"
Write-Host "stderr: $stderr"
Write-Host "exit code: " + $p.ExitCode
#====================================httpd-vhosts.conf=============================================
#edit httpd-vhosts.conf
#note: do not need
$virtualHost=@"
<VirtualHost $ngrok_id.ngrok.com>
DocumentRoot "D:/HoangData/xampp_htdocs/"
ServerName $ngrok_id.ngrok.com
ServerAlias $ngrok_id.ngrok.com
CustomLog "logs/dummy-host2.$ngrok_id.ngrok.com-access.log" combined
ErrorLog "logs/dummy-host2.$ngrok_id.ngrok.com-error.log"
<Directory "D:/HoangData/xampp_htdocs/wpmultisite">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
"@;
$vhosts="C:\xampp\apache\conf\extra\httpd-vhosts.conf"
$content = (get-content $vhosts|out-string)
if($content -notmatch "ngrok.com"){
#add-content $vhosts $virtualHost
}
else{
if( $content -match "(\.|\s)[\w]+\.ngrok\.com"){
$content=$content -replace $matches[0]," $change_ngrok"
}
#$content
#if( $content -match "(\s)[\w]+\.ngrok\.com"){
# $content=$content -replace $matches[0]," $change_ngrok"
#}
#$content|set-content $vhosts
}
#===================================HOSTS======================================================
# Uncomment lines with localhost on them:
$hostsPath = "$env:windir\System32\drivers\etc\hosts"
$hostsPath1 = "E:\HoangData\softwares\utilities\hosts file manager\hosts"
$hostsPath1_HOSTS = $hostsPath1+"\HOSTS";
$hosts = get-content $hostsPath1_HOSTS|Out-String
####
# modify hosts file
####
if($hosts -match '.+ngrok.com')
{
#$hosts = $hosts -replace "`n", "`r`n"
$hosts=($hosts -replace $matches[0],"127.0.0.1 $ngrok_id.ngrok.com")
$hosts=$hosts | Foreach {$_.TrimEnd()} | where {$_ -ne ""}
$hosts|set-content $hostsPath1_HOSTS -Encoding UTF8
}
else {
#$hosts=$hosts | Foreach {$_.TrimEnd()} | where {$_ -ne ""}
"`n127.0.0.1 $ngrok_id.ngrok.com"|add-content $hostsPath1_HOSTS -Encoding UTF8
}
####update hosts file
Start-Process "$hostsPath1/mvps.bat" -verb runAs
#=================================restart XAMPP==========================================
$comment=@'
$mypid=tcpPort "80"
$mypid
if([string]::IsNullOrEmpty($mypid)){
}else{
stop-process -id $mypid
}
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
#$pinfo.Arguments = "localhost"
$pinfo.FileName = "c:/xampp/xampp_stop.exe"
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$pinfo.FileName = "c:/xampp/xampp_start.exe"
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
'@;
#open url
Start-Process "http://localhost:4040" #track ngrok request
Start-Process "http://$ngrok_id.ngrok.com/wpmultisite/"
#Write-Host "Press any key to continue ..."
#Start-Sleep -s 60