mirror of
https://github.com/tailscale/tailscale.git
synced 2025-04-19 05:02:34 +00:00
ipn/ipnserver: make ipnserver also be an HTTP server for localhost clients
For now it just says hello to show auth works. More later.
This commit is contained in:
parent
3af64765fd
commit
4f71319f7c
@ -8,22 +8,29 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"os/user"
|
||||||
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"inet.af/netaddr"
|
||||||
"tailscale.com/control/controlclient"
|
"tailscale.com/control/controlclient"
|
||||||
"tailscale.com/ipn"
|
"tailscale.com/ipn"
|
||||||
"tailscale.com/logtail/backoff"
|
"tailscale.com/logtail/backoff"
|
||||||
|
"tailscale.com/net/netstat"
|
||||||
"tailscale.com/safesocket"
|
"tailscale.com/safesocket"
|
||||||
"tailscale.com/smallzstd"
|
"tailscale.com/smallzstd"
|
||||||
"tailscale.com/types/logger"
|
"tailscale.com/types/logger"
|
||||||
|
"tailscale.com/util/pidowner"
|
||||||
"tailscale.com/version"
|
"tailscale.com/version"
|
||||||
"tailscale.com/wgengine"
|
"tailscale.com/wgengine"
|
||||||
)
|
)
|
||||||
@ -84,11 +91,22 @@ type server struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) serveConn(ctx context.Context, c net.Conn, logf logger.Logf) {
|
func (s *server) serveConn(ctx context.Context, c net.Conn, logf logger.Logf) {
|
||||||
|
br := bufio.NewReader(c)
|
||||||
|
|
||||||
|
// First see if it's an HTTP request.
|
||||||
|
c.SetReadDeadline(time.Now().Add(time.Second))
|
||||||
|
peek, _ := br.Peek(4)
|
||||||
|
c.SetReadDeadline(time.Time{})
|
||||||
|
if string(peek) == "GET " {
|
||||||
|
http.Serve(&oneConnListener{altReaderNetConn{br, c}}, localhostHandler(c))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
s.addConn(c)
|
s.addConn(c)
|
||||||
logf("incoming control connection")
|
logf("incoming control connection")
|
||||||
defer s.removeAndCloseConn(c)
|
defer s.removeAndCloseConn(c)
|
||||||
for ctx.Err() == nil {
|
for ctx.Err() == nil {
|
||||||
msg, err := ipn.ReadMsg(c)
|
msg, err := ipn.ReadMsg(br)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if ctx.Err() == nil {
|
if ctx.Err() == nil {
|
||||||
logf("ReadMsg: %v", err)
|
logf("ReadMsg: %v", err)
|
||||||
@ -394,3 +412,83 @@ func BabysitProc(ctx context.Context, args []string, logf logger.Logf) {
|
|||||||
func FixedEngine(eng wgengine.Engine) func() (wgengine.Engine, error) {
|
func FixedEngine(eng wgengine.Engine) func() (wgengine.Engine, error) {
|
||||||
return func() (wgengine.Engine, error) { return eng, nil }
|
return func() (wgengine.Engine, error) { return eng, nil }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type dummyAddr string
|
||||||
|
type oneConnListener struct {
|
||||||
|
conn net.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *oneConnListener) Accept() (c net.Conn, err error) {
|
||||||
|
c = l.conn
|
||||||
|
if c == nil {
|
||||||
|
err = io.EOF
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = nil
|
||||||
|
l.conn = nil
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *oneConnListener) Close() error { return nil }
|
||||||
|
|
||||||
|
func (l *oneConnListener) Addr() net.Addr { return dummyAddr("unused-address") }
|
||||||
|
|
||||||
|
func (a dummyAddr) Network() string { return string(a) }
|
||||||
|
func (a dummyAddr) String() string { return string(a) }
|
||||||
|
|
||||||
|
type altReaderNetConn struct {
|
||||||
|
r io.Reader
|
||||||
|
net.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a altReaderNetConn) Read(p []byte) (int, error) { return a.r.Read(p) }
|
||||||
|
|
||||||
|
func localhostHandler(c net.Conn) http.Handler {
|
||||||
|
la, lerr := netaddr.ParseIPPort(c.LocalAddr().String())
|
||||||
|
ra, rerr := netaddr.ParseIPPort(c.RemoteAddr().String())
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintf(w, "<html><body><h1>tailscale</h1>\n")
|
||||||
|
if lerr != nil || rerr != nil {
|
||||||
|
io.WriteString(w, "failed to parse remote address")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !la.IP.IsLoopback() || !ra.IP.IsLoopback() {
|
||||||
|
io.WriteString(w, "not loopback")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
tab, err := netstat.Get()
|
||||||
|
if err == netstat.ErrNotImplemented {
|
||||||
|
io.WriteString(w, "status page not available on "+runtime.GOOS)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
io.WriteString(w, "failed to get netstat table")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pid := peerPid(tab.Entries, la, ra)
|
||||||
|
if pid == 0 {
|
||||||
|
io.WriteString(w, "peer pid not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
uid, err := pidowner.OwnerOfPID(pid)
|
||||||
|
if err != nil {
|
||||||
|
io.WriteString(w, "owner of peer pid not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
u, err := user.LookupId(uid)
|
||||||
|
if err != nil {
|
||||||
|
io.WriteString(w, "User lookup failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "Hello, %s", html.EscapeString(u.Username))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func peerPid(entries []netstat.Entry, la, ra netaddr.IPPort) int {
|
||||||
|
for _, e := range entries {
|
||||||
|
if e.Local == ra && e.Remote == la {
|
||||||
|
return e.Pid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user