proxies.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. class ProxyConfig {
  2. final String name;
  3. final String type;
  4. final String server;
  5. final int port;
  6. final bool? udp;
  7. // vless
  8. final String? flow;
  9. final String? servername;
  10. final bool? tls;
  11. final Reality? realityOpts;
  12. // V2Ray/VLESS特定属性
  13. final String? uuid;
  14. final int? alterId;
  15. // Shadowsocks特定属性
  16. final String? cipher;
  17. final String? password;
  18. final String? clientFingerprint;
  19. final String? network;
  20. final GrpcOpts? grpcOpts;
  21. final WsOpts? wsOpts;
  22. ProxyConfig({
  23. required this.name,
  24. required this.type,
  25. required this.server,
  26. required this.port,
  27. this.password,
  28. this.udp,
  29. this.flow,
  30. this.servername,
  31. this.tls,
  32. this.realityOpts, // 新增字段
  33. this.grpcOpts,
  34. this.wsOpts,
  35. this.uuid,
  36. this.alterId,
  37. this.cipher,
  38. this.clientFingerprint,
  39. this.network
  40. });
  41. // 更新解析方法
  42. factory ProxyConfig.fromMap(Map<String, dynamic> map) {
  43. return ProxyConfig(
  44. name: map['name'],
  45. type: map['type'],
  46. server: map['server'],
  47. port: map['port'],
  48. password: map['password'],
  49. udp: map['udp'],
  50. flow: map['flow'],
  51. servername: map['servername'],
  52. tls: map['tls'],
  53. realityOpts: map['reality-opts'] != null ? Reality.fromMap(map['reality-opts']) : null,
  54. grpcOpts: map['grpc-opts'] != null ? GrpcOpts.fromMap(map['grpc-opts']) : null,
  55. wsOpts: map['ws-opts'] != null ? WsOpts.fromMap(map['ws-opts']) : null,
  56. uuid: map['uuid'],
  57. alterId: map['alterId'],
  58. cipher:map['cipher'],
  59. clientFingerprint:map['client-fingerprint'],
  60. network:map['network']
  61. );
  62. }
  63. // 更新 JSON 序列化方法
  64. Map<String, dynamic> toJson() {
  65. return {
  66. 'name': name,
  67. 'type': type,
  68. 'server': server,
  69. 'port': port,
  70. 'password': password,
  71. 'udp': udp,
  72. 'flow': flow,
  73. 'servername': servername,
  74. 'tls': tls,
  75. 'reality-opts': realityOpts?.toJson(),
  76. 'uuid': uuid,
  77. 'alterId': alterId,
  78. 'cipher' : cipher,
  79. 'client-fingerprint': clientFingerprint,
  80. 'grpc-opts': grpcOpts,
  81. 'ws-opts': wsOpts,
  82. 'network' : network,
  83. };
  84. }
  85. String toYaml() {
  86. var yaml = '''
  87. - name: $name
  88. type: $type
  89. server: $server
  90. port: $port''';
  91. if (password != null) yaml += '\n password: $password';
  92. if (flow != null) yaml += '\n flow: $flow';
  93. if (servername != null) yaml += '\n servername: $servername';
  94. if (tls != null) yaml += '\n tls: $tls';
  95. final realityOpts = this.realityOpts;
  96. if (realityOpts != null) {
  97. yaml += '\n reality-opts:';
  98. if (realityOpts.publicKey != null) {
  99. yaml += '\n public-key: ${realityOpts.publicKey}';
  100. }
  101. if (realityOpts.shortID != null) {
  102. yaml += '\n short-id: ${realityOpts.shortID}';
  103. }
  104. }
  105. if (uuid != null) {
  106. yaml += '\n uuid: $uuid';
  107. }
  108. if (alterId != null) yaml += '\n alterId: $alterId';
  109. if (cipher != null) yaml += '\n cipher: $cipher';
  110. if (clientFingerprint != null) '\n client-fingerprint: $clientFingerprint';
  111. if(network != null) yaml += '\n network: $network';
  112. if(udp != null) yaml += '\n udp: $udp';
  113. if (grpcOpts != null) {
  114. yaml += '\n ${grpcOpts!.toYaml()}';
  115. }
  116. if (wsOpts != null) {
  117. yaml += '\n ${wsOpts!.toYaml()}';
  118. }
  119. return yaml;
  120. }
  121. }
  122. class Reality {
  123. final String? publicKey;
  124. final String? shortID;
  125. Reality({this.publicKey,this.shortID});
  126. factory Reality.fromMap(Map<String, dynamic> map) {
  127. return Reality(
  128. publicKey: map['public-key'] as String?,
  129. shortID: map['short-id'] as String?,
  130. );
  131. }
  132. Map<String, dynamic> toJson() {
  133. return {
  134. 'public-key': publicKey,
  135. 'short-id' : shortID
  136. };
  137. }
  138. String toYaml() {
  139. var yaml = '';
  140. if (publicKey != null) {
  141. yaml += ' reality-opts:\n public-key: $publicKey\n';
  142. }
  143. if (shortID != null) {
  144. yaml += ' short-id: $shortID\n';
  145. }
  146. return yaml;
  147. }
  148. }
  149. class GrpcOpts {
  150. final String? grpcServiceName;
  151. GrpcOpts({this.grpcServiceName});
  152. factory GrpcOpts.fromMap(Map<String, dynamic> map) {
  153. return GrpcOpts(
  154. grpcServiceName: map['grpc-service-name'] as String?,
  155. );
  156. }
  157. Map<String, dynamic> toJson() {
  158. return {
  159. if (grpcServiceName != null) 'grpc-service-name': grpcServiceName,
  160. };
  161. }
  162. String toYaml() {
  163. if (grpcServiceName == null) {
  164. return '';
  165. }
  166. return ' grpc-opts:\n grpc-service-name: "$grpcServiceName"';
  167. }
  168. }
  169. class WsOpts {
  170. final String? path;
  171. final Map<String, String>? headers;
  172. WsOpts({this.path, this.headers});
  173. factory WsOpts.fromMap(Map<String, dynamic> map) {
  174. return WsOpts(
  175. path: map['path'] as String?,
  176. headers: (map['headers'] as Map?)?.cast<String, String>(),
  177. );
  178. }
  179. Map<String, dynamic> toJson() {
  180. return {
  181. if (path != null) 'path': path,
  182. if (headers != null) 'headers': headers,
  183. };
  184. }
  185. String toYaml() {
  186. var yaml = 'ws-opts:';
  187. if (path != null) {
  188. yaml += '\n path: $path';
  189. }
  190. if (headers != null && headers!.isNotEmpty) {
  191. yaml += '\n headers:';
  192. headers!.forEach((key, value) {
  193. yaml += '\n $key: $value';
  194. });
  195. }
  196. return yaml;
  197. }
  198. }