mirror of
https://github.com/tailscale/tailscale.git
synced 2025-10-27 20:19:31 +00:00
cmd/tailscaled: make the outbound HTTP/SOCKS5 proxy modular
Updates #12614 Change-Id: Icba6f1c0838dce6ee13aa2dc662fb551813262e4 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
committed by
Brad Fitzpatrick
parent
ecfdd86fc9
commit
5e698a81b6
@@ -1,7 +1,7 @@
|
||||
// Copyright (c) Tailscale Inc & AUTHORS
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
//go:build go1.19
|
||||
//go:build !ts_omit_outboundproxy
|
||||
|
||||
// HTTP proxy code
|
||||
|
||||
@@ -9,13 +9,105 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"strings"
|
||||
|
||||
"tailscale.com/net/proxymux"
|
||||
"tailscale.com/net/socks5"
|
||||
"tailscale.com/net/tsdial"
|
||||
"tailscale.com/net/tshttpproxy"
|
||||
"tailscale.com/types/logger"
|
||||
)
|
||||
|
||||
func init() {
|
||||
hookRegisterOutboundProxyFlags.Set(registerOutboundProxyFlags)
|
||||
hookOutboundProxyListen.Set(outboundProxyListen)
|
||||
}
|
||||
|
||||
func registerOutboundProxyFlags() {
|
||||
flag.StringVar(&args.socksAddr, "socks5-server", "", `optional [ip]:port to run a SOCK5 server (e.g. "localhost:1080")`)
|
||||
flag.StringVar(&args.httpProxyAddr, "outbound-http-proxy-listen", "", `optional [ip]:port to run an outbound HTTP proxy (e.g. "localhost:8080")`)
|
||||
}
|
||||
|
||||
// outboundProxyListen creates listeners for local SOCKS and HTTP proxies, if
|
||||
// the respective addresses are not empty. args.socksAddr and args.httpProxyAddr
|
||||
// can be the same, in which case the SOCKS5 Listener will receive connections
|
||||
// that look like they're speaking SOCKS and httpListener will receive
|
||||
// everything else.
|
||||
//
|
||||
// socksListener and httpListener can be nil, if their respective addrs are
|
||||
// empty.
|
||||
//
|
||||
// The returned func closes over those two (possibly nil) listeners and
|
||||
// starts the respective servers on the listener when called.
|
||||
func outboundProxyListen() proxyStartFunc {
|
||||
socksAddr, httpAddr := args.socksAddr, args.httpProxyAddr
|
||||
|
||||
if socksAddr == httpAddr && socksAddr != "" && !strings.HasSuffix(socksAddr, ":0") {
|
||||
ln, err := net.Listen("tcp", socksAddr)
|
||||
if err != nil {
|
||||
log.Fatalf("proxy listener: %v", err)
|
||||
}
|
||||
return mkProxyStartFunc(proxymux.SplitSOCKSAndHTTP(ln))
|
||||
}
|
||||
|
||||
var socksListener, httpListener net.Listener
|
||||
var err error
|
||||
if socksAddr != "" {
|
||||
socksListener, err = net.Listen("tcp", socksAddr)
|
||||
if err != nil {
|
||||
log.Fatalf("SOCKS5 listener: %v", err)
|
||||
}
|
||||
if strings.HasSuffix(socksAddr, ":0") {
|
||||
// Log kernel-selected port number so integration tests
|
||||
// can find it portably.
|
||||
log.Printf("SOCKS5 listening on %v", socksListener.Addr())
|
||||
}
|
||||
}
|
||||
if httpAddr != "" {
|
||||
httpListener, err = net.Listen("tcp", httpAddr)
|
||||
if err != nil {
|
||||
log.Fatalf("HTTP proxy listener: %v", err)
|
||||
}
|
||||
if strings.HasSuffix(httpAddr, ":0") {
|
||||
// Log kernel-selected port number so integration tests
|
||||
// can find it portably.
|
||||
log.Printf("HTTP proxy listening on %v", httpListener.Addr())
|
||||
}
|
||||
}
|
||||
|
||||
return mkProxyStartFunc(socksListener, httpListener)
|
||||
}
|
||||
|
||||
func mkProxyStartFunc(socksListener, httpListener net.Listener) proxyStartFunc {
|
||||
return func(logf logger.Logf, dialer *tsdial.Dialer) {
|
||||
var addrs []string
|
||||
if httpListener != nil {
|
||||
hs := &http.Server{Handler: httpProxyHandler(dialer.UserDial)}
|
||||
go func() {
|
||||
log.Fatalf("HTTP proxy exited: %v", hs.Serve(httpListener))
|
||||
}()
|
||||
addrs = append(addrs, httpListener.Addr().String())
|
||||
}
|
||||
if socksListener != nil {
|
||||
ss := &socks5.Server{
|
||||
Logf: logger.WithPrefix(logf, "socks5: "),
|
||||
Dialer: dialer.UserDial,
|
||||
}
|
||||
go func() {
|
||||
log.Fatalf("SOCKS5 server exited: %v", ss.Serve(socksListener))
|
||||
}()
|
||||
addrs = append(addrs, socksListener.Addr().String())
|
||||
}
|
||||
tshttpproxy.SetSelfProxy(addrs...)
|
||||
}
|
||||
}
|
||||
|
||||
// httpProxyHandler returns an HTTP proxy http.Handler using the
|
||||
// provided backend dialer.
|
||||
func httpProxyHandler(dialer func(ctx context.Context, netw, addr string) (net.Conn, error)) http.Handler {
|
||||
|
||||
Reference in New Issue
Block a user