-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (71 loc) · 2.07 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
package main
import (
"fmt"
"os"
"gopkg.in/ini.v1"
"main/ports"
"main/server"
"main/store"
)
// ServerConf contains information for a server service. It is
// recommended to use GetDefaultServerConf instead of creating this object
// directly, so that all unspecified fields have reasonable default values.
type ServerConf struct {
// Original string.
AllowPorts string `ini:"-" json:"-"`
}
func UnmarshalServerConfFromIni(source interface{}) (ServerConf, error) {
f, err := ini.LoadSources(ini.LoadOptions{
Insensitive: false,
InsensitiveSections: false,
InsensitiveKeys: false,
IgnoreInlineComment: true,
AllowBooleanKeys: true,
}, source)
if err != nil {
return ServerConf{}, err
}
s, err := f.GetSection("common")
if err != nil {
return ServerConf{}, err
}
common := ServerConf{}
err = s.MapTo(&common)
if err != nil {
return ServerConf{}, err
}
// allow_ports
allowPortStr := s.Key("allow_ports").String()
if allowPortStr != "" {
common.AllowPorts = allowPortStr
} else {
fmt.Println("⚠ common.allow_ports not specified in config, falling back to 1000-65535 port range")
common.AllowPorts = "1000-65535"
}
return common, nil
}
func init() {
fmt.Println("🐔 Initializing the plugin...")
// Get the frps.ini file path from the environment variable
frpsIniPath := os.Getenv("FRPS_INI_PATH")
// If the environment variable is empty or not set
if frpsIniPath == "" {
// Use the default directory
frpsIniPath = "./frps.ini"
}
// Check if frps.ini exists
if _, err := os.Stat(frpsIniPath); os.IsNotExist(err) {
panicMsg, _ := fmt.Printf("frps.ini does not exist at path %s; move the frp-port-keeper binary to the same folder where the frps.ini located and call frp-port-keeper from there.", frpsIniPath)
panic(panicMsg)
}
var commonSection, err = UnmarshalServerConfFromIni(frpsIniPath)
if err != nil {
fmt.Println("got error: ", err)
}
fmt.Println("got allow_ports value: ", commonSection.AllowPorts)
ports.InitPortsGenerator(commonSection.AllowPorts)
}
func main() {
defer store.DB.Close()
server.Start()
}