Commit 20bf966b authored by Mathias Stanger's avatar Mathias Stanger

initial commit

parents
Pipeline #96 skipped
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
sshclient
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
<entry name="!?*.aj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<component name="CopyrightManager">
<settings default="" />
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/app" />
</set>
</option>
<option name="myModules">
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/app" />
</set>
</option>
</GradleProjectSettings>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="NullableNotNullManager">
<option name="myDefaultNullable" value="android.support.annotation.Nullable" />
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
<option name="myNullables">
<value>
<list size="4">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />
</list>
</value>
</option>
<option name="myNotNulls">
<value>
<list size="4">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" />
</list>
</value>
</option>
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
<option name="id" value="Android" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
<module fileurl="file://$PROJECT_DIR$/sshclient.iml" filepath="$PROJECT_DIR$/sshclient.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="org.jetbrains.plugins.gradle.execution.test.runner.AllInPackageGradleConfigurationProducer" />
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestClassGradleConfigurationProducer" />
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestMethodGradleConfigurationProducer" />
</set>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "de.matsta.sshclient"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.jcraft:jsch:0.1.53'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.redmadrobot:chronos:1.0.5'
compile 'com.squareup:otto:1.3.8'
}
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\User\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
package de.matsta.yasc;
import android.app.Application;
import android.test.ApplicationTestCase;
/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
\ No newline at end of file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.matsta.yasc">
<!-- Falls der Key auf dem externen Speicher liegt -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- Wir bauen eine SSH-Verbindung übers Netz auf -->
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:name=".BusApplication"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package de.matsta.yasc;
import android.app.Application;
import com.squareup.otto.Bus;
import com.squareup.otto.ThreadEnforcer;
public class BusApplication extends Application {
/* Bitte einsteigen, Otto fährt gleich weiter. */
public static Bus bus = new Bus(ThreadEnforcer.ANY);
public static Bus getBus() {
return bus;
}
}
package de.matsta.yasc;
import com.redmadrobot.chronos.gui.activity.ChronosActivity;
public class ChronosBusSupportActivity extends ChronosActivity {
@Override
protected void onResume() {
super.onResume();
// Alle Events der Activity registrieren
BusApplication.getBus().register(this);
}
@Override
protected void onPause() {
// Alle Events abklemmen, damit wir nicht darauf reagieren während die Activity nicht sichtbar ist.
BusApplication.getBus().unregister(this);
super.onPause();
}
}
package de.matsta.yasc;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.redmadrobot.chronos.gui.activity.ChronosSupportActivity;
import com.squareup.otto.Subscribe;
import java.net.URISyntaxException;
import butterknife.Bind;
import butterknife.ButterKnife;
public class MainActivity extends ChronosSupportActivity {
private static final int FILE_SELECT_CODE = 0;
@Bind(R.id.btnExecute)
Button mBtnExecute;
@Bind(R.id.edtUsername)
EditText mEdtUsername;
@Bind(R.id.edtKeyPass)
EditText mEdtKeyPass;
@Bind(R.id.btnSelectKey)
Button mBtnSelectKey;
@Bind(R.id.btnBuzz)
Button mBtnBuzz;
@Bind(R.id.btnOpen)
Button mBtnOpen;
@Bind(R.id.btnClose)
Button mBtnClose;
@Bind(R.id.btnStatus)
Button mBtnStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mBtnExecute.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save();
executeRemote("who");
}
});
mBtnStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save();
executeRemote("status");
}
});
mBtnBuzz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save();
executeRemote("buzz");
}
});
mBtnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save();
executeRemote("open");
}
});
mBtnClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save();
executeRemote("close");
}
});
mBtnSelectKey.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFileChooser();
}
});
SharedPrefsTools sharedPrefs = new SharedPrefsTools(getApplicationContext());
// Daten aus SharedPreferences laden
mEdtUsername.setText(sharedPrefs.getUsername());
mEdtKeyPass.setText(sharedPrefs.getKeyPwd());
}
public void onOperationFinished(final SshOperation.Result result) {
// Wird das hier eigentlich jemals aufgerufen?
if (result.isSuccessful()) {
Toast.makeText(this, "onOperationFinished: " + result.getOutput(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "onOperationFinished: " + result.getErrorMessage(), Toast.LENGTH_LONG).show();
Log.d(this.getClass().getSimpleName(), result.getErrorMessage());
}
}
private void executeRemote(String command) {
SharedPrefsTools sharedPrefsTools = new SharedPrefsTools(getApplicationContext());
runOperation(new SshOperation(
// TODO Die Serveradresse sollte konfigurierbar sein.
"sphinx", sharedPrefsTools.getUsername(), sharedPrefsTools.getKey(), sharedPrefsTools.getKeyPwd(), 22, command
));
}
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, getString(R.string.selectPrivateKey)),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, R.string.installFilebrowser,
Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Wir erhalten eine URI um auf die Datei zuzugreifen
Uri uri = data.getData();
// Wir wollen aber keine Uri sondern den Pfad
try {
String path = getPath(this, uri);
if (path == null) {
throw new Exception(getString(R.string.exceptionPathNotParseable));
}
new SharedPrefsTools(this).setKey(path);
} catch (Exception e) {
Log.e(this.getClass().getSimpleName(), e.getLocalizedMessage());
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
public static String getPath(Context context, Uri uri) throws URISyntaxException {
if ("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = {"_data"};
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow("_data");
if (cursor.moveToFirst()) {
return cursor.getString(column_index);
}
} catch (Exception e) {
// Eat it
}
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
private void save() {
// Wir speichern die Eingaben in den SharedPreferences
SharedPrefsTools sharedPrefsTools = new SharedPrefsTools(getApplicationContext());
sharedPrefsTools.setUsername(mEdtUsername.getText().toString());
// TODO Schlüssel im Keystore / Authenticator speichern.
sharedPrefsTools.setKeyPwd(mEdtKeyPass.getText().toString());
}
@Subscribe
public void onShellMessage(ShellMessageEvent event) {
// Wir bekommen die gesamte Ausgabe, also ganzen Text mit allen Befehlen und deren Rückgaben. Hier könnte man vielleicht noch etwas filtern oder besser darauf reagieren.
Toast.makeText(this, event.getMessage(), Toast.LENGTH_LONG).show();
}
}
package de.matsta.yasc;
import android.content.Context;
import android.content.SharedPreferences;
public class SharedPrefsTools {
private static final String KEY_KEY = "key";
private static final String KEY_USERNAME = "username";
private static final String KEY_KEYPWD = "keypwd";
private SharedPreferences.Editor mEditor;
private SharedPreferences mPerf;
public SharedPrefsTools(Context context) {
// Preferences öffnen / anlegen. Durch MODE_PRIVATE können nur Apps mit unserem Benutzer (aktuell nur wir) darauf zugreifen
mPerf = context.getSharedPreferences("SharedPrefs", Context.MODE_PRIVATE);
mEditor = mPerf.edit();
}
public String getUsername() {
return mPerf.getString(KEY_USERNAME, "");
}
public void setUsername(String value) {
mEditor.putString(KEY_USERNAME, value);
mEditor.commit();
}
public String getKey() {
return mPerf.getString(KEY_KEY, "");
}
public void setKey(String value) {
mEditor.putString(KEY_KEY, value);
mEditor.commit();
}
public String getKeyPwd() {
return mPerf.getString(KEY_KEYPWD, "");
}
public void setKeyPwd(String value) {
mEditor.putString(KEY_KEYPWD, value);
mEditor.commit();
}
}
package de.matsta.yasc;
public class ShellMessageEvent {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public ShellMessageEvent(String message) {
this.message = message;
}
}
package de.matsta.yasc;
import android.support.annotation.NonNull;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.redmadrobot.chronos.ChronosOperation;
import com.redmadrobot.chronos.ChronosOperationResult;
import org.jetbrains.annotations.Nullable;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Properties;
public class SshOperation extends ChronosOperation<String> {
private String hostname;
private String username;
private String keyFile;
private String keyFilePwd;
private Integer port;
private String command;
public SshOperation(String hostname, String username, String keyFile, String keyFilePwd, Integer port, String command) {
this.hostname = hostname;
this.username = username;
this.keyFile = keyFile;
this.keyFilePwd = keyFilePwd;
this.port = port;
this.command = command;
}
private static String readChannelOutput(Channel channel) {
StringBuilder sb = new StringBuilder();
byte[] buffer = new byte[1024];
try {
InputStream in = channel.getInputStream();
String line = "";
while (true) {