-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (64 loc) · 1.88 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
package main
import (
"context"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
log "github.com/sirupsen/logrus"
)
var db *gorm.DB
var domainService *DomainService
func initDB() {
var err error
if _, err := os.Stat(Config.TunnelDatabasePath); os.IsNotExist(err) {
file, err := os.Create(Config.TunnelDatabasePath)
if err != nil {
log.Fatalf("Failed to create database file: %v", err)
}
file.Close()
}
db, err = gorm.Open(sqlite.Open(Config.TunnelDatabasePath), &gorm.Config{})
if err != nil {
log.Fatalf("Error opening database: %v", err)
}
err = db.AutoMigrate(&Dns2TcpdTunnel{}, &NameServer{})
if err != nil {
log.Fatalf("Error migrating database: %v", err)
}
}
func main() {
log.SetLevel(Config.LogLevel)
initDB()
// For the domain service
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
domainService = NewDomainService(ctx, 15*time.Minute, 1*time.Hour)
httpServer := startAPI()
configPath := Config.Dns2tcpdConfigPath
if _, err := os.Stat(configPath); os.IsNotExist(err) {
log.Fatalf("Config path %s does not exist", configPath)
}
tmpFilePath := filepath.Join(configPath, ".tmp-write-test")
file, err := os.OpenFile(tmpFilePath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
log.Fatalf("Config path %s is not writable", configPath)
}
file.Close()
os.Remove(tmpFilePath)
log.Infof("Config path %s is valid and writable", configPath)
dnsServer := startDNS()
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := httpServer.Shutdown(shutdownCtx); err != nil {
log.Fatal("Failed to shutdown HTTP server:", err)
}
if err := dnsServer.ShutdownContext(shutdownCtx); err != nil {
log.Fatal("Failed to shutdown DNS server:", err)
}
log.Println("Servers gracefully shutdown")
}