2020-07-30 20:10:15 +00:00
/ *
2022-01-01 19:03:05 +00:00
* Copyright ( c ) 2019 - 2022 GeyserMC . http : //geysermc.org
2020-07-30 20:10:15 +00:00
*
* Permission is hereby granted , free of charge , to any person obtaining a copy
* of this software and associated documentation files ( the " Software " ) , to deal
* in the Software without restriction , including without limitation the rights
* to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is
* furnished to do so , subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software .
*
* THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE .
*
* @author GeyserMC
* @link https : //github.com/GeyserMC/Geyser
* /
2021-11-20 21:34:30 +00:00
package org.geysermc.geyser.platform.standalone ;
2020-03-24 07:56:04 +00:00
2021-11-20 23:29:46 +00:00
import org.geysermc.geyser.text.ChatColor ;
import org.geysermc.geyser.text.GeyserLocale ;
2020-07-05 23:35:51 +00:00
2020-03-24 07:56:04 +00:00
import java.io.InputStream ;
import java.nio.file.Files ;
import java.nio.file.Paths ;
2020-03-25 05:55:09 +00:00
public class LoopbackUtil {
2020-03-24 07:56:04 +00:00
private static final String checkExemption = " powershell -Command \" CheckNetIsolation LoopbackExempt -s \" " ; // Java's Exec feature runs as CMD, NetIsolation is only accessible from PowerShell.
private static final String loopbackCommand = " powershell -Command \" CheckNetIsolation LoopbackExempt -a -n='Microsoft.MinecraftUWP_8wekyb3d8bbwe' \" " ;
private static final String startScript = " powershell -Command \" Start-Process 'cmd' -ArgumentList /c,%temp%/loopback_minecraft.bat -Verb runAs \" " ;
2020-04-29 20:01:53 +00:00
public static void checkLoopback ( GeyserStandaloneLogger geyserLogger ) {
2020-03-24 07:56:04 +00:00
if ( System . getProperty ( " os.name " ) . equalsIgnoreCase ( " Windows 10 " ) ) {
try {
Process process = Runtime . getRuntime ( ) . exec ( checkExemption ) ;
InputStream is = process . getInputStream ( ) ;
StringBuilder sb = new StringBuilder ( ) ;
while ( process . isAlive ( ) ) {
if ( is . available ( ) ! = 0 ) {
sb . append ( ( char ) is . read ( ) ) ;
}
}
String result = sb . toString ( ) ;
if ( ! result . contains ( " minecraftuwp " ) ) {
2021-06-21 01:42:22 +00:00
Files . write ( Paths . get ( System . getenv ( " temp " ) + " /loopback_minecraft.bat " ) , loopbackCommand . getBytes ( ) ) ;
2021-02-16 21:25:46 +00:00
Runtime . getRuntime ( ) . exec ( startScript ) ;
2020-03-24 07:56:04 +00:00
2021-11-20 23:29:46 +00:00
geyserLogger . info ( ChatColor . AQUA + GeyserLocale . getLocaleStringLog ( " geyser.bootstrap.loopback.added " ) ) ;
2020-03-24 07:56:04 +00:00
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
2021-11-20 23:29:46 +00:00
geyserLogger . error ( GeyserLocale . getLocaleStringLog ( " geyser.bootstrap.loopback.failed " ) ) ;
2020-03-24 07:56:04 +00:00
}
}
}
}