Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbe118e6

Browse files
committed
reduce scope
1 parent578ebb0 commitbe118e6

File tree

6 files changed

+66
-181
lines changed

6 files changed

+66
-181
lines changed

‎cli/cliutil/stdioconn.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,31 @@ import (
66
"time"
77
)
88

9-
typeStdioConnstruct {
9+
typeReaderWriterConnstruct {
1010
io.Reader
1111
io.Writer
1212
}
1313

14-
func (*StdioConn)Close() (errerror) {
14+
func (*ReaderWriterConn)Close() (errerror) {
1515
returnnil
1616
}
1717

18-
func (*StdioConn)LocalAddr() net.Addr {
18+
func (*ReaderWriterConn)LocalAddr() net.Addr {
1919
returnnil
2020
}
2121

22-
func (*StdioConn)RemoteAddr() net.Addr {
22+
func (*ReaderWriterConn)RemoteAddr() net.Addr {
2323
returnnil
2424
}
2525

26-
func (*StdioConn)SetDeadline(_ time.Time)error {
26+
func (*ReaderWriterConn)SetDeadline(_ time.Time)error {
2727
returnnil
2828
}
2929

30-
func (*StdioConn)SetReadDeadline(_ time.Time)error {
30+
func (*ReaderWriterConn)SetReadDeadline(_ time.Time)error {
3131
returnnil
3232
}
3333

34-
func (*StdioConn)SetWriteDeadline(_ time.Time)error {
34+
func (*ReaderWriterConn)SetWriteDeadline(_ time.Time)error {
3535
returnnil
3636
}

‎cli/ssh.go

Lines changed: 50 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (r *RootCmd) ssh() *serpent.Command {
6767
stdiobool
6868
hostPrefixstring
6969
hostnameSuffixstring
70-
forceTunnelbool
70+
forceNewTunnelbool
7171
forwardAgentbool
7272
forwardGPGbool
7373
identityAgentstring
@@ -278,27 +278,38 @@ func (r *RootCmd) ssh() *serpent.Command {
278278
returnerr
279279
}
280280

281-
// See if we can use the Coder Connect tunnel
282-
if!forceTunnel {
281+
// If we're in stdio mode, check to see if we can use Coder Connect.
282+
// We don't support Coder Connect over non-stdio coder ssh yet.
283+
ifstdio&&!forceNewTunnel {
283284
connInfo,err:=wsClient.AgentConnectionInfoGeneric(ctx)
284285
iferr!=nil {
285286
returnxerrors.Errorf("get agent connection info: %w",err)
286287
}
287-
288288
coderConnectHost:=fmt.Sprintf("%s.%s.%s.%s",
289289
workspaceAgent.Name,workspace.Name,workspace.OwnerName,connInfo.HostnameSuffix)
290290
exists,_:=workspacesdk.ExistsViaCoderConnect(ctx,coderConnectHost)
291291
ifexists {
292292
_,_=fmt.Fprintln(inv.Stderr,"Connecting to workspace via Coder Connect...")
293293
defercancel()
294-
addr:=fmt.Sprintf("%s:22",coderConnectHost)
295-
ifstdio {
294+
295+
ifnetworkInfoDir!="" {
296296
iferr:=writeCoderConnectNetInfo(ctx,networkInfoDir);err!=nil {
297297
logger.Error(ctx,"failed to write coder connect net info file",slog.Error(err))
298298
}
299-
returnrunCoderConnectStdio(ctx,addr,stdioReader,stdioWriter,stack)
300299
}
301-
returnrunCoderConnectPTY(ctx,addr,inv.Stdin,inv.Stdout,inv.Stderr,stack)
300+
301+
stopPolling:=tryPollWorkspaceAutostop(ctx,client,workspace)
302+
deferstopPolling()
303+
304+
usageAppName:=getUsageAppName(usageApp)
305+
ifusageAppName!="" {
306+
closeUsage:=client.UpdateWorkspaceUsageWithBodyContext(ctx,workspace.ID, codersdk.PostWorkspaceUsageRequest{
307+
AgentID:workspaceAgent.ID,
308+
AppName:usageAppName,
309+
})
310+
defercloseUsage()
311+
}
312+
returnrunCoderConnectStdio(ctx,fmt.Sprintf("%s:22",coderConnectHost),stdioReader,stdioWriter,stack)
302313
}
303314
}
304315

@@ -481,11 +492,36 @@ func (r *RootCmd) ssh() *serpent.Command {
481492
stdinFile,validIn:=inv.Stdin.(*os.File)
482493
stdoutFile,validOut:=inv.Stdout.(*os.File)
483494
ifvalidIn&&validOut&&isatty.IsTerminal(stdinFile.Fd())&&isatty.IsTerminal(stdoutFile.Fd()) {
484-
restorePtyFn,err:=configurePTY(ctx,stdinFile,stdoutFile,sshSession)
485-
deferrestorePtyFn()
495+
inState,err:=pty.MakeInputRaw(stdinFile.Fd())
496+
iferr!=nil {
497+
returnerr
498+
}
499+
deferfunc() {
500+
_=pty.RestoreTerminal(stdinFile.Fd(),inState)
501+
}()
502+
outState,err:=pty.MakeOutputRaw(stdoutFile.Fd())
486503
iferr!=nil {
487-
returnxerrors.Errorf("configure pty: %w",err)
504+
returnerr
488505
}
506+
deferfunc() {
507+
_=pty.RestoreTerminal(stdoutFile.Fd(),outState)
508+
}()
509+
510+
windowChange:=listenWindowSize(ctx)
511+
gofunc() {
512+
for {
513+
select {
514+
case<-ctx.Done():
515+
return
516+
case<-windowChange:
517+
}
518+
width,height,err:=term.GetSize(int(stdoutFile.Fd()))
519+
iferr!=nil {
520+
continue
521+
}
522+
_=sshSession.WindowChange(height,width)
523+
}
524+
}()
489525
}
490526

491527
for_,kv:=rangeparsedEnv {
@@ -667,48 +703,14 @@ func (r *RootCmd) ssh() *serpent.Command {
667703
{
668704
Flag:"force-new-tunnel",
669705
Description:"Force the creation of a new tunnel to the workspace, even if the Coder Connect tunnel is available.",
670-
Value:serpent.BoolOf(&forceTunnel),
706+
Value:serpent.BoolOf(&forceNewTunnel),
707+
Hidden:true,
671708
},
672709
sshDisableAutostartOption(serpent.BoolOf(&disableAutostart)),
673710
}
674711
returncmd
675712
}
676713

677-
funcconfigurePTY(ctx context.Context,stdinFile*os.File,stdoutFile*os.File,sshSession*gossh.Session) (restoreFnfunc(),errerror) {
678-
inState,err:=pty.MakeInputRaw(stdinFile.Fd())
679-
iferr!=nil {
680-
returnrestoreFn,err
681-
}
682-
restoreFn=func() {
683-
_=pty.RestoreTerminal(stdinFile.Fd(),inState)
684-
}
685-
outState,err:=pty.MakeOutputRaw(stdoutFile.Fd())
686-
iferr!=nil {
687-
returnrestoreFn,err
688-
}
689-
restoreFn=func() {
690-
_=pty.RestoreTerminal(stdinFile.Fd(),inState)
691-
_=pty.RestoreTerminal(stdoutFile.Fd(),outState)
692-
}
693-
694-
windowChange:=listenWindowSize(ctx)
695-
gofunc() {
696-
for {
697-
select {
698-
case<-ctx.Done():
699-
return
700-
case<-windowChange:
701-
}
702-
width,height,err:=term.GetSize(int(stdoutFile.Fd()))
703-
iferr!=nil {
704-
continue
705-
}
706-
_=sshSession.WindowChange(height,width)
707-
}
708-
}()
709-
returnrestoreFn,nil
710-
}
711-
712714
// findWorkspaceAndAgentByHostname parses the hostname from the commandline and finds the workspace and agent it
713715
// corresponds to, taking into account any name prefixes or suffixes configured (e.g. myworkspace.coder, or
714716
// vscode-coder--myusername--myworkspace).
@@ -1502,87 +1504,14 @@ func runCoderConnectStdio(ctx context.Context, addr string, stdin io.Reader, std
15021504
returnerr
15031505
}
15041506

1505-
agentssh.Bicopy(ctx,conn,&cliutil.StdioConn{
1507+
agentssh.Bicopy(ctx,conn,&cliutil.ReaderWriterConn{
15061508
Reader:stdin,
15071509
Writer:stdout,
15081510
})
15091511

15101512
returnnil
15111513
}
15121514

1513-
funcrunCoderConnectPTY(ctx context.Context,addrstring,stdin io.Reader,stdout io.Writer,stderr io.Writer,stack*closerStack)error {
1514-
client,err:=gossh.Dial("tcp",addr,&gossh.ClientConfig{
1515-
// We've already checked the agent's address
1516-
// is within the Coder service prefix.
1517-
// #nosec
1518-
HostKeyCallback:gossh.InsecureIgnoreHostKey(),
1519-
})
1520-
iferr!=nil {
1521-
returnxerrors.Errorf("dial coder connect host: %w",err)
1522-
}
1523-
iferr:=stack.push("ssh client",client);err!=nil {
1524-
returnerr
1525-
}
1526-
1527-
session,err:=client.NewSession()
1528-
iferr!=nil {
1529-
returnxerrors.Errorf("create ssh session: %w",err)
1530-
}
1531-
iferr:=stack.push("ssh session",session);err!=nil {
1532-
returnerr
1533-
}
1534-
1535-
stdinFile,validIn:=stdin.(*os.File)
1536-
stdoutFile,validOut:=stdout.(*os.File)
1537-
ifvalidIn&&validOut&&isatty.IsTerminal(stdinFile.Fd())&&isatty.IsTerminal(stdoutFile.Fd()) {
1538-
restorePtyFn,err:=configurePTY(ctx,stdinFile,stdoutFile,session)
1539-
deferrestorePtyFn()
1540-
iferr!=nil {
1541-
returnxerrors.Errorf("configure pty: %w",err)
1542-
}
1543-
}
1544-
1545-
session.Stdin=stdin
1546-
session.Stdout=stdout
1547-
session.Stderr=stderr
1548-
1549-
err=session.RequestPty("xterm-256color",80,24, gossh.TerminalModes{})
1550-
iferr!=nil {
1551-
returnxerrors.Errorf("request pty: %w",err)
1552-
}
1553-
1554-
err=session.Shell()
1555-
iferr!=nil {
1556-
returnxerrors.Errorf("start shell: %w",err)
1557-
}
1558-
1559-
ifvalidOut {
1560-
// Set initial window size.
1561-
width,height,err:=term.GetSize(int(stdoutFile.Fd()))
1562-
iferr==nil {
1563-
_=session.WindowChange(height,width)
1564-
}
1565-
}
1566-
1567-
err=session.Wait()
1568-
iferr!=nil {
1569-
ifexitErr:= (&gossh.ExitError{});errors.As(err,&exitErr) {
1570-
// Clear the error since it's not useful beyond
1571-
// reporting status.
1572-
returnExitError(exitErr.ExitStatus(),nil)
1573-
}
1574-
// If the connection drops unexpectedly, we get an
1575-
// ExitMissingError but no other error details, so try to at
1576-
// least give the user a better message
1577-
iferrors.Is(err,&gossh.ExitMissingError{}) {
1578-
returnExitError(255,xerrors.New("SSH connection ended unexpectedly"))
1579-
}
1580-
returnxerrors.Errorf("session ended: %w",err)
1581-
}
1582-
1583-
returnnil
1584-
}
1585-
15861515
funcwriteCoderConnectNetInfo(ctx context.Context,networkInfoDirstring)error {
15871516
fs,ok:=ctx.Value("fs").(afero.Fs)
15881517
if!ok {

‎cli/ssh_internal_test.go

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
"github.com/coder/coder/v2/cli/cliutil"
2424
"github.com/coder/coder/v2/codersdk"
25-
"github.com/coder/coder/v2/pty/ptytest"
2625
"github.com/coder/coder/v2/testutil"
2726
)
2827

@@ -226,37 +225,6 @@ func TestCloserStack_Timeout(t *testing.T) {
226225
testutil.TryReceive(ctx,t,closed)
227226
}
228227

229-
funcTestCoderConnectPTY(t*testing.T) {
230-
t.Parallel()
231-
232-
ctx:=testutil.Context(t,testutil.WaitShort)
233-
logger:=slogtest.Make(t,nil).Leveled(slog.LevelDebug)
234-
stack:=newCloserStack(ctx,logger,quartz.NewMock(t))
235-
236-
server:=newSSHServer("127.0.0.1:0")
237-
ln,err:=net.Listen("tcp",server.server.Addr)
238-
require.NoError(t,err)
239-
240-
gofunc() {
241-
_=server.Serve(ln)
242-
}()
243-
t.Cleanup(func() {
244-
_=server.Close()
245-
})
246-
247-
ptty:=ptytest.New(t)
248-
ptyDone:=make(chanstruct{})
249-
gofunc() {
250-
err:=runCoderConnectPTY(ctx,ln.Addr().String(),ptty.Output(),ptty.Input(),ptty.Output(),stack)
251-
assert.NoError(t,err)
252-
close(ptyDone)
253-
}()
254-
ptty.ExpectMatch("Connected!")
255-
// Shells on Mac, Windows, and Linux all exit shells with the "exit" command.
256-
ptty.WriteLine("exit")
257-
<-ptyDone
258-
}
259-
260228
funcTestCoderConnectStdio(t*testing.T) {
261229
t.Parallel()
262230

@@ -290,7 +258,7 @@ func TestCoderConnectStdio(t *testing.T) {
290258
close(stdioDone)
291259
}()
292260

293-
conn,channels,requests,err:=ssh.NewClientConn(&cliutil.StdioConn{
261+
conn,channels,requests,err:=ssh.NewClientConn(&cliutil.ReaderWriterConn{
294262
Reader:serverOutput,
295263
Writer:clientInput,
296264
},"",&ssh.ClientConfig{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp