Formatting
This commit is contained in:
parent
ae8d5bff1d
commit
5a247576c4
@ -61,7 +61,8 @@ import java.security.SecureRandom;
|
|||||||
* @author Damien Miller
|
* @author Damien Miller
|
||||||
* @version 0.2
|
* @version 0.2
|
||||||
*/
|
*/
|
||||||
public class BCrypt {
|
public class BCrypt
|
||||||
|
{
|
||||||
// BCrypt parameters
|
// BCrypt parameters
|
||||||
private static final int GENSALT_DEFAULT_LOG2_ROUNDS = 10;
|
private static final int GENSALT_DEFAULT_LOG2_ROUNDS = 10;
|
||||||
private static final int BCRYPT_SALT_LEN = 16;
|
private static final int BCRYPT_SALT_LEN = 16;
|
||||||
@ -383,8 +384,8 @@ public class BCrypt {
|
|||||||
* @return base64-encoded string
|
* @return base64-encoded string
|
||||||
* @exception IllegalArgumentException if the length is invalid
|
* @exception IllegalArgumentException if the length is invalid
|
||||||
*/
|
*/
|
||||||
private static String encode_base64(byte d[], int len)
|
private static String encode_base64(byte d[], int len) throws IllegalArgumentException
|
||||||
throws IllegalArgumentException {
|
{
|
||||||
int off = 0;
|
int off = 0;
|
||||||
StringBuffer rs = new StringBuffer();
|
StringBuffer rs = new StringBuffer();
|
||||||
int c1, c2;
|
int c1, c2;
|
||||||
@ -392,19 +393,23 @@ public class BCrypt {
|
|||||||
if (len <= 0 || len > d.length)
|
if (len <= 0 || len > d.length)
|
||||||
throw new IllegalArgumentException ("Invalid len");
|
throw new IllegalArgumentException ("Invalid len");
|
||||||
|
|
||||||
while (off < len) {
|
while (off < len)
|
||||||
|
{
|
||||||
c1 = d[off++] & 0xff;
|
c1 = d[off++] & 0xff;
|
||||||
rs.append(base64_code[(c1 >> 2) & 0x3f]);
|
rs.append(base64_code[(c1 >> 2) & 0x3f]);
|
||||||
c1 = (c1 & 0x03) << 4;
|
c1 = (c1 & 0x03) << 4;
|
||||||
if (off >= len) {
|
if (off >= len)
|
||||||
|
{
|
||||||
rs.append(base64_code[c1 & 0x3f]);
|
rs.append(base64_code[c1 & 0x3f]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
c2 = d[off++] & 0xff;
|
c2 = d[off++] & 0xff;
|
||||||
c1 |= (c2 >> 4) & 0x0f;
|
c1 |= (c2 >> 4) & 0x0f;
|
||||||
rs.append(base64_code[c1 & 0x3f]);
|
rs.append(base64_code[c1 & 0x3f]);
|
||||||
c1 = (c2 & 0x0f) << 2;
|
c1 = (c2 & 0x0f) << 2;
|
||||||
if (off >= len) {
|
if (off >= len)
|
||||||
|
{
|
||||||
rs.append(base64_code[c1 & 0x3f]);
|
rs.append(base64_code[c1 & 0x3f]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -422,9 +427,11 @@ public class BCrypt {
|
|||||||
* @param x the base64-encoded value
|
* @param x the base64-encoded value
|
||||||
* @return the decoded value of x
|
* @return the decoded value of x
|
||||||
*/
|
*/
|
||||||
private static byte char64(char x) {
|
private static byte char64(char x)
|
||||||
|
{
|
||||||
if ((int)x < 0 || (int)x > index_64.length)
|
if ((int)x < 0 || (int)x > index_64.length)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
return index_64[(int)x];
|
return index_64[(int)x];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -437,8 +444,8 @@ public class BCrypt {
|
|||||||
* @return an array containing the decoded bytes
|
* @return an array containing the decoded bytes
|
||||||
* @throws IllegalArgumentException if maxolen is invalid
|
* @throws IllegalArgumentException if maxolen is invalid
|
||||||
*/
|
*/
|
||||||
private static byte[] decode_base64(String s, int maxolen)
|
private static byte[] decode_base64(String s, int maxolen) throws IllegalArgumentException
|
||||||
throws IllegalArgumentException {
|
{
|
||||||
StringBuffer rs = new StringBuffer();
|
StringBuffer rs = new StringBuffer();
|
||||||
int off = 0, slen = s.length(), olen = 0;
|
int off = 0, slen = s.length(), olen = 0;
|
||||||
byte ret[];
|
byte ret[];
|
||||||
@ -447,7 +454,8 @@ public class BCrypt {
|
|||||||
if (maxolen <= 0)
|
if (maxolen <= 0)
|
||||||
throw new IllegalArgumentException ("Invalid maxolen");
|
throw new IllegalArgumentException ("Invalid maxolen");
|
||||||
|
|
||||||
while (off < slen - 1 && olen < maxolen) {
|
while (off < slen - 1 && olen < maxolen)
|
||||||
|
{
|
||||||
c1 = char64(s.charAt(off++));
|
c1 = char64(s.charAt(off++));
|
||||||
c2 = char64(s.charAt(off++));
|
c2 = char64(s.charAt(off++));
|
||||||
if (c1 == -1 || c2 == -1)
|
if (c1 == -1 || c2 == -1)
|
||||||
@ -475,6 +483,7 @@ public class BCrypt {
|
|||||||
ret = new byte[olen];
|
ret = new byte[olen];
|
||||||
for (off = 0; off < olen; off++)
|
for (off = 0; off < olen; off++)
|
||||||
ret[off] = (byte)rs.charAt(off);
|
ret[off] = (byte)rs.charAt(off);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -484,11 +493,13 @@ public class BCrypt {
|
|||||||
* @param lr an array containing the two 32-bit half blocks
|
* @param lr an array containing the two 32-bit half blocks
|
||||||
* @param off the position in the array of the blocks
|
* @param off the position in the array of the blocks
|
||||||
*/
|
*/
|
||||||
private final void encipher(int lr[], int off) {
|
private final void encipher(int lr[], int off)
|
||||||
|
{
|
||||||
int i, n, l = lr[off], r = lr[off + 1];
|
int i, n, l = lr[off], r = lr[off + 1];
|
||||||
|
|
||||||
l ^= P[0];
|
l ^= P[0];
|
||||||
for (i = 0; i <= BLOWFISH_NUM_ROUNDS - 2;) {
|
for (i = 0; i <= BLOWFISH_NUM_ROUNDS - 2;)
|
||||||
|
{
|
||||||
// Feistel substitution on left word
|
// Feistel substitution on left word
|
||||||
n = S[(l >> 24) & 0xff];
|
n = S[(l >> 24) & 0xff];
|
||||||
n += S[0x100 | ((l >> 16) & 0xff)];
|
n += S[0x100 | ((l >> 16) & 0xff)];
|
||||||
@ -514,12 +525,14 @@ public class BCrypt {
|
|||||||
* current offset into data
|
* current offset into data
|
||||||
* @return the next word of material from data
|
* @return the next word of material from data
|
||||||
*/
|
*/
|
||||||
private static int streamtoword(byte data[], int offp[]) {
|
private static int streamtoword(byte data[], int offp[])
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
int word = 0;
|
int word = 0;
|
||||||
int off = offp[0];
|
int off = offp[0];
|
||||||
|
|
||||||
for (i = 0; i < 4; i++) {
|
for (i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
word = (word << 8) | (data[off] & 0xff);
|
word = (word << 8) | (data[off] & 0xff);
|
||||||
off = (off + 1) % data.length;
|
off = (off + 1) % data.length;
|
||||||
}
|
}
|
||||||
@ -531,7 +544,8 @@ public class BCrypt {
|
|||||||
/**
|
/**
|
||||||
* Initialise the Blowfish key schedule
|
* Initialise the Blowfish key schedule
|
||||||
*/
|
*/
|
||||||
private void init_key() {
|
private void init_key()
|
||||||
|
{
|
||||||
P = (int[])P_orig.clone();
|
P = (int[])P_orig.clone();
|
||||||
S = (int[])S_orig.clone();
|
S = (int[])S_orig.clone();
|
||||||
}
|
}
|
||||||
@ -540,7 +554,8 @@ public class BCrypt {
|
|||||||
* Key the Blowfish cipher
|
* Key the Blowfish cipher
|
||||||
* @param key an array containing the key
|
* @param key an array containing the key
|
||||||
*/
|
*/
|
||||||
private void key(byte key[]) {
|
private void key(byte key[])
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
int koffp[] = { 0 };
|
int koffp[] = { 0 };
|
||||||
int lr[] = { 0, 0 };
|
int lr[] = { 0, 0 };
|
||||||
@ -549,13 +564,15 @@ public class BCrypt {
|
|||||||
for (i = 0; i < plen; i++)
|
for (i = 0; i < plen; i++)
|
||||||
P[i] = P[i] ^ streamtoword(key, koffp);
|
P[i] = P[i] ^ streamtoword(key, koffp);
|
||||||
|
|
||||||
for (i = 0; i < plen; i += 2) {
|
for (i = 0; i < plen; i += 2)
|
||||||
|
{
|
||||||
encipher(lr, 0);
|
encipher(lr, 0);
|
||||||
P[i] = lr[0];
|
P[i] = lr[0];
|
||||||
P[i + 1] = lr[1];
|
P[i + 1] = lr[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < slen; i += 2) {
|
for (i = 0; i < slen; i += 2)
|
||||||
|
{
|
||||||
encipher(lr, 0);
|
encipher(lr, 0);
|
||||||
S[i] = lr[0];
|
S[i] = lr[0];
|
||||||
S[i + 1] = lr[1];
|
S[i + 1] = lr[1];
|
||||||
@ -569,7 +586,8 @@ public class BCrypt {
|
|||||||
* @param data salt information
|
* @param data salt information
|
||||||
* @param key password information
|
* @param key password information
|
||||||
*/
|
*/
|
||||||
private void ekskey(byte data[], byte key[]) {
|
private void ekskey(byte data[], byte key[])
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
int koffp[] = { 0 }, doffp[] = { 0 };
|
int koffp[] = { 0 }, doffp[] = { 0 };
|
||||||
int lr[] = { 0, 0 };
|
int lr[] = { 0, 0 };
|
||||||
@ -578,7 +596,8 @@ public class BCrypt {
|
|||||||
for (i = 0; i < plen; i++)
|
for (i = 0; i < plen; i++)
|
||||||
P[i] = P[i] ^ streamtoword(key, koffp);
|
P[i] = P[i] ^ streamtoword(key, koffp);
|
||||||
|
|
||||||
for (i = 0; i < plen; i += 2) {
|
for (i = 0; i < plen; i += 2)
|
||||||
|
{
|
||||||
lr[0] ^= streamtoword(data, doffp);
|
lr[0] ^= streamtoword(data, doffp);
|
||||||
lr[1] ^= streamtoword(data, doffp);
|
lr[1] ^= streamtoword(data, doffp);
|
||||||
encipher(lr, 0);
|
encipher(lr, 0);
|
||||||
@ -586,7 +605,8 @@ public class BCrypt {
|
|||||||
P[i + 1] = lr[1];
|
P[i + 1] = lr[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < slen; i += 2) {
|
for (i = 0; i < slen; i += 2)
|
||||||
|
{
|
||||||
lr[0] ^= streamtoword(data, doffp);
|
lr[0] ^= streamtoword(data, doffp);
|
||||||
lr[1] ^= streamtoword(data, doffp);
|
lr[1] ^= streamtoword(data, doffp);
|
||||||
encipher(lr, 0);
|
encipher(lr, 0);
|
||||||
@ -604,7 +624,8 @@ public class BCrypt {
|
|||||||
* of rounds of hashing to apply
|
* of rounds of hashing to apply
|
||||||
* @return an array containing the binary hashed password
|
* @return an array containing the binary hashed password
|
||||||
*/
|
*/
|
||||||
private byte[] crypt_raw(byte password[], byte salt[], int log_rounds) {
|
private byte[] crypt_raw(byte password[], byte salt[], int log_rounds)
|
||||||
|
{
|
||||||
int rounds, i, j;
|
int rounds, i, j;
|
||||||
int cdata[] = (int[])bf_crypt_ciphertext.clone();
|
int cdata[] = (int[])bf_crypt_ciphertext.clone();
|
||||||
int clen = cdata.length;
|
int clen = cdata.length;
|
||||||
@ -612,24 +633,28 @@ public class BCrypt {
|
|||||||
|
|
||||||
if (log_rounds < 4 || log_rounds > 31)
|
if (log_rounds < 4 || log_rounds > 31)
|
||||||
throw new IllegalArgumentException ("Bad number of rounds");
|
throw new IllegalArgumentException ("Bad number of rounds");
|
||||||
|
|
||||||
rounds = 1 << log_rounds;
|
rounds = 1 << log_rounds;
|
||||||
if (salt.length != BCRYPT_SALT_LEN)
|
if (salt.length != BCRYPT_SALT_LEN)
|
||||||
throw new IllegalArgumentException ("Bad salt length");
|
throw new IllegalArgumentException ("Bad salt length");
|
||||||
|
|
||||||
init_key();
|
init_key();
|
||||||
ekskey(salt, password);
|
ekskey(salt, password);
|
||||||
for (i = 0; i < rounds; i++) {
|
for (i = 0; i < rounds; i++)
|
||||||
|
{
|
||||||
key(password);
|
key(password);
|
||||||
key(salt);
|
key(salt);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < 64; i++) {
|
for (i = 0; i < 64; i++)
|
||||||
|
{
|
||||||
for (j = 0; j < (clen >> 1); j++)
|
for (j = 0; j < (clen >> 1); j++)
|
||||||
encipher(cdata, j << 1);
|
encipher(cdata, j << 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = new byte[clen * 4];
|
ret = new byte[clen * 4];
|
||||||
for (i = 0, j = 0; i < clen; i++) {
|
for (i = 0, j = 0; i < clen; i++)
|
||||||
|
{
|
||||||
ret[j++] = (byte)((cdata[i] >> 24) & 0xff);
|
ret[j++] = (byte)((cdata[i] >> 24) & 0xff);
|
||||||
ret[j++] = (byte)((cdata[i] >> 16) & 0xff);
|
ret[j++] = (byte)((cdata[i] >> 16) & 0xff);
|
||||||
ret[j++] = (byte)((cdata[i] >> 8) & 0xff);
|
ret[j++] = (byte)((cdata[i] >> 8) & 0xff);
|
||||||
@ -645,7 +670,8 @@ public class BCrypt {
|
|||||||
* using BCrypt.gensalt)
|
* using BCrypt.gensalt)
|
||||||
* @return the hashed password
|
* @return the hashed password
|
||||||
*/
|
*/
|
||||||
public static String hashpw(String password, String salt) {
|
public static String hashpw(String password, String salt)
|
||||||
|
{
|
||||||
BCrypt B;
|
BCrypt B;
|
||||||
String real_salt;
|
String real_salt;
|
||||||
byte passwordb[], saltb[], hashed[];
|
byte passwordb[], saltb[], hashed[];
|
||||||
@ -655,9 +681,11 @@ public class BCrypt {
|
|||||||
|
|
||||||
if (salt.charAt(0) != '$' || salt.charAt(1) != '2')
|
if (salt.charAt(0) != '$' || salt.charAt(1) != '2')
|
||||||
throw new IllegalArgumentException ("Invalid salt version");
|
throw new IllegalArgumentException ("Invalid salt version");
|
||||||
|
|
||||||
if (salt.charAt(2) == '$')
|
if (salt.charAt(2) == '$')
|
||||||
off = 3;
|
off = 3;
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
minor = salt.charAt(2);
|
minor = salt.charAt(2);
|
||||||
if (minor != 'a' || salt.charAt(3) != '$')
|
if (minor != 'a' || salt.charAt(3) != '$')
|
||||||
throw new IllegalArgumentException ("Invalid salt revision");
|
throw new IllegalArgumentException ("Invalid salt revision");
|
||||||
@ -670,9 +698,12 @@ public class BCrypt {
|
|||||||
rounds = Integer.parseInt(salt.substring(off, off + 2));
|
rounds = Integer.parseInt(salt.substring(off, off + 2));
|
||||||
|
|
||||||
real_salt = salt.substring(off + 3, off + 25);
|
real_salt = salt.substring(off + 3, off + 25);
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
passwordb = (password + (minor >= 'a' ? "\000" : "")).getBytes("UTF-8");
|
passwordb = (password + (minor >= 'a' ? "\000" : "")).getBytes("UTF-8");
|
||||||
} catch (UnsupportedEncodingException uee) {
|
}
|
||||||
|
catch (UnsupportedEncodingException uee)
|
||||||
|
{
|
||||||
throw new AssertionError("UTF-8 is not supported");
|
throw new AssertionError("UTF-8 is not supported");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -684,14 +715,17 @@ public class BCrypt {
|
|||||||
rs.append("$2");
|
rs.append("$2");
|
||||||
if (minor >= 'a')
|
if (minor >= 'a')
|
||||||
rs.append(minor);
|
rs.append(minor);
|
||||||
|
|
||||||
rs.append("$");
|
rs.append("$");
|
||||||
if (rounds < 10)
|
if (rounds < 10)
|
||||||
rs.append("0");
|
rs.append("0");
|
||||||
|
|
||||||
rs.append(Integer.toString(rounds));
|
rs.append(Integer.toString(rounds));
|
||||||
rs.append("$");
|
rs.append("$");
|
||||||
rs.append(encode_base64(saltb, saltb.length));
|
rs.append(encode_base64(saltb, saltb.length));
|
||||||
rs.append(encode_base64(hashed,
|
rs.append(encode_base64(hashed,
|
||||||
bf_crypt_ciphertext.length * 4 - 1));
|
bf_crypt_ciphertext.length * 4 - 1));
|
||||||
|
|
||||||
return rs.toString();
|
return rs.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -703,7 +737,8 @@ public class BCrypt {
|
|||||||
* @param random an instance of SecureRandom to use
|
* @param random an instance of SecureRandom to use
|
||||||
* @return an encoded salt value
|
* @return an encoded salt value
|
||||||
*/
|
*/
|
||||||
public static String gensalt(int log_rounds, SecureRandom random) {
|
public static String gensalt(int log_rounds, SecureRandom random)
|
||||||
|
{
|
||||||
StringBuffer rs = new StringBuffer();
|
StringBuffer rs = new StringBuffer();
|
||||||
byte rnd[] = new byte[BCRYPT_SALT_LEN];
|
byte rnd[] = new byte[BCRYPT_SALT_LEN];
|
||||||
|
|
||||||
@ -712,6 +747,7 @@ public class BCrypt {
|
|||||||
rs.append("$2a$");
|
rs.append("$2a$");
|
||||||
if (log_rounds < 10)
|
if (log_rounds < 10)
|
||||||
rs.append("0");
|
rs.append("0");
|
||||||
|
|
||||||
rs.append(Integer.toString(log_rounds));
|
rs.append(Integer.toString(log_rounds));
|
||||||
rs.append("$");
|
rs.append("$");
|
||||||
rs.append(encode_base64(rnd, rnd.length));
|
rs.append(encode_base64(rnd, rnd.length));
|
||||||
@ -725,7 +761,8 @@ public class BCrypt {
|
|||||||
* 2**log_rounds.
|
* 2**log_rounds.
|
||||||
* @return an encoded salt value
|
* @return an encoded salt value
|
||||||
*/
|
*/
|
||||||
public static String gensalt(int log_rounds) {
|
public static String gensalt(int log_rounds)
|
||||||
|
{
|
||||||
return gensalt(log_rounds, new SecureRandom());
|
return gensalt(log_rounds, new SecureRandom());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -735,7 +772,8 @@ public class BCrypt {
|
|||||||
* rounds to apply
|
* rounds to apply
|
||||||
* @return an encoded salt value
|
* @return an encoded salt value
|
||||||
*/
|
*/
|
||||||
public static String gensalt() {
|
public static String gensalt()
|
||||||
|
{
|
||||||
return gensalt(GENSALT_DEFAULT_LOG2_ROUNDS);
|
return gensalt(GENSALT_DEFAULT_LOG2_ROUNDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -746,7 +784,8 @@ public class BCrypt {
|
|||||||
* @param hashed the previously-hashed password
|
* @param hashed the previously-hashed password
|
||||||
* @return true if the passwords match, false otherwise
|
* @return true if the passwords match, false otherwise
|
||||||
*/
|
*/
|
||||||
public static boolean checkpw(String plaintext, String hashed) {
|
public static boolean checkpw(String plaintext, String hashed)
|
||||||
|
{
|
||||||
return (hashed.compareTo(hashpw(plaintext, hashed)) == 0);
|
return (hashed.compareTo(hashpw(plaintext, hashed)) == 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,8 +11,8 @@ import java.util.Map;
|
|||||||
/**
|
/**
|
||||||
* Copyright 2013 Ryan Michela
|
* Copyright 2013 Ryan Michela
|
||||||
*/
|
*/
|
||||||
public class ConfigPasswordAuthenticator implements PasswordAuthenticator {
|
public class ConfigPasswordAuthenticator implements PasswordAuthenticator
|
||||||
|
{
|
||||||
private Map<String, Integer> FailCounts = new HashMap<String, Integer>();
|
private Map<String, Integer> FailCounts = new HashMap<String, Integer>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -12,33 +12,41 @@ import java.util.List;
|
|||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public class ConsoleCommandCompleter implements Completer {
|
public class ConsoleCommandCompleter implements Completer
|
||||||
|
{
|
||||||
public int complete(final String buffer, final int cursor, final List<CharSequence> candidates) {
|
public int complete(final String buffer, final int cursor, final List<CharSequence> candidates)
|
||||||
Waitable<List<String>> waitable = new Waitable<List<String>>() {
|
{
|
||||||
|
Waitable<List<String>> waitable = new Waitable<List<String>>()
|
||||||
|
{
|
||||||
@Override
|
@Override
|
||||||
protected List<String> evaluate() {
|
protected List<String> evaluate()
|
||||||
|
{
|
||||||
CommandMap commandMap = ReflectionUtil.getProtectedValue(Bukkit.getServer(), "commandMap");
|
CommandMap commandMap = ReflectionUtil.getProtectedValue(Bukkit.getServer(), "commandMap");
|
||||||
return commandMap.tabComplete(Bukkit.getServer().getConsoleSender(), buffer);
|
return commandMap.tabComplete(Bukkit.getServer().getConsoleSender(), buffer);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Bukkit.getScheduler().runTask(SshdPlugin.instance, waitable);
|
Bukkit.getScheduler().runTask(SshdPlugin.instance, waitable);
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
List<String> offers = waitable.get();
|
List<String> offers = waitable.get();
|
||||||
if (offers == null) {
|
if (offers == null)
|
||||||
return cursor;
|
return cursor;
|
||||||
}
|
|
||||||
candidates.addAll(offers);
|
candidates.addAll(offers);
|
||||||
|
|
||||||
final int lastSpace = buffer.lastIndexOf(' ');
|
final int lastSpace = buffer.lastIndexOf(' ');
|
||||||
if (lastSpace == -1) {
|
if (lastSpace == -1)
|
||||||
return cursor - buffer.length();
|
return cursor - buffer.length();
|
||||||
} else {
|
else
|
||||||
return cursor - (buffer.length() - lastSpace - 1);
|
return cursor - (buffer.length() - lastSpace - 1);
|
||||||
}
|
}
|
||||||
} catch (ExecutionException e) {
|
catch (ExecutionException e)
|
||||||
|
{
|
||||||
SshdPlugin.instance.getLogger().log(Level.WARNING, "Unhandled exception when tab completing", e);
|
SshdPlugin.instance.getLogger().log(Level.WARNING, "Unhandled exception when tab completing", e);
|
||||||
} catch (InterruptedException e) {
|
}
|
||||||
|
catch (InterruptedException e)
|
||||||
|
{
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
return cursor;
|
return cursor;
|
||||||
|
@ -14,51 +14,63 @@ import java.io.OutputStream;
|
|||||||
/**
|
/**
|
||||||
* Copyright 2013 Ryan Michela
|
* Copyright 2013 Ryan Michela
|
||||||
*/
|
*/
|
||||||
public class ConsoleCommandFactory implements CommandFactory {
|
public class ConsoleCommandFactory implements CommandFactory
|
||||||
|
{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Command createCommand(ChannelSession cs, String command) {
|
public Command createCommand(ChannelSession cs, String command)
|
||||||
|
{
|
||||||
return new ConsoleCommand(command);
|
return new ConsoleCommand(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ConsoleCommand implements Command {
|
public class ConsoleCommand implements Command
|
||||||
|
{
|
||||||
private String command;
|
private String command;
|
||||||
|
|
||||||
private InputStream in;
|
private InputStream in;
|
||||||
private OutputStream out;
|
private OutputStream out;
|
||||||
private OutputStream err;
|
private OutputStream err;
|
||||||
private ExitCallback callback;
|
private ExitCallback callback;
|
||||||
|
|
||||||
public ConsoleCommand(String command) {
|
public ConsoleCommand(String command)
|
||||||
|
{
|
||||||
this.command = command;
|
this.command = command;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setInputStream(InputStream in) {
|
public void setInputStream(InputStream in)
|
||||||
|
{
|
||||||
this.in = in;
|
this.in = in;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOutputStream(OutputStream out) {
|
public void setOutputStream(OutputStream out)
|
||||||
|
{
|
||||||
this.out = out;
|
this.out = out;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setErrorStream(OutputStream err) {
|
public void setErrorStream(OutputStream err)
|
||||||
|
{
|
||||||
this.err = err;
|
this.err = err;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setExitCallback(ExitCallback callback) {
|
public void setExitCallback(ExitCallback callback)
|
||||||
|
{
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(ChannelSession cs, Environment environment) throws IOException {
|
public void start(ChannelSession cs, Environment environment) throws IOException
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
SshdPlugin.instance.getLogger()
|
SshdPlugin.instance.getLogger()
|
||||||
.info("[U: " + environment.getEnv().get(Environment.ENV_USER) + "] " + command);
|
.info("[U: " + environment.getEnv().get(Environment.ENV_USER) + "] " + command);
|
||||||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
|
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
|
||||||
} catch (Exception e) {
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
SshdPlugin.instance.getLogger().severe("Error processing command from SSH -" + e.getMessage());
|
SshdPlugin.instance.getLogger().severe("Error processing command from SSH -" + e.getMessage());
|
||||||
} finally {
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
callback.onExit(0);
|
callback.onExit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,13 +32,16 @@ import java.util.StringTokenizer;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.StreamHandler;
|
import java.util.logging.StreamHandler;
|
||||||
|
|
||||||
public class ConsoleShellFactory implements ShellFactory {
|
public class ConsoleShellFactory implements ShellFactory
|
||||||
|
{
|
||||||
|
|
||||||
public Command createShell(ChannelSession cs) {
|
public Command createShell(ChannelSession cs)
|
||||||
|
{
|
||||||
return new ConsoleShell();
|
return new ConsoleShell();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ConsoleShell implements Command, Runnable {
|
public class ConsoleShell implements Command, Runnable
|
||||||
|
{
|
||||||
|
|
||||||
private InputStream in;
|
private InputStream in;
|
||||||
private OutputStream out;
|
private OutputStream out;
|
||||||
@ -52,35 +55,43 @@ public class ConsoleShellFactory implements ShellFactory {
|
|||||||
public ConsoleReader ConsoleReader;
|
public ConsoleReader ConsoleReader;
|
||||||
public SSHDCommandSender SshdCommandSender;
|
public SSHDCommandSender SshdCommandSender;
|
||||||
|
|
||||||
public InputStream getIn() {
|
public InputStream getIn()
|
||||||
|
{
|
||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OutputStream getOut() {
|
public OutputStream getOut()
|
||||||
|
{
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OutputStream getErr() {
|
public OutputStream getErr()
|
||||||
|
{
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Environment getEnvironment() {
|
public Environment getEnvironment()
|
||||||
|
{
|
||||||
return environment;
|
return environment;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setInputStream(InputStream in) {
|
public void setInputStream(InputStream in)
|
||||||
|
{
|
||||||
this.in = in;
|
this.in = in;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOutputStream(OutputStream out) {
|
public void setOutputStream(OutputStream out)
|
||||||
|
{
|
||||||
this.out = out;
|
this.out = out;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setErrorStream(OutputStream err) {
|
public void setErrorStream(OutputStream err)
|
||||||
|
{
|
||||||
this.err = err;
|
this.err = err;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setExitCallback(ExitCallback callback) {
|
public void setExitCallback(ExitCallback callback)
|
||||||
|
{
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,10 +140,10 @@ class Cryptography
|
|||||||
private static byte[] FromHex(String hex) throws NoSuchAlgorithmException
|
private static byte[] FromHex(String hex) throws NoSuchAlgorithmException
|
||||||
{
|
{
|
||||||
byte[] bytes = new byte[hex.length() / 2];
|
byte[] bytes = new byte[hex.length() / 2];
|
||||||
|
|
||||||
for (int i = 0; i < bytes.length; i++)
|
for (int i = 0; i < bytes.length; i++)
|
||||||
{
|
|
||||||
bytes[i] = (byte)Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
|
bytes[i] = (byte)Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
|
||||||
}
|
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,6 +152,7 @@ class Cryptography
|
|||||||
BigInteger bi = new BigInteger(1, array);
|
BigInteger bi = new BigInteger(1, array);
|
||||||
String hex = bi.toString(16);
|
String hex = bi.toString(16);
|
||||||
int paddingLength = (array.length * 2) - hex.length();
|
int paddingLength = (array.length * 2) - hex.length();
|
||||||
|
|
||||||
if (paddingLength > 0)
|
if (paddingLength > 0)
|
||||||
return String.format("%0" + paddingLength + "d", 0) + hex;
|
return String.format("%0" + paddingLength + "d", 0) + hex;
|
||||||
else
|
else
|
||||||
|
@ -10,39 +10,51 @@ import java.util.logging.*;
|
|||||||
/**
|
/**
|
||||||
* Copyright 2013 Ryan Michela
|
* Copyright 2013 Ryan Michela
|
||||||
*/
|
*/
|
||||||
public class FlushyStreamHandler extends StreamHandler {
|
public class FlushyStreamHandler extends StreamHandler
|
||||||
|
{
|
||||||
private ConsoleReader reader;
|
private ConsoleReader reader;
|
||||||
|
|
||||||
public FlushyStreamHandler(OutputStream out, Formatter formatter, ConsoleReader reader) {
|
public FlushyStreamHandler(OutputStream out, Formatter formatter, ConsoleReader reader)
|
||||||
|
{
|
||||||
super(out, formatter);
|
super(out, formatter);
|
||||||
this.reader = reader;
|
this.reader = reader;
|
||||||
setLevel(Level.INFO);
|
setLevel(Level.INFO);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void publish(LogRecord record) {
|
public synchronized void publish(LogRecord record)
|
||||||
|
{
|
||||||
record.setMessage(record.getMessage().replace("\n", "\n\r"));
|
record.setMessage(record.getMessage().replace("\n", "\n\r"));
|
||||||
super.publish(record);
|
super.publish(record);
|
||||||
flush();
|
flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void flush() {
|
public synchronized void flush()
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
reader.print(ConsoleReader.RESET_LINE + "");
|
reader.print(ConsoleReader.RESET_LINE + "");
|
||||||
reader.flush();
|
reader.flush();
|
||||||
super.flush();
|
super.flush();
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
reader.drawLine();
|
reader.drawLine();
|
||||||
} catch (Throwable ex) {
|
}
|
||||||
|
catch (Throwable ex)
|
||||||
|
{
|
||||||
reader.getCursorBuffer().clear();
|
reader.getCursorBuffer().clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
reader.flush();
|
reader.flush();
|
||||||
super.flush();
|
super.flush();
|
||||||
} catch (SshException ex) {
|
}
|
||||||
|
catch (SshException ex)
|
||||||
|
{
|
||||||
// do nothing
|
// do nothing
|
||||||
} catch (IOException ex) {
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
Logger.getLogger(FlushyStreamHandler.class.getName()).log(Level.SEVERE, null, ex);
|
Logger.getLogger(FlushyStreamHandler.class.getName()).log(Level.SEVERE, null, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,16 +9,20 @@ import java.lang.reflect.Modifier;
|
|||||||
*/
|
*/
|
||||||
public class ReflectionUtil {
|
public class ReflectionUtil {
|
||||||
|
|
||||||
public static void setProtectedValue(Object o, String field, Object newValue) {
|
public static void setProtectedValue(Object o, String field, Object newValue)
|
||||||
|
{
|
||||||
setProtectedValue(o.getClass(), o, field, newValue);
|
setProtectedValue(o.getClass(), o, field, newValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setProtectedValue(Class c, String field, Object newValue) {
|
public static void setProtectedValue(Class c, String field, Object newValue)
|
||||||
|
{
|
||||||
setProtectedValue(c, null, field, newValue);
|
setProtectedValue(c, null, field, newValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setProtectedValue(Class c, Object o, String field, Object newValue) {
|
public static void setProtectedValue(Class c, Object o, String field, Object newValue)
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
|
||||||
Field f = c.getDeclaredField(field);
|
Field f = c.getDeclaredField(field);
|
||||||
|
|
||||||
@ -29,18 +33,25 @@ public class ReflectionUtil {
|
|||||||
modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
|
modifiersField.setInt(f, f.getModifiers() & ~Modifier.FINAL);
|
||||||
|
|
||||||
f.set(o, newValue);
|
f.set(o, newValue);
|
||||||
} catch (NoSuchFieldException | IllegalAccessException ex) {
|
}
|
||||||
|
catch (NoSuchFieldException | IllegalAccessException ex)
|
||||||
|
{
|
||||||
System.out.println("*** " + c.getName() + ":" + ex);
|
System.out.println("*** " + c.getName() + ":" + ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> T getProtectedValue(Object obj, String fieldName) {
|
public static <T> T getProtectedValue(Object obj, String fieldName)
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
Class c = obj.getClass();
|
Class c = obj.getClass();
|
||||||
while (c != Object.class) {
|
while (c != Object.class)
|
||||||
|
{
|
||||||
Field[] fields = c.getDeclaredFields();
|
Field[] fields = c.getDeclaredFields();
|
||||||
for (Field f : fields) {
|
for (Field f : fields)
|
||||||
if (f.getName() == fieldName) {
|
{
|
||||||
|
if (f.getName() == fieldName)
|
||||||
|
{
|
||||||
f.setAccessible(true);
|
f.setAccessible(true);
|
||||||
return (T) f.get(obj);
|
return (T) f.get(obj);
|
||||||
}
|
}
|
||||||
@ -49,46 +60,58 @@ public class ReflectionUtil {
|
|||||||
}
|
}
|
||||||
System.out.println("*** " + obj.getClass().getName() + ":No such field");
|
System.out.println("*** " + obj.getClass().getName() + ":No such field");
|
||||||
return null;
|
return null;
|
||||||
} catch (Exception ex) {
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
System.out.println("*** " + obj.getClass().getName() + ":" + ex);
|
System.out.println("*** " + obj.getClass().getName() + ":" + ex);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> T getProtectedValue(Class c, String field) {
|
public static <T> T getProtectedValue(Class c, String field)
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
Field f = c.getDeclaredField(field);
|
Field f = c.getDeclaredField(field);
|
||||||
f.setAccessible(true);
|
f.setAccessible(true);
|
||||||
return (T) f.get(c);
|
return (T) f.get(c);
|
||||||
} catch (Exception ex) {
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
System.out.println("*** " + c.getName() + ":" + ex);
|
System.out.println("*** " + c.getName() + ":" + ex);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object invokeProtectedMethod(Class c, String method, Object... args) {
|
public static Object invokeProtectedMethod(Class c, String method, Object... args)
|
||||||
|
{
|
||||||
return invokeProtectedMethod(c, null, method, args);
|
return invokeProtectedMethod(c, null, method, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object invokeProtectedMethod(Object o, String method, Object... args) {
|
public static Object invokeProtectedMethod(Object o, String method, Object... args)
|
||||||
|
{
|
||||||
return invokeProtectedMethod(o.getClass(), o, method, args);
|
return invokeProtectedMethod(o.getClass(), o, method, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object invokeProtectedMethod(Class c, Object o, String method, Object... args) {
|
public static Object invokeProtectedMethod(Class c, Object o, String method, Object... args)
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
Class[] pTypes = new Class[args.length];
|
Class[] pTypes = new Class[args.length];
|
||||||
for (int i = 0; i < args.length; i++) {
|
for (int i = 0; i < args.length; i++)
|
||||||
if (args[i] instanceof Integer) {
|
{
|
||||||
|
if (args[i] instanceof Integer)
|
||||||
pTypes[i] = int.class;
|
pTypes[i] = int.class;
|
||||||
} else {
|
else
|
||||||
pTypes[i] = args[i].getClass();
|
pTypes[i] = args[i].getClass();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Method m = c.getDeclaredMethod(method, pTypes);
|
Method m = c.getDeclaredMethod(method, pTypes);
|
||||||
m.setAccessible(true);
|
m.setAccessible(true);
|
||||||
return m.invoke(o, args);
|
return m.invoke(o, args);
|
||||||
} catch (Exception ex) {
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
System.out.println("*** " + c.getName() + "." + method + "(): " + ex);
|
System.out.println("*** " + c.getName() + "." + method + "(): " + ex);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -5,14 +5,16 @@ import jline.TerminalSupport;
|
|||||||
/**
|
/**
|
||||||
* Copyright 2013 Ryan Michela
|
* Copyright 2013 Ryan Michela
|
||||||
*/
|
*/
|
||||||
public class SshTerminal extends TerminalSupport {
|
public class SshTerminal extends TerminalSupport
|
||||||
|
{
|
||||||
protected SshTerminal() {
|
protected SshTerminal()
|
||||||
|
{
|
||||||
super(true);
|
super(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() throws Exception {
|
public void init() throws Exception
|
||||||
|
{
|
||||||
setAnsiSupported(true);
|
setAnsiSupported(true);
|
||||||
setEchoEnabled(true);
|
setEchoEnabled(true);
|
||||||
}
|
}
|
||||||
|
@ -13,80 +13,88 @@ import java.util.logging.StreamHandler;
|
|||||||
/**
|
/**
|
||||||
* Copyright 2014 Ryan Michela
|
* Copyright 2014 Ryan Michela
|
||||||
*/
|
*/
|
||||||
public class StreamHandlerAppender implements Appender {
|
public class StreamHandlerAppender implements Appender
|
||||||
|
{
|
||||||
|
|
||||||
private StreamHandler streamHandler;
|
private StreamHandler streamHandler;
|
||||||
private UUID uuid;
|
private UUID uuid;
|
||||||
|
|
||||||
public StreamHandlerAppender(StreamHandler streamHandler) {
|
public StreamHandlerAppender(StreamHandler streamHandler)
|
||||||
|
{
|
||||||
this.streamHandler = streamHandler;
|
this.streamHandler = streamHandler;
|
||||||
uuid = UUID.randomUUID();
|
uuid = UUID.randomUUID();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void append(LogEvent logEvent) {
|
public void append(LogEvent logEvent)
|
||||||
|
{
|
||||||
java.util.logging.Level level;
|
java.util.logging.Level level;
|
||||||
|
|
||||||
if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.DEBUG)) {
|
if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.DEBUG))
|
||||||
level = java.util.logging.Level.FINE;
|
level = java.util.logging.Level.FINE;
|
||||||
} else if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.INFO)) {
|
else if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.INFO))
|
||||||
level = java.util.logging.Level.INFO;
|
level = java.util.logging.Level.INFO;
|
||||||
} else if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.WARN)) {
|
else if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.WARN))
|
||||||
level = java.util.logging.Level.WARNING;
|
level = java.util.logging.Level.WARNING;
|
||||||
} else if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.ERROR)) {
|
else if (logEvent.getLevel().equals(org.apache.logging.log4j.Level.ERROR))
|
||||||
level = java.util.logging.Level.SEVERE;
|
level = java.util.logging.Level.SEVERE;
|
||||||
} else {
|
else
|
||||||
level = java.util.logging.Level.INFO;
|
level = java.util.logging.Level.INFO;
|
||||||
}
|
|
||||||
|
|
||||||
String message = logEvent.getMessage().getFormattedMessage();
|
String message = logEvent.getMessage().getFormattedMessage();
|
||||||
|
|
||||||
|
|
||||||
LogRecord logRecord = new LogRecord(level, message);
|
LogRecord logRecord = new LogRecord(level, message);
|
||||||
streamHandler.publish(logRecord);
|
streamHandler.publish(logRecord);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName()
|
||||||
|
{
|
||||||
return "StreamHandlerAppender:" + uuid.toString();
|
return "StreamHandlerAppender:" + uuid.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Layout<? extends Serializable> getLayout() {
|
public Layout<? extends Serializable> getLayout()
|
||||||
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean ignoreExceptions() {
|
public boolean ignoreExceptions()
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ErrorHandler getHandler() {
|
public ErrorHandler getHandler()
|
||||||
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setHandler(ErrorHandler errorHandler) {
|
public void setHandler(ErrorHandler errorHandler)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isStarted() {
|
public boolean isStarted()
|
||||||
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isStopped() {
|
public boolean isStopped()
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,11 @@ import java.util.concurrent.ExecutionException;
|
|||||||
/**
|
/**
|
||||||
* Copyright 2013 Ryan Michela
|
* Copyright 2013 Ryan Michela
|
||||||
*/
|
*/
|
||||||
public abstract class Waitable<T> implements Runnable {
|
public abstract class Waitable<T> implements Runnable
|
||||||
|
{
|
||||||
|
|
||||||
private enum Status {
|
private enum Status
|
||||||
|
{
|
||||||
WAITING,
|
WAITING,
|
||||||
RUNNING,
|
RUNNING,
|
||||||
FINISHED,
|
FINISHED,
|
||||||
@ -17,19 +19,28 @@ public abstract class Waitable<T> implements Runnable {
|
|||||||
T value = null;
|
T value = null;
|
||||||
Status status = Status.WAITING;
|
Status status = Status.WAITING;
|
||||||
|
|
||||||
public final void run() {
|
public final void run()
|
||||||
synchronized (this) {
|
{
|
||||||
if (status != Status.WAITING) {
|
synchronized (this)
|
||||||
|
{
|
||||||
|
if (status != Status.WAITING)
|
||||||
throw new IllegalStateException("Invalid state " + status);
|
throw new IllegalStateException("Invalid state " + status);
|
||||||
}
|
|
||||||
status = Status.RUNNING;
|
status = Status.RUNNING;
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
|
try
|
||||||
|
{
|
||||||
value = evaluate();
|
value = evaluate();
|
||||||
} catch (Throwable t) {
|
}
|
||||||
|
catch (Throwable t)
|
||||||
|
{
|
||||||
this.t = t;
|
this.t = t;
|
||||||
} finally {
|
}
|
||||||
synchronized (this) {
|
finally
|
||||||
|
{
|
||||||
|
synchronized (this)
|
||||||
|
{
|
||||||
status = Status.FINISHED;
|
status = Status.FINISHED;
|
||||||
this.notifyAll();
|
this.notifyAll();
|
||||||
}
|
}
|
||||||
@ -38,13 +49,14 @@ public abstract class Waitable<T> implements Runnable {
|
|||||||
|
|
||||||
protected abstract T evaluate();
|
protected abstract T evaluate();
|
||||||
|
|
||||||
public synchronized T get() throws InterruptedException, ExecutionException {
|
public synchronized T get() throws InterruptedException, ExecutionException
|
||||||
while (status != Status.FINISHED) {
|
{
|
||||||
|
while (status != Status.FINISHED)
|
||||||
this.wait();
|
this.wait();
|
||||||
}
|
|
||||||
if (t != null) {
|
if (t != null)
|
||||||
throw new ExecutionException(t);
|
throw new ExecutionException(t);
|
||||||
}
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,91 +61,113 @@ public class SSHDCommandSender implements ConsoleCommandSender, CommandSender
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendMessage(String[] messages) {
|
public void sendMessage(String[] messages)
|
||||||
|
{
|
||||||
Arrays.asList(messages).forEach(this::sendMessage);
|
Arrays.asList(messages).forEach(this::sendMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName()
|
||||||
|
{
|
||||||
return "SSHD Console";
|
return "SSHD Console";
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isOp() {
|
public boolean isOp()
|
||||||
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOp(boolean value) {
|
public void setOp(boolean value)
|
||||||
|
{
|
||||||
throw new UnsupportedOperationException("Cannot change operator status of server console");
|
throw new UnsupportedOperationException("Cannot change operator status of server console");
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean beginConversation(Conversation conversation) {
|
public boolean beginConversation(Conversation conversation)
|
||||||
|
{
|
||||||
return this.conversationTracker.beginConversation(conversation);
|
return this.conversationTracker.beginConversation(conversation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void abandonConversation(Conversation conversation) {
|
public void abandonConversation(Conversation conversation)
|
||||||
|
{
|
||||||
this.conversationTracker.abandonConversation(conversation, new ConversationAbandonedEvent(conversation, new ManuallyAbandonedConversationCanceller()));
|
this.conversationTracker.abandonConversation(conversation, new ConversationAbandonedEvent(conversation, new ManuallyAbandonedConversationCanceller()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void abandonConversation(Conversation conversation, ConversationAbandonedEvent details) {
|
public void abandonConversation(Conversation conversation, ConversationAbandonedEvent details)
|
||||||
|
{
|
||||||
this.conversationTracker.abandonConversation(conversation, details);
|
this.conversationTracker.abandonConversation(conversation, details);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void acceptConversationInput(String input) {
|
public void acceptConversationInput(String input)
|
||||||
|
{
|
||||||
this.conversationTracker.acceptConversationInput(input);
|
this.conversationTracker.acceptConversationInput(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isConversing() {
|
public boolean isConversing()
|
||||||
|
{
|
||||||
return this.conversationTracker.isConversing();
|
return this.conversationTracker.isConversing();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPermissionSet(String name) {
|
public boolean isPermissionSet(String name)
|
||||||
|
{
|
||||||
return this.perm.isPermissionSet(name);
|
return this.perm.isPermissionSet(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPermissionSet(Permission perm) {
|
public boolean isPermissionSet(Permission perm)
|
||||||
|
{
|
||||||
return this.perm.isPermissionSet(perm);
|
return this.perm.isPermissionSet(perm);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasPermission(String name) {
|
public boolean hasPermission(String name)
|
||||||
|
{
|
||||||
return this.perm.hasPermission(name);
|
return this.perm.hasPermission(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasPermission(Permission perm) {
|
public boolean hasPermission(Permission perm)
|
||||||
|
{
|
||||||
return this.perm.hasPermission(perm);
|
return this.perm.hasPermission(perm);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value) {
|
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value)
|
||||||
|
{
|
||||||
return this.perm.addAttachment(plugin, name, value);
|
return this.perm.addAttachment(plugin, name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PermissionAttachment addAttachment(Plugin plugin) {
|
public PermissionAttachment addAttachment(Plugin plugin)
|
||||||
|
{
|
||||||
return this.perm.addAttachment(plugin);
|
return this.perm.addAttachment(plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value, int ticks) {
|
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value, int ticks)
|
||||||
|
{
|
||||||
return this.perm.addAttachment(plugin, name, value, ticks);
|
return this.perm.addAttachment(plugin, name, value, ticks);
|
||||||
}
|
}
|
||||||
|
|
||||||
public PermissionAttachment addAttachment(Plugin plugin, int ticks) {
|
public PermissionAttachment addAttachment(Plugin plugin, int ticks)
|
||||||
|
{
|
||||||
return this.perm.addAttachment(plugin, ticks);
|
return this.perm.addAttachment(plugin, ticks);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeAttachment(PermissionAttachment attachment) {
|
public void removeAttachment(PermissionAttachment attachment)
|
||||||
|
{
|
||||||
this.perm.removeAttachment(attachment);
|
this.perm.removeAttachment(attachment);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void recalculatePermissions() {
|
public void recalculatePermissions()
|
||||||
|
{
|
||||||
this.perm.recalculatePermissions();
|
this.perm.recalculatePermissions();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
|
public Set<PermissionAttachmentInfo> getEffectivePermissions()
|
||||||
|
{
|
||||||
return this.perm.getEffectivePermissions();
|
return this.perm.getEffectivePermissions();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPlayer() {
|
public boolean isPlayer()
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Server getServer() {
|
public Server getServer()
|
||||||
|
{
|
||||||
return Bukkit.getServer();
|
return Bukkit.getServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,13 +8,17 @@ import org.bukkit.conversations.ManuallyAbandonedConversationCanceller;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public class SSHDConversationTracker {
|
public class SSHDConversationTracker
|
||||||
|
{
|
||||||
private LinkedList<Conversation> conversationQueue = new LinkedList<>();
|
private LinkedList<Conversation> conversationQueue = new LinkedList<>();
|
||||||
|
|
||||||
synchronized boolean beginConversation(Conversation conversation) {
|
synchronized boolean beginConversation(Conversation conversation)
|
||||||
if (!this.conversationQueue.contains(conversation)) {
|
{
|
||||||
|
if (!this.conversationQueue.contains(conversation))
|
||||||
|
{
|
||||||
this.conversationQueue.addLast(conversation);
|
this.conversationQueue.addLast(conversation);
|
||||||
if (this.conversationQueue.getFirst() == conversation) {
|
if (this.conversationQueue.getFirst() == conversation)
|
||||||
|
{
|
||||||
conversation.begin();
|
conversation.begin();
|
||||||
conversation.outputNextPrompt();
|
conversation.outputNextPrompt();
|
||||||
return true;
|
return true;
|
||||||
@ -24,55 +28,66 @@ public class SSHDConversationTracker {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized void abandonConversation(Conversation conversation, ConversationAbandonedEvent details) {
|
synchronized void abandonConversation(Conversation conversation, ConversationAbandonedEvent details)
|
||||||
if (!this.conversationQueue.isEmpty()) {
|
{
|
||||||
if (this.conversationQueue.getFirst() == conversation) {
|
if (!this.conversationQueue.isEmpty())
|
||||||
|
{
|
||||||
|
if (this.conversationQueue.getFirst() == conversation)
|
||||||
conversation.abandon(details);
|
conversation.abandon(details);
|
||||||
}
|
|
||||||
|
|
||||||
if (this.conversationQueue.contains(conversation)) {
|
if (this.conversationQueue.contains(conversation))
|
||||||
this.conversationQueue.remove(conversation);
|
this.conversationQueue.remove(conversation);
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.conversationQueue.isEmpty()) {
|
if (!this.conversationQueue.isEmpty())
|
||||||
this.conversationQueue.getFirst().outputNextPrompt();
|
this.conversationQueue.getFirst().outputNextPrompt();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void abandonAllConversations() {
|
public synchronized void abandonAllConversations()
|
||||||
|
{
|
||||||
LinkedList<Conversation> oldQueue = this.conversationQueue;
|
LinkedList<Conversation> oldQueue = this.conversationQueue;
|
||||||
this.conversationQueue = new LinkedList<>();
|
this.conversationQueue = new LinkedList<>();
|
||||||
|
|
||||||
for (Conversation conversation : oldQueue) {
|
for (Conversation conversation : oldQueue)
|
||||||
try {
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
conversation.abandon(new ConversationAbandonedEvent(conversation, new ManuallyAbandonedConversationCanceller()));
|
conversation.abandon(new ConversationAbandonedEvent(conversation, new ManuallyAbandonedConversationCanceller()));
|
||||||
} catch (Throwable var5) {
|
}
|
||||||
|
catch (Throwable var5)
|
||||||
|
{
|
||||||
Bukkit.getLogger().log(Level.SEVERE, "Unexpected exception while abandoning a conversation", var5);
|
Bukkit.getLogger().log(Level.SEVERE, "Unexpected exception while abandoning a conversation", var5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized void acceptConversationInput(String input) {
|
synchronized void acceptConversationInput(String input)
|
||||||
if (this.isConversing()) {
|
{
|
||||||
|
if (this.isConversing())
|
||||||
|
{
|
||||||
Conversation conversation = this.conversationQueue.getFirst();
|
Conversation conversation = this.conversationQueue.getFirst();
|
||||||
|
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
conversation.acceptInput(input);
|
conversation.acceptInput(input);
|
||||||
} catch (Throwable var4) {
|
}
|
||||||
|
catch (Throwable var4)
|
||||||
|
{
|
||||||
conversation.getContext().getPlugin().getLogger().log(Level.WARNING, String.format("Plugin %s generated an exception whilst handling conversation input", conversation.getContext().getPlugin().getDescription().getFullName()), var4);
|
conversation.getContext().getPlugin().getLogger().log(Level.WARNING, String.format("Plugin %s generated an exception whilst handling conversation input", conversation.getContext().getPlugin().getDescription().getFullName()), var4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized boolean isConversing() {
|
synchronized boolean isConversing()
|
||||||
|
{
|
||||||
return !this.conversationQueue.isEmpty();
|
return !this.conversationQueue.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized boolean isConversingModaly() {
|
public synchronized boolean isConversingModaly()
|
||||||
|
{
|
||||||
return this.isConversing() && this.conversationQueue.getFirst().isModal();
|
return this.isConversing() && this.conversationQueue.getFirst().isModal();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user