-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Georglider/master
1.1.0
- Loading branch information
Showing
10 changed files
with
234 additions
and
160 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
group 'georglider' | ||
version '1.1.0' | ||
|
||
jar { | ||
manifest { | ||
attributes( | ||
'Main-Class': 'georglider.twitch.Application' | ||
) | ||
} | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' | ||
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
} |
153 changes: 153 additions & 0 deletions
153
VirtualTwitchServer/src/main/java/georglider/twitch/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package georglider.twitch; | ||
|
||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpHandler; | ||
import com.sun.net.httpserver.HttpServer; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.datatransfer.StringSelection; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.InetSocketAddress; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.Scanner; | ||
|
||
/** | ||
* @author Georglider (Georglider#3660) | ||
* <p> Main creation on 10.01.2023 at 20:46 | ||
**/ | ||
|
||
public class Application { | ||
|
||
static Scanner scanner = new Scanner(System.in); | ||
|
||
public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException { | ||
boolean cli = args.length > 0; | ||
|
||
int port = Integer.parseInt(dialog("Please provide port which would be used for current session", cli)); | ||
String clientId = dialog("Please paste your clientId from Twitch here!", cli); | ||
|
||
String token = startServer(port, clientId, cli); | ||
System.out.println("\n" + token); | ||
if (!cli) { | ||
int a = JOptionPane.showConfirmDialog(null, | ||
String.format("Here's your token %s, would you like to copy it?", token), | ||
"Would you like to copy your token?", | ||
JOptionPane.YES_NO_OPTION, | ||
JOptionPane.QUESTION_MESSAGE, | ||
null); | ||
if (a == 0) { | ||
Toolkit.getDefaultToolkit() | ||
.getSystemClipboard() | ||
.setContents(new StringSelection(token), null); | ||
} | ||
} | ||
} | ||
|
||
public static String startServer(int port, String clientId, Boolean cli) throws IOException, InterruptedException, URISyntaxException { | ||
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); | ||
String s = new InetSocketAddress(port).toString(); | ||
LoggedInHandler loggedInHandler = new LoggedInHandler(); | ||
StartHandler startHandler = new StartHandler(s, clientId); | ||
server.createContext("/", startHandler); | ||
server.createContext("/token", loggedInHandler); | ||
server.setExecutor(null); // creates a default executor | ||
server.start(); | ||
|
||
if (!cli && Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { | ||
Desktop.getDesktop().browse(new URI("http://localhost:" + port)); | ||
} else { | ||
System.out.printf("Go to the http://localhost:%d", port); | ||
} | ||
|
||
while (loggedInHandler.token.equals("")) { | ||
Thread.sleep(1000); | ||
} | ||
server.stop(10); | ||
return loggedInHandler.token; | ||
} | ||
|
||
static class LoggedInHandler implements HttpHandler { | ||
String token = ""; | ||
|
||
public LoggedInHandler() { | ||
} | ||
|
||
@Override | ||
public void handle(HttpExchange t) throws IOException { | ||
if (t.getRequestURI().toString().length() < 50) { | ||
String response = """ | ||
<html> | ||
<body> | ||
<h1>Something went wrong, please try again!</h1> | ||
</body> | ||
</html>"""; | ||
t.sendResponseHeaders(401, response.length()); | ||
OutputStream os = t.getResponseBody(); | ||
os.write(response.getBytes()); | ||
os.close(); | ||
} else { | ||
this.token = t.getRequestURI().toString().substring(20, 50); | ||
|
||
String response = String.format(""" | ||
<html> | ||
<body> | ||
<h1>Authenticated!</h1> | ||
<h2>This is your token -> %s</h2> | ||
</body> | ||
</html>""", this.token); | ||
t.sendResponseHeaders(200, response.length()); | ||
OutputStream os = t.getResponseBody(); | ||
os.write(response.getBytes()); | ||
os.close(); | ||
} | ||
} | ||
} | ||
|
||
static class StartHandler implements HttpHandler { | ||
private final String clientId; | ||
private final String server; | ||
|
||
public StartHandler(String server, String clientId) { | ||
this.server = new StringBuilder(server.replace("0.0.0.0/0.0.0.0", "localhost")).insert(0, "http://").toString(); | ||
this.clientId = clientId; | ||
} | ||
|
||
@Override | ||
public void handle(HttpExchange t) throws IOException { | ||
String response = String.format(""" | ||
<html> | ||
<body> | ||
<a href=%s>Login with Twitch</a> | ||
</body> | ||
<script> | ||
if (location.href.indexOf('#') != -1) {location.replace(location.href.replace('#', "token/"))}</script> | ||
</html>""", ("https://id.twitch.tv/oauth2/authorize?client_id=" + clientId + | ||
"&redirect_uri=" + server + "&response_type=token" + | ||
"&scope=channel:manage:redemptions channel:read:redemptions")); | ||
t.sendResponseHeaders(200, response.length()); | ||
OutputStream os = t.getResponseBody(); | ||
os.write(response.getBytes()); | ||
os.close(); | ||
} | ||
} | ||
|
||
public static String dialog(String message, Boolean cliMode) { | ||
if (cliMode) { | ||
System.out.println(message); | ||
|
||
return scanner.nextLine(); | ||
} | ||
return JOptionPane.showInputDialog(message); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ plugins { | |
} | ||
|
||
group = 'georglider' | ||
version = '1.0.1' | ||
version = '1.1.0' | ||
|
||
repositories { | ||
mavenCentral() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
rootProject.name = 'twitch' | ||
include 'VirtualTwitchServer' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.