react-native-kookit 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,170 @@
1
+ # Android SMB库对比分析与推荐
2
+
3
+ ## 当前使用的库:SMBJ
4
+
5
+ ### 优点
6
+
7
+ - ✅ 轻量级,专注SMB2/3协议
8
+ - ✅ 现代化设计,良好的性能
9
+ - ✅ 活跃维护,最新版本0.13.0 (2023)
10
+ - ✅ 支持SMB2/3的高级功能
11
+
12
+ ### 缺点
13
+
14
+ - ❌ **不支持share枚举** - 这是我们当前的问题
15
+ - ❌ 没有NetShareEnum API
16
+ - ❌ 需要手动测试常见share名称
17
+
18
+ ## 备选方案1:JCIFS (CodeLibs版本)
19
+
20
+ ### 基本信息
21
+
22
+ ```gradle
23
+ implementation 'org.codelibs:jcifs:2.1.39'
24
+ ```
25
+
26
+ ### 优点
27
+
28
+ - ✅ **完整的SMB协议支持** (SMB1/2/3)
29
+ - ✅ **支持网络浏览** - 可能包括share枚举
30
+ - ✅ 成熟的库,有长期历史
31
+ - ✅ 支持Java 17+
32
+ - ✅ 现代化API设计
33
+
34
+ ### 缺点
35
+
36
+ - ❌ 体积较大
37
+ - ❌ 需要额外学习新API
38
+ - ❌ 依赖Bouncy Castle加密库
39
+
40
+ ### Share枚举支持
41
+
42
+ 基于文档分析,JCIFS应该支持类似这样的用法:
43
+
44
+ ```java
45
+ // 可能的API (需要验证)
46
+ SmbFile server = new SmbFile("smb://192.168.1.100/", context);
47
+ SmbFile[] shares = server.listFiles(); // 列出所有shares
48
+ ```
49
+
50
+ ## 备选方案2:JCIFS-NG (AgNO3版本)
51
+
52
+ ### 基本信息
53
+
54
+ ```gradle
55
+ implementation 'eu.agno3.jcifs:jcifs-ng:2.1.9'
56
+ ```
57
+
58
+ ### 优点
59
+
60
+ - ✅ **支持SMB2支持** (2.02协议级别)
61
+ - ✅ 基于原始JCIFS但经过清理和改进
62
+ - ✅ 移除了全局状态,线程安全
63
+
64
+ ### 缺点
65
+
66
+ - ❌ **明确弃用了server browsing功能** (2.1版本)
67
+ - ❌ 不再支持share枚举
68
+ - ❌ API与原始JCIFS有差异
69
+
70
+ ## 备选方案3:SMB4J
71
+
72
+ ### 基本信息
73
+
74
+ ```gradle
75
+ implementation 'com.github.smb4j:smb4j:1.0.0'
76
+ ```
77
+
78
+ ### 优点
79
+
80
+ - ✅ 现代化设计
81
+ - ✅ 支持SMB2/3
82
+
83
+ ### 缺点
84
+
85
+ - ❌ 相对较新,生态不够成熟
86
+ - ❌ 文档不够完整
87
+
88
+ ## 推荐方案:尝试JCIFS (CodeLibs)
89
+
90
+ ### 理由
91
+
92
+ 1. **完整功能支持** - 作为最全面的SMB库,很可能支持share枚举
93
+ 2. **现代维护** - CodeLibs版本是维护最积极的JCIFS分支
94
+ 3. **向后兼容** - 支持所有SMB版本,兼容性最好
95
+
96
+ ### 实施建议
97
+
98
+ #### 第一步:添加依赖测试
99
+
100
+ ```gradle
101
+ dependencies {
102
+ // 当前SMBJ
103
+ implementation 'com.hierynomus:smbj:0.13.0'
104
+
105
+ // 添加JCIFS测试
106
+ implementation 'org.codelibs:jcifs:2.1.39'
107
+ implementation 'org.slf4j:slf4j-nop:2.0.13'
108
+ }
109
+ ```
110
+
111
+ #### 第二步:创建JCIFS实现的listShares
112
+
113
+ ```kotlin
114
+ import org.codelibs.jcifs.smb.CIFSContext
115
+ import org.codelibs.jcifs.smb.context.SingletonContext
116
+ import org.codelibs.jcifs.smb.impl.SmbFile
117
+ import org.codelibs.jcifs.smb.impl.NtlmPasswordAuthenticator
118
+
119
+ class JcifsShareEnumerator {
120
+ fun listShares(host: String, username: String, password: String, domain: String? = null): List<String> {
121
+ val context = SingletonContext.getInstance()
122
+ val auth = NtlmPasswordAuthenticator(domain, username, password)
123
+ val authContext = context.withCredentials(auth)
124
+
125
+ return try {
126
+ val serverUrl = "smb://$host/"
127
+ val server = SmbFile(serverUrl, authContext)
128
+
129
+ server.listFiles()?.map { share ->
130
+ share.name.removeSuffix("/")
131
+ } ?: emptyList()
132
+ } catch (e: Exception) {
133
+ emptyList()
134
+ }
135
+ }
136
+ }
137
+ ```
138
+
139
+ #### 第三步:集成到现有API
140
+
141
+ ```kotlin
142
+ // 在SmbClient.kt中添加
143
+ @Synchronized
144
+ fun listSharesJcifs(): List<String> {
145
+ // 使用JCIFS实现
146
+ return JcifsShareEnumerator().listShares(host, username, password, domain)
147
+ }
148
+
149
+ @Synchronized
150
+ fun listShares(): List<String> {
151
+ // 首先尝试JCIFS方法
152
+ val jcifsShares = listSharesJcifs()
153
+ if (jcifsShares.isNotEmpty()) {
154
+ return jcifsShares
155
+ }
156
+
157
+ // 如果失败,回退到当前的测试方法
158
+ return listSharesViaTesting()
159
+ }
160
+ ```
161
+
162
+ ## 总结
163
+
164
+ **推荐采用JCIFS (CodeLibs版本)** 作为share枚举的补充方案:
165
+
166
+ 1. **混合策略** - 保留SMBJ作为主要SMB客户端,仅使用JCIFS进行share枚举
167
+ 2. **向后兼容** - 如果JCIFS枚举失败,回退到当前的测试方法
168
+ 3. **最小改动** - 只需要添加一个枚举方法,不需要重写整个SMB客户端
169
+
170
+ 这样既能解决share枚举问题,又能保持现有代码的稳定性。
@@ -0,0 +1,160 @@
1
+ # SMB 混合实现方案
2
+
3
+ ## 概述
4
+
5
+ 为了解决SMB share枚举的问题,我们实现了一个混合方案:
6
+
7
+ - 主要SMB客户端使用SMBJ(可靠性高)
8
+ - Share枚举使用JCIFS(功能更完整)
9
+
10
+ ## 实现细节
11
+
12
+ ### 1. 依赖库
13
+
14
+ - **SMBJ**: 主要SMB客户端操作(连接、文件操作等)
15
+ - **JCIFS**: Share枚举和发现(org.codelibs:jcifs:2.1.39)
16
+
17
+ ### 2. 架构设计
18
+
19
+ #### SmbClient.kt 主类
20
+
21
+ - 保留SMBJ作为主要SMB客户端
22
+ - 添加JcifsShareEnumerator辅助类
23
+ - 存储连接凭据以供JCIFS使用
24
+
25
+ #### JcifsShareEnumerator.kt 辅助类
26
+
27
+ - 专门用于Share枚举
28
+ - 使用JCIFS进行完整的Share发现
29
+ - 提供连接测试功能
30
+
31
+ ### 3. listShares() 方法流程
32
+
33
+ ```kotlin
34
+ fun listShares(): List<String> {
35
+ // 1. 尝试使用JCIFS进行完整Share枚举
36
+ try {
37
+ val shares = jcifsEnumerator.listShares(host, username, password, domain)
38
+ if (shares.isNotEmpty()) {
39
+ return shares
40
+ }
41
+ } catch (e: Exception) {
42
+ // 继续到fallback方法
43
+ }
44
+
45
+ // 2. Fallback: 使用SMBJ测试常见Share名称
46
+ try {
47
+ // 测试常见Windows和用户定义的Share
48
+ val commonShares = listOf("C$", "D$", "shared", "public", ...)
49
+ // 返回可访问的Share
50
+ } catch (e: Exception) {
51
+ // 最终fallback: 返回常见Share名称
52
+ }
53
+ }
54
+ ```
55
+
56
+ ### 4. 优势
57
+
58
+ 1. **可靠性**: SMBJ作为主要客户端,稳定性高
59
+ 2. **完整性**: JCIFS提供完整的Share枚举
60
+ 3. **容错性**: 多层fallback机制
61
+ 4. **性能**: JCIFS仅用于Share枚举,不影响主要操作
62
+
63
+ ### 5. Share参数说明
64
+
65
+ #### 对于列出SMB根目录(Share根目录):
66
+
67
+ ```typescript
68
+ // 连接到具体Share
69
+ await smbClient.connect({
70
+ host: "192.168.1.100",
71
+ username: "user",
72
+ password: "pass",
73
+ share: "C$", // 指定具体的Share
74
+ });
75
+
76
+ // 列出Share根目录
77
+ const files = await smbClient.listFiles(""); // 空字符串表示Share根目录
78
+ ```
79
+
80
+ #### 对于列出所有可用Share:
81
+
82
+ ```typescript
83
+ // 只需要认证,不指定share
84
+ await smbClient.connect({
85
+ host: "192.168.1.100",
86
+ username: "user",
87
+ password: "pass",
88
+ // 不指定share参数
89
+ });
90
+
91
+ // 获取所有可用Share
92
+ const shares = await smbClient.listShares();
93
+ ```
94
+
95
+ ### 6. 常见Share类型
96
+
97
+ #### Windows系统Share:
98
+
99
+ - `C$`, `D$`, `E$`: 驱动器共享(需要管理员权限)
100
+ - `ADMIN$`: 管理共享
101
+ - `IPC$`: 进程间通信共享
102
+
103
+ #### 用户定义Share:
104
+
105
+ - `shared`, `public`, `data`: 常见的用户共享文件夹
106
+ - `home`, `Users`: 用户目录共享
107
+ - `documents`, `media`: 媒体和文档共享
108
+
109
+ ## 使用示例
110
+
111
+ ```typescript
112
+ const smbClient = new SmbClient();
113
+
114
+ // 1. 连接到服务器
115
+ await smbClient.connect({
116
+ host: "192.168.1.100",
117
+ username: "myuser",
118
+ password: "mypass",
119
+ });
120
+
121
+ // 2. 获取所有可用Share
122
+ const shares = await smbClient.listShares();
123
+ console.log("Available shares:", shares);
124
+ // 输出: ['C$', 'shared', 'public', 'documents']
125
+
126
+ // 3. 连接到具体Share
127
+ await smbClient.openShare("shared");
128
+
129
+ // 4. 列出Share根目录
130
+ const files = await smbClient.listFiles("");
131
+
132
+ // 5. 列出子目录
133
+ const subFiles = await smbClient.listFiles("subfolder");
134
+ ```
135
+
136
+ ## 故障排除
137
+
138
+ ### 如果JCIFS枚举失败:
139
+
140
+ - 自动fallback到SMBJ测试方法
141
+ - 返回测试可访问的常见Share
142
+
143
+ ### 如果所有方法都失败:
144
+
145
+ - 返回最常见的Share名称列表
146
+ - 用户可以手动尝试连接
147
+
148
+ ## 技术细节
149
+
150
+ ### JCIFS配置:
151
+
152
+ - 使用NtlmPasswordAuthenticator进行认证
153
+ - 通过SmbFile访问服务器根目录
154
+ - 枚举所有可用的Share
155
+
156
+ ### SMBJ fallback:
157
+
158
+ - 测试常见Share名称的可访问性
159
+ - 短暂连接后立即关闭以测试可用性
160
+ - 收集所有可访问的Share名称
@@ -43,8 +43,10 @@ android {
43
43
  }
44
44
 
45
45
  dependencies {
46
- // SMB client library
46
+ // SMB client library (primary)
47
47
  implementation 'com.hierynomus:smbj:0.13.0'
48
+ // JCIFS library for share enumeration
49
+ implementation 'org.codelibs:jcifs:2.1.39'
48
50
  // Suppress SLF4J binding warnings
49
51
  implementation 'org.slf4j:slf4j-nop:2.0.13'
50
52
  }
@@ -0,0 +1,75 @@
1
+ package expo.modules.kookit
2
+
3
+ import jcifs.CIFSContext
4
+ import jcifs.context.SingletonContext
5
+ import jcifs.smb.SmbFile
6
+ import jcifs.smb.NtlmPasswordAuthenticator
7
+ import java.util.*
8
+
9
+ /**
10
+ * JCIFS-based SMB share enumerator
11
+ * Used as a supplement to SMBJ for share enumeration functionality
12
+ */
13
+ class JcifsShareEnumerator {
14
+
15
+ /**
16
+ * List all available shares on an SMB server using JCIFS
17
+ * @param host SMB server hostname or IP
18
+ * @param username Username for authentication
19
+ * @param password Password for authentication
20
+ * @param domain Domain name (optional)
21
+ * @return List of share names, empty if enumeration fails
22
+ */
23
+ fun listShares(host: String, username: String, password: String, domain: String? = null): List<String> {
24
+ return try {
25
+ // Create JCIFS context with authentication
26
+ val context = SingletonContext.getInstance()
27
+ val auth = NtlmPasswordAuthenticator(domain, username, password)
28
+ val authContext = context.withCredentials(auth)
29
+
30
+ // Connect to server root to enumerate shares
31
+ val serverUrl = "smb://$host/"
32
+ val server = SmbFile(serverUrl, authContext)
33
+
34
+ // List all shares
35
+ val shares = server.listFiles()
36
+ shares?.mapNotNull { shareFile ->
37
+ val shareName = shareFile.name.removeSuffix("/")
38
+ // Filter out hidden/administrative shares if needed
39
+ if (shareName.isNotBlank() && !shareName.startsWith(".")) {
40
+ shareName
41
+ } else null
42
+ } ?: emptyList()
43
+
44
+ } catch (e: Exception) {
45
+ // If JCIFS enumeration fails, return empty list
46
+ // The caller can fall back to other methods
47
+ emptyList()
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Test if JCIFS can connect to the server
53
+ * @param host SMB server hostname or IP
54
+ * @param username Username for authentication
55
+ * @param password Password for authentication
56
+ * @param domain Domain name (optional)
57
+ * @return true if connection successful, false otherwise
58
+ */
59
+ fun testConnection(host: String, username: String, password: String, domain: String? = null): Boolean {
60
+ return try {
61
+ val context = SingletonContext.getInstance()
62
+ val auth = NtlmPasswordAuthenticator(domain, username, password)
63
+ val authContext = context.withCredentials(auth)
64
+
65
+ val serverUrl = "smb://$host/"
66
+ val server = SmbFile(serverUrl, authContext)
67
+
68
+ // Test if we can access the server
69
+ server.exists()
70
+ true
71
+ } catch (e: Exception) {
72
+ false
73
+ }
74
+ }
75
+ }
@@ -41,11 +41,23 @@ class SmbClient {
41
41
  private var session: Session? = null
42
42
  private var share: DiskShare? = null
43
43
  private var connected = false
44
+ private val jcifsEnumerator = JcifsShareEnumerator()
45
+ private var lastHost: String? = null
46
+ private var lastUsername: String? = null
47
+ private var lastPassword: String? = null
48
+ private var lastDomain: String? = null
44
49
 
45
50
  @Synchronized
46
51
  @Throws(IOException::class)
47
52
  fun connect(config: SmbConnectionConfig) {
48
53
  disconnect()
54
+
55
+ // Store connection parameters for share enumeration
56
+ lastHost = config.host
57
+ lastUsername = config.username
58
+ lastPassword = config.password
59
+ lastDomain = config.domain
60
+
49
61
  val conn = smb.connect(config.host)
50
62
  connection = conn
51
63
  val auth = AuthenticationContext(
@@ -70,9 +82,26 @@ class SmbClient {
70
82
  @Synchronized
71
83
  fun listShares(): List<String> {
72
84
  val s = session ?: throw IOException("Not authenticated")
85
+
86
+ // First try JCIFS for more complete share enumeration
87
+ if (lastHost != null && lastUsername != null && lastPassword != null) {
88
+ try {
89
+ val shares = jcifsEnumerator.listShares(
90
+ lastHost!!,
91
+ lastUsername!!,
92
+ lastPassword!!,
93
+ lastDomain ?: ""
94
+ )
95
+ if (shares.isNotEmpty()) {
96
+ return shares
97
+ }
98
+ } catch (e: Exception) {
99
+ // JCIFS failed, fall back to SMBJ testing approach
100
+ }
101
+ }
102
+
73
103
  return try {
74
- // SMBJ doesn't provide direct share enumeration API
75
- // We implement a practical approach by testing common share names
104
+ // Fallback: SMBJ testing approach
76
105
  val availableShares = mutableListOf<String>()
77
106
 
78
107
  // Common Windows shares
@@ -95,12 +124,12 @@ class SmbClient {
95
124
 
96
125
  // If no shares found, suggest some common ones
97
126
  if (availableShares.isEmpty()) {
98
- return listOf("C$", "shared", "public")
127
+ listOf("C$", "shared", "public")
128
+ } else {
129
+ availableShares
99
130
  }
100
-
101
- availableShares
102
131
  } catch (e: Exception) {
103
- // Fallback to common share names if enumeration fails
132
+ // Final fallback to common share names if all enumeration methods fail
104
133
  listOf("C$", "shared", "public", "data")
105
134
  }
106
135
  }
@@ -114,6 +143,12 @@ class SmbClient {
114
143
  session = null
115
144
  connection = null
116
145
  connected = false
146
+
147
+ // Clear stored credentials
148
+ lastHost = null
149
+ lastUsername = null
150
+ lastPassword = null
151
+ lastDomain = null
117
152
  }
118
153
 
119
154
  @Synchronized
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-kookit",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "React Native module for intercepting volume button presses on iOS and Android, with FTP client functionality",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",