-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
130 lines (114 loc) · 2.63 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"errors"
"fmt"
"log"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"github.com/BurntSushi/toml"
)
type Config struct {
Listen string
MinecraftServer string
BaseDomain string
BotToken string
AdminID int64
OnlineMessageID int64
OnlineMessageChatID int64
SupportName string
Lang string
}
var (
storage *Storage
cfg Config
configFile string
)
func SaveConfig(config Config) {
file, err := os.Create(configFile)
if err != nil {
log.Printf("failed to create config file: %s", err)
return
}
defer file.Close()
encoder := toml.NewEncoder(file)
if err := encoder.Encode(config); err != nil {
log.Printf("failed to encode config: %s", err)
return
}
}
func isValidMinecraftUsername(username string) bool {
if username == "online" || username == "list" || username == "delete" {
return false
}
if len(username) < 3 || len(username) > 16 {
return false
}
match, _ := regexp.MatchString(`^[A-Za-z0-9_]+$`, username)
return match
}
func getUserInfoByHostname(host string) *StorageRecord {
// Remove port if present
parts := strings.SplitN(host, ":", 2)
host = parts[0]
// Check server address
token, correctDomain := strings.CutSuffix(host, "."+cfg.BaseDomain)
if !correctDomain {
log.Printf("Someone tried to connect using address: %s\n", host)
return nil
}
// Check token
userInfo, err := storage.FindByToken(token)
if err != nil {
log.Printf("Someone tried to connect using bad token: %s\n", host)
return nil
}
return userInfo
}
func main() {
configFile = "./config.toml"
if len(os.Args) > 1 {
configFile = os.Args[1]
}
_, err := toml.DecodeFile(configFile, &cfg)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
log.Fatalf("Configuration file `%s` not found", configFile)
} else {
log.Fatal(err)
}
}
//
if !strings.Contains(cfg.Listen, ":") {
cfg.Listen = "0.0.0.0:" + cfg.Listen
}
if !strings.Contains(cfg.MinecraftServer, ":") {
cfg.MinecraftServer = "127.0.0.1:" + cfg.MinecraftServer
}
storage = NewStorage("data.txt")
go startMinecraftProxy()
updater := startTgBot()
// Add telegram users to bot access
usersInfo, err := storage.readRecords()
if err != nil {
log.Fatal(err)
}
for _, userInfo := range usersInfo {
allowedIDs.Add(int64(userInfo.ID))
}
// And admin too
allowedIDs.Add(cfg.AdminID)
updateOnlineMessage()
go startServerStatusChecker()
// Handling Ctrl+C
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT)
go func() {
<-sigChan
fmt.Println("Received SIGINT, stopping bot...")
updater.Stop()
}()
updater.Idle()
}