adding velocity control
All checks were successful
Build and Deploy (internal) / Build Transmission Manager Image (push) Successful in 1m28s
Build and Deploy (internal) / Deploy Transmission Manager (internal) (push) Successful in 15s

This commit is contained in:
2026-07-09 18:15:27 -03:00
parent 41cc37d85e
commit 44889338c2
5 changed files with 276 additions and 8 deletions

100
main.go
View File

@@ -165,7 +165,7 @@ var torrentFields = []string{
type rpcRequest struct {
Method string `json:"method"`
Arguments rpcArgs `json:"arguments"`
Arguments interface{} `json:"arguments"`
Tag interface{} `json:"tag,omitempty"`
}
@@ -179,6 +179,16 @@ type rpcResponse struct {
Args json.RawMessage `json:"arguments"`
}
var sessionFields = []string{
"alt-speed-enabled", "alt-speed-down", "alt-speed-up",
"speed-limit-down-enabled", "speed-limit-down",
"speed-limit-up-enabled", "speed-limit-up", "units",
}
type sessionSetArgs struct {
AltSpeedEnabled bool `json:"alt-speed-enabled"`
}
func (c *Client) Torrents(ctx context.Context) (json.RawMessage, error) {
reqBody, _ := json.Marshal(rpcRequest{
Method: "torrent-get",
@@ -216,6 +226,44 @@ func (c *Client) Stats(ctx context.Context) (json.RawMessage, error) {
return resp.Args, nil
}
func (c *Client) Session(ctx context.Context) (json.RawMessage, error) {
reqBody, _ := json.Marshal(rpcRequest{
Method: "session-get",
Arguments: rpcArgs{Fields: sessionFields},
})
data, err := c.rpc(ctx, reqBody)
if err != nil {
return nil, err
}
var resp rpcResponse
if err := json.Unmarshal(data, &resp); err != nil {
return nil, err
}
if resp.Result != "success" {
return nil, fmt.Errorf("rpc result: %s", resp.Result)
}
return resp.Args, nil
}
func (c *Client) SetAlternativeSpeed(ctx context.Context, enabled bool) error {
reqBody, _ := json.Marshal(rpcRequest{
Method: "session-set",
Arguments: sessionSetArgs{AltSpeedEnabled: enabled},
})
data, err := c.rpc(ctx, reqBody)
if err != nil {
return err
}
var resp rpcResponse
if err := json.Unmarshal(data, &resp); err != nil {
return err
}
if resp.Result != "success" {
return fmt.Errorf("rpc result: %s", resp.Result)
}
return nil
}
func (c *Client) TorrentAction(ctx context.Context, method string, ids []int) error {
reqBody, _ := json.Marshal(rpcRequest{
Method: method,
@@ -450,6 +498,38 @@ func handleTorrentAction(client *Client, setupErr error) http.HandlerFunc {
}
}
func handleAlternativeSpeed(client *Client, setupErr error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost)
writeError(w, http.StatusMethodNotAllowed, "method not allowed")
return
}
if !requireTransmissionClient(w, client, setupErr) {
return
}
var payload struct {
Enabled *bool `json:"enabled"`
}
decoder := json.NewDecoder(io.LimitReader(r.Body, 1024))
if err := decoder.Decode(&payload); err != nil || payload.Enabled == nil {
writeError(w, http.StatusBadRequest, "request body must include a boolean enabled field")
return
}
if err := decoder.Decode(&struct{}{}); err != io.EOF {
writeError(w, http.StatusBadRequest, "request body must contain one JSON object")
return
}
if err := client.SetAlternativeSpeed(r.Context(), *payload.Enabled); err != nil {
log.Printf("alternative speed: %v", err)
writeError(w, http.StatusBadGateway, err.Error())
return
}
writeJSON(w, json.RawMessage(`{"ok":true}`))
}
}
func handleStaticFiles(fileServer http.Handler, client *Client, setupErr error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && (r.URL.Path == "/" || r.URL.Path == "/index.html") {
@@ -509,6 +589,24 @@ func main() {
}
writeJSON(w, args)
})
mux.HandleFunc("/api/session", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
writeError(w, http.StatusMethodNotAllowed, "method not allowed")
return
}
if !requireTransmissionClient(w, client, setupErr) {
return
}
args, err := client.Session(r.Context())
if err != nil {
log.Printf("session: %v", err)
writeError(w, http.StatusBadGateway, err.Error())
return
}
writeJSON(w, args)
})
mux.HandleFunc("/api/session/alternative-speed", handleAlternativeSpeed(client, setupErr))
// Static files from embedded web/ directory
webRoot, err := fs.Sub(webFS, "web")