GoBlog/tailscale.go

60 lines
1.3 KiB
Go
Raw Normal View History

2021-09-23 06:42:00 +00:00
package main
import (
"crypto/tls"
2021-10-13 07:01:54 +00:00
"errors"
2022-01-13 20:08:46 +00:00
"log"
2021-10-13 07:01:54 +00:00
"net"
"os"
"path/filepath"
2021-09-23 06:42:00 +00:00
2021-10-13 07:01:54 +00:00
"tailscale.com/tsnet"
2021-09-23 06:42:00 +00:00
)
2021-10-13 07:01:54 +00:00
func (a *goBlog) tailscaleEnabled() bool {
return a.cfg.Server != nil &&
a.cfg.Server.Tailscale != nil &&
2022-01-13 20:08:46 +00:00
a.cfg.Server.Tailscale.Enabled
2021-10-13 07:01:54 +00:00
}
func (a *goBlog) getTailscaleListener(addr string) (net.Listener, error) {
if !a.tailscaleEnabled() {
return nil, errors.New("tailscale not configured")
}
a.tsinit.Do(func() {
tsconfig := a.cfg.Server.Tailscale
2022-01-13 20:08:46 +00:00
if tsconfig.AuthKey != "" {
// Set Auth Key
_ = os.Setenv("TS_AUTHKEY", tsconfig.AuthKey)
}
2021-10-13 07:01:54 +00:00
// Enable Tailscale WIP code
_ = os.Setenv("TAILSCALE_USE_WIP_CODE", "true")
// Init server
tailscaleDir := filepath.Join("data", "tailscale")
_ = os.MkdirAll(tailscaleDir, 0777)
a.tss = &tsnet.Server{
Hostname: tsconfig.Hostname,
Dir: tailscaleDir,
2022-03-16 07:28:03 +00:00
Logf: func(format string, args ...any) {
2022-01-13 20:08:46 +00:00
log.Printf("tailscale: "+format, args...)
2021-10-13 07:01:54 +00:00
},
}
})
ln, err := a.tss.Listen("tcp", addr)
if err != nil {
return nil, err
2021-09-23 06:42:00 +00:00
}
2022-06-07 18:39:11 +00:00
lc, err := a.tss.LocalClient()
if err != nil {
return nil, err
}
2021-10-13 07:01:54 +00:00
// Tailscale HTTPS
if addr == ":443" && a.cfg.Server.TailscaleHTTPS {
ln = tls.NewListener(ln, &tls.Config{
2022-06-07 18:39:11 +00:00
GetCertificate: lc.GetCertificate,
MinVersion: tls.VersionTLS12,
2021-10-13 07:01:54 +00:00
})
2021-09-23 06:42:00 +00:00
}
2021-10-13 07:01:54 +00:00
return ln, nil
2021-09-23 06:42:00 +00:00
}