-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
110 lines (87 loc) · 2.14 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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"ixowhitelistdaemon/database"
whitelist_domain "ixowhitelistdaemon/whitelist"
"log"
"os"
"github.com/gofiber/fiber/v2"
)
func status(c *fiber.Ctx) error {
return c.SendString("Server is running! Send your request")
}
func setupRoutes(app *fiber.App) {
app.Get("/", status)
app.Get("/api/getwhitelist", whitelist_domain.GetAllWhitelisteDomains)
app.Post("/api/createwhitelistitem", whitelist_domain.CreateWhitelistedDomain)
}
func GenerateRsaKeyPair() (*rsa.PrivateKey, *rsa.PublicKey) {
privkey, _ := rsa.GenerateKey(rand.Reader, 4096)
return privkey, &privkey.PublicKey
}
func ExportRsaPrivateKeyAsPemStr(privkey *rsa.PrivateKey) string {
privkey_bytes := x509.MarshalPKCS1PrivateKey(privkey)
privkey_pem := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: privkey_bytes,
},
)
return string(privkey_pem)
}
func ExportRsaPublicKeyAsPemStr(pubkey *rsa.PublicKey) (string, error) {
pubkey_bytes, err := x509.MarshalPKIXPublicKey(pubkey)
if err != nil {
return "", err
}
pubkey_pem := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: pubkey_bytes,
},
)
return string(pubkey_pem), nil
}
func doesFileExist(fileName string) bool {
_, error := os.Stat(fileName)
// check if error is "file not exists"
if os.IsNotExist(error) {
return false
} else {
return true
}
}
func main() {
app := fiber.New()
dbErr := database.InitDatabase()
//Setup server RSA Keys
filecheck := doesFileExist("private.key")
//Check for local key
if !filecheck {
fmt.Println("File Not Found")
priv, pub := GenerateRsaKeyPair()
// Export the keys to pem string
priv_pem := ExportRsaPrivateKeyAsPemStr(priv)
pub_pem, _ := ExportRsaPublicKeyAsPemStr(pub)
priv_data := []byte(priv_pem)
pub_data := []byte(pub_pem)
ioerr := ioutil.WriteFile("private.key", priv_data, 777)
if ioerr != nil {
log.Fatal(ioerr)
}
ioerr2 := ioutil.WriteFile("public.key", pub_data, 777)
if ioerr2 != nil {
log.Fatal(ioerr2)
}
}
if dbErr != nil {
panic(dbErr)
}
setupRoutes(app)
app.Listen(":3000")
}