cmd/tailscale/cli: adjust parameter order and update output message

This commit updates the parameter order for IsTCPForwardingOnPort and SetWebHandler.
Also updated the message msgServiceIPNotAssigned to msgServiceWaitingApproval to adapt to
latest terminologies around services.

Signed-off-by: KevinLiang10 <37811973+KevinLiang10@users.noreply.github.com>
This commit is contained in:
KevinLiang10 2025-06-26 17:07:51 -04:00
parent d40755be85
commit 71fe9a9860
7 changed files with 26 additions and 29 deletions

View File

@ -358,16 +358,12 @@ func (e *serveEnv) handleWebServe(ctx context.Context, srvPort uint16, useTLS bo
if err != nil {
return err
}
if sc.IsTCPForwardingOnPort(srvPort, dnsName) {
if sc.IsTCPForwardingOnPort(dnsName, srvPort) {
fmt.Fprintf(Stderr, "error: cannot serve web; already serving TCP\n")
return errHelp
}
st, err := e.getLocalClientStatusWithoutPeers(ctx)
if err != nil {
return fmt.Errorf("getting client status: %w", err)
}
sc.SetWebHandler(st, h, dnsName, srvPort, mount, useTLS)
sc.SetWebHandler(h, dnsName, srvPort, mount, useTLS, nil)
if !reflect.DeepEqual(cursc, sc) {
if err := e.lc.SetServeConfig(ctx, sc); err != nil {
@ -419,7 +415,7 @@ func (e *serveEnv) handleWebServeRemove(ctx context.Context, srvPort uint16, mou
if err != nil {
return err
}
if sc.IsTCPForwardingOnPort(srvPort, dnsName) {
if sc.IsTCPForwardingOnPort(dnsName, srvPort) {
return errors.New("cannot remove web handler; currently serving TCP")
}
hp := ipn.HostPort(net.JoinHostPort(dnsName, strconv.Itoa(int(srvPort))))

View File

@ -529,7 +529,7 @@ func (e *serveEnv) setServe(sc *ipn.ServeConfig, st *ipnstate.Status, dnsName st
var (
msgFunnelAvailable = "Available on the internet:"
msgServeAvailable = "Available within your tailnet:"
msgServiceIPNotAssigned = "This service %s is being served by this machine, but is not approved to have VIPs assigned yet. Once approved, it will be available in your Tailnet as:"
msgServiceWaitingApproval = "This machine is configured as a service proxy for %s, but approval from an admin is required. Once approved, it will be available in your Tailnet as:"
msgRunningInBackground = "%s started and running in the background."
msgRunningTunService = "IPv4 and IPv6 traffic to %s is being routed to your operating system."
msgDisableProxy = "To disable the proxy, run: tailscale %s --%s=%d off"
@ -581,7 +581,7 @@ func (e *serveEnv) messageForPort(sc *ipn.ServeConfig, st *ipnstate.Status, dnsN
if err != nil || len(serviceIPMaps) == 0 || serviceIPMaps[0][svcName] == nil {
// The capmap does not contain IPs for this service yet. Usually this means
// the service hasn't been added to prefs and sent to control yet.
output.WriteString(fmt.Sprintf(msgServiceIPNotAssigned, svcName.String()))
output.WriteString(fmt.Sprintf(msgServiceWaitingApproval, svcName.String()))
ips = nil
} else {
output.WriteString(msgServeAvailable)
@ -698,11 +698,11 @@ func (e *serveEnv) applyWebServe(sc *ipn.ServeConfig, st *ipnstate.Status, dnsNa
}
// TODO: validation needs to check nested foreground configs
if sc.IsTCPForwardingOnPort(srvPort, dnsName) {
if sc.IsTCPForwardingOnPort(dnsName, srvPort) {
return errors.New("cannot serve web; already serving TCP")
}
sc.SetWebHandler(st, h, dnsName, srvPort, mount, useTLS)
sc.SetWebHandler(h, dnsName, srvPort, mount, useTLS, st)
return nil
}
@ -938,7 +938,7 @@ func (e *serveEnv) removeWebServe(sc *ipn.ServeConfig, st *ipnstate.Status, dnsN
hp := ipn.HostPort(net.JoinHostPort(hostName, portStr))
if sc.IsTCPForwardingOnPort(srvPort, dnsName) {
if sc.IsTCPForwardingOnPort(dnsName, srvPort) {
return errors.New("cannot remove web handler; currently serving TCP")
}
var targetExists bool

View File

@ -1389,7 +1389,7 @@ func TestMessageForPort(t *testing.T) {
srvType: serveTypeHTTP,
srvPort: 80,
expected: strings.Join([]string{
fmt.Sprintf(msgServiceIPNotAssigned, "svc:bar"),
fmt.Sprintf(msgServiceWaitingApproval, "svc:bar"),
"",
"http://bar.test.ts.net/",
"|-- proxy http://localhost:3000",

View File

@ -260,7 +260,7 @@ func printFunnelStatus(ctx context.Context) {
}
sni, portStr, _ := net.SplitHostPort(string(hp))
p, _ := strconv.ParseUint(portStr, 10, 16)
isTCP := sc.IsTCPForwardingOnPort(uint16(p), sni)
isTCP := sc.IsTCPForwardingOnPort(sni, uint16(p))
url := "https://"
if isTCP {
url = "tcp://"

View File

@ -266,9 +266,9 @@ func serveOnLocalTailscaled(ctx context.Context, lc *local.Client, st *ipnstate.
fmt.Printf("setting funnel for %s:%v\n", serverURL, dstPort)
foregroundSc.SetFunnel(serverURL, dstPort, shouldFunnel)
foregroundSc.SetWebHandler(st, &ipn.HTTPHandler{
foregroundSc.SetWebHandler(&ipn.HTTPHandler{
Proxy: fmt.Sprintf("https://%s", net.JoinHostPort(serverURL, strconv.Itoa(int(dstPort)))),
}, serverURL, uint16(*flagPort), "/", true)
}, serverURL, uint16(*flagPort), "/", true, st)
err = lc.SetServeConfig(ctx, sc)
if err != nil {
return nil, watcherChan, fmt.Errorf("could not set serve config: %v", err)

View File

@ -247,7 +247,7 @@ func (sc *ServeConfig) IsTCPForwardingAny() bool {
// IsTCPForwardingOnPort reports whether ServeConfig is currently forwarding
// in TCPForward mode on the given port for a DNSName. DNSName will be either node's DNSName, or a
// serviceName for service hosted on node. This is exclusive of Web/HTTPS serving.
func (sc *ServeConfig) IsTCPForwardingOnPort(port uint16, dnsName string) bool {
func (sc *ServeConfig) IsTCPForwardingOnPort(dnsName string, port uint16) bool {
if sc == nil {
return false
}
@ -338,8 +338,9 @@ func (sc *ServeConfig) FindConfig(port uint16) (*ServeConfig, bool) {
// SetWebHandler sets the given HTTPHandler at the specified host, port,
// and mount in the serve config. sc.TCP is also updated to reflect web
// serving usage of the given port.
func (sc *ServeConfig) SetWebHandler(st *ipnstate.Status, handler *HTTPHandler, host string, port uint16, mount string, useTLS bool) {
// serving usage of the given port. The st argument is needed when setting
// a web handler for a service, other wise it can be nil.
func (sc *ServeConfig) SetWebHandler(handler *HTTPHandler, host string, port uint16, mount string, useTLS bool, st *ipnstate.Status) {
if sc == nil {
sc = new(ServeConfig)
}

View File

@ -235,7 +235,7 @@ func TestIsTCPForwardingOnPort(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.cfg.IsTCPForwardingOnPort(tt.port, tt.dns)
got := tt.cfg.IsTCPForwardingOnPort(tt.dns, tt.port)
if tt.want != got {
t.Errorf("IsTCPForwardingOnPort() = %v, want %v", got, tt.want)
}