152 lines
2.9 KiB
Go
152 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/netip"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/juanfont/headscale/hscontrol"
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
func main() {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
fmt.Println("starting headscale")
|
|
// go func() {
|
|
fmt.Println(test(ctx))
|
|
// }()
|
|
|
|
return
|
|
startTime := time.Now()
|
|
for {
|
|
if time.Now().Sub(startTime) > 4*time.Second {
|
|
fmt.Println("stopping headscale")
|
|
cancel()
|
|
|
|
time.Sleep(2 * time.Second)
|
|
break
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
}
|
|
|
|
func test(ctx context.Context) error {
|
|
configPath := "orig-headscale.yaml"
|
|
// viper.SetConfigFile(configPath)
|
|
types.LoadConfig(configPath, true)
|
|
|
|
ncfg, err := types.LoadServerConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("%+v\n", ncfg)
|
|
|
|
return nil
|
|
// dump config
|
|
cfg, err := configHS()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// b, err := yaml.Marshal(cfg)
|
|
// if err != nil {
|
|
// return err
|
|
// }
|
|
|
|
// if err := os.WriteFile("config.yaml", b, 0666); err != nil {
|
|
// return err
|
|
// }
|
|
return startHS(ctx, cfg)
|
|
}
|
|
|
|
func configHS() (types.Config, error) {
|
|
ipv4prefix, err := netip.ParsePrefix("100.64.0.0/10")
|
|
if err != nil {
|
|
return types.Config{}, err
|
|
}
|
|
|
|
ipv6prefix, err := netip.ParsePrefix("fd7a:115c:a1e0::/48")
|
|
if err != nil {
|
|
return types.Config{}, err
|
|
}
|
|
|
|
derpURL, err := url.Parse("https://controlplane.tailscale.com/derpmap/default")
|
|
if err != nil {
|
|
return types.Config{}, err
|
|
}
|
|
|
|
cfg := types.Config{
|
|
Addr: "127.0.0.1:8080", // listen_addr
|
|
GRPCAddr: "127.0.0.1:8081",
|
|
MetricsAddr: "127.0.0.1:8082",
|
|
GRPCAllowInsecure: true,
|
|
Log: types.LogConfig{
|
|
Format: "text",
|
|
// Level: zerolog.InfoLevel,
|
|
Level: zerolog.TraceLevel,
|
|
},
|
|
Policy: types.PolicyConfig{
|
|
Mode: types.PolicyMode("file"),
|
|
},
|
|
PrefixV4: &ipv4prefix,
|
|
PrefixV6: &ipv6prefix,
|
|
|
|
EphemeralNodeInactivityTimeout: 30 * time.Minute,
|
|
|
|
DisableUpdateCheck: true,
|
|
|
|
NoisePrivateKeyPath: "noise.key",
|
|
|
|
Database: types.DatabaseConfig{
|
|
Type: "sqlite3",
|
|
Sqlite: types.SqliteConfig{
|
|
Path: "db.sqlite",
|
|
WriteAheadLog: true,
|
|
},
|
|
},
|
|
|
|
DERP: types.DERPConfig{
|
|
AutoUpdate: true,
|
|
ServerPrivateKeyPath: "depr_server_private.key",
|
|
UpdateFrequency: 24 * time.Hour,
|
|
URLs: []url.URL{*derpURL},
|
|
},
|
|
DNSConfig: types.DNSConfig{
|
|
BaseDomain: "tailnet.fricloud.dk",
|
|
MagicDNS: true,
|
|
},
|
|
|
|
Tuning: types.Tuning{
|
|
NotifierSendTimeout: 1 * time.Second,
|
|
BatchChangeDelay: 1 * time.Second,
|
|
},
|
|
|
|
UnixSocket: "headscale.sock",
|
|
UnixSocketPermission: 0700,
|
|
|
|
// cli things
|
|
CLI: types.CLIConfig{
|
|
Address: "",
|
|
// APIKey: "lol",
|
|
Timeout: 2 * time.Second,
|
|
Insecure: true,
|
|
},
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
func startHS(ctx context.Context, cfg types.Config) error {
|
|
hs, err := hscontrol.NewHeadscale(&cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return hs.Serve(ctx)
|
|
}
|