mirror of
https://github.com/tailscale/tailscale.git
synced 2025-07-31 00:03:47 +00:00
some code simplification and add asServiceName
This commit cotains code simplification for IsServingHTTPS, SetWebHandler, SetTCPForwarding Signed-off-by: KevinLiang10 <37811973+KevinLiang10@users.noreply.github.com>
This commit is contained in:
parent
59c86c90cd
commit
ec35e6c1d8
@ -55,7 +55,6 @@ func (s *serviceNameFlag) Set(sv string) error {
|
|||||||
return fmt.Errorf("invalid service name: %q", sv)
|
return fmt.Errorf("invalid service name: %q", sv)
|
||||||
}
|
}
|
||||||
v := tailcfg.ServiceName(sv)
|
v := tailcfg.ServiceName(sv)
|
||||||
fmt.Printf("Setting service name to %q\n", v)
|
|
||||||
*s.Value = v
|
*s.Value = v
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -927,22 +926,20 @@ func (e *serveEnv) removeWebServe(sc *ipn.ServeConfig, st *ipnstate.Status, dnsN
|
|||||||
}
|
}
|
||||||
forService := ipn.IsServiceName(dnsName)
|
forService := ipn.IsServiceName(dnsName)
|
||||||
portStr := strconv.Itoa(int(srvPort))
|
portStr := strconv.Itoa(int(srvPort))
|
||||||
var hp ipn.HostPort
|
hostName := dnsName
|
||||||
var webServeMap map[ipn.HostPort]*ipn.WebServerConfig
|
webServeMap := sc.Web
|
||||||
if forService {
|
if forService {
|
||||||
svcName := tailcfg.ServiceName(dnsName)
|
svcName := tailcfg.ServiceName(dnsName)
|
||||||
dnsNameForService := svcName.WithoutPrefix() + "." + st.CurrentTailnet.MagicDNSSuffix
|
svc := sc.Services[svcName]
|
||||||
hp = ipn.HostPort(net.JoinHostPort(dnsNameForService, portStr))
|
if svc == nil {
|
||||||
if svc, ok := sc.Services[svcName]; !ok || svc == nil {
|
|
||||||
return errors.New("service does not exist")
|
return errors.New("service does not exist")
|
||||||
} else {
|
|
||||||
webServeMap = svc.Web
|
|
||||||
}
|
}
|
||||||
} else {
|
hostName = svcName.WithoutPrefix() + "." + st.CurrentTailnet.MagicDNSSuffix
|
||||||
hp = ipn.HostPort(net.JoinHostPort(dnsName, portStr))
|
webServeMap = svc.Web
|
||||||
webServeMap = sc.Web
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hp := ipn.HostPort(net.JoinHostPort(hostName, portStr))
|
||||||
|
|
||||||
if sc.IsTCPForwardingOnPort(srvPort, dnsName) {
|
if sc.IsTCPForwardingOnPort(srvPort, dnsName) {
|
||||||
return errors.New("cannot remove web handler; currently serving TCP")
|
return errors.New("cannot remove web handler; currently serving TCP")
|
||||||
}
|
}
|
||||||
|
79
ipn/serve.go
79
ipn/serve.go
@ -249,6 +249,13 @@ func IsServiceName(s string) bool {
|
|||||||
return tailcfg.ServiceName(s).Validate() == nil
|
return tailcfg.ServiceName(s).Validate() == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// asServiceName reports whether if the given string is a valid service name,
|
||||||
|
// and if so returns the name as a [tailcfg.ServiceName].
|
||||||
|
func asServiceName(s string) (svcName tailcfg.ServiceName, ok bool) {
|
||||||
|
svcName = tailcfg.ServiceName(s)
|
||||||
|
return svcName, svcName.Validate() == nil
|
||||||
|
}
|
||||||
|
|
||||||
// IsTCPForwardingOnPort reports whether ServeConfig is currently forwarding
|
// 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
|
// 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.
|
// serviceName for service hosted on node. This is exclusive of Web/HTTPS serving.
|
||||||
@ -285,18 +292,20 @@ func (sc *ServeConfig) IsServingHTTPS(port uint16, dnsName string) bool {
|
|||||||
if sc == nil {
|
if sc == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if IsServiceName(dnsName) {
|
var tcpHandlers map[uint16]*TCPPortHandler
|
||||||
if svc, ok := sc.Services[tailcfg.ServiceName(dnsName)]; ok && svc != nil {
|
if svcName, ok := asServiceName(dnsName); ok {
|
||||||
if svc.TCP[port] != nil {
|
if svc := sc.Services[svcName]; svc != nil {
|
||||||
return svc.TCP[port].HTTPS
|
tcpHandlers = svc.TCP
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
tcpHandlers = sc.TCP
|
||||||
|
}
|
||||||
|
|
||||||
|
th := tcpHandlers[port]
|
||||||
|
if th == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if sc.TCP[port] == nil {
|
return th.HTTPS
|
||||||
return false
|
|
||||||
}
|
|
||||||
return sc.TCP[port].HTTPS
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsServingHTTP reports whether ServeConfig is currently serving HTTP on the
|
// IsServingHTTP reports whether ServeConfig is currently serving HTTP on the
|
||||||
@ -346,30 +355,28 @@ func (sc *ServeConfig) SetWebHandler(st *ipnstate.Status, handler *HTTPHandler,
|
|||||||
if sc == nil {
|
if sc == nil {
|
||||||
sc = new(ServeConfig)
|
sc = new(ServeConfig)
|
||||||
}
|
}
|
||||||
var hp HostPort
|
|
||||||
var webCfg *WebServerConfig
|
tcpMap := &sc.TCP
|
||||||
|
webServerMap := &sc.Web
|
||||||
|
hostName := host
|
||||||
if IsServiceName(host) {
|
if IsServiceName(host) {
|
||||||
svcName := tailcfg.ServiceName(host)
|
svcName := tailcfg.ServiceName(host)
|
||||||
dnsNameForService := svcName.WithoutPrefix() + "." + st.CurrentTailnet.MagicDNSSuffix
|
hostName = svcName.WithoutPrefix() + "." + st.CurrentTailnet.MagicDNSSuffix
|
||||||
if _, ok := sc.Services[svcName]; !ok {
|
if _, ok := sc.Services[svcName]; !ok {
|
||||||
mak.Set(&sc.Services, svcName, new(ServiceConfig))
|
mak.Set(&sc.Services, svcName, new(ServiceConfig))
|
||||||
}
|
}
|
||||||
mak.Set(&sc.Services[svcName].TCP, port, &TCPPortHandler{HTTPS: useTLS, HTTP: !useTLS})
|
tcpMap = &sc.Services[svcName].TCP
|
||||||
hp = HostPort(net.JoinHostPort(dnsNameForService, strconv.Itoa(int(port))))
|
webServerMap = &sc.Services[svcName].Web
|
||||||
if _, ok := sc.Services[svcName].Web[hp]; !ok {
|
|
||||||
mak.Set(&sc.Services[svcName].Web, hp, new(WebServerConfig))
|
|
||||||
}
|
|
||||||
mak.Set(&sc.Services[svcName].Web[hp].Handlers, mount, handler)
|
|
||||||
webCfg = sc.Services[svcName].Web[hp]
|
|
||||||
} else {
|
|
||||||
mak.Set(&sc.TCP, port, &TCPPortHandler{HTTPS: useTLS, HTTP: !useTLS})
|
|
||||||
hp = HostPort(net.JoinHostPort(host, strconv.Itoa(int(port))))
|
|
||||||
if _, ok := sc.Web[hp]; !ok {
|
|
||||||
mak.Set(&sc.Web, hp, new(WebServerConfig))
|
|
||||||
}
|
|
||||||
mak.Set(&sc.Web[hp].Handlers, mount, handler)
|
|
||||||
webCfg = sc.Web[hp]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mak.Set(tcpMap, port, &TCPPortHandler{HTTPS: useTLS, HTTP: !useTLS})
|
||||||
|
hp := HostPort(net.JoinHostPort(hostName, strconv.Itoa(int(port))))
|
||||||
|
webCfg, ok := (*webServerMap)[hp]
|
||||||
|
if !ok {
|
||||||
|
webCfg = new(WebServerConfig)
|
||||||
|
mak.Set(webServerMap, hp, webCfg)
|
||||||
|
}
|
||||||
|
mak.Set(&webCfg.Handlers, mount, handler)
|
||||||
// TODO(tylersmalley): handle multiple web handlers from foreground mode
|
// TODO(tylersmalley): handle multiple web handlers from foreground mode
|
||||||
for k, v := range webCfg.Handlers {
|
for k, v := range webCfg.Handlers {
|
||||||
if v == handler {
|
if v == handler {
|
||||||
@ -395,20 +402,20 @@ func (sc *ServeConfig) SetTCPForwarding(port uint16, fwdAddr string, terminateTL
|
|||||||
if sc == nil {
|
if sc == nil {
|
||||||
sc = new(ServeConfig)
|
sc = new(ServeConfig)
|
||||||
}
|
}
|
||||||
var tcpPortHandler *TCPPortHandler
|
tcpPortHandler := &sc.TCP
|
||||||
if IsServiceName(host) {
|
if IsServiceName(host) {
|
||||||
svcName := tailcfg.ServiceName(host)
|
svcName := tailcfg.ServiceName(host)
|
||||||
if _, ok := sc.Services[svcName]; !ok {
|
svcConfig, ok := sc.Services[svcName]
|
||||||
mak.Set(&sc.Services, svcName, new(ServiceConfig))
|
if !ok {
|
||||||
|
svcConfig = new(ServiceConfig)
|
||||||
|
mak.Set(&sc.Services, svcName, svcConfig)
|
||||||
}
|
}
|
||||||
mak.Set(&sc.Services[svcName].TCP, port, &TCPPortHandler{TCPForward: fwdAddr})
|
tcpPortHandler = &svcConfig.TCP
|
||||||
tcpPortHandler = sc.Services[svcName].TCP[port]
|
|
||||||
} else {
|
|
||||||
mak.Set(&sc.TCP, port, &TCPPortHandler{TCPForward: fwdAddr})
|
|
||||||
tcpPortHandler = sc.TCP[port]
|
|
||||||
}
|
}
|
||||||
|
mak.Set(tcpPortHandler, port, &TCPPortHandler{TCPForward: fwdAddr})
|
||||||
|
|
||||||
if terminateTLS {
|
if terminateTLS {
|
||||||
tcpPortHandler.TerminateTLS = host
|
(*tcpPortHandler)[port].TerminateTLS = host
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user