syntax = "proto3";
package burrow;

import "google/protobuf/timestamp.proto";

service Tunnel {
    rpc TunnelConfiguration (Empty) returns (stream TunnelConfigurationResponse);
    rpc TunnelPackets (stream TunnelPacket) returns (stream TunnelPacket);
    rpc TunnelStart (Empty) returns (Empty);
    rpc TunnelStop (Empty) returns (Empty);
    rpc TunnelStatus (Empty) returns (stream TunnelStatusResponse);
}

service Networks {
    rpc NetworkAdd (Network) returns (Empty);
    rpc NetworkList (Empty) returns (stream NetworkListResponse);
    rpc NetworkReorder (NetworkReorderRequest) returns (Empty);
    rpc NetworkDelete (NetworkDeleteRequest) returns (Empty);
}

service TailnetControl {
    rpc Discover (TailnetDiscoverRequest) returns (TailnetDiscoverResponse);
    rpc Probe (TailnetProbeRequest) returns (TailnetProbeResponse);
    rpc LoginStart (TailnetLoginStartRequest) returns (TailnetLoginStatusResponse);
    rpc LoginStatus (TailnetLoginStatusRequest) returns (TailnetLoginStatusResponse);
    rpc LoginCancel (TailnetLoginCancelRequest) returns (Empty);
}

message NetworkReorderRequest {
    int32 id = 1;
    int32 index = 2;
}

message WireGuardPeer {
    string endpoint = 1;
    repeated string subnet = 2;
}

message WireGuardNetwork {
    string address = 1;
    string dns = 2;
    repeated WireGuardPeer peer = 3;
}

message NetworkDeleteRequest {
    int32 id = 1;
}

message Network {
    int32 id = 1;
    NetworkType type = 2;
    bytes payload = 3;
}

enum NetworkType {
    WireGuard = 0;
    Tailnet = 1;
}

message NetworkListResponse {
    repeated Network network = 1;
}

message Empty {

}

message TailnetDiscoverRequest {
    string email = 1;
}

message TailnetDiscoverResponse {
    string domain = 1;
    string authority = 2;
    string oidc_issuer = 3;
    bool managed = 4;
}

message TailnetProbeRequest {
    string authority = 1;
}

message TailnetProbeResponse {
    string authority = 1;
    int32 status_code = 2;
    string summary = 3;
    string detail = 4;
    bool reachable = 5;
}

message TailnetLoginStartRequest {
    string account_name = 1;
    string identity_name = 2;
    string hostname = 3;
    string authority = 4;
}

message TailnetLoginStatusRequest {
    string session_id = 1;
}

message TailnetLoginCancelRequest {
    string session_id = 1;
}

message TailnetLoginStatusResponse {
    string session_id = 1;
    string backend_state = 2;
    string auth_url = 3;
    bool running = 4;
    bool needs_login = 5;
    string tailnet_name = 6;
    string magic_dns_suffix = 7;
    string self_dns_name = 8;
    repeated string tailnet_ips = 9;
    repeated string health = 10;
}

enum State {
    Stopped = 0;
    Running = 1;
}

message TunnelStatusResponse {
    State state = 1;
    optional google.protobuf.Timestamp start = 2;
}

message TunnelConfigurationResponse {
    repeated string addresses = 1;
    int32 mtu = 2;
    repeated string routes = 3;
    repeated string dns_servers = 4;
    repeated string search_domains = 5;
    bool include_default_route = 6;
}

message TunnelPacket {
    bytes payload = 1;
}
