Skip to content
This repository has been archived by the owner on Jan 3, 2019. It is now read-only.

Commit

Permalink
inital push
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-krueger committed Apr 9, 2016
1 parent 5ce3dfb commit 1c39012
Show file tree
Hide file tree
Showing 25 changed files with 956 additions and 9 deletions.
53 changes: 44 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
*.class
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# Mobile Tools for Java (J2ME)
.mtj.tmp/
# User-specific stuff:
.idea/workspace.xml
.idea/tasks.xml
.idea/dictionaries
.idea/vcs.xml
.idea/jsLibraryMappings.xml
.idea

# Package Files #
*.jar
*.war
*.ear
# Sensitive or high-churn files:
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# Gradle:
.idea/gradle.xml
.idea/libraries

# Mongo Explorer plugin:
.idea/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

.class
36 changes: 36 additions & 0 deletions Pipeline.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: io.netty:netty-all:4.0.36.Final" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.spongepowered:spongeapi:4.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: ninja.leaping.configurate:configurate-hocon:3.1.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.typesafe:config:1.3.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: ninja.leaping.configurate:configurate-core:3.1.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.flowpowered:flow-math:1.0.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.ow2.asm:asm:5.0.3" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: ninja.leaping.configurate:configurate-yaml:3.1.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.16" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: ninja.leaping.configurate:configurate-gson:3.1.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.inject:guice:4.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.inject:javax.inject:1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: aopalliance:aopalliance:1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.slf4j:slf4j-api:1.7.14" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.spongepowered:event-gen-core:1.1.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.gson:gson:2.2.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.spongepowered:spongegradle:0.5-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.google.guava:guava:17.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.flowpowered:flow-noise:1.0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.apache.commons:commons-lang3:3.3.2" level="project" />
</component>
</module>
58 changes: 58 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.McAPI</groupId>
<artifactId>Pipeline</artifactId>
<version>0.1-alpha</version>

<repositories>
<repository>
<id>sponge-maven-repo</id>
<name>Sponge maven repo</name>
<url>http://repo.spongepowered.org/maven</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<build>
<defaultGoal>clean package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>F:\Sponge Server\mods</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.36.Final</version>
</dependency>
<dependency>
<groupId>org.spongepowered</groupId>
<artifactId>spongeapi</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
204 changes: 204 additions & 0 deletions src/main/java/de/McAPI/Pipeline/Pipeline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package de.McAPI.Pipeline;

import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteStreams;
import com.google.inject.Inject;
import de.McAPI.Pipeline.protocol.PipelineHandshakeHandler;
import de.McAPI.Pipeline.protocol.PipelineRequestDecoder;
import de.McAPI.Pipeline.protocol.PipelineResponseHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.util.AttributeKey;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
import org.slf4j.Logger;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
import org.spongepowered.api.plugin.Plugin;

import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.Key;
import java.util.Optional;

@Plugin(id = "pipeline", name = "Pipeline", version = "0.1-alpha", url = "http://mcapi.de", authors = "Yonas")
public class Pipeline {

public final static AttributeKey<Pipeline> PLUGIN_ATTRIBUTE_KEY = AttributeKey.valueOf("key_pipeline_plugin");

@Inject
private Logger logger;

@Inject
@ConfigDir(sharedRoot = false)
private Path directory;

private boolean debug = true;

private NioEventLoopGroup group = new NioEventLoopGroup(1);

private Key signature;
private String version;

public Pipeline() {}

@Listener
public void onStart(GameStartedServerEvent event) {

// get the host
String host = null;
Optional<InetSocketAddress> address = Sponge.getServer().getBoundAddress();
host = (address.isPresent() ? address.get().getAddress().getHostAddress() : "0.0.0.0");

// parse version
this.version = Sponge.getGame().getPluginManager().getPlugin("pipeline").get().getVersion().get();

// Config
Path config = this.directory.resolve("config.yml");
try {
Files.createDirectories(this.directory);
} catch (IOException e) {
e.printStackTrace();
}


if(!(Files.exists(config))) {

try {

String defaultConfig = new String(
ByteStreams.toByteArray(Pipeline.class.getClassLoader().getResourceAsStream("config.yml")),
StandardCharsets.UTF_8
);

defaultConfig = defaultConfig
.replace("%default_signature%", Token.generateSignature())
.replace("%ip%", host);


Files.write(config, ImmutableList.<CharSequence>of(defaultConfig), StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}

}

ConfigurationLoader loader = YAMLConfigurationLoader.builder().setPath(config).build();

ConfigurationNode node;

try {
node = loader.load();
} catch (IOException e) {
this.logger.info("Failed to load config.");
return;
}

// load all values
this.signature = new SecretKeySpec(node.getNode("signature").getString().getBytes(StandardCharsets.UTF_8), "HmacSHA256");
this.debug = node.getNode("debug").getBoolean(true);
int port = node.getNode("port").getInt(20000);

new ServerBootstrap()
.channel(NioServerSocketChannel.class)
.group(this.group)
.childHandler(new ChannelInitializer<NioSocketChannel>() {

@Override
protected void initChannel(NioSocketChannel channel) throws Exception {

Session session = new Session();

if (isDebug()) {
logger.info(String.format(
"[%s] Creating a new request.",
session.getDebugKey()
));
}


channel.attr(Session.SESSION_ATTRIBUTE_KEY).set(session);
channel.attr(Pipeline.PLUGIN_ATTRIBUTE_KEY).set(Pipeline.this);

if (isDebug()) {
logger.info(String.format(
"[%s] Establishing connection with %s.",
session.getDebugKey(),
channel.remoteAddress().toString()
));
}

channel.pipeline().addLast("handshakeHandler", new PipelineHandshakeHandler());
channel.pipeline().addAfter("handshakeHandler", "stringDecoder", new StringDecoder(StandardCharsets.UTF_8));
channel.pipeline().addAfter("stringDecoder", "requestDecoder", new PipelineRequestDecoder());
channel.pipeline().addAfter("requestDecoder", "response", new PipelineResponseHandler());
}

})
.bind(host, port)
.addListener(new ChannelFutureListener() {

public void operationComplete(ChannelFuture channelFuture) throws Exception {

if (channelFuture.isSuccess()) {
logger.info("Pipeline is now open.");
} else {
logger.info("Pipeline wasn't able to let the oil threw...");

if (isDebug() && !(channelFuture.cause() == null)) {
logger.info(channelFuture.cause().getMessage());
}
}

}

});

}

/**
* This method returns the related signature.
* @return
*/
public Key getSignature() {
return this.signature;
}

/**
* Returns the logger.
* @return
*/
public Logger logger() {
return this.logger;
}

/**
* Returns the plugin version in a human-readable format.
* @return
*/
public String getVersion() {
return this.version;
}

/**
* Returns true if the debug modus is active.
* @return
*/
public boolean isDebug() {
return this.debug;
}

}
33 changes: 33 additions & 0 deletions src/main/java/de/McAPI/Pipeline/Session.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.McAPI.Pipeline;

import io.netty.util.AttributeKey;

public class Session {

public final static AttributeKey<Session> SESSION_ATTRIBUTE_KEY = AttributeKey.valueOf("key_pipeline_session");

private final String key;
private final String debugKey;

public Session() {
this.key = Token.generateToken();
this.debugKey = Token.pseudo();
}

/**
* This method returns the related key.
* @return
*/
public String getKey() {
return this.key;
}

/**
* Returns the debug key.
* @return
*/
public String getDebugKey() {
return this.debugKey;
}

}
Loading

0 comments on commit 1c39012

Please sign in to comment.