-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
178 lines (145 loc) · 4.53 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"fmt"
"net"
"os"
"path/filepath"
"sync"
"time"
"github.com/NetSepio/sotreus/api"
"github.com/NetSepio/sotreus/core"
grpc "github.com/NetSepio/sotreus/gRPC"
"github.com/NetSepio/sotreus/util"
"github.com/NetSepio/sotreus/util/pkg/auth"
helmet "github.com/danielkov/gin-helmet"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
)
var wg sync.WaitGroup
func init() {
log.SetFormatter(&log.JSONFormatter{})
log.SetOutput(os.Stderr)
log.SetLevel(log.DebugLevel)
// Get Hostname for updating Log StandardFields
HostName, err := os.Hostname()
if err != nil {
log.Infof("Error in getting the Hostname: %v", err)
} else {
util.StandardFields = log.Fields{
"hostname": HostName,
"appname": "sotreus",
}
}
// Check if loading environment variables from .env file is required
if os.Getenv("LOAD_CONFIG_FILE") == "" {
// Load environment variables from .env file
err = godotenv.Load()
if err != nil {
log.WithFields(util.StandardFields).Fatalf("Error in reading the config file: %v", err)
}
}
auth.Init()
}
func RungRPCServer() {
grpc_server := grpc.Initialize()
port := os.Getenv("WG_GRPC_PORT")
log.WithFields(util.StandardFields).Info("Starting gRPC Api, Listening on Port :", port)
listener, err := net.Listen("tcp", ":"+port)
if err != nil {
wg.Done()
log.Fatal("Unable to listen on port", port)
}
//Server GRPC
if err := grpc_server.Serve(listener); err != nil {
wg.Done()
log.Fatal("Faied to create GRPC server!")
}
wg.Done()
}
func main() {
log.WithFields(util.StandardFields).Infof("Starting Lazarus Network - sotreus Version: %s", util.Version)
// check directories or create it
if !util.DirectoryExists(filepath.Join(os.Getenv("WG_CONF_DIR"))) {
err := os.Mkdir(filepath.Join(os.Getenv("WG_CONF_DIR")), 0755)
if err != nil {
log.WithFields(log.Fields{
"err": err,
"dir": filepath.Join(os.Getenv("WG_CONF_DIR")),
}).Fatal("failed to create wireguard configuration directory")
}
}
// check directories or create it
if !util.DirectoryExists(filepath.Join(os.Getenv("WG_CLIENTS_DIR"))) {
err := os.Mkdir(filepath.Join(os.Getenv("WG_CLIENTS_DIR")), 0755)
if err != nil {
log.WithFields(log.Fields{
"err": err,
"dir": filepath.Join(os.Getenv("WG_CLIENTS_DIR")),
}).Fatal("failed to create wireguard clients directory")
}
}
// check if server.json exists otherwise create it with default values
if !util.FileExists(filepath.Join(os.Getenv("WG_CONF_DIR"), "server.json")) {
_, err := core.ReadServer()
if err != nil {
log.WithFields(util.StandardFields).Fatal("server.json does not exist and unable to open")
}
}
if os.Getenv("RUNTYPE") == "debug" {
// set gin release debug
gin.SetMode(gin.DebugMode)
} else {
// set gin release mode
gin.SetMode(gin.ReleaseMode)
// disable console color
gin.DisableConsoleColor()
// log level info
log.SetLevel(log.InfoLevel)
}
// dump wg config file
err := core.UpdateServerConfigWg()
util.CheckError("Error while creating WireGuard config file: ", err)
if os.Getenv("WG_GRPC_PORT") != "" {
//Add gRPC routine to wait group
wg.Add(1)
//run gRPC server
go RungRPCServer()
}
if os.Getenv("WG_HTTP_PORT") != "" {
// creates a gin router with default middleware: logger and recovery (crash-free) middleware
ginApp := gin.Default()
// cors middleware
config := cors.DefaultConfig()
config.AllowAllOrigins = true
config.AllowHeaders = []string{"Authorization", "Content-Type"}
ginApp.Use(cors.New(config))
// protection middleware
ginApp.Use(helmet.Default())
// add cache storage to gin ginApp
ginApp.Use(func(ctx *gin.Context) {
ctx.Set("cache", cache.New(60*time.Minute, 10*time.Minute))
ctx.Next()
})
// serve static files
ginApp.Use(static.Serve("/", static.LocalFile("./webapp", false)))
//ginApp.Use(static.Serve("/docs", static.LocalFile("./docs", false)))
/*opt := openapimiddleware.RedocOpts{SpecURL: "/docs/swagger.yml"}
handler := openapimiddleware.Redoc(opt, nil)
*/
//ginApp.Static("docs", "./docs")
// no route redirect to frontend app
ginApp.NoRoute(func(c *gin.Context) {
c.JSON(404, gin.H{"status": 404, "message": "Invalid Endpoint Request"})
})
// Apply API Routes
api.ApplyRoutes(ginApp)
err = ginApp.Run(fmt.Sprintf("%s:%s", os.Getenv("SERVER"), os.Getenv("WG_HTTP_PORT")))
util.CheckError("Failed to Start HTTP Server: ", err)
}
//wait untill all servers are stopped
wg.Wait()
}