mirror of
https://github.com/moparisthebest/Yaaic
synced 2024-11-26 10:52:16 -05:00
Merge branch 'ssl_support'
This commit is contained in:
commit
512ddacaf5
@ -88,8 +88,7 @@ along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
|
||||
android:id="@+id/useSSL"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/server_useSSL"
|
||||
android:visibility="gone" />
|
||||
android:text="@string/server_useSSL" />
|
||||
<TextView
|
||||
android:text="User"
|
||||
android:layout_width="fill_parent"
|
||||
|
@ -29,6 +29,13 @@ import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
import org.yaaic.ssl.NaiveTrustManager;
|
||||
|
||||
/**
|
||||
* PircBot is a Java framework for writing IRC bots quickly and easily.
|
||||
* <p>
|
||||
@ -143,7 +150,26 @@ public abstract class PircBot implements ReplyConstants {
|
||||
this.removeAllChannels();
|
||||
|
||||
// Connect to the server.
|
||||
Socket socket = new Socket(hostname, port);
|
||||
|
||||
// XXX: PircBot Patch for SSL
|
||||
Socket socket;
|
||||
if (_useSSL) {
|
||||
try {
|
||||
SSLContext context = SSLContext.getInstance("TLS");
|
||||
context.init(null, new X509TrustManager[] { new NaiveTrustManager() }, null);
|
||||
SSLSocketFactory factory = context.getSocketFactory();
|
||||
SSLSocket ssocket = (SSLSocket) factory.createSocket(hostname, port);
|
||||
ssocket.startHandshake();
|
||||
socket = ssocket;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
// XXX: It's not really an IOException :)
|
||||
throw new IOException("Cannot open SSL socket");
|
||||
}
|
||||
} else {
|
||||
socket = new Socket(hostname, port);
|
||||
}
|
||||
|
||||
_inetAddress = socket.getLocalAddress();
|
||||
|
||||
@ -250,6 +276,17 @@ public abstract class PircBot implements ReplyConstants {
|
||||
}
|
||||
connect(getServer(), getPort(), getPassword());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set wether SSL should be used to connect to the server
|
||||
*
|
||||
* @author Sebastian Kaspari <sebastian@yaaic.org>
|
||||
*/
|
||||
public void setUseSSL(boolean useSSL)
|
||||
{
|
||||
_useSSL = useSSL;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@ -3013,6 +3050,8 @@ public abstract class PircBot implements ReplyConstants {
|
||||
|
||||
// Default settings for the PircBot.
|
||||
private boolean _autoNickChange = false;
|
||||
private boolean _useSSL = false;
|
||||
|
||||
private String _name = "PircBot";
|
||||
private String _nick = _name;
|
||||
private String _login = "PircBot";
|
||||
|
@ -30,6 +30,7 @@ import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.Toast;
|
||||
@ -87,6 +88,7 @@ public class AddServerActivity extends Activity implements OnClickListener
|
||||
((EditText) findViewById(R.id.nickname)).setText(server.getIdentity().getNickname());
|
||||
((EditText) findViewById(R.id.ident)).setText(server.getIdentity().getIdent());
|
||||
((EditText) findViewById(R.id.realname)).setText(server.getIdentity().getRealName());
|
||||
((CheckBox) findViewById(R.id.useSSL)).setChecked(server.useSSL());
|
||||
|
||||
((Button) findViewById(R.id.add)).setText("Save");
|
||||
|
||||
@ -161,7 +163,7 @@ public class AddServerActivity extends Activity implements OnClickListener
|
||||
server.getPort(),
|
||||
server.getPassword(),
|
||||
false, // auto connect
|
||||
false, // use ssl
|
||||
server.useSSL(),
|
||||
identityId,
|
||||
server.getCharset()
|
||||
);
|
||||
@ -192,7 +194,7 @@ public class AddServerActivity extends Activity implements OnClickListener
|
||||
server.getPort(),
|
||||
server.getPassword(),
|
||||
false, // auto connect
|
||||
false, // use ssl
|
||||
server.useSSL(),
|
||||
identityId,
|
||||
server.getCharset()
|
||||
);
|
||||
@ -225,10 +227,10 @@ public class AddServerActivity extends Activity implements OnClickListener
|
||||
int port = Integer.parseInt(((EditText) findViewById(R.id.port)).getText().toString());
|
||||
String password = ((EditText) findViewById(R.id.password)).getText().toString();
|
||||
String charset = ((Spinner) findViewById(R.id.charset)).getSelectedItem().toString();
|
||||
Boolean useSSL = ((CheckBox) findViewById(R.id.useSSL)).isChecked();
|
||||
|
||||
// not in use yet
|
||||
//boolean autoConnect = ((CheckBox) findViewById(R.id.autoconnect)).isChecked();
|
||||
//boolean useSSL = ((CheckBox) findViewById(R.id.useSSL)).isChecked();
|
||||
|
||||
Server server = new Server();
|
||||
server.setHost(host);
|
||||
@ -236,6 +238,7 @@ public class AddServerActivity extends Activity implements OnClickListener
|
||||
server.setPassword(password);
|
||||
server.setTitle(title);
|
||||
server.setCharset(charset);
|
||||
server.setUseSSL(useSSL);
|
||||
server.setStatus(Status.DISCONNECTED);
|
||||
|
||||
return server;
|
||||
|
@ -162,7 +162,8 @@ public class Database extends SQLiteOpenHelper
|
||||
|
||||
this.getWritableDatabase().update(
|
||||
ServerConstants.TABLE_NAME,
|
||||
values, ServerConstants._ID + " = " + serverId,
|
||||
values,
|
||||
ServerConstants._ID + " = " + serverId,
|
||||
null
|
||||
);
|
||||
}
|
||||
@ -280,6 +281,12 @@ public class Database extends SQLiteOpenHelper
|
||||
server.setPassword(cursor.getString(cursor.getColumnIndex(ServerConstants.PASSWORD)));
|
||||
server.setId(cursor.getInt(cursor.getColumnIndex((ServerConstants._ID))));
|
||||
server.setCharset(cursor.getString(cursor.getColumnIndex(ServerConstants.CHARSET)));
|
||||
|
||||
String useSSLvalue = cursor.getString(cursor.getColumnIndex(ServerConstants.USE_SSL));
|
||||
if (useSSLvalue != null && useSSLvalue.equals("1")) {
|
||||
server.setUseSSL(true);
|
||||
}
|
||||
|
||||
server.setStatus(Status.DISCONNECTED);
|
||||
|
||||
// Load identity for server
|
||||
|
@ -68,6 +68,7 @@ public class IRCBinder extends Binder
|
||||
connection.setNickname(server.getIdentity().getNickname());
|
||||
connection.setIdent(server.getIdentity().getIdent());
|
||||
connection.setRealName(server.getIdentity().getRealName());
|
||||
connection.setUseSSL(server.useSSL());
|
||||
|
||||
if (server.getCharset() != null) {
|
||||
connection.setEncoding(server.getCharset());
|
||||
|
@ -39,6 +39,7 @@ public class Server
|
||||
private int port;
|
||||
private String password;
|
||||
private String charset;
|
||||
private boolean useSSL = false;
|
||||
private Identity identity;
|
||||
|
||||
private LinkedHashMap<String, Conversation> conversations = new LinkedHashMap<String, Conversation>();
|
||||
@ -195,6 +196,24 @@ public class Server
|
||||
return charset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if this connections needs to use ssl
|
||||
*/
|
||||
public void setUseSSL(boolean useSSL)
|
||||
{
|
||||
this.useSSL = useSSL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does this connection use SSL?
|
||||
*
|
||||
* @return true if SSL should be used, false otherwise
|
||||
*/
|
||||
public boolean useSSL()
|
||||
{
|
||||
return useSSL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set connection status of server
|
||||
*
|
||||
|
62
src/org/yaaic/ssl/NaiveTrustManager.java
Normal file
62
src/org/yaaic/ssl/NaiveTrustManager.java
Normal file
@ -0,0 +1,62 @@
|
||||
/*
|
||||
Yaaic - Yet Another Android IRC Client
|
||||
|
||||
Copyright 2009-2010 Sebastian Kaspari
|
||||
|
||||
This file is part of Yaaic.
|
||||
|
||||
Yaaic is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Yaaic is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.yaaic.ssl;
|
||||
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
/**
|
||||
* Naive Trust Manager that accepts every certificate
|
||||
*
|
||||
* @author Sebastian Kaspari <sebastian@yaaic.org>
|
||||
*/
|
||||
public class NaiveTrustManager implements X509TrustManager
|
||||
{
|
||||
/**
|
||||
* Check client trusted
|
||||
*
|
||||
* @throws CertificateException if not trusted
|
||||
*/
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException
|
||||
{
|
||||
// No Exception == Trust
|
||||
}
|
||||
|
||||
/**
|
||||
* Check server trusted
|
||||
*
|
||||
* @throws CertificateException if not trusted
|
||||
*/
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException
|
||||
{
|
||||
// No Exception == Trust
|
||||
}
|
||||
|
||||
/**
|
||||
* Get accepted issuers
|
||||
*/
|
||||
public X509Certificate[] getAcceptedIssuers()
|
||||
{
|
||||
return new X509Certificate[0];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user