-
Notifications
You must be signed in to change notification settings - Fork 0
/
licserver.go
219 lines (181 loc) · 4.92 KB
/
licserver.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package licserver
import (
"fmt"
"io/ioutil"
"bufio"
"math/rand"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
)
var Configs []Config
func init() {
caddy.RegisterPlugin("licserver", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
type Config struct {
Dbname string
}
type Licserver struct {
Next httpserver.Handler
Configs []Config
}
// setup configures a new gzip middleware instance.
func setup(c *caddy.Controller) error {
var err error
Configs, err = parse(c)
if err != nil {
return err
}
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
return Licserver{Next: next, Configs: Configs}
})
return nil
}
func (h Licserver) ServeHTTP(response http.ResponseWriter, request *http.Request) (int, error) {
for _, c := range h.Configs {
//LICENSE:EXPDATE:EMAIL:HWID = 0,1,2
fmt.Println("License Server")
if !CheckFileExist(c.Dbname) {
fmt.Println("Database does not exist, creating new database.")
_ = createFile(c.Dbname)
}
database, _ := readLines(c.Dbname)
fmt.Println("Total Licenses:", len(database))
request.ParseForm()
license := request.FormValue("license")
hwid := request.FormValue("hwid")
//println(license, hwid)
database, _ = readLines(c.Dbname)
for _, table := range database {
row := strings.Split(table, ":")
t, err := time.Parse("2006-01-02", row[1])
if err != nil {
fmt.Println("ERROR: Error reading database")
}
t2, _ := time.Parse("2006-01-02", time.Now().Format("2006-01-02"))
if license == row[0] && t.After(t2) {
if hwid == row[3] {
fmt.Fprintf(response, "0") //Registed, Good licnese
} else if row[3] == "NOTSET" {
b, err := ioutil.ReadFile(c.Dbname)
if err != nil {
fmt.Println("READfromCHECK")
//os.Exit(0)
}
str := string(b)
edit := row[0] + ":" + row[1] + ":" + row[2] + ":" + hwid
res := strings.Replace(str, table, edit, -1)
err = ioutil.WriteFile(c.Dbname, []byte(res), 0644)
if err != nil {
fmt.Println("WRITEfromCHECK")
//os.Exit(0)
}
fmt.Fprintf(response, "2") //Registed, Good licnese
}
} else if license == row[0] && !t.After(t2) {
fmt.Fprintf(response, "1") //registerd but license experied
}
}
}
return h.Next.ServeHTTP(response, request)
}
func parse(c *caddy.Controller) ([]Config, error) {
var configs []Config
for c.Next() { // skip the directive name
conf := Config{}
for c.NextBlock() {
switch c.Val() {
case "dbname":
if !c.NextArg() {
return configs, c.ArgErr()
}
conf.Dbname = c.Val()
default:
return configs, c.ArgErr()
}
}
configs = append(configs, conf)
}
return configs, nil
}
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func CheckFileExist(filePath string) bool {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return false
} else {
return true
}
}
func createFile(pathFile string) error {
file, err := os.Create(pathFile)
if err != nil {
return err
}
defer file.Close()
return nil
}
func randomString(n int) string {
var letterRunes = []rune("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
//LICENSE:EXPDATE:EMAIL:HWID = 0,1,2
func checkHandler(response http.ResponseWriter, request *http.Request) {
request.ParseForm()
license := request.FormValue("license")
hwid := request.FormValue("hwid")
database, _ := readLines("db")
for _, table := range database {
row := strings.Split(table, ":")
t, err := time.Parse("2006-01-02", row[1])
if err != nil {
fmt.Println("ERROR: Error reading database")
}
t2, _ := time.Parse("2006-01-02", time.Now().Format("2006-01-02"))
if license == row[0] && t.After(t2) {
if hwid == row[3] {
fmt.Fprintf(response, "0") //Registed, Good licnese
} else if row[3] == "NOTSET" {
b, err := ioutil.ReadFile("db")
if err != nil {
fmt.Println("READfromCHECK")
os.Exit(0)
}
str := string(b)
edit := row[0] + ":" + row[1] + ":" + row[2] + ":" + hwid
res := strings.Replace(str, table, edit, -1)
err = ioutil.WriteFile("db", []byte(res), 0644)
if err != nil {
fmt.Println("WRITEfromCHECK")
os.Exit(0)
}
fmt.Fprintf(response, "2") //Registed, Good licnese
}
} else if license == row[0] && !t.After(t2) {
fmt.Fprintf(response, "1") //registerd but license experied
}
}
}