Skip to content

Commit

Permalink
feat(android): secure Ti.Network.Socket (#13774)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Barber <chris@cb1inc.com>
  • Loading branch information
m1ga and cb1kenobi committed May 20, 2023
1 parent ffebf85 commit 0f5743d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiStreamHelper;

import javax.net.ssl.SSLSocketFactory;

import ti.modules.titanium.BufferProxy;

@Kroll.proxy(creatableInModule = SocketModule.class)
Expand All @@ -35,6 +37,7 @@ public class TCPProxy extends KrollProxy implements TiStream
private KrollDict acceptOptions = null;
private int state = 0;

private boolean secure = false;
public TCPProxy()
{
super();
Expand All @@ -47,6 +50,7 @@ public void connect() throws Exception
if ((state != SocketModule.LISTENING) && (state != SocketModule.CONNECTED)) {
Object host = getProperty("host");
Object port = getProperty("port");
secure = TiConvert.toBoolean(getProperty("secure"), false);
if ((host != null) && (port != null) && (TiConvert.toInt(port) > 0)) {
new ConnectedSocketThread().start();

Expand Down Expand Up @@ -191,17 +195,25 @@ public void run()
{
String host = TiConvert.toString(getProperty("host"));
Object timeoutProperty = getProperty("timeout");
secure = TiConvert.toBoolean(getProperty("secure"), false);

try {
if (timeoutProperty != null) {
int timeout = TiConvert.toInt(timeoutProperty, 0);

if (secure) {
SSLSocketFactory s = (SSLSocketFactory) SSLSocketFactory.getDefault();
clientSocket = s.createSocket();
} else {
clientSocket = new Socket();
clientSocket.connect(new InetSocketAddress(host, TiConvert.toInt(getProperty("port"))), timeout);
}

InetSocketAddress endpoint = new InetSocketAddress(host, TiConvert.toInt(getProperty("port")));

if (timeoutProperty != null) {
int timeout = TiConvert.toInt(timeoutProperty, 0);
clientSocket.connect(endpoint, timeout);
} else {
clientSocket = new Socket(host, TiConvert.toInt(getProperty("port")));
clientSocket.connect(endpoint);
}

updateState(SocketModule.CONNECTED, "connected", buildConnectedCallbackArgs());

} catch (UnknownHostException e) {
Expand Down
7 changes: 7 additions & 0 deletions apidoc/Titanium/Network/Socket/TCP.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ properties:
description: |
Can only be modified when this socket is in the [INITIALIZED](Titanium.Network.Socket.INITIALIZED) state.
- name: secure
type: Boolean
summary: Creates a secure socket.
since: "12.1.0"
default: false
platforms: [android]

- name: listenQueueSize
type: Number
summary: Max number of pending incoming connections to be allowed when the socket is
Expand Down

0 comments on commit 0f5743d

Please sign in to comment.