-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
116 lines (99 loc) · 2.86 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"context"
"net"
"os"
"os/signal"
"sync"
"syscall"
"tailscale.com/tsnet"
)
type Logf func(format string, args ...any)
func main() {
logger := CreateLogger("main", Info)
config, err := GetConfig()
if err != nil {
logger.Fatalf("invalid config: %v", err)
}
if err := config.ProcessServices(); err != nil {
logger.Fatalf("invalid service config: %v", err)
}
tsnet := new(tsnet.Server)
tsnet.Hostname = config.Tailscale.Hostname
tsnet.AuthKey = config.Tailscale.AuthKey
tsnet.Ephemeral = config.Tailscale.Ephemeral
tsnet.Dir = config.Tailscale.StateDir
var tsLogLevel LogLevel
if config.Tailscale.Verbose {
tsLogLevel = Verbose
} else {
tsLogLevel = Info
}
tsLogger := CreateLogger("tailscale", tsLogLevel)
tsnet.Logf = tsLogger.Verbosef
tsnet.UserLogf = tsLogger.Infof
shutdownCh := make(chan struct{})
shutdownWg := &sync.WaitGroup{}
serviceContext := &ServiceContext{
TsNet: tsnet,
ShutdownCh: shutdownCh,
ShutdownWg: shutdownWg,
}
usingTailscale := false
if config.Tailscale.Listen.Socks5 != "" || config.Tailscale.Listen.HTTP != "" {
usingTailscale = true
}
var services []*Service
for name, serviceConfig := range config.Services {
service, err := CreateService(serviceContext, name, serviceConfig)
if err != nil {
logger.Fatalf("failed to create service %q: %v", name, err)
}
if service.ListenType == AddressTailscaleTCP || service.ConnectType == AddressTailscaleTCP {
usingTailscale = true
}
services = append(services, service)
}
if usingTailscale {
if err := config.ValidateTailscaleConfig(); err != nil {
logger.Fatalf("Tailscale used but got invalid Tailscale config: %v", err)
}
if config.Tailscale.AuthKey == "" {
logger.Infof("Tailscale authkey not provided, will try interactive login")
}
if err := tsnet.Start(); err != nil {
logger.Fatalf("failed to start Tailscale: %v", err)
}
defer tsnet.Close()
}
somethingRunning := false
if config.Tailscale.Listen.Socks5 != "" || config.Tailscale.Listen.HTTP != "" {
proxyDial := func(ctx context.Context, network, address string) (net.Conn, error) {
ctx2, cancel := context.WithTimeout(ctx, config.Timeout)
defer cancel()
return tsnet.Dial(ctx2, network, address)
}
if config.Tailscale.Listen.Socks5 != "" {
somethingRunning = true
StartProxy(tsLogger, config.Tailscale.Listen.Socks5, proxyDial, Socks5)
}
if config.Tailscale.Listen.HTTP != "" {
somethingRunning = true
StartProxy(tsLogger, config.Tailscale.Listen.HTTP, proxyDial, HTTP)
}
}
// Start services.
for _, service := range services {
somethingRunning = true
go service.Start()
}
if !somethingRunning {
logger.Fatalf("no listener defined. run %s -h for help", os.Args[0])
}
// Wait for signal to shutdown.
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
<-c
close(shutdownCh)
shutdownWg.Wait()
}