This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
maryo.go
158 lines (108 loc) · 3.5 KB
/
maryo.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
/*
maryo/maryo.go
written by superwhiskers, licensed under gnu gplv3.
if you want a copy, go to http://www.gnu.org/licenses/
*/
package main
import (
// internals
"flag"
"fmt"
"os"
)
// main function
func main() {
// reset term colors
consoleSequence(fmt.Sprintf("%s", code("reset")))
// parse some flags here
config := flag.String("config", "maryo-data/config.json", "value for config file path (default is maryo/config.json)")
logging := flag.Bool("logging", false, "if set, the proxy will log all request data (only needed for debugging)")
doSetup := flag.Bool("setup", false, "if set, maryo will go through setup again")
generateCerts := flag.Bool("regencerts", false, "if set, maryo will generate self-signed certificates for private use")
flag.Parse()
// set window title
ttitle("maryo")
// generate the certs if needed
if *generateCerts == true {
// that's it, really
doCertGen(*config)
// clear the screen
clear()
// give the user a message
fmt.Printf("your certificate and key pair have been generated\n")
fmt.Printf("reload the program to use them.\n")
// close the program
os.Exit(0)
}
// if not forced to do setup
if *doSetup == false {
// can't have that ugliness there
clear()
// map for holding file status
fileMap := make(map[string]string)
// config.json -- if nonexistent, it follows the user's instruction to create one, or use a builtin copy
// set it to nonexistent beforehand
fileMap["config"] = "ne"
// check if config exists
if doesFileExist(*config) != false {
// check if the file is valid JSON
if checkJSONValidity(*config) != true {
// set fileMap to have the correct status for the file
fileMap["config"] = "iv"
// if it is valid
} else {
// "ditto"
fileMap["config"] = "va"
}
}
// cert.pem and key.pem -- if nonexistent, just do setup
fileMap["cert"] = "ne"
// check the cert
if doesFileExist("maryo-data/cert.pem") != false {
// check the pubkey
if doesFileExist("maryo-data/public-key.pem") != false {
// check the privatekey
if doesFileExist("maryo-data/private-key.pem") != false {
// say it is valid if it is there
fileMap["cert"] = "va"
}
}
}
// do the setup function if the file isn't completely correct
if fileMap["config"] == "ne" {
// perform setup
setup(fileMap)
// if it's invalid
} else if fileMap["config"] == "iv" {
// i'm not just going to perform autosetup because they might have some stuff in there
fmt.Printf("your config is invalid.\n")
fmt.Printf("you have three different options:\n")
fmt.Printf(" 1. run this program with the --setup flag\n")
fmt.Printf(" 2. delete the config and run this program\n")
fmt.Printf(" 3. fix the config\n")
os.Exit(1)
// if the certificates don't exist
} else if fileMap["cert"] == "ne" {
// i'm not going to force you to set it up again
fmt.Printf("you don't have any certs in the maryo-data folder\n")
fmt.Printf("you have three different options:\n")
fmt.Printf(" 1. run this program with the --regencerts flag\n")
fmt.Printf(" 2. run this program with the --setup flag\n")
fmt.Printf(" 3. provide your own certs\n")
os.Exit(1)
// otherwise, start the proxy
} else {
// start the proxy
startProxy(*config, *logging)
}
// run setup function
} else {
// fileMap
var fileMap map[string]string
fileMap = make(map[string]string)
// place config value in there
fileMap["config"] = "uk"
// just do the setup function
setup(fileMap)
}
}