mirror of
https://github.com/RoboSats/robosats.git
synced 2024-12-14 03:16:24 +00:00
9bda934ee5
* Android Clipboard and Tor Status Icon * working clipboard and lintern * Fix * Add style for Tor connection component * Fix Freeze and Internet out * Fix Typo Co-authored-by: Reckless_Satoshi <reckless.satoshi@protonmail.com>
92 lines
2.2 KiB
TypeScript
92 lines
2.2 KiB
TypeScript
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,
|
|
});
|
|
}
|
|
|
|
private connectDaemon: () => void = async () => {
|
|
try {
|
|
this.daemon.startIfNotStarted();
|
|
} catch {
|
|
console.log('TOR already started');
|
|
}
|
|
};
|
|
|
|
public get: (path: string) => Promise<object> = async (path) => {
|
|
await this.connectDaemon();
|
|
|
|
return await new Promise<object>(async (resolve, reject) => {
|
|
try {
|
|
const response = await this.daemon.get(`${this.baseUrl}${path}`);
|
|
|
|
resolve(response.json);
|
|
} catch (error) {
|
|
reject(error);
|
|
}
|
|
});
|
|
};
|
|
|
|
public delete: (path: string, headers: object) => Promise<object> = async (path, headers) => {
|
|
await this.connectDaemon();
|
|
|
|
return await new Promise<object>(async (resolve, reject) => {
|
|
try {
|
|
const response = await this.daemon.delete(`${this.baseUrl}${path}`, '', headers);
|
|
|
|
resolve(response.json);
|
|
} catch (error) {
|
|
reject(error);
|
|
}
|
|
});
|
|
};
|
|
|
|
public request: (path: string) => Promise<object> = async (path) => {
|
|
await this.connectDaemon();
|
|
|
|
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,
|
|
) => {
|
|
await this.connectDaemon();
|
|
|
|
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);
|
|
|
|
resolve(response.json);
|
|
} catch (error) {
|
|
reject(error);
|
|
}
|
|
});
|
|
};
|
|
}
|
|
|
|
export const torClient = new TorClient();
|
|
|
|
export default TorClient;
|