Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/SftpPlugin/SimpleSftpServer.java
Show First 20 Lines • Show All 72 Lines • ▼ Show 20 Line(s) | 65 | class SimpleSftpServer { | |||
---|---|---|---|---|---|
73 | 73 | | |||
74 | private final SimplePasswordAuthenticator passwordAuth = new SimplePasswordAuthenticator(); | 74 | private final SimplePasswordAuthenticator passwordAuth = new SimplePasswordAuthenticator(); | ||
75 | private final SimplePublicKeyAuthenticator keyAuth = new SimplePublicKeyAuthenticator(); | 75 | private final SimplePublicKeyAuthenticator keyAuth = new SimplePublicKeyAuthenticator(); | ||
76 | 76 | | |||
77 | static { | 77 | static { | ||
78 | Security.insertProviderAt(SslHelper.BC, 1); | 78 | Security.insertProviderAt(SslHelper.BC, 1); | ||
79 | SecurityUtils.setRegisterBouncyCastle(false); | 79 | SecurityUtils.setRegisterBouncyCastle(false); | ||
80 | } | 80 | } | ||
81 | | ||||
81 | private final SshServer sshd = SshServer.setUpDefaultServer(); | 82 | private final SshServer sshd = SshServer.setUpDefaultServer(); | ||
82 | 83 | | |||
83 | public void init(Context context, Device device) { | 84 | public void init(Context context, Device device) { | ||
84 | 85 | | |||
85 | sshd.setKeyExchangeFactories(Arrays.asList( | 86 | sshd.setKeyExchangeFactories(Arrays.asList( | ||
86 | new DHG14.Factory(), | 87 | new DHG14.Factory(), | ||
87 | new DHG1.Factory())); | 88 | new DHG1.Factory())); | ||
88 | 89 | | |||
89 | sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(context.getFilesDir() + "/sftpd.ser")); | 90 | sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(context.getFilesDir() + "/sftpd.ser")); | ||
90 | 91 | | |||
91 | sshd.setFileSystemFactory(new AndroidFileSystemFactory(context)); | 92 | sshd.setFileSystemFactory(new AndroidFileSystemFactory(context)); | ||
92 | sshd.setCommandFactory(new ScpCommandFactory()); | 93 | sshd.setCommandFactory(new ScpCommandFactory()); | ||
93 | sshd.setSubsystemFactories(Collections.singletonList((NamedFactory<Command>)new SftpSubsystem.Factory())); | 94 | sshd.setSubsystemFactories(Collections.singletonList((NamedFactory<Command>) new SftpSubsystem.Factory())); | ||
94 | 95 | | |||
95 | if (device.publicKey != null) { | 96 | if (device.publicKey != null) { | ||
96 | keyAuth.deviceKey = device.publicKey; | 97 | keyAuth.deviceKey = device.publicKey; | ||
97 | sshd.setPublickeyAuthenticator(keyAuth); | 98 | sshd.setPublickeyAuthenticator(keyAuth); | ||
98 | } | 99 | } | ||
99 | sshd.setPasswordAuthenticator(passwordAuth); | 100 | sshd.setPasswordAuthenticator(passwordAuth); | ||
100 | } | 101 | } | ||
101 | 102 | | |||
102 | public boolean start() { | 103 | public boolean start() { | ||
103 | if (!started) { | 104 | if (!started) { | ||
104 | 105 | | |||
105 | passwordAuth.password = RandomHelper.randomString(28); | 106 | passwordAuth.password = RandomHelper.randomString(28); | ||
106 | 107 | | |||
107 | port = STARTPORT; | 108 | port = STARTPORT; | ||
108 | while(!started) { | 109 | while (!started) { | ||
109 | try { | 110 | try { | ||
110 | sshd.setPort(port); | 111 | sshd.setPort(port); | ||
111 | sshd.start(); | 112 | sshd.start(); | ||
112 | started = true; | 113 | started = true; | ||
113 | } catch(Exception e) { | 114 | } catch (Exception e) { | ||
114 | e.printStackTrace(); | 115 | e.printStackTrace(); | ||
115 | port++; | 116 | port++; | ||
116 | if (port >= ENDPORT) { | 117 | if (port >= ENDPORT) { | ||
117 | port = -1; | 118 | port = -1; | ||
118 | Log.e("SftpServer", "No more ports available"); | 119 | Log.e("SftpServer", "No more ports available"); | ||
119 | return false; | 120 | return false; | ||
120 | } | 121 | } | ||
121 | } | 122 | } | ||
Show All 18 Lines | |||||
140 | 141 | | |||
141 | public int getPort() { | 142 | public int getPort() { | ||
142 | return port; | 143 | return port; | ||
143 | } | 144 | } | ||
144 | 145 | | |||
145 | public String getLocalIpAddress() { | 146 | public String getLocalIpAddress() { | ||
146 | String ip6 = null; | 147 | String ip6 = null; | ||
147 | try { | 148 | try { | ||
148 | for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { | 149 | for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { | ||
149 | NetworkInterface intf = en.nextElement(); | 150 | NetworkInterface intf = en.nextElement(); | ||
150 | 151 | | |||
151 | // Anything with rmnet is related to cellular connections or USB | 152 | // Anything with rmnet is related to cellular connections or USB | ||
152 | // tethering mechanisms. See: | 153 | // tethering mechanisms. See: | ||
153 | // | 154 | // | ||
154 | // https://android.googlesource.com/kernel/msm/+/android-msm-flo-3.4-kitkat-mr1/Documentation/usb/gadget_rmnet.txt | 155 | // https://android.googlesource.com/kernel/msm/+/android-msm-flo-3.4-kitkat-mr1/Documentation/usb/gadget_rmnet.txt | ||
155 | // | 156 | // | ||
156 | // If we run across an interface that has this, we can safely | 157 | // If we run across an interface that has this, we can safely | ||
157 | // ignore it. In fact, it's much safer to do. If we don't, we | 158 | // ignore it. In fact, it's much safer to do. If we don't, we | ||
158 | // might get invalid IP adddresses out of it. | 159 | // might get invalid IP adddresses out of it. | ||
159 | if(intf.getDisplayName().contains("rmnet")) continue; | 160 | if (intf.getDisplayName().contains("rmnet")) continue; | ||
160 | 161 | | |||
161 | for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { | 162 | for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { | ||
162 | InetAddress inetAddress = enumIpAddr.nextElement(); | 163 | InetAddress inetAddress = enumIpAddr.nextElement(); | ||
163 | if (!inetAddress.isLoopbackAddress()) { | 164 | if (!inetAddress.isLoopbackAddress()) { | ||
164 | String address = inetAddress.getHostAddress(); | 165 | String address = inetAddress.getHostAddress(); | ||
165 | if(inetAddress instanceof Inet4Address) { //Prefer IPv4 over IPv6, because sshfs doesn't seem to like IPv6 | 166 | if (inetAddress instanceof Inet4Address) { //Prefer IPv4 over IPv6, because sshfs doesn't seem to like IPv6 | ||
166 | return address; | 167 | return address; | ||
167 | } else { | 168 | } else { | ||
168 | ip6 = address; | 169 | ip6 = address; | ||
169 | } | 170 | } | ||
170 | } | 171 | } | ||
171 | } | 172 | } | ||
172 | } | 173 | } | ||
173 | } catch (SocketException ex) { | 174 | } catch (SocketException ex) { | ||
▲ Show 20 Lines • Show All 118 Lines • Show Last 20 Lines |