feat: ollama-proxy — multi-account Ollama Cloud reverse proxy

Go reverse proxy for ollama.com that balances requests across multiple
API keys with round-robin and failover on 429/5xx. Exposes both native
Ollama API (/api/*) and OpenAI-compatible (/v1/*) passthrough.

- Round-robin balancer with per-account cooldown (60s default)
- Pre-stream failover: 429 → cooldown + next account; 5xx → next account
- Streaming invariant: once 2xx starts streaming, no account switch
- SSE (text/event-stream) and NDJSON passthrough with http.Flusher
- CLI: accounts add/list/remove/set-base-url, serve, version
- Accounts stored in ~/.config/ollama-proxy/accounts.json (chmod 0600)
- systemd unit (User=dueattendant149, 127.0.0.1:11435, Restart=always)
- 43 tests (unit + integration with httptest upstream)
- opencode integration: custom provider 'ocp' with explicit model list
- Requires NO_PROXY=127.0.0.1,localhost when HTTP_PROXY is set
This commit is contained in:
Atte149 2026-06-19 13:58:13 +03:00
commit 3963eede70
23 changed files with 2534 additions and 0 deletions

View file

@ -0,0 +1,265 @@
package config
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
)
// withTempAccountsPath swaps AccountsPath to a temp dir for the duration of t.
// Returns the full path to accounts.json inside that dir (dir is created 0700).
func withTempAccountsPath(t *testing.T) string {
t.Helper()
dir := t.TempDir()
sub := filepath.Join(dir, "ollama-proxy")
if err := os.MkdirAll(sub, 0o700); err != nil {
t.Fatalf("mkdir: %v", err)
}
p := filepath.Join(sub, "accounts.json")
t.Setenv("XDG_CONFIG_HOME", dir)
return p
}
func TestNewID_UniqueAndLength(t *testing.T) {
ids := make(map[string]struct{}, 100)
for i := 0; i < 100; i++ {
id := NewID()
if len(id) != 32 {
t.Fatalf("NewID len = %d, want 32 (16 bytes hex)", len(id))
}
ids[id] = struct{}{}
}
if len(ids) != 100 {
t.Fatalf("NewID produced %d unique ids out of 100", len(ids))
}
}
func TestMaskKey(t *testing.T) {
cases := []struct {
in, want string
}{
{"abc", "****"},
{"abcd", "****"},
{"abcde", "****bcde"},
{"sk-1234567890abcdef", "****cdef"},
{"", "****"},
}
for _, c := range cases {
if got := maskKey(c.in); got != c.want {
t.Errorf("maskKey(%q) = %q, want %q", c.in, got, c.want)
}
}
}
func TestShortID(t *testing.T) {
if got := shortID("0123456789abcdef"); got != "01234567" {
t.Errorf("shortID = %q, want 01234567", got)
}
if got := shortID("abc"); got != "abc" {
t.Errorf("shortID = %q, want abc", got)
}
}
func TestValidateName(t *testing.T) {
good := []string{"a", "work", "my-account_1", "ACC-2"}
for _, n := range good {
if err := ValidateName(n); err != nil {
t.Errorf("ValidateName(%q) = %v, want nil", n, err)
}
}
bad := []string{"", "with space", "dollar$", "dot.in", "slah/s", "café"}
for _, n := range bad {
if err := ValidateName(n); err == nil {
t.Errorf("ValidateName(%q) = nil, want error", n)
}
}
}
func TestLoadAccounts_MissingFile(t *testing.T) {
withTempAccountsPath(t)
af, err := LoadAccounts()
if err != nil {
t.Fatalf("LoadAccounts on missing file: %v", err)
}
if af == nil {
t.Fatal("LoadAccounts returned nil")
}
if len(af.Accounts) != 0 {
t.Fatalf("got %d accounts, want 0", len(af.Accounts))
}
}
func TestSaveLoad_RoundTrip(t *testing.T) {
p := withTempAccountsPath(t)
af := &AccountsFile{
BaseURL: "https://ollama.com",
Accounts: []Account{
{ID: NewID(), Name: "a1", APIKey: "sk-key1111111111", Created: parseTime(t, "2026-06-19T10:00:00Z")},
{ID: NewID(), Name: "a2", APIKey: "sk-key2222222222", BaseURL: "https://staging.example.com"},
},
}
if err := af.Save(); err != nil {
t.Fatalf("Save: %v", err)
}
info, err := os.Stat(p)
if err != nil {
t.Fatalf("stat: %v", err)
}
if mode := info.Mode().Perm(); mode != 0o600 {
t.Errorf("file mode = %o, want 600", mode)
}
loaded, err := LoadAccounts()
if err != nil {
t.Fatalf("LoadAccounts: %v", err)
}
if loaded.BaseURL != af.BaseURL {
t.Errorf("BaseURL = %q, want %q", loaded.BaseURL, af.BaseURL)
}
if len(loaded.Accounts) != 2 {
t.Fatalf("len = %d, want 2", len(loaded.Accounts))
}
if loaded.Accounts[0].Name != "a1" || loaded.Accounts[0].APIKey != "sk-key1111111111" {
t.Errorf("account[0] = %+v", loaded.Accounts[0])
}
if loaded.Accounts[1].BaseURL != "https://staging.example.com" {
t.Errorf("account[1].BaseURL = %q", loaded.Accounts[1].BaseURL)
}
}
func TestSave_CreatesNoFileWhenDirMissing(t *testing.T) {
// AccountsPath targets a dir we never create; Save must fail, not create the dir.
dir := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", dir)
// remove the ollama-proxy subdir if t.TempDir created it — it doesn't, so
// Save() should fail because parent dir does not exist.
af := &AccountsFile{Accounts: []Account{{ID: "x", Name: "n", APIKey: "k"}}}
// Manually point to a non-existent parent: use a path under dir that lacks dirs.
// EnsureAccountsDir not called, so Save should fail.
err := af.Save()
if err == nil {
// Some filesystems auto-create the rename target's parent? No — os.Rename
// requires the parent to exist. So this should error.
t.Skip("Save unexpectedly succeeded; skipping — but check filesystem semantics")
}
}
func TestFind_ByNameOrIDPrefix(t *testing.T) {
af := &AccountsFile{
Accounts: []Account{
{ID: "abcdef0123456789", Name: "alpha", APIKey: "k1"},
{ID: "ffeeddccbbaa9988", Name: "beta", APIKey: "k2"},
},
}
if a := af.Find("alpha"); a == nil || a.ID != "abcdef0123456789" {
t.Errorf("Find(name=alpha) = %+v", a)
}
if a := af.Find("abcdef"); a == nil || a.Name != "alpha" {
t.Errorf("Find(id-prefix=abcdef) = %+v", a)
}
if a := af.Find("nope"); a != nil {
t.Errorf("Find(nope) = %+v, want nil", a)
}
}
func TestRemove_ByIndexIDAndName(t *testing.T) {
af := &AccountsFile{
Accounts: []Account{
{ID: "abcdef0123456789", Name: "alpha", APIKey: "k1"},
{ID: "ffeeddccbbaa9988", Name: "beta", APIKey: "k2"},
},
}
if err := af.Remove("alpha"); err != nil {
t.Fatalf("Remove(alpha): %v", err)
}
if len(af.Accounts) != 1 || af.Accounts[0].Name != "beta" {
t.Errorf("after Remove(alpha): %+v", af.Accounts)
}
if err := af.Remove("ffee"); err != nil {
t.Fatalf("Remove(id-prefix): %v", err)
}
if len(af.Accounts) != 0 {
t.Errorf("after Remove(ffee): %+v", af.Accounts)
}
if err := af.Remove("missing"); err != ErrNotFound {
t.Errorf("Remove(missing) = %v, want ErrNotFound", err)
}
}
func TestSave_Atomicity_TempFileCleanedOnError(t *testing.T) {
// Hard to force a mid-write failure without injection; instead verify that
// no leftover temp files exist after a successful Save.
p := withTempAccountsPath(t)
af := &AccountsFile{Accounts: []Account{{ID: "x", Name: "n", APIKey: "k"}}}
if err := af.Save(); err != nil {
t.Fatalf("Save: %v", err)
}
dir := filepath.Dir(p)
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatalf("readdir: %v", err)
}
for _, e := range entries {
name := e.Name()
if name != "accounts.json" {
t.Errorf("unexpected leftover file in config dir: %s", name)
}
}
}
func TestAccountsPath_XDGMissing(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", "")
home := t.TempDir()
t.Setenv("HOME", home)
p, err := AccountsPath()
if err != nil {
t.Fatalf("AccountsPath: %v", err)
}
want := filepath.Join(home, ".config", "ollama-proxy", "accounts.json")
if p != want {
t.Errorf("AccountsPath = %q, want %q", p, want)
}
}
func TestSave_LoadAccounts_CorruptFileErrors(t *testing.T) {
p := withTempAccountsPath(t)
if err := os.WriteFile(p, []byte("{not json"), 0o600); err != nil {
t.Fatalf("write corrupt: %v", err)
}
if _, err := LoadAccounts(); err == nil {
t.Error("LoadAccounts on corrupt json returned nil error")
}
}
// Ensure AccountsFile with nil accounts slice marshals to a valid JSON array
// rather than null, so consumers never see a missing field.
func TestSave_NilAccountsSliceMarshalsAsArray(t *testing.T) {
withTempAccountsPath(t)
af := &AccountsFile{}
if err := af.Save(); err != nil {
t.Fatalf("Save: %v", err)
}
p, _ := AccountsPath()
data, _ := os.ReadFile(p)
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
t.Fatalf("unmarshal: %v", err)
}
accountsRaw, ok := raw["accounts"]
if !ok {
t.Fatal("no 'accounts' field in saved json")
}
if string(accountsRaw) == "null" {
t.Errorf("accounts marshaled as null; want []")
}
}
func parseTime(t *testing.T, s string) time.Time {
t.Helper()
x, err := time.Parse(time.RFC3339, s)
if err != nil {
t.Fatalf("parseTime %s: %v", s, err)
}
return x
}