Skip to content

Commit

Permalink
socks5: display settings for peers, add possibility to update setting…
Browse files Browse the repository at this point in the history
…s for peers and update current active proxy
  • Loading branch information
pymq committed Oct 12, 2024
1 parent fa3be01 commit ff7a526
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 20 deletions.
36 changes: 35 additions & 1 deletion lib/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const GetAuthRequestsPath = V0Prefix + "peers/auth_requests";
const GetMyPeerInfoPath = V0Prefix + "settings/peer_info";
const UpdateMyInfoPath = V0Prefix + "settings/update";
const ExportServerConfigPath = V0Prefix + "settings/export_server_config";
const ListAvailableProxiesPath = V0Prefix + "settings/list_proxies";
const UpdateProxySettingsPath = V0Prefix + "settings/set_proxy";

// Debug
const GetP2pDebugInfoPath = V0Prefix + "debug/p2p_info";
Expand All @@ -45,7 +47,8 @@ Future<MyPeerInfo> fetchMyPeerInfo(http.Client client) async {
return MyPeerInfo.fromJson(parsed);
} catch (e) {
print("error in fetchMyPeerInfo: '${e.toString()}'.");
return MyPeerInfo("", "", Duration.zero, "–", NetworkStats(0, 0, 0, 0), 0, 0, "", "", false);
return MyPeerInfo(
"", "", Duration.zero, "–", NetworkStats(0, 0, 0, 0), 0, 0, "", "", false, SOCKS5Info("", false, false, "", ""));
}
}

Expand All @@ -61,6 +64,18 @@ Future<List<KnownPeer>?> fetchKnownPeers(http.Client client) async {
}
}

Future<ListAvailableProxiesResponse?> fetchAvailableProxies(http.Client client) async {
try {
final response = await client.get(Uri.parse(serverAddress + ListAvailableProxiesPath));
final Map<String, dynamic> parsed = jsonDecode(response.body);

return ListAvailableProxiesResponse.fromJson(parsed);
} catch (e) {
print("error in fetchAvailableProxies: '${e.toString()}'.");
return null;
}
}

Future<Map<String, dynamic>?> fetchDebugInfo(http.Client client) async {
final response = await client.get(Uri.parse(serverAddress + GetP2pDebugInfoPath));
// final parsed = await compute(jsonDecode, response.body);
Expand Down Expand Up @@ -175,6 +190,25 @@ Future<String> updateMySettings(http.Client client, String name) async {
return "";
}

Future<String> updateProxySettings(http.Client client, String usingPeerID) async {
var payload = {
"UsingPeerID": usingPeerID,
};

var request = http.Request("POST", Uri.parse(serverAddress + UpdateProxySettingsPath));
request.headers.addAll(<String, String>{"Content-Type": "application/json"});
request.body = jsonEncode(payload);

final response = await client.send(request);
var responseBody = await response.stream.bytesToString();
if (response.statusCode != 200) {
final Map<String, dynamic> parsed = jsonDecode(responseBody);
return ApiError.fromJson(parsed).error;
}

return "";
}

Future<String> removePeer(http.Client client, String peerID) async {
var payload = PeerIDRequest(peerID);

Expand Down
7 changes: 7 additions & 0 deletions lib/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,10 @@ String byteCountIEC(int b) {

return "${format(val)} ${"KMGTPE"[exp]}iB";
}

String formatBoolWithEmoji(bool val) {
if (val) {
return "✅";
}
return "❌";
}
5 changes: 5 additions & 0 deletions lib/data_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,15 @@ var knownPeersDataService = ServerDataService<List<KnownPeer>?>(() {
return fetchKnownPeers(http.Client());
});

var availableProxiesDataService = ServerDataService<ListAvailableProxiesResponse?>(() {
return fetchAvailableProxies(http.Client());
});

Future<void> fetchAllData() async {
var futures = <Future>[
myPeerInfoDataService.fetchData(),
knownPeersDataService.fetchData(),
availableProxiesDataService.fetchData(),
];
await Future.wait(futures);
}
57 changes: 53 additions & 4 deletions lib/entities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ class KnownPeer {
final bool connected;
final bool confirmed;
final bool declined;
final bool weAllowUsingAsExitNode;
final bool allowedUsingAsExitNode;
final DateTime lastSeen;
final List<ConnectionInfo> connections;
final NetworkStats networkStats;

KnownPeer(this.peerID, this.displayName, this.version, this.ipAddr, this.connected, this.confirmed, this.lastSeen,
this.connections, this.networkStats, this.domainName, this.declined);
this.connections,
this.networkStats,
this.domainName,
this.declined,
this.weAllowUsingAsExitNode,
this.allowedUsingAsExitNode);

factory KnownPeer.fromJson(Map<String, dynamic> json) => _$KnownPeerFromJson(json);

Expand Down Expand Up @@ -63,9 +70,11 @@ class MyPeerInfo {
final String reachability;
final String awlDNSAddress;
final bool isAwlDNSSetAsSystem;
@JsonKey(name: "SOCKS5")
final SOCKS5Info socks5;

MyPeerInfo(this.peerID, this.name, this.uptime, this.serverVersion, this.networkStats, this.totalBootstrapPeers,
this.connectedBootstrapPeers, this.reachability, this.awlDNSAddress, this.isAwlDNSSetAsSystem);
this.connectedBootstrapPeers, this.reachability, this.awlDNSAddress, this.isAwlDNSSetAsSystem, this.socks5);

factory MyPeerInfo.fromJson(Map<String, dynamic> json) => _$MyPeerInfoFromJson(json);

Expand All @@ -76,6 +85,21 @@ class MyPeerInfo {
static int? _durationToNanoseconds(Duration? duration) => duration == null ? null : duration.inMilliseconds * 1000;
}

@JsonSerializable(fieldRename: FieldRename.pascal)
class SOCKS5Info {
final String listenAddress;
final bool proxyingEnabled;
final bool listenerEnabled;
final String usingPeerID;
final String usingPeerName;

SOCKS5Info(this.listenAddress, this.proxyingEnabled, this.listenerEnabled, this.usingPeerID, this.usingPeerName);

factory SOCKS5Info.fromJson(Map<String, dynamic> json) => _$SOCKS5InfoFromJson(json);

Map<String, dynamic> toJson() => _$SOCKS5InfoToJson(this);
}

@JsonSerializable(fieldRename: FieldRename.pascal)
class NetworkStats {
final int totalIn;
Expand Down Expand Up @@ -110,6 +134,29 @@ class FriendRequest {
Map<String, dynamic> toJson() => _$FriendRequestToJson(this);
}

@JsonSerializable(fieldRename: FieldRename.pascal)
class ListAvailableProxiesResponse {
final List<AvailableProxy> proxies;

ListAvailableProxiesResponse(this.proxies);

factory ListAvailableProxiesResponse.fromJson(Map<String, dynamic> json) => _$ListAvailableProxiesResponseFromJson(json);

Map<String, dynamic> toJson() => _$ListAvailableProxiesResponseToJson(this);
}

@JsonSerializable(fieldRename: FieldRename.pascal)
class AvailableProxy {
final String peerID;
final String peerName;

AvailableProxy(this.peerID, this.peerName);

factory AvailableProxy.fromJson(Map<String, dynamic> json) => _$AvailableProxyFromJson(json);

Map<String, dynamic> toJson() => _$AvailableProxyToJson(this);
}

@JsonSerializable(fieldRename: FieldRename.pascal)
class FriendRequestReply {
final String peerID;
Expand Down Expand Up @@ -164,8 +211,9 @@ class KnownPeerConfig {
final String alias;
final String ipAddr;
final String domainName;
final bool weAllowUsingAsExitNode;

KnownPeerConfig(this.peerId, this.name, this.alias, this.ipAddr, this.domainName);
KnownPeerConfig(this.peerId, this.name, this.alias, this.ipAddr, this.domainName, this.weAllowUsingAsExitNode);

factory KnownPeerConfig.fromJson(Map<String, dynamic> json) => _$KnownPeerConfigFromJson(json);

Expand All @@ -177,8 +225,9 @@ class UpdateKnownPeerConfigRequest {
final String peerID;
final String alias;
final String domainName;
final bool allowUsingAsExitNode;

UpdateKnownPeerConfigRequest(this.peerID, this.alias, this.domainName);
UpdateKnownPeerConfigRequest(this.peerID, this.alias, this.domainName, this.allowUsingAsExitNode);

factory UpdateKnownPeerConfigRequest.fromJson(Map<String, dynamic> json) =>
_$UpdateKnownPeerConfigRequestFromJson(json);
Expand Down
47 changes: 45 additions & 2 deletions lib/entities.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ff7a526

Please sign in to comment.