-
-
Notifications
You must be signed in to change notification settings - Fork 214
/
main.go
96 lines (83 loc) · 2.85 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
package main
import (
"fmt"
"io/ioutil"
"os"
"runtime"
"time"
"github.com/WangYihang/Platypus/internal/cli/dispatcher"
"github.com/WangYihang/Platypus/internal/context"
"github.com/WangYihang/Platypus/internal/util/assets"
"github.com/WangYihang/Platypus/internal/util/config"
"github.com/WangYihang/Platypus/internal/util/fs"
"github.com/WangYihang/Platypus/internal/util/log"
"github.com/WangYihang/Platypus/internal/util/update"
"github.com/pkg/browser"
"gopkg.in/yaml.v2"
)
func main() {
// Detect and create config file
configFilenameWithVersion := fmt.Sprintf("config-v%s.yml", update.Version)
if !fs.FileExists(configFilenameWithVersion) {
content, _ := assets.Asset("assets/config.example.yml")
ioutil.WriteFile(configFilenameWithVersion, content, 0644)
}
var configFilename string
if runtime.GOOS == "windows" {
configFilename = configFilenameWithVersion
} else {
configFilename = "config.yml"
if fs.FileExists(configFilename) {
os.Remove(configFilename)
}
os.Symlink(configFilenameWithVersion, configFilename)
}
// Read config file
var config config.Config
content, _ := ioutil.ReadFile(configFilename)
err := yaml.Unmarshal(content, &config)
if err != nil {
log.Error("Read config file failed, please check syntax of file `%s`, or just delete the `%s` to force regenerate config file", configFilename, configFilename)
return
}
// Display platypus information
log.Success("Platypus %s is starting...", update.Version)
// Create context
context.CreateContext()
context.Ctx.Config = &config
// Detect new version
if config.Update {
update.ConfirmAndSelfUpdate()
}
// Init distributor server from config file
rh := config.Distributor.Host
rp := config.Distributor.Port
distributor := context.CreateDistributorServer(rh, rp, config.Distributor.Url)
go distributor.Run(fmt.Sprintf("%s:%d", rh, rp))
// Init RESTful Server from config file
if config.RESTful.Enable {
rh := config.RESTful.Host
rp := config.RESTful.Port
rest := context.CreateRESTfulAPIServer()
go rest.Run(fmt.Sprintf("%s:%d", rh, rp))
log.Success("Web FrontEnd started at: http://%s:%d/", rh, rp)
log.Success("You can use Web FrontEnd to manager all your clients with any web browser.")
log.Success("RESTful API EndPoint at: http://%s:%d/api/", rh, rp)
log.Success("You can use PythonSDK to manager all your clients automatically.")
context.Ctx.RESTful = rest
}
// Init servers from config file
for _, s := range config.Servers {
server := context.CreateTCPServer(s.Host, uint16(s.Port), s.HashFormat, s.Encrypted, s.DisableHistory, s.PublicIP, s.ShellPath)
if server != nil {
// avoid terminal being disrupted
time.Sleep(0x100 * time.Millisecond)
go (*server).Run()
}
}
if config.OpenBrowser {
browser.OpenURL(fmt.Sprintf("http://%s:%d/", config.RESTful.Host, config.RESTful.Port))
}
// Run main loop
dispatcher.REPL()
}