Skip to content

Commit

Permalink
Provide information on service underlying a proxy
Browse files Browse the repository at this point in the history
Closes #5

Signed-off-by: Tyler Smalley <tyler@tailscale.com>
Co-authored-by: Marwan Sulaiman <marwan.sameer@gmail.com>
  • Loading branch information
marwan-at-work authored and tylersmalley committed Jun 5, 2023
1 parent e6f39d1 commit 6faeaae
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export interface Handlers {
export interface ServeStatus {
ServeConfig?: ServeConfig;
FunnelPorts?: number[];
Services: {
[port: number]: string;
};
BackendState: string;
Self: PeerStatus;
Errors?: RelayError[];
Expand Down
12 changes: 12 additions & 0 deletions src/webviews/serve-panel/simple-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ export const SimpleView = () => {
</Tooltip>
)}
</div>
<div className="pt-4">
{data?.Services[port] ? (
<div className="italic">
Port {port} is currently started by "{data?.Services[port]}"
</div>
) : (
<div className="text-errorForeground">
It seems there's no service currently utilizing port {port}. Please ensure you start a
local service that is bound to port {port}.
</div>
)}
</div>
</form>
);
}
Expand Down
50 changes: 50 additions & 0 deletions tsrelay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ type httpHandler struct {
// to some helper fields for the typescript frontend
type serveStatus struct {
ServeConfig *ipn.ServeConfig
Services map[uint16]string
BackendState string
Self *peerStatus
FunnelPorts []int
Expand Down Expand Up @@ -201,6 +202,29 @@ func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
case http.MethodGet:
var wg sync.WaitGroup
wg.Add(1)
portMap := map[uint16]string{}
go func() {
defer wg.Done()
p := &portlist.Poller{
Interval: 10 * time.Second,
IncludeLocalhost: true,
}
defer p.Close()
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
ch, err := p.Run(ctx)
if err != nil {
h.l.Printf("error polling for serve: %v", err)
return
}
update := <-ch
for _, p := range update.List {
portMap[p.Port] = p.Process
}
}()

st, sc, err := h.getConfigs(ctx)
if err != nil {
var oe *net.OpError
Expand All @@ -221,10 +245,36 @@ func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

s := serveStatus{
ServeConfig: sc,
Services: make(map[uint16]string),
BackendState: st.BackendState,
FunnelPorts: []int{},
}

wg.Wait()
if sc != nil {
for _, webCfg := range sc.Web {
for _, addr := range webCfg.Handlers {
if addr.Proxy == "" {
continue
}
u, err := url.Parse(addr.Proxy)
if err != nil {
h.l.Printf("error parsing address proxy %q: %v", addr.Proxy, err)
continue
}
portInt, err := strconv.Atoi(u.Port())
if err != nil {
h.l.Printf("error parsing port %q of proxy %q: %v", u.Port(), addr.Proxy, err)
continue
}
port := uint16(portInt)
if process, ok := portMap[port]; ok {
s.Services[port] = process
}
}
}
}

if st.Self != nil {
s.Self = &peerStatus{
DNSName: st.Self.DNSName,
Expand Down

0 comments on commit 6faeaae

Please sign in to comment.