2020-07-30 20:10:15 +00:00
/ *
* Copyright ( c ) 2019 - 2020 GeyserMC . http : //geysermc.org
*
* 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
* /
2020-03-24 07:56:04 +00:00
package org.geysermc.platform.standalone ;
2020-07-05 23:35:51 +00:00
import org.geysermc.connector.common.ChatColor ;
import org.geysermc.connector.utils.LanguageUtils ;
2020-03-24 07:56:04 +00:00
import java.io.InputStream ;
import java.nio.file.Files ;
import java.nio.file.OpenOption ;
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 " ) ) {
Files . write ( Paths . get ( System . getenv ( " temp " ) + " /loopback_minecraft.bat " ) , loopbackCommand . getBytes ( ) , new OpenOption [ 0 ] ) ;
process = Runtime . getRuntime ( ) . exec ( startScript ) ;
2020-07-05 23:35:51 +00:00
geyserLogger . info ( ChatColor . AQUA + LanguageUtils . getLocaleStringLog ( " geyser.bootstrap.loopback.added " ) ) ;
2020-03-24 07:56:04 +00:00
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
2020-07-05 23:35:51 +00:00
geyserLogger . error ( LanguageUtils . getLocaleStringLog ( " geyser.bootstrap.loopback.failed " ) ) ;
2020-03-24 07:56:04 +00:00
}
}
}
}