2022-09-28 12:08:16 +00:00
|
|
|
import Tor from 'react-native-tor';
|
|
|
|
|
|
|
|
class TorClient {
|
|
|
|
baseUrl: string;
|
|
|
|
daemon: ReturnType<typeof Tor>;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.baseUrl = 'http://robosats6tkf3eva7x2voqso3a5wcorsnw34jveyxfqi2fu7oyheasid.onion';
|
|
|
|
this.daemon = Tor({
|
|
|
|
stopDaemonOnBackground: false,
|
|
|
|
numberConcurrentRequests: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-15 18:39:43 +00:00
|
|
|
private readonly connectDaemon: () => void = async () => {
|
2022-10-07 14:10:21 +00:00
|
|
|
try {
|
|
|
|
this.daemon.startIfNotStarted();
|
|
|
|
} catch {
|
|
|
|
console.log('TOR already started');
|
|
|
|
}
|
2022-09-28 12:08:16 +00:00
|
|
|
};
|
|
|
|
|
2022-10-15 12:00:21 +00:00
|
|
|
public reset: () => void = async () => {
|
|
|
|
console.log('Reset TOR');
|
|
|
|
await this.daemon.stopIfRunning();
|
|
|
|
await this.daemon.startIfNotStarted();
|
|
|
|
};
|
|
|
|
|
2022-10-10 12:40:22 +00:00
|
|
|
public get: (path: string, headers: object) => Promise<object> = async (path, headers) => {
|
2022-09-28 12:08:16 +00:00
|
|
|
return await new Promise<object>(async (resolve, reject) => {
|
|
|
|
try {
|
2022-10-10 12:40:22 +00:00
|
|
|
const response = await this.daemon.get(`${this.baseUrl}${path}`, headers);
|
2022-09-28 12:08:16 +00:00
|
|
|
|
2022-10-10 12:40:22 +00:00
|
|
|
resolve(response);
|
2022-09-28 12:08:16 +00:00
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
public delete: (path: string, headers: object) => Promise<object> = async (path, headers) => {
|
|
|
|
return await new Promise<object>(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const response = await this.daemon.delete(`${this.baseUrl}${path}`, '', headers);
|
|
|
|
|
2022-10-10 12:40:22 +00:00
|
|
|
resolve(response);
|
2022-09-28 12:08:16 +00:00
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
public request: (path: string) => Promise<object> = async (path) => {
|
|
|
|
return await new Promise<object>(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const response = await this.daemon
|
|
|
|
.request(`${this.baseUrl}${path}`, 'GET', '', {}, true)
|
|
|
|
.then((resp) => {
|
|
|
|
resolve(resp);
|
|
|
|
});
|
|
|
|
|
|
|
|
resolve(response);
|
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
public post: (path: string, body: object, headers: object) => Promise<object> = async (
|
|
|
|
path,
|
|
|
|
body,
|
|
|
|
headers,
|
|
|
|
) => {
|
|
|
|
return await new Promise<object>(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
const json = JSON.stringify(body);
|
|
|
|
const response = await this.daemon.post(`${this.baseUrl}${path}`, json, headers);
|
|
|
|
|
2022-10-10 12:40:22 +00:00
|
|
|
resolve(response);
|
2022-09-28 12:08:16 +00:00
|
|
|
} catch (error) {
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TorClient;
|