@@ -18,30 +18,36 @@ import java.io.IOException
18
18
19
19
/* *
20
20
* Holds Tor configuration information.
21
+ *
22
+ * See [Companion.createConfig] or [Builder] to instantiate.
21
23
*/
22
24
class TorConfig private constructor(
23
25
val geoIpFile : File ,
24
26
val geoIpv6File : File ,
25
- torrcFile : File ,
27
+ mTorrcFile : File ,
26
28
val torExecutableFile : File ,
27
29
val hiddenServiceDir : File ,
28
30
val dataDir : File ,
29
31
val configDir : File ,
30
32
val homeDir : File ,
31
33
32
34
/* *
33
- * The <base32-encoded-fingerprint>.onion domain name for this hidden service. If the hidden service is
34
- * restricted to authorized clients only, this file also contains authorization data for all clients.
35
+ * The <base32-encoded-fingerprint>.onion domain name for this hidden service.
36
+ * If the hidden service is restricted to authorized clients only, this file
37
+ * also contains authorization data for all clients.
35
38
*
36
- * @return hostname file
37
- </base32-encoded-fingerprint> */
39
+ * @return [hostnameFile] </base32-encoded-fingerprint>
40
+ * */
38
41
val hostnameFile : File ,
39
42
40
43
/* *
41
- * Used for cookie authentication with the controller. Location can be overridden by the CookieAuthFile config option.
42
- * Regenerated on startup. See control-spec.txt in torspec for details. Only used when cookie authentication is enabled.
44
+ * Used for cookie authentication with the controller. Location can be
45
+ * overridden by the CookieAuthFile config option. Regenerated on startup.
46
+ * See control-spec.txt in torspec for details.
43
47
*
44
- * @return
48
+ * Only used when cookie authentication is enabled.
49
+ *
50
+ * @return [cookieAuthFile]
45
51
*/
46
52
val cookieAuthFile : File ,
47
53
val libraryPath : File ,
@@ -50,9 +56,9 @@ class TorConfig private constructor(
50
56
val installDir : File ,
51
57
52
58
/* *
53
- * When tor starts it waits for the control port and cookie auth files to be created before it proceeds to the
54
- * next step in startup. If these files are not created after a certain amount of time, then the startup has
55
- * failed.
59
+ * When tor starts it waits for the control port and cookie auth files to be created
60
+ * before it proceeds to the next step in startup. If these files are not created
61
+ * after a certain amount of time, then the startup has failed.
56
62
*
57
63
* This method returns how much time to wait in seconds until failing the startup.
58
64
*/
@@ -74,78 +80,94 @@ class TorConfig private constructor(
74
80
/* *
75
81
* Convenience method for if you're including in your App's jniLibs directory
76
82
* the `libTor.so` binary, or utilizing those maintained by this project.
83
+ *
84
+ * @param [context] Context
85
+ * @param [configDir] context.getDir("dir_name_here", Context.MODE_PRIVATE)
86
+ * @param [dataDir] if you wish it in a different location than lib/tor
77
87
* */
78
88
fun createConfig (context : Context , configDir : File , dataDir : File ? = null): TorConfig {
79
- val nativeDir = File (context.applicationInfo.nativeLibraryDir)
80
- val builder = Builder (nativeDir , configDir)
89
+ val installDir = File (context.applicationInfo.nativeLibraryDir)
90
+ val builder = Builder (installDir , configDir)
81
91
if (dataDir != null )
82
92
builder.dataDir(dataDir)
83
93
return builder.build()
84
94
}
95
+
96
+ /* *
97
+ * Convenience method for setting up all of your files and directories in their
98
+ * default locations.
99
+ *
100
+ * @param context Context
101
+ * */
102
+ fun createConfig (context : Context ): TorConfig =
103
+ createConfig(context, context.getDir(" torservice" , Context .MODE_PRIVATE ))
85
104
}
86
105
87
106
private val configLock = Object ()
88
- private var mTorrcFile = torrcFile
89
- fun getTorrcFile (): File = mTorrcFile
107
+ var torrcFile = mTorrcFile
108
+ private set
90
109
91
110
/* *
92
- * Resolves the tor configuration file. If the torrc file hasn't been set, then this method will attempt to
93
- * resolve the config file by looking in the root of the $configDir and then in $user.home directory
111
+ * Resolves the tor configuration file. If the torrc file hasn't been set, then
112
+ * this method will attempt to resolve the config file by looking in the root of
113
+ * the $configDir and then in $user.home directory
94
114
*
95
- * @return torrc file
96
- * @throws IOException if torrc file is not resolved
115
+ * @throws [IOException] If torrc file is not resolved.
116
+ * @throws [SecurityException] if unable to access the file.
117
+ *
118
+ * @return [torrcFile]
97
119
*/
98
- @Throws(IOException ::class )
120
+ @Throws(IOException ::class , SecurityException :: class )
99
121
fun resolveTorrcFile (): File {
100
122
101
123
synchronized(configLock) {
102
- if (! mTorrcFile.exists()) {
103
- var tmpTorrcFile = File (configDir,
104
- TORRC_NAME
105
- )
124
+ if (! torrcFile.exists()) {
125
+ var tmpTorrcFile = File (configDir, TORRC_NAME )
106
126
107
127
if (! tmpTorrcFile.exists()) {
108
128
tmpTorrcFile = File (homeDir, " .$TORRC_NAME " )
109
129
110
130
if (! tmpTorrcFile.exists()) {
111
- mTorrcFile = File (configDir,
112
- TORRC_NAME
113
- )
131
+ torrcFile = File (configDir, TORRC_NAME )
114
132
115
- if (! mTorrcFile .createNewFile()) {
133
+ if (! torrcFile .createNewFile()) {
116
134
throw IOException (" Failed to create torrc file" )
117
135
}
118
136
119
137
} else {
120
- mTorrcFile = tmpTorrcFile
138
+ torrcFile = tmpTorrcFile
121
139
}
122
140
} else {
123
- mTorrcFile = tmpTorrcFile
141
+ torrcFile = tmpTorrcFile
124
142
}
125
143
}
126
- return mTorrcFile
144
+ return torrcFile
127
145
}
128
146
}
129
147
130
148
override fun toString (): String {
131
- return " TorConfig{" +
132
- " geoIpFile=" + geoIpFile +
133
- " , geoIpv6File=" + geoIpv6File +
134
- " , torrcFile=" + mTorrcFile +
135
- " , torExecutableFile=" + torExecutableFile +
136
- " , hiddenServiceDir=" + hiddenServiceDir +
137
- " , dataDir=" + dataDir +
138
- " , configDir=" + configDir +
139
- " , installDir=" + installDir +
140
- " , homeDir=" + homeDir +
141
- " , hostnameFile=" + hostnameFile +
142
- " , cookieAuthFile=" + cookieAuthFile +
143
- " , libraryPath=" + libraryPath +
144
- ' }'
149
+ return " TorConfig{ " +
150
+ " geoIpFile=$geoIpFile , " +
151
+ " geoIpv6File=$geoIpv6File , " +
152
+ " torrcFile=$torrcFile , " +
153
+ " torExecutableFile=$torExecutableFile , " +
154
+ " hiddenServiceDir=$hiddenServiceDir , " +
155
+ " dataDir=$dataDir , " +
156
+ " configDir=$configDir , " +
157
+ " installDir=$installDir , " +
158
+ " homeDir=$homeDir , " +
159
+ " hostnameFile=$hostnameFile , " +
160
+ " cookieAuthFile=$cookieAuthFile , " +
161
+ " libraryPath=$libraryPath }"
145
162
}
146
163
147
164
/* *
148
165
* Builder for TorConfig.
166
+ *
167
+ * See also [Companion.createConfig] for convenience methods.
168
+ *
169
+ * @param [installDir] directory where the tor binaries are installed.
170
+ * @param [configDir] directory where the filesystem will be setup for tor.
149
171
*/
150
172
class Builder (private val installDir : File , private val configDir : File ) {
151
173
@@ -166,12 +188,12 @@ class TorConfig private constructor(
166
188
/* *
167
189
* Home directory of user.
168
190
*
191
+ * Default value: $home.user if $home.user environment property exists, otherwise
192
+ * $configDir. On Android, this will always default to $configDir.
169
193
*
170
- * Default value: $home.user if $home.user environment property exists, otherwise $configDir. On Android, this
171
- * will always default to $configDir.
194
+ * @param [homeDir] the home directory of the user
172
195
*
173
- * @param homeDir the home directory of the user
174
- * @return builder
196
+ * @return [Builder]
175
197
*/
176
198
fun homeDir (homeDir : File ): Builder {
177
199
this .mHomeDir = homeDir
@@ -184,16 +206,19 @@ class TorConfig private constructor(
184
206
}
185
207
186
208
/* *
187
- * Store data files for a hidden service in DIRECTORY. Every hidden service must have a separate directory.
188
- * You may use this option multiple times to specify multiple services. If DIRECTORY does not exist, Tor will
189
- * create it. (Note: in current versions of Tor, if DIRECTORY is a relative path, it will be relative to the
190
- * current working directory of Tor instance, not to its DataDirectory. Do not rely on this behavior; it is not
191
- * guaranteed to remain the same in future versions.)
209
+ * Store data files for a hidden service in DIRECTORY. Every hidden service must
210
+ * have a separate directory. You may use this option multiple times to specify
211
+ * multiple services. If DIRECTORY does not exist, Tor will create it. (Note: in
212
+ * current versions of Tor, if DIRECTORY is a relative path, it will be relative
213
+ * to the current working directory of Tor instance, not to its DataDirectory. Do
214
+ * not rely on this behavior; it is not guaranteed to remain the same in future
215
+ * versions.)
192
216
*
193
217
* Default value: $configDir/hiddenservices
194
218
*
195
- * @param directory hidden services directory
196
- * @return builder
219
+ * @param [directory] hidden services directory
220
+ *
221
+ * @return [Builder]
197
222
*/
198
223
fun hiddenServiceDir (directory : File ): Builder {
199
224
mHiddenServiceDir = directory
@@ -205,8 +230,9 @@ class TorConfig private constructor(
205
230
*
206
231
* Default value: $configDir/geoip6
207
232
*
208
- * @param file geoip6 file
209
- * @return builder
233
+ * @param [file] geoip6 file
234
+ *
235
+ * @return [Builder]
210
236
*/
211
237
fun geoipv6 (file : File ): Builder {
212
238
mGeoIpv6File = file
@@ -218,8 +244,9 @@ class TorConfig private constructor(
218
244
*
219
245
* Default value: $configDir/geoip
220
246
*
221
- * @param file geoip file
222
- * @return builder
247
+ * @param [file] geoip file
248
+ *
249
+ * @return [Builder]
223
250
*/
224
251
fun geoip (file : File ): Builder {
225
252
mGeoIpFile = file
@@ -231,8 +258,9 @@ class TorConfig private constructor(
231
258
*
232
259
* Default value: $configDir/lib/tor
233
260
*
234
- * @param directory directory where tor runtime data is stored
235
- * @return builder
261
+ * @param [directory] directory where tor runtime data is stored
262
+ *
263
+ * @return [Builder]
236
264
*/
237
265
fun dataDir (directory : File ): Builder {
238
266
mDataDir = directory
@@ -244,8 +272,9 @@ class TorConfig private constructor(
244
272
*
245
273
* Default value: $configDir/torrc
246
274
*
247
- * @param file
248
- * @return
275
+ * @param [file] your torrc file
276
+ *
277
+ * @return [Builder]
249
278
*/
250
279
fun torrc (file : File ): Builder {
251
280
mTorrcFile = file
@@ -273,23 +302,27 @@ class TorConfig private constructor(
273
302
}
274
303
275
304
/* *
276
- * When tor starts it waits for the control port and cookie auth files to be created before it proceeds to the
277
- * next step in startup. If these files are not created after a certain amount of time, then the startup has
278
- * failed.
305
+ * When tor starts it waits for the control port and cookie auth files to be
306
+ * created before it proceeds to the next step in startup. If these files are
307
+ * not created after a certain amount of time, then the startup has failed.
279
308
*
280
309
* This method specifies how much time to wait until failing the startup.
281
310
*
282
- * @param timeout in seconds
311
+ * Default value is 15 seconds
312
+ *
313
+ * @param [timeoutSeconds] Int
314
+ *
315
+ * @return [Builder]
283
316
*/
284
- fun fileCreationTimeout (timeout : Int ): Builder {
285
- mFileCreationTimeout = timeout
317
+ fun fileCreationTimeout (timeoutSeconds : Int ): Builder {
318
+ mFileCreationTimeout = timeoutSeconds
286
319
return this
287
320
}
288
321
289
322
/* *
290
323
* Builds torConfig and sets default values if not explicitly configured through builder.
291
324
*
292
- * @return torConfig
325
+ * @return [TorConfig]
293
326
*/
294
327
fun build (): TorConfig {
295
328
if (! ::mHomeDir.isInitialized) {
0 commit comments