mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 21:36:40 -10:00
devvm agent login codex|opencode logs the agent in from inside this
folder's VM, so this machine never needs Codex or opencode, and devvm's
agent store keeps the login for every VM with the agent. It sits under
devvm agent beside import and save: it fills the store, as they do, and
a bare devvm login would read as a verb on the VM itself.
- Browser login (the default): the agent's own login runs in the VM on
this terminal (codex -c cli_auth_credentials_store="file" login;
opencode auth login --provider openai --method "ChatGPT Pro/Plus
(browser)"), and devvm forwards this machine's localhost:1455, and
1457 (Codex's fallback) when free, on 127.0.0.1 and ::1, to the same
ports on the VM's loopback, only while the login runs: closed when it
ends, fails or is interrupted (AddCleanup), and an interrupt also ends
the agent's login in the VM (its pid kept in ~/.cache/devvm-login.pid),
so its server doesn't linger for the VM's port forwarder. Port 1455
taken on this machine: refused, naming --device-code.
- --device-code: codex login --device-auth, or opencode's headless
ChatGPT login; nothing forwarded.
- --api-key [--provider anthropic|openai|openrouter] [--stdin]: the key
is asked for on this machine (msg.Question answered by --stdin) and
saved in the store, never in the VM: Codex's auth.json becomes
{auth_mode: apikey, OPENAI_API_KEY}, opencode's gets the provider's
{type: api, key} beside the others; the running VM gets its copy with
a placeholder and its proxy the binding at once.
- The proxy's capture (agentlogin's capturers) saves ChatGPT logins;
devvm then confirms what the store saved, with the account's email,
and fails when the login saved nothing new.
- opencode 1.19.5 has no Anthropic OAuth in its core (no Anthropic
plugin among its built-ins), so there's no pasted-code capturer:
Anthropic is an API key. opencode's OpenAI login listens on 1455 only;
Codex 0.166 on 1455, falling back to 1457.
- vmssh: SSH.ForwardLoopback (LoopbackForward: listeners in devvm's own
process, each connection its own ssh -W to the VM's 127.0.0.1, no
master; ErrPortTaken).
- agentlogin: KeyProviders, File.SetKey, File.ChatGPT.
- secrets: Store.SaveKey, LoginSum, ChatGPT (loginsDir shared with
Capture).
- vm: AgentLogin (agentlogin.go), LoginAgents.
- cli: agentlogin.go (--device-code, --api-key, --provider, --stdin,
--here/--parent); usage errors before anything runs.
- assets/guest/sync.sh: cli_auth_credentials_store = "file" first in
the VM's ~/.codex/config.toml on every launch.
- Messages: agent_login_* and agent_key_*; codex_no_key,
codex_no_chatgpt, codex_refresh_failed and opencode_refresh_failed
point at devvm agent login.
- Tests: agent_login.txtar (usage errors, no VM, no agent, device code
argv, browser forward and its end, the agent failing, a login saved
while it runs, port taken, interrupt, API keys, the VM's copy failing,
no terminal, an empty key), unit tests for the forward, SetKey,
ChatGPT, SaveKey, the confirmation and the argv; coverage baselines
raised.
- e2e (network): the real Codex's login in the VM against the run's
fake token endpoint, its callback opened through the forward, the
store's tokens and key, placeholders in the VM, the forward gone; an
interrupted login takes its forward away.
- Docs: Logging in with devvm agent login (using/coding-agents.md),
the Commands page, the questions table, Contributing's e2e, the cli
skill's tree, AGENTS.md; the CLI reference.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
126 lines
3.8 KiB
Go
126 lines
3.8 KiB
Go
package vmssh
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"slices"
|
|
"strconv"
|
|
"sync"
|
|
"syscall"
|
|
|
|
"github.com/w3cj/devvm/internal/platform"
|
|
)
|
|
|
|
// LoopbackForward forwards ports on this machine's loopback to the same
|
|
// ports on a VM's loopback, for as long as it's open: a login's callback,
|
|
// which a browser on this machine opens, reaches the agent's own server in
|
|
// the VM through it. It runs in devvm's own process, so it ends with it.
|
|
// It listens on 127.0.0.1 and ::1, as a browser reaches localhost at
|
|
// either, never on a port a program on this machine has (ListenLoopback),
|
|
// and each connection is an ssh channel of its own (ssh -W) to the VM's
|
|
// 127.0.0.1, which the VM's sshd opens: no master, nothing forwarded the
|
|
// other way.
|
|
type LoopbackForward struct {
|
|
ls []net.Listener
|
|
cancel context.CancelFunc
|
|
wg sync.WaitGroup
|
|
}
|
|
|
|
// ErrPortTaken is ForwardLoopback's error when a program on this machine
|
|
// has the port it needs on 127.0.0.1.
|
|
var ErrPortTaken = errors.New("a program on this machine has the port")
|
|
|
|
// ForwardLoopback forwards need, and each of also that it can listen on,
|
|
// from this machine's loopback to the same port in the VM, until Close or
|
|
// ctx's end. Without 127.0.0.1's need it forwards nothing and returns
|
|
// ErrPortTaken, or the listener's error.
|
|
func (o SSH) ForwardLoopback(ctx context.Context, need int, also ...int) (*LoopbackForward, error) {
|
|
return o.forwardLoopback(ctx, func(ctx context.Context, conn net.Conn, port int) {
|
|
pipeSSH(ctx, conn, o.loopbackArgs(port))
|
|
}, need, also...)
|
|
}
|
|
|
|
// forwardLoopback is ForwardLoopback, each connection carried by carry.
|
|
func (o SSH) forwardLoopback(ctx context.Context, carry func(context.Context, net.Conn, int), need int, also ...int) (*LoopbackForward, error) {
|
|
listen := func(host string, port int) (net.Listener, error) {
|
|
return platform.ListenLoopback(ctx, net.JoinHostPort(host, strconv.Itoa(port)))
|
|
}
|
|
l, err := listen(Loopback, need)
|
|
if err != nil {
|
|
if errors.Is(err, syscall.EADDRINUSE) {
|
|
return nil, ErrPortTaken
|
|
}
|
|
return nil, err
|
|
}
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
f := &LoopbackForward{ls: []net.Listener{l}, cancel: cancel}
|
|
for _, p := range slices.Concat([]int{need}, also) {
|
|
for _, h := range []string{Loopback, "::1"} {
|
|
if p == need && h == Loopback {
|
|
continue
|
|
}
|
|
if l, err := listen(h, p); err == nil {
|
|
f.ls = append(f.ls, l)
|
|
}
|
|
}
|
|
}
|
|
for _, l := range f.ls {
|
|
port := l.Addr().(*net.TCPAddr).Port
|
|
f.wg.Go(func() { f.serve(ctx, l, port, carry) })
|
|
}
|
|
go func() {
|
|
<-ctx.Done()
|
|
f.closeListeners()
|
|
}()
|
|
return f, nil
|
|
}
|
|
|
|
// serve accepts l's connections until it's closed, carrying each to port.
|
|
func (f *LoopbackForward) serve(ctx context.Context, l net.Listener, port int, carry func(context.Context, net.Conn, int)) {
|
|
var conns sync.WaitGroup
|
|
defer conns.Wait()
|
|
for {
|
|
c, err := l.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
conns.Go(func() { carry(ctx, c, port) })
|
|
}
|
|
}
|
|
|
|
func (f *LoopbackForward) closeListeners() {
|
|
for _, l := range f.ls {
|
|
_ = l.Close()
|
|
}
|
|
}
|
|
|
|
// Ports are the ports it listens on, each once, in order.
|
|
func (f *LoopbackForward) Ports() []int {
|
|
var ps []int
|
|
for _, l := range f.ls {
|
|
if p := l.Addr().(*net.TCPAddr).Port; !slices.Contains(ps, p) {
|
|
ps = append(ps, p)
|
|
}
|
|
}
|
|
return ps
|
|
}
|
|
|
|
// Close stops listening, ends the connections it carries and waits for
|
|
// them. It can be called more than once.
|
|
func (f *LoopbackForward) Close() {
|
|
f.cancel()
|
|
f.closeListeners()
|
|
f.wg.Wait()
|
|
}
|
|
|
|
// loopbackArgs are ssh's arguments for one forwarded connection: a
|
|
// connection of its own to the VM, as connArgs has it, and a channel to
|
|
// port on the VM's 127.0.0.1.
|
|
func (o SSH) loopbackArgs(port int) []string {
|
|
return slices.Concat(
|
|
[]string{"-F", "none", "-o", "ControlMaster=no", "-o", "ControlPath=none", "-W", net.JoinHostPort(Loopback, strconv.Itoa(port))},
|
|
o.connArgs(),
|
|
)
|
|
}
|