Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 216 additions & 0 deletions contracts/api/v2/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -28284,6 +28284,52 @@
],
"type": "object"
},
"HistoryImportCapability": {
"additionalProperties": false,
"properties": {
"allowed": {
"description": "Whether the current principal may use the capability",
"type": "boolean"
},
"max_plex_connections": {
"description": "How many Plex addresses one run races, counting plex_base_url",
"examples": [
8
],
"format": "int64",
"type": "integer"
},
"plex_connection_fallback": {
"description": "Whether plex_base_urls is honored and the advertised connections are raced",
"examples": [
true
],
"type": "boolean"
},
"revision": {
"description": "Opaque revision of this document",
"type": "string"
},
"state": {
"description": "Support and configuration state, not health",
"enum": [
"available",
"disabled",
"not_configured",
"unsupported"
],
"type": "string"
}
},
"required": [
"plex_connection_fallback",
"max_plex_connections",
"revision",
"state",
"allowed"
],
"type": "object"
},
"HistoryImportRun": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -28526,6 +28572,19 @@
"description": "Plex: a server address when the client holds its own token",
"type": "string"
},
"plex_base_urls": {
"description": "Plex: the other addresses plex.tv advertised for the same server, in preference order. Addresses the account may not reach under the local network policy are skipped, and the run races up to max_plex_connections of the rest, counting plex_base_url, keeping the first that answers. plex_base_url stays the preferred one.",
"examples": [
[
"https://relay.plex.direct:443"
]
],
"items": {
"type": "string"
},
"maxItems": 31,
"type": "array"
},
"plex_server_id": {
"description": "Plex: the client identifier of the server chosen from checkPlexPin",
"type": "string"
Expand Down Expand Up @@ -151651,6 +151710,163 @@
"x-silo-service-backed": true
}
},
"/api/v2/history-imports/capability": {
"get": {
"operationId": "getHistoryImportCapability",
"parameters": [
{
"description": "Optional first precondition, evaluated before If-None-Match: a tag that does not match the current representation is 412 precondition_failed.",
"in": "header",
"name": "If-Match",
"schema": {
"type": "string"
}
},
{
"in": "header",
"name": "If-None-Match",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/HistoryImportCapability"
}
}
},
"description": "OK",
"headers": {
"Cache-Control": {
"schema": {
"type": "string"
}
},
"ETag": {
"description": "The strong, opaque validator of the representation; send it back in If-Match on a guarded mutation or If-None-Match on a conditional read.",
"schema": {
"type": "string"
}
}
}
},
"304": {
"description": "The representation named by If-None-Match is current; no body.",
"headers": {
"ETag": {
"description": "The strong, opaque validator of the representation; send it back in If-Match on a guarded mutation or If-None-Match on a conditional read.",
"schema": {
"type": "string"
}
}
}
},
"400": {
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/Problem"
}
}
},
"description": "Bad Request"
},
"401": {
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/Problem"
}
}
},
"description": "Unauthorized"
},
"406": {
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/Problem"
}
}
},
"description": "Not Acceptable"
},
"412": {
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/Problem"
}
}
},
"description": "Precondition Failed",
"headers": {
"ETag": {
"description": "The strong, opaque validator of the representation; send it back in If-Match on a guarded mutation or If-None-Match on a conditional read.",
"schema": {
"type": "string"
}
}
}
},
"422": {
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/Problem"
}
}
},
"description": "Unprocessable Entity"
},
"429": {
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/Problem"
}
}
},
"description": "Too Many Requests"
},
"500": {
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/Problem"
}
}
},
"description": "Internal Server Error"
},
"503": {
"content": {
"application/problem+json": {
"schema": {
"$ref": "#/components/schemas/Problem"
}
}
},
"description": "Service Unavailable"
}
},
"security": [
{
"bearerAuth": []
}
],
"summary": "Describe what an import run may rely on, including Plex connection fallback.",
"tags": [
"history-imports"
],
"x-silo-class": "authenticated",
"x-silo-conditional": true
}
},
"/api/v2/history-imports/emby-connect/login": {
"post": {
"operationId": "loginEmbyConnect",
Expand Down
13 changes: 13 additions & 0 deletions docs/admin-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,19 @@ admitted before the setting was turned off fails with the same message. v1 run
responses and realtime history-import events carry the same safe summaries as
the v2 monitors. See [Outbound address guard](architecture/outbound-address-guard.md).

A Plex import races the addresses plex.tv advertised for the selected server, up to
eight, and keeps the first that returns a library listing for the rest of the run. A
session-backed run (`plex_session_id`) takes the list from the stored session; a client
holding its own token sends the preferred address in `plex_base_url` and the rest in
`plex_base_urls` (up to 31). When any address is HTTPS, cleartext ones are dropped,
because each probe carries the Plex token. Addresses the account may not reach under
the policy above are skipped, and the run is refused only when none remain; the eight
raced are the first that remain. The Plex token is removed from any redirect that
leaves the host it was sent to or downgrades from HTTPS to HTTP.
`GET /api/v2/history-imports/capability` reports `plex_connection_fallback` and
`max_plex_connections`. A server without it rejects `plex_base_urls` as an unknown
member, so check the capability before sending the field.

New queued personal imports survive server restart. Source changes invalidate captured
configuration without retargeting the import; stale running executions fail without replay.
Already committed history effects are retained. Historical personal jobs without durable
Expand Down
8 changes: 8 additions & 0 deletions docs/architecture/outbound-address-guard.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ was already accepted. A refused address fails with a message that tells the
user what to change (`historyimport.PrivateAddressMessage`), never as an
unreachable server.

A Plex run carries every address plex.tv advertised for the server and races
them. Admission checks each one and drops those the account may not reach,
refusing the run only when none remain. A session-backed run may name only
addresses the stored session advertised; the enqueue transaction recomputes
that list rather than trusting the queued credential. The Plex client removes
`X-Plex-Token` from any redirect that leaves the first request's host or
downgrades from HTTPS to HTTP, which `net/http` does not do for custom headers.

Run monitors, v1 run responses, and realtime history-import events carry only
the fixed summaries from `historyimport.PublicRun`; stored diagnostics can hold
an upstream response body and stay on the server.
Expand Down
2 changes: 2 additions & 0 deletions internal/api/testdata/media_routes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ DELETE /api/v2/favorites/{item_id} non-media
GET /api/v2/favorites/{item_id} non-media
PUT /api/v2/favorites/{item_id} non-media
GET /api/v2/history non-media
GET /api/v2/history-imports/capability non-media
POST /api/v2/history-imports/emby-connect/login non-media
POST /api/v2/history-imports/plex/auth/check non-media
POST /api/v2/history-imports/plex/auth/pin non-media
Expand Down Expand Up @@ -1806,6 +1807,7 @@ DELETE /api/v2/favorites/{item_id} non-media
GET /api/v2/favorites/{item_id} non-media
PUT /api/v2/favorites/{item_id} non-media
GET /api/v2/history non-media
GET /api/v2/history-imports/capability non-media
POST /api/v2/history-imports/emby-connect/login non-media
POST /api/v2/history-imports/plex/auth/check non-media
POST /api/v2/history-imports/plex/auth/pin non-media
Expand Down
69 changes: 54 additions & 15 deletions internal/apiv2/history_imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,40 @@ type runPosition struct {
// source needs depend on source; the service rejects an incomplete
// combination with a validation problem.
type HistoryImportRunCreate struct {
ProfileID ID `json:"profile_id" minLength:"1" doc:"The profile to write history into" example:"p-owner"`
Source string `json:"source" enum:"emby,jellyfin,plex" doc:"Which kind of server to import from" example:"plex"`
ConnectSessionID string `json:"connect_session_id,omitempty" doc:"Emby: the session from loginEmbyConnect" example:"9c3e1f0a-1b2c-4d5e-8f6a-7b8c9d0e1f2a"`
ServerID string `json:"server_id,omitempty" doc:"Emby: the server chosen from the connect session"`
SourceID *ID `json:"source_id,omitempty" nullable:"false" doc:"Emby or Plex: a configured source from listHistoryImportSources" example:"1"`
Username string `json:"username,omitempty" doc:"Emby: the source server user name when importing from a configured source"`
Password string `json:"password,omitempty" doc:"Emby: the source server password when importing from a configured source; empty for an account without one"`
JellyfinBaseURL string `json:"jellyfin_base_url,omitempty" doc:"Jellyfin: the server address" example:"https://jellyfin.example.test"`
JellyfinUsername string `json:"jellyfin_username,omitempty" doc:"Jellyfin: the user name"`
JellyfinPassword string `json:"jellyfin_password,omitempty" doc:"Jellyfin: the password"`
PlexSessionID string `json:"plex_session_id,omitempty" doc:"Plex: the authenticated session from createPlexPin"`
PlexServerID string `json:"plex_server_id,omitempty" doc:"Plex: the client identifier of the server chosen from checkPlexPin"`
PlexBaseURL string `json:"plex_base_url,omitempty" doc:"Plex: a server address when the client holds its own token"`
PlexToken string `json:"plex_token,omitempty" doc:"Plex: the server access token that goes with plex_base_url"`
PlexAccountToken string `json:"plex_account_token,omitempty" doc:"Plex: the plex.tv account token, for watchlist import alongside a server token"`
ProfileID ID `json:"profile_id" minLength:"1" doc:"The profile to write history into" example:"p-owner"`
Source string `json:"source" enum:"emby,jellyfin,plex" doc:"Which kind of server to import from" example:"plex"`
ConnectSessionID string `json:"connect_session_id,omitempty" doc:"Emby: the session from loginEmbyConnect" example:"9c3e1f0a-1b2c-4d5e-8f6a-7b8c9d0e1f2a"`
ServerID string `json:"server_id,omitempty" doc:"Emby: the server chosen from the connect session"`
SourceID *ID `json:"source_id,omitempty" nullable:"false" doc:"Emby or Plex: a configured source from listHistoryImportSources" example:"1"`
Username string `json:"username,omitempty" doc:"Emby: the source server user name when importing from a configured source"`
Password string `json:"password,omitempty" doc:"Emby: the source server password when importing from a configured source; empty for an account without one"`
JellyfinBaseURL string `json:"jellyfin_base_url,omitempty" doc:"Jellyfin: the server address" example:"https://jellyfin.example.test"`
JellyfinUsername string `json:"jellyfin_username,omitempty" doc:"Jellyfin: the user name"`
JellyfinPassword string `json:"jellyfin_password,omitempty" doc:"Jellyfin: the password"`
PlexSessionID string `json:"plex_session_id,omitempty" doc:"Plex: the authenticated session from createPlexPin"`
PlexServerID string `json:"plex_server_id,omitempty" doc:"Plex: the client identifier of the server chosen from checkPlexPin"`
PlexBaseURL string `json:"plex_base_url,omitempty" doc:"Plex: a server address when the client holds its own token"`
PlexBaseURLs []string `json:"plex_base_urls,omitempty" maxItems:"31" doc:"Plex: the other addresses plex.tv advertised for the same server, in preference order. Addresses the account may not reach under the local network policy are skipped, and the run races up to max_plex_connections of the rest, counting plex_base_url, keeping the first that answers. plex_base_url stays the preferred one." example:"[\"https://relay.plex.direct:443\"]"`
PlexToken string `json:"plex_token,omitempty" doc:"Plex: the server access token that goes with plex_base_url"`
PlexAccountToken string `json:"plex_account_token,omitempty" doc:"Plex: the plex.tv account token, for watchlist import alongside a server token"`
}

// HistoryImportCapability describes what a client may rely on when it submits
// an import run. A server without Plex connection fallback rejects
// plex_base_urls as an unknown member, so a client checks this before sending
// it and falls back to plex_base_url alone.
type HistoryImportCapability struct {
Capability
PlexConnectionFallback bool `json:"plex_connection_fallback" doc:"Whether plex_base_urls is honored and the advertised connections are raced" example:"true"`
MaxPlexConnections int `json:"max_plex_connections" doc:"How many Plex addresses one run races, counting plex_base_url" example:"8"`
}

// HistoryImportCapabilityOutput is the getHistoryImportCapability response.
type HistoryImportCapabilityOutput struct {
Status int
ETag string `header:"ETag"`
CacheControl string `header:"Cache-Control"`
Body HistoryImportCapability
}

// HistoryImportRunCreateInput is the createHistoryImportRun request.
Expand Down Expand Up @@ -314,6 +333,25 @@ func registerHistoryImports(reg *Registry) {

Register(reg, accountOp(humaOp(http.MethodPost, Prefix+"/history-imports/emby-connect/login", "loginEmbyConnect", "history-imports",
"Sign in to Emby Connect and list the account's servers in a short-lived session.")), reg.loginEmbyConnect)

Register(reg, Operation{
Operation: humaOp(http.MethodGet, Prefix+"/history-imports/capability", "getHistoryImportCapability", "history-imports",
"Describe what an import run may rely on, including Plex connection fallback."),
Class: ClassAuthenticated,
}, reg.getHistoryImportCapability)
}

// getHistoryImportCapability answers from constants and the wiring check, so
// it stays usable on a deployment whose import service cannot reach anything.
func (reg *Registry) getHistoryImportCapability(_ context.Context, _ *CapabilityInput) (*HistoryImportCapabilityOutput, error) {
return &HistoryImportCapabilityOutput{
CacheControl: cachePrivateNoCache,
Body: HistoryImportCapability{
Capability: Capability{State: configuredCapabilityState(reg.deps.HistoryImports != nil)},
PlexConnectionFallback: true,
MaxPlexConnections: historyimport.MaxPlexConnectionCandidates,
},
}, nil
}

// historyImports is the wired service, or the fail-closed problem.
Expand Down Expand Up @@ -530,6 +568,7 @@ func (b HistoryImportRunCreate) toInput() (historyimport.CreateRunInput, *Proble
PlexSessionID: b.PlexSessionID,
PlexServerID: b.PlexServerID,
PlexBaseURL: b.PlexBaseURL,
PlexBaseURLs: b.PlexBaseURLs,
PlexToken: b.PlexToken,
PlexAccountToken: b.PlexAccountToken,
}
Expand Down
Loading
Loading