mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
* chore: rename `AgentConn` to `WorkspaceAgentConn` The codersdk was becoming bloated with consts for the workspace agent that made no sense to a reader. `Tailnet*` is an example of these consts. * chore: remove `Get` prefix from *Client functions * chore: remove `BypassRatelimits` option in `codersdk.Client` It feels wrong to have this as a direct option because it's so infrequently needed by API callers. It's better to directly modify headers in the two places that we actually use it. * Merge `appearance.go` and `buildinfo.go` into `deployment.go` * Merge `experiments.go` and `features.go` into `deployment.go` * Fix `make gen` referencing old type names * Merge `error.go` into `client.go` `codersdk.Response` lived in `error.go`, which is wrong. * chore: refactor workspace agent functions into agentsdk It was odd conflating the codersdk that clients should use with functions that only the agent should use. This separates them into two SDKs that are closely coupled, but separate. * Merge `insights.go` into `deployment.go` * Merge `organizationmember.go` into `organizations.go` * Merge `quota.go` into `workspaces.go` * Rename `sse.go` to `serversentevents.go` * Rename `codersdk.WorkspaceAppHostResponse` to `codersdk.AppHostResponse` * Format `.vscode/settings.json` * Fix outdated naming in `api.ts` * Fix app host response * Fix unsupported type * Fix imported type
65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
//go:build linux || (windows && amd64)
|
|
|
|
package agent
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/cakturk/go-netstat/netstat"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/codersdk"
|
|
)
|
|
|
|
func (lp *listeningPortsHandler) getListeningPorts() ([]codersdk.WorkspaceAgentListeningPort, error) {
|
|
lp.mut.Lock()
|
|
defer lp.mut.Unlock()
|
|
|
|
if time.Since(lp.mtime) < time.Second {
|
|
// copy
|
|
ports := make([]codersdk.WorkspaceAgentListeningPort, len(lp.ports))
|
|
copy(ports, lp.ports)
|
|
return ports, nil
|
|
}
|
|
|
|
tabs, err := netstat.TCPSocks(func(s *netstat.SockTabEntry) bool {
|
|
return s.State == netstat.Listen
|
|
})
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("scan listening ports: %w", err)
|
|
}
|
|
|
|
seen := make(map[uint16]struct{}, len(tabs))
|
|
ports := []codersdk.WorkspaceAgentListeningPort{}
|
|
for _, tab := range tabs {
|
|
if tab.LocalAddr == nil || tab.LocalAddr.Port < codersdk.WorkspaceAgentMinimumListeningPort {
|
|
continue
|
|
}
|
|
|
|
// Don't include ports that we've already seen. This can happen on
|
|
// Windows, and maybe on Linux if you're using a shared listener socket.
|
|
if _, ok := seen[tab.LocalAddr.Port]; ok {
|
|
continue
|
|
}
|
|
seen[tab.LocalAddr.Port] = struct{}{}
|
|
|
|
procName := ""
|
|
if tab.Process != nil {
|
|
procName = tab.Process.Name
|
|
}
|
|
ports = append(ports, codersdk.WorkspaceAgentListeningPort{
|
|
ProcessName: procName,
|
|
Network: "tcp",
|
|
Port: tab.LocalAddr.Port,
|
|
})
|
|
}
|
|
|
|
lp.ports = ports
|
|
lp.mtime = time.Now()
|
|
|
|
// copy
|
|
ports = make([]codersdk.WorkspaceAgentListeningPort, len(lp.ports))
|
|
copy(ports, lp.ports)
|
|
return ports, nil
|
|
}
|