syntax = "proto2";

package libcore;
option go_package = "./;gen";

service LibcoreService {
  rpc Start(LoadConfigReq) returns (ErrorResp);
  rpc Stop(EmptyReq) returns (ErrorResp);
  rpc CheckConfig(LoadConfigReq) returns (ErrorResp);
  rpc Test(TestReq) returns (TestResp);
  rpc StopTest(EmptyReq) returns (EmptyResp);
  rpc QueryURLTest(EmptyReq) returns (QueryURLTestResponse);
  rpc QueryStats(EmptyReq) returns (QueryStatsResp);
  rpc ListConnections(EmptyReq) returns (ListConnectionsResp);
  //
  rpc SetSystemDNS(SetSystemDNSRequest) returns (EmptyResp);
  //
  rpc IsPrivileged(EmptyReq) returns (IsPrivilegedResponse);
  //
  rpc SpeedTest(SpeedTestRequest) returns(SpeedTestResponse);
  rpc QuerySpeedTest(EmptyReq) returns(QuerySpeedTestResponse);
  rpc QueryCountryTest(EmptyReq) returns(QueryCountryTestResponse);
  //
  rpc Clash2Singbox(Clash2SingboxRequest) returns(Clash2SingboxResponse);
}

message EmptyReq {}

message EmptyResp {}

message ErrorResp {
  optional string error = 1 [default = ""];
}

message LoadConfigReq {
  optional string core_config = 1 [default = ""];
  optional bool disable_stats = 2 [default = false];
  optional bool need_extra_process = 3 [default = false];
  optional string extra_process_path = 4 [default = ""];
  optional string extra_process_args = 5 [default = ""];
  optional string extra_process_conf = 6 [default = ""];
  optional string extra_process_conf_dir = 7 [default = ""];
  optional bool extra_no_out = 8 [default = false];
}

message URLTestResp {
  optional string outbound_tag = 1 [default = ""];
  optional int32 latency_ms = 2 [default = 0 ];
  optional string error = 3 [default = ""];
}

message TestReq {
  optional string config = 1 [default = ""];
  repeated string outbound_tags = 2;
  optional bool use_default_outbound = 3 [default = false];
  optional string url = 4 [default = ""];
  optional bool test_current = 5 [default = false];
  optional int32 max_concurrency = 6 [default = 0];
  optional int32 test_timeout_ms = 7 [default = 0];
}

message TestResp {
  repeated URLTestResp results = 1;
}

message QueryStatsResp{
  map<string, int64> ups = 1;
  map<string, int64> downs = 2;
}

message ListConnectionsResp {
  repeated ConnectionMetaData connections = 1;
}

message ConnectionMetaData {
  optional string id = 1 [default = ""];
  optional int64 created_at = 2 [default = 0];
  optional int64 upload = 3 [default = 0];
  optional int64 download = 4 [default = 0];
  optional string outbound = 5 [default = ""];
  optional string network = 6 [default = ""];
  optional string dest = 7 [default = ""];
  optional string protocol = 8 [default = ""];
  optional string domain = 9 [default = ""];
  optional string process = 10 [default = ""];
}

message SetSystemDNSRequest {
  optional bool clear = 1 [default = false];
}

message IsPrivilegedResponse {
  optional bool has_privilege = 1 [default = false];
}

message SpeedTestRequest {
  optional string config = 1 [default = ""];
  repeated string outbound_tags = 2;
  optional bool test_current = 3 [default = false];
  optional bool use_default_outbound = 4 [default = false];
  optional bool test_download = 5 [default = false];
  optional bool test_upload = 6 [default = false];
  optional bool simple_download = 7 [default = false];
  optional string simple_download_addr = 8 [default = ""];
  optional int32 timeout_ms = 9 [default = 0];
  optional bool only_country = 10 [default = false];
  optional int32 country_concurrency = 11 [default = 0];
}

message SpeedTestResult {
  optional string dl_speed = 1 [default = ""];
  optional string ul_speed = 2 [default = ""];
  optional int32 latency = 3 [default = 0];
  optional string outbound_tag = 4 [default = ""];
  optional string error = 5 [default = ""];
  optional string server_name = 6 [default = ""];
  optional string server_country = 7 [default = ""];
  optional bool cancelled = 8 [default = false];
}

message SpeedTestResponse {
  repeated SpeedTestResult results = 1;
}

message QuerySpeedTestResponse {
  optional SpeedTestResult result = 1;
  optional bool is_running = 2 [default = false];
}

message QueryCountryTestResponse {
  repeated SpeedTestResult results = 1;
}

message QueryURLTestResponse {
  repeated URLTestResp results = 1;
}

message Clash2SingboxRequest {
  optional string clash_config = 1 [default = ""];
}

message Clash2SingboxResponse {
  optional string singbox_config = 1 [default = ""];
  optional string error = 2 [default = ""];
}
